Java Code Examples for org.eclipse.jgit.api.RemoteAddCommand#call()

The following examples show how to use org.eclipse.jgit.api.RemoteAddCommand#call() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BackupService.java    From app-runner with MIT License 7 votes vote down vote up
public static BackupService prepare(File localDir, URIish remoteUri, int backupTimeInMinutes) throws GitAPIException, IOException {
    Git local;
    try {
        local = Git.open(localDir);
    } catch (RepositoryNotFoundException e) {
        log.info("Initialising " + fullPath(localDir) + " as a git repo for backup purposes");
        local = Git.init().setDirectory(localDir).setBare(false).call();
    }
    log.info("Setting backup URL to " + remoteUri);
    if (local.remoteList().call().stream().anyMatch(remoteConfig -> remoteConfig.getName().equals("origin"))) {
        RemoteSetUrlCommand remoteSetUrlCommand = local.remoteSetUrl();
        remoteSetUrlCommand.setRemoteName("origin");
        remoteSetUrlCommand.setRemoteUri(remoteUri);
        remoteSetUrlCommand.call();
    } else {
        RemoteAddCommand remoteAddCommand = local.remoteAdd();
        remoteAddCommand.setName("origin");
        remoteAddCommand.setUri(remoteUri);
        remoteAddCommand.call();
    }

    URL inputUrl = BackupService.class.getResource("/dataDirGitIgnore.txt");
    FileUtils.copyURLToFile(inputUrl, new File(localDir, ".gitignore"));

    return new BackupService(local, remoteUri.toString(), backupTimeInMinutes);
}
 
Example 2
Source File: UIGitTest.java    From hop with Apache License 2.0 5 votes vote down vote up
private RemoteConfig setupRemote() throws Exception {
  URIish uri = new URIish(
      db2.getDirectory().toURI().toURL() );
  RemoteAddCommand cmd = git.remoteAdd();
  cmd.setName( Constants.DEFAULT_REMOTE_NAME );
  cmd.setUri( uri );
  return cmd.call();
}
 
Example 3
Source File: GitService.java    From Refactoring-Bot with MIT License 5 votes vote down vote up
/**
 * This method adds an Remote to the fork/bot-repository.
 * 
 * @param gitConfig
 * @throws GitWorkflowException
 */
public void addRemote(GitConfiguration gitConfig) throws GitWorkflowException {
	try (Git git = Git.open(new File(botConfig.getBotRefactoringDirectory() + gitConfig.getConfigurationId()))) {
		// Add Remote as 'upstream'
		RemoteAddCommand remoteAddCommand = git.remoteAdd();
		remoteAddCommand.setName("upstream");
		remoteAddCommand.setUri(new URIish(gitConfig.getRepoGitLink()));
		remoteAddCommand.call();
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
		throw new GitWorkflowException(
				"Could not add as remote " + "'" + gitConfig.getRepoGitLink() + "' successfully!");
	}
}
 
Example 4
Source File: GitService.java    From jhipster-online with Apache License 2.0 5 votes vote down vote up
public void pushNewApplicationToGit(User user, File workingDir, String organization, String applicationName,
    GitProvider gitProvider)
    throws GitAPIException, URISyntaxException {

    log.info("Create Git repository for {}", workingDir);
    Git git = Git.init().setDirectory(workingDir).call();

    addAllFilesToRepository(git, workingDir);
    commit(git, workingDir, "Initial application generation by JHipster");

    log.debug("Adding remote repository {} / {}", organization, applicationName);
    URIish urIish = null;
    if (gitProvider.equals(GitProvider.GITHUB)) {
        urIish = new URIish(applicationProperties.getGithub().getHost() + "/" + organization + "/" + applicationName + ".git");
    } else if (gitProvider.equals(GitProvider.GITLAB)) {
        urIish = new URIish(applicationProperties.getGitlab().getHost() + "/" + organization + "/" +
            applicationName + ".git").setPass(user.getGitlabOAuthToken());
    }
    RemoteAddCommand remoteAddCommand = git.remoteAdd();
    remoteAddCommand.setName("origin");
    remoteAddCommand.setUri(urIish);
    remoteAddCommand.call();

    this.push(git, workingDir, user, organization, applicationName, gitProvider);

    log.debug("Repository successfully pushed!");
}
 
Example 5
Source File: AbstractRepairStep.java    From repairnator with MIT License 5 votes vote down vote up
protected void pushPatches(Git git, String forkedRepo,String branchName) throws IOException, GitAPIException, URISyntaxException {
    RemoteAddCommand remoteAddCommand = git.remoteAdd();
    remoteAddCommand.setUri(new URIish(forkedRepo));
    remoteAddCommand.setName("fork-patch");
    remoteAddCommand.call();

    git.push().add(branchName).setRemote("fork-patch").setCredentialsProvider(new UsernamePasswordCredentialsProvider(RepairnatorConfig.getInstance().getGithubToken(), "")).call();
}
 
Example 6
Source File: UIGitTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@Test
public void testPush() throws Exception {
  // Set remote
  Git git2 = new Git( db2 );
  UIGit uiGit2 = new UIGit();
  uiGit2.setGit( git2 );
  URIish uri = new URIish(
    db2.getDirectory().toURI().toURL() );
  RemoteAddCommand cmd = git.remoteAdd();
  cmd.setName( Constants.DEFAULT_REMOTE_NAME );
  cmd.setUri( uri );
  cmd.call();

  assertTrue( uiGit.hasRemote() );

  // create some refs via commits and tag
  RevCommit commit = git.commit().setMessage( "initial commit" ).call();
  Ref tagRef = git.tag().setName( "tag" ).call();

  try {
    db2.resolve( commit.getId().getName() + "^{commit}" );
    fail( "id shouldn't exist yet" );
  } catch ( MissingObjectException e ) {
    // we should get here
  }

  boolean success = uiGit.push();
  assertTrue( success );
  assertEquals( commit.getId(),
      db2.resolve( commit.getId().getName() + "^{commit}" ) );
  assertEquals( tagRef.getObjectId(),
      db2.resolve( tagRef.getObjectId().getName() ) );

  // Push a tag
  EnterSelectionDialog esd = mock( EnterSelectionDialog.class );
  doReturn( "tag" ).when( esd ).open();
  doReturn( esd ).when( uiGit ).getEnterSelectionDialog( any(), anyString(), anyString() );
  uiGit.push( IVCS.TYPE_TAG );
  assertTrue( success );
  assertTrue( uiGit2.getTags().contains( "tag" ) );

  // Another commit and push a branch again
  writeTrashFile( "Test2.txt", "Hello world" );
  git.add().addFilepattern( "Test2.txt" ).call();
  commit = git.commit().setMessage( "second commit" ).call();
  doReturn( Constants.MASTER ).when( esd ).open();
  uiGit.push( IVCS.TYPE_BRANCH );
  assertTrue( success );
  assertEquals( commit.getId(),
      db2.resolve( commit.getId().getName() + "^{commit}" ) );

  assertEquals( "refs/remotes/origin/master", uiGit.getExpandedName( "origin/master", "branch" ) );
}
 
Example 7
Source File: GitHelper.java    From repairnator with MIT License 4 votes vote down vote up
public boolean mergeTwoCommitsForPR(Git git, Build build, PullRequest prInformation, String repository, AbstractStep step, List<String> paths) {
    try {
        String remoteBranchPath = Utils.getCompleteGithubRepoUrl(prInformation.getOtherRepo().getFullName());

        RemoteAddCommand remoteBranchCommand = git.remoteAdd();
        remoteBranchCommand.setName("PR");
        remoteBranchCommand.setUri(new URIish(remoteBranchPath));
        remoteBranchCommand.call();

        git.fetch().setRemote("PR").call();

        String commitHeadSha = this.testCommitExistence(git, prInformation.getHead().getSHA1(), step, build);
        String commitBaseSha = this.testCommitExistence(git, prInformation.getBase().getSHA1(), step, build);

        if (commitHeadSha == null) {
            step.addStepError("Commit head ref cannot be retrieved from the repository: "
                    + prInformation.getHead().getSHA1() + ". Operation aborted.");
            return false;
        }

        if (commitBaseSha == null) {
            step.addStepError("Commit base ref cannot be retrieved from the repository: "
                    + prInformation.getBase().getSHA1() + ". Operation aborted.");
            return false;
        }

        this.getLogger().debug("Step " + step.getName() + " - Get the commit " + commitHeadSha + " for repo " + repository);

        if (paths != null) {
            this.gitResetPaths(commitHeadSha, paths, git.getRepository().getDirectory().getParentFile());
            git.commit().setMessage("Undo changes on source code").setAuthor(this.getCommitterIdent()).setCommitter(this.getCommitterIdent()).call();
        } else {
            git.checkout().setName(commitHeadSha).call();
        }

        RevWalk revwalk = new RevWalk(git.getRepository());
        RevCommit revCommitBase = revwalk.lookupCommit(git.getRepository().resolve(commitBaseSha));

        this.getLogger().debug("Step " + step.getName() + " - Do the merge with the PR commit for repo " + repository);
        MergeResult result = git.merge().include(revCommitBase).setFastForward(MergeCommand.FastForwardMode.NO_FF).call();
        this.nbCommits++;
    } catch (Exception e) {
        step.addStepError(e.getMessage());
        this.getLogger().error("Step " + step.getName() + " - Repository " + repository + " cannot be cloned.",e);
        return false;
    }
    return true;
}