Example usage for org.eclipse.jgit.api InitCommand setDirectory

List of usage examples for org.eclipse.jgit.api InitCommand setDirectory

Introduction

In this page you can find the example usage for org.eclipse.jgit.api InitCommand setDirectory.

Prototype

public InitCommand setDirectory(File directory) throws IllegalStateException 

Source Link

Document

The optional directory associated with the init operation.

Usage

From source file:com.athomas.androidkickstartr.util.GitHubber.java

License:Apache License

public Repository createCommit(File srcFolder, String applicationName) throws IOException, GitAPIException {
    Repository repository = repositoryService.createRepository(new Repository().setName(applicationName));

    String cloneUrl = repository.getCloneUrl();

    InitCommand init = new InitCommand();
    init.setDirectory(srcFolder);
    init.setBare(false);/* w ww  . ja  va  2  s .c  o  m*/
    Git git = init.call();

    StoredConfig config = git.getRepository().getConfig();
    config.setString("remote", "origin", "url", cloneUrl);
    config.save();

    UsernamePasswordCredentialsProvider user = new UsernamePasswordCredentialsProvider(accessToken, "");
    git.add().addFilepattern(".").call();
    git.commit().setMessage(COMMIT_MESSAGE).call();
    git.push().setCredentialsProvider(user).call();

    return repository;
}

From source file:com.denimgroup.threadfix.service.repository.GitServiceImpl.java

License:Mozilla Public License

@Override
public boolean testConfiguration(Application application, String repo, String branch) throws GitAPIException {
    InitCommand initCommand = new InitCommand();
    File applicationDirectory = DiskUtils.getScratchFile(baseDirectory + application.getId() + "-test");
    initCommand.setDirectory(applicationDirectory);

    Git otherGit = initCommand.call();// ww w .j av  a 2  s  . c om

    otherGit.getRepository().getConfig().setString("remote", "origin", "url", repo);

    String targetRefSpec = branch == null || branch.isEmpty() ? Constants.R_HEADS + "*:refs/remotes/origin/*"
            : Constants.R_HEADS + branch;

    FetchCommand fetchCommand = otherGit.fetch()
            .setCredentialsProvider(getUnencryptedApplicationCredentials(application)).setDryRun(true)
            .setRefSpecs(new RefSpec(targetRefSpec)).setRemote("origin");

    fetchCommand.call();

    return true;
}

From source file:com.gmail.cjbooms.thesis.pythonappengine.server.git.GitCommandsServiceImpl.java

License:Open Source License

/**
 * Initialize a new empty GIT repository
 *
 * @param pathToNewRepository Location to build new Repo
 * @throws IOException, JGitInternalException
 *//*from   ww w. j av a 2 s .  c o m*/
public void initializeNewRepository(String pathToNewRepository) throws IOException, JGitInternalException {
    File directory = new File(pathToNewRepository);
    InitCommand command = new InitCommand();
    command.setDirectory(directory);
    command.call();
}

From source file:com.meltmedia.cadmium.blackbox.test.GitBareRepoInitializer.java

License:Apache License

public void init(String repoPath, String sourceDir, String sourceConfigDir) throws Exception {
    File repoDir = new File(repoPath);
    if (repoDir.exists()) {
        FileUtils.forceDelete(repoDir);/*from  w w w.  ja v  a2  s.c  o m*/
    }

    File checkoutDir = new File(repoDir.getAbsoluteFile().getParent(), repoDir.getName() + ".checkout");
    if (checkoutDir.exists()) {
        FileUtils.forceDelete(checkoutDir);
    }
    checkoutPath = checkoutDir.getAbsolutePath();

    InitCommand init = Git.init();
    init.setBare(true);
    init.setDirectory(repoDir);
    bareRepo = init.call();

    clonedGit = GitService.cloneRepo(repoPath, checkoutPath);
    clonedGit.checkinNewContent(sourceConfigDir, "Initial commit");
    clonedGit.push(false);
    clonedGit.newEmtpyRemoteBranch("cd-master");
    clonedGit.switchBranch("cd-master");
    clonedGit.checkinNewContent(sourceDir, "Initial commit");
    clonedGit.push(false);
    clonedGit.newEmtpyRemoteBranch("cfg-master");
    clonedGit.switchBranch("cfg-master");
    clonedGit.checkinNewContent(sourceConfigDir, "Initial commit");
    clonedGit.push(false);
}

From source file:com.photon.phresco.framework.impl.SCMManagerImpl.java

License:Apache License

private void importToGITRepo(RepoDetail repodetail, ApplicationInfo appInfo, File appDir) throws Exception {
    if (debugEnabled) {
        S_LOGGER.debug("Entering Method  SCMManagerImpl.importToGITRepo()");
    }/*from  w  ww.java  2s.c o  m*/
    boolean gitExists = false;
    if (new File(appDir.getPath() + FORWARD_SLASH + DOT + GIT).exists()) {
        gitExists = true;
    }
    try {
        //For https and ssh
        additionalAuthentication(repodetail.getPassPhrase());

        CredentialsProvider cp = new UsernamePasswordCredentialsProvider(repodetail.getUserName(),
                repodetail.getPassword());

        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(appDir).readEnvironment().findGitDir().build();
        String dirPath = appDir.getPath();
        File gitignore = new File(dirPath + GITIGNORE_FILE);
        gitignore.createNewFile();
        if (gitignore.exists()) {
            String contents = FileUtils.readFileToString(gitignore);
            if (!contents.isEmpty() && !contents.contains(DO_NOT_CHECKIN_DIR)) {
                String source = NEWLINE + DO_NOT_CHECKIN_DIR + NEWLINE;
                OutputStream out = new FileOutputStream((dirPath + GITIGNORE_FILE), true);
                byte buf[] = source.getBytes();
                out.write(buf);
                out.close();
            } else if (contents.isEmpty()) {
                String source = NEWLINE + DO_NOT_CHECKIN_DIR + NEWLINE;
                OutputStream out = new FileOutputStream((dirPath + GITIGNORE_FILE), true);
                byte buf[] = source.getBytes();
                out.write(buf);
                out.close();
            }
        }

        Git git = new Git(repository);

        InitCommand initCommand = Git.init();
        initCommand.setDirectory(appDir);
        git = initCommand.call();

        AddCommand add = git.add();
        add.addFilepattern(".");
        add.call();

        CommitCommand commit = git.commit().setAll(true);
        commit.setMessage(repodetail.getCommitMessage()).call();
        StoredConfig config = git.getRepository().getConfig();

        config.setString(REMOTE, ORIGIN, URL, repodetail.getRepoUrl());
        config.setString(REMOTE, ORIGIN, FETCH, REFS_HEADS_REMOTE_ORIGIN);
        config.setString(BRANCH, MASTER, REMOTE, ORIGIN);
        config.setString(BRANCH, MASTER, MERGE, REF_HEAD_MASTER);
        config.save();

        try {
            PushCommand pc = git.push();
            pc.setCredentialsProvider(cp).setForce(true);
            pc.setPushAll().call();
        } catch (Exception e) {
            git.getRepository().close();
            PomProcessor appPomProcessor = new PomProcessor(new File(appDir, appInfo.getPhrescoPomFile()));
            appPomProcessor.removeProperty(Constants.POM_PROP_KEY_SRC_REPO_URL);
            appPomProcessor.save();
            throw e;
        }

        if (appInfo != null) {
            updateSCMConnection(appInfo, repodetail.getRepoUrl());
        }
        git.getRepository().close();
    } catch (Exception e) {
        Exception s = e;
        resetLocalCommit(appDir, gitExists, e);
        throw s;
    }
}

From source file:com.photon.phresco.framework.impl.SCMManagerImpl.java

License:Apache License

private void resetLocalCommit(File appDir, boolean gitExists, Exception e) throws PhrescoException {
    try {/*from www .  j  ava 2 s  .c o m*/
        if (gitExists == true && e.getLocalizedMessage().contains("not authorized")) {
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repository = builder.setGitDir(appDir).readEnvironment().findGitDir().build();
            Git git = new Git(repository);

            InitCommand initCommand = Git.init();
            initCommand.setDirectory(appDir);
            git = initCommand.call();

            ResetCommand reset = git.reset();
            ResetType mode = ResetType.SOFT;
            reset.setRef("HEAD~1").setMode(mode);
            reset.call();

            git.getRepository().close();
        }
    } catch (Exception pe) {
        new PhrescoException(pe);
    }
}

From source file:com.photon.phresco.framework.impl.SCMManagerImpl.java

License:Apache License

public List<RepoFileInfo> getGITCommitableFiles(File path) throws IOException, GitAPIException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(path).readEnvironment().findGitDir().build();
    Git git = new Git(repository);
    List<RepoFileInfo> fileslist = new ArrayList<RepoFileInfo>();
    InitCommand initCommand = Git.init();
    initCommand.setDirectory(path);
    git = initCommand.call();//from  w  ww  . j a  va 2 s  .c o m
    Status status = git.status().call();

    Set<String> added = status.getAdded();
    Set<String> changed = status.getChanged();
    Set<String> conflicting = status.getConflicting();
    Set<String> missing = status.getMissing();
    Set<String> modified = status.getModified();
    Set<String> removed = status.getRemoved();
    Set<String> untracked = status.getUntracked();

    if (!added.isEmpty()) {
        for (String add : added) {
            RepoFileInfo repoFileInfo = new RepoFileInfo();
            String filePath = path + BACK_SLASH + add;
            repoFileInfo.setCommitFilePath(filePath);
            repoFileInfo.setStatus("A");
            fileslist.add(repoFileInfo);
        }
    }

    if (!changed.isEmpty()) {
        for (String change : changed) {
            RepoFileInfo repoFileInfo = new RepoFileInfo();
            String filePath = path + BACK_SLASH + change;
            repoFileInfo.setCommitFilePath(filePath);
            repoFileInfo.setStatus("M");
            fileslist.add(repoFileInfo);
        }
    }

    if (!conflicting.isEmpty()) {
        for (String conflict : conflicting) {
            RepoFileInfo repoFileInfo = new RepoFileInfo();
            String filePath = path + BACK_SLASH + conflict;
            repoFileInfo.setCommitFilePath(filePath);
            repoFileInfo.setStatus("C");
            fileslist.add(repoFileInfo);
        }
    }

    if (!missing.isEmpty()) {
        for (String miss : missing) {
            RepoFileInfo repoFileInfo = new RepoFileInfo();
            String filePath = path + BACK_SLASH + miss;
            repoFileInfo.setCommitFilePath(filePath);
            repoFileInfo.setStatus("!");
            fileslist.add(repoFileInfo);
        }
    }

    if (!modified.isEmpty()) {
        for (String modify : modified) {
            RepoFileInfo repoFileInfo = new RepoFileInfo();
            String filePath = path + BACK_SLASH + modify;
            repoFileInfo.setCommitFilePath(filePath);
            repoFileInfo.setStatus("M");
            fileslist.add(repoFileInfo);
        }
    }

    if (!removed.isEmpty()) {
        for (String remove : removed) {
            RepoFileInfo repoFileInfo = new RepoFileInfo();
            String filePath = path + BACK_SLASH + remove;
            repoFileInfo.setCommitFilePath(filePath);
            repoFileInfo.setStatus("D");
            fileslist.add(repoFileInfo);
        }
    }

    if (!untracked.isEmpty()) {
        for (String untrack : untracked) {
            RepoFileInfo repoFileInfo = new RepoFileInfo();
            String filePath = path + BACK_SLASH + untrack;
            repoFileInfo.setCommitFilePath(filePath);
            repoFileInfo.setStatus("?");
            fileslist.add(repoFileInfo);
        }
    }
    git.getRepository().close();
    return fileslist;
}

From source file:com.photon.phresco.framework.rest.api.RepositoryService.java

License:Apache License

/**
 * Check git project.//from ww  w  .j av a2 s  .  com
 *
 * @param applicationInfo the application info
 * @param setRepoExistForCommit the set repo exist for commit
 * @return true, if successful
 * @throws PhrescoException the phresco exception
 */
private boolean checkGitProject(File workingDir, boolean setRepoExistForCommit) throws PhrescoException {
    setRepoExistForCommit = true;
    String url = "";
    InitCommand initCommand = Git.init();
    initCommand.setDirectory(workingDir);
    Git git = null;
    try {
        git = initCommand.call();
    } catch (GitAPIException e) {
        throw new PhrescoException(e);
    }

    Config storedConfig = git.getRepository().getConfig();
    url = storedConfig.getString(REMOTE, ORIGIN, URL);
    if (StringUtils.isEmpty(url)) {
        File toDelete = git.getRepository().getDirectory();
        try {
            FileUtils.deleteDirectory(toDelete);
        } catch (IOException e) {
            throw new PhrescoException(e);
        }
        setRepoExistForCommit = false;

    }
    git.getRepository().close();

    return setRepoExistForCommit;
}

From source file:eu.atos.paas.git.Repository.java

License:Open Source License

public static Repository init(File path) throws GitAPIException, IOException {

    InitCommand cmd = Git.init();
    logger.debug("Start git init in {}", path.getPath());
    Git gitRepo = cmd.setDirectory(path).call();
    gitRepo.close();//  w w w. jav  a 2  s .  c o  m
    logger.debug("End git init in {}", path.getPath());
    return new Repository(path);
}

From source file:fr.treeptik.cloudunit.utils.GitUtils.java

License:Open Source License

/**
 * List all GIT Tags of an application with an index
 * After this index is use to choose on which tag user want to restre his application
 * with resetOnChosenGitTag() method//  www  . j av a 2 s .c o m
 *
 * @param application
 * @param dockerManagerAddress
 * @param containerGitAddress
 * @return
 * @throws GitAPIException
 * @throws IOException
 */
public static List<String> listGitTagsOfApplication(Application application, String dockerManagerAddress,
        String containerGitAddress) throws GitAPIException, IOException {

    List<String> listTagsName = new ArrayList<>();

    User user = application.getUser();
    String sshPort = application.getServers().get(0).getSshPort();
    String password = user.getPassword();
    String userNameGit = user.getLogin();
    String dockerManagerIP = dockerManagerAddress.substring(0, dockerManagerAddress.length() - 5);
    String remoteRepository = "ssh://" + userNameGit + "@" + dockerManagerIP + ":" + sshPort
            + containerGitAddress;

    Path myTempDirPath = Files.createTempDirectory(Paths.get("/tmp"), null);
    File gitworkDir = myTempDirPath.toFile();

    InitCommand initCommand = Git.init();
    initCommand.setDirectory(gitworkDir);
    initCommand.call();
    FileRepository gitRepo = new FileRepository(gitworkDir);
    LsRemoteCommand lsRemoteCommand = new LsRemoteCommand(gitRepo);

    CredentialsProvider credentialsProvider = configCredentialsForGit(userNameGit, password);

    lsRemoteCommand.setCredentialsProvider(credentialsProvider);
    lsRemoteCommand.setRemote(remoteRepository);
    lsRemoteCommand.setTags(true);
    Collection<Ref> collectionRefs = lsRemoteCommand.call();
    List<Ref> listRefs = new ArrayList<>(collectionRefs);

    for (Ref ref : listRefs) {
        listTagsName.add(ref.getName());
    }
    Collections.sort(listTagsName);
    FilesUtils.deleteDirectory(gitworkDir);

    return listTagsName;

}