org.eclipse.jgit.api.RebaseResult Java Examples

The following examples show how to use org.eclipse.jgit.api.RebaseResult. 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: LocalGitProvider.java    From judgels with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean rebase(List<String> rootDirPath) {
    File root = new File(fileSystemProvider.getURL(rootDirPath));

    try {
        Repository repo = FileRepositoryBuilder.create(new File(root, ".git"));
        RebaseResult result = new Git(repo).rebase().setUpstream("origin/master").call();

        if (result.getStatus() == RebaseResult.Status.STOPPED) {
            new Git(repo).rebase().setOperation(RebaseCommand.Operation.ABORT).call();
            repo.close();
            return false;
        }
        repo.close();
        return true;

    } catch (IOException | GitAPIException e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: RebaseCommand.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private GitRebaseResult createResult (RebaseResult res) {
    String currHead;
    Repository repository = getRepository();
    try {
        currHead = repository.resolve(Constants.HEAD).name();
    } catch (IOException ex) {
        currHead = Constants.HEAD;
    }
    List<File> conflicts;
    if (res.getStatus() == RebaseResult.Status.STOPPED) {
        conflicts = getConflicts(res.getCurrentCommit());
    } else {
        conflicts = Collections.<File>emptyList();
    }
    return getClassFactory().createRebaseResult(res, conflicts, getFailures(res), currHead);
}
 
Example #3
Source File: GitRebaseTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testRebaseSelf() throws Exception {
	createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
	IPath[] clonePaths = createTestProjects(workspaceLocation);

	for (IPath clonePath : clonePaths) {
		// clone a  repo
		String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);

		// get project metadata
		WebRequest request = getGetRequest(contentLocation);
		WebResponse response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
		JSONObject project = new JSONObject(response.getText());
		JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);

		String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

		// "git rebase master"
		JSONObject rebase = rebase(gitHeadUri, Constants.MASTER);
		RebaseResult.Status rebaseResult = RebaseResult.Status.valueOf(rebase.getString(GitConstants.KEY_RESULT));
		assertEquals(RebaseResult.Status.UP_TO_DATE, rebaseResult);
	}
}
 
Example #4
Source File: SquashActionHandler.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public RebaseResponse process(Repository repository, String message) throws GitAPIException, IOException {

    try (Git git = Git.wrap(repository)) {
        git.commit()
                .setMessage(stripCommentLines(message))
                .setAmend(true).setNoVerify(true).call();

        getRebaseFile(repository, MESSAGE_SQUASH).delete();
        getRebaseFile(repository, MESSAGE_FIXUP).delete();

        createFile(repository, MESSAGE, message);

        RebaseResult result = git.rebase()
                .setOperation(RebaseCommand.Operation.SKIP)
                .runInteractively(handler)
                .call();

        return new RebaseResponse(result);
    }
}
 
Example #5
Source File: EditActionHandler.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public RebaseResponse process(Repository repository, String message) throws GitAPIException {
    try (Git git = Git.wrap(repository)) {
        git.commit()
                .setAll(true)
                .setAmend(true)
                .setNoVerify(true)
                .setMessage(message)
                .call();

        RebaseResult result = git.rebase()
                .setOperation(RebaseCommand.Operation.CONTINUE)
                .runInteractively(handler)
                .call();

        // 如果 conflict and edit,amend 后 continue 会返回 NOTHING_TO_COMMIT
        // so skip this commit
        if (result.getStatus().equals(RebaseResult.Status.NOTHING_TO_COMMIT)) {
            result = git.rebase().setOperation(RebaseCommand.Operation.SKIP).call();
        }

        return new RebaseResponse(result);
    }
}
 
Example #6
Source File: RewordActionHandler.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public RebaseResponse process(Repository repository, String message) throws GitAPIException, IOException {

    try (Git git = Git.wrap(repository)) {
        git.commit()
                .setMessage(message)
                .setAmend(true)
                .setNoVerify(true)
                .call();

        RebaseResult result = git.rebase()
                .setOperation(RebaseCommand.Operation.SKIP)
                .runInteractively(handler)
                .call();

        return new RebaseResponse(result);
    }
}
 
Example #7
Source File: JGitOperator.java    From verigreen with Apache License 2.0 6 votes vote down vote up
@Override
public boolean rebase(String upStreamBranchName) {
    
    RebaseCommand command = _git.rebase();
    RebaseResult result = null;
    try {
        command.setUpstream(upStreamBranchName);
        result = command.call();
        // if there are merge conflicts (rebase interactive) - reset the repository
        if (!result.getStatus().isSuccessful()) {
            _git.rebase().setOperation(Operation.ABORT).call();
        }
    } catch (Throwable e) {
        throw new RuntimeException(String.format(
                "Failed to rebase with upstream [%s]",
                upStreamBranchName), e);
    }
    
    return result.getStatus().isSuccessful();
}
 
Example #8
Source File: GitRebaseResult.java    From netbeans with Apache License 2.0 5 votes vote down vote up
GitRebaseResult (RebaseResult result, List<File> rebaseConflicts, List<File> failures, String currentHead) {
    this.rebaseStatus = parseRebaseStatus(result.getStatus());
    this.currentHead = currentHead;
    if (result.getCurrentCommit() == null) {
        this.currentCommit = null;
    } else {
        this.currentCommit = result.getCurrentCommit().getId().getName();
    }
    this.conflicts = rebaseConflicts;
    this.failures = failures;
}
 
Example #9
Source File: GitRebaseResult.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static RebaseStatus parseRebaseStatus (RebaseResult.Status rebaseStatus) {
    switch (rebaseStatus) {
        case EDIT:
            return RebaseStatus.STOPPED;
        case UNCOMMITTED_CHANGES:
            return RebaseStatus.FAILED;
        case INTERACTIVE_PREPARED:
            return RebaseStatus.STOPPED;
        case STASH_APPLY_CONFLICTS:
            return RebaseStatus.CONFLICTS;
            
    }
    return GitRebaseResult.RebaseStatus.valueOf(rebaseStatus.name());
}
 
Example #10
Source File: GitSynchronizeWindow.java    From XACML with MIT License 5 votes vote down vote up
protected void synchronize() {
	//
	// Grab our working repository
	//
	Path repoPath = ((XacmlAdminUI)getUI()).getUserGitPath();
	try {
		final Git git = Git.open(repoPath.toFile());
		
		PullResult result = git.pull().call();
		FetchResult fetch = result.getFetchResult();
		MergeResult merge = result.getMergeResult();
		RebaseResult rebase = result.getRebaseResult();
		if (result.isSuccessful()) {
			//
			// TODO add more notification
			//
			this.textAreaResults.setValue("Successful!");
		} else {
			//
			// TODO
			//
			this.textAreaResults.setValue("Failed.");
		}
	} catch (IOException | GitAPIException e) {
		e.printStackTrace();
	}
	this.buttonSynchronize.setCaption("Ok");
}
 
Example #11
Source File: GitClassFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public abstract GitRebaseResult createRebaseResult (RebaseResult rebaseResult, List<File> rebaseConflicts, List<File> failures,
String newHead);
 
Example #12
Source File: GitClassFactoryImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public GitRebaseResult createRebaseResult (RebaseResult rebaseResult, List<File> rebaseConflicts, List<File> failures,
        String newHead) {
    return new GitRebaseResult(rebaseResult, rebaseConflicts, failures, newHead);
}
 
Example #13
Source File: GitEnumsStateTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testRebaseStatus () {
    for (RebaseResult.Status status : RebaseResult.Status.values()) {
        assertNotNull(GitRebaseResult.parseRebaseStatus(status));
    }
}
 
Example #14
Source File: GitRebaseTest.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testRebase() throws Exception {
	createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
	IPath[] clonePaths = createTestProjects(workspaceLocation);

	for (IPath clonePath : clonePaths) {
		// clone a  repo
		JSONObject clone = clone(clonePath);

		String contentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
		String cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION);
		String branchesLocation = clone.getString(GitConstants.KEY_BRANCH);

		// get project metadata
		WebRequest request = getGetRequest(contentLocation);
		WebResponse response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
		JSONObject project = new JSONObject(response.getText());

		JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
		String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);
		String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
		String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);

		// create branch 'a'
		branch(branchesLocation, "a");

		// checkout 'a'
		Repository db1 = getRepositoryForContentLocation(contentLocation);
		Git git = Git.wrap(db1);
		assertBranchExist(git, "a");
		checkoutBranch(cloneLocation, "a");

		// modify while on 'a'
		JSONObject testTxt = getChild(project, "test.txt");
		modifyFile(testTxt, "change in a");

		// "git add ."
		request = GitAddTest.getPutGitIndexRequest(gitIndexUri);
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

		// commit all
		request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit on a", false);
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

		// assert clean
		assertStatus(StatusResult.CLEAN, gitStatusUri);

		// checkout 'master'
		checkoutBranch(cloneLocation, Constants.MASTER);

		// modify a different file on master
		JSONObject folder = getChild(project, "folder");
		JSONObject folderTxt = getChild(folder, "folder.txt");
		modifyFile(folderTxt, "change in master");

		gitSection = project.getJSONObject(GitConstants.KEY_GIT);
		gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
		gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);
		gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

		// "git add ."
		request = GitAddTest.getPutGitIndexRequest(gitIndexUri);
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

		// commit all
		request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit on master", false);
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

		// assert clean
		assertStatus(StatusResult.CLEAN, gitStatusUri);

		// rebase: "git rebase a"
		JSONObject rebase = rebase(gitHeadUri, "a");
		RebaseResult.Status rebaseResult = RebaseResult.Status.valueOf(rebase.getString(GitConstants.KEY_RESULT));
		assertEquals(RebaseResult.Status.OK, rebaseResult);

		// assert clean
		assertStatus(StatusResult.CLEAN, gitStatusUri);

		request = getGetRequest(testTxt.getString(ProtocolConstants.KEY_LOCATION));
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
		assertEquals("change in a", response.getText());

		request = getGetRequest(folderTxt.getString(ProtocolConstants.KEY_LOCATION));
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
		assertEquals("change in master", response.getText());
	}
}
 
Example #15
Source File: GitRebaseTest.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testRebaseOnRemote() throws Exception {
	createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
	IPath[][] clonePaths = createTestClonePairs(workspaceLocation);

	for (IPath[] clonePath : clonePaths) {
		// clone1
		String contentLocation1 = clone(clonePath[0]).getString(ProtocolConstants.KEY_CONTENT_LOCATION);

		// get project1 metadata
		WebRequest request = getGetRequest(contentLocation1);
		WebResponse response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
		JSONObject project1 = new JSONObject(response.getText());
		JSONObject gitSection1 = project1.getJSONObject(GitConstants.KEY_GIT);
		String gitRemoteUri1 = gitSection1.getString(GitConstants.KEY_REMOTE);

		// clone2
		String contentLocation2 = clone(clonePath[1]).getString(ProtocolConstants.KEY_CONTENT_LOCATION);

		// get project2 metadata
		request = getGetRequest(contentLocation2);
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
		JSONObject project2 = new JSONObject(response.getText());
		JSONObject gitSection2 = project2.getJSONObject(GitConstants.KEY_GIT);
		String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);
		String gitIndexUri2 = gitSection2.getString(GitConstants.KEY_INDEX);
		String gitHeadUri2 = gitSection2.getString(GitConstants.KEY_HEAD);

		// clone1: get remote details
		JSONObject details = getRemoteBranch(gitRemoteUri1, 1, 0, Constants.MASTER);
		String refId1 = details.getString(ProtocolConstants.KEY_ID);
		String remoteBranchLocation1 = details.getString(ProtocolConstants.KEY_LOCATION);

		// clone2: change
		JSONObject testTxt2 = getChild(project2, "test.txt");
		modifyFile(testTxt2, "incoming change");

		// clone2: add
		request = GitAddTest.getPutGitIndexRequest(gitIndexUri2);
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

		// clone2: commit
		request = GitCommitTest.getPostGitCommitRequest(gitHeadUri2, "incoming change commit", false);
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

		// clone2: push
		ServerStatus pushStatus = push(gitRemoteUri2, 1, 0, Constants.MASTER, Constants.HEAD, false);
		assertEquals(true, pushStatus.isOK());

		// clone1: fetch
		request = GitFetchTest.getPostGitRemoteRequest(remoteBranchLocation1, true, false);
		response = webConversation.getResponse(request);
		ServerStatus status = waitForTask(response);
		assertTrue(status.toString(), status.isOK());

		// clone1: get remote details again
		JSONObject remoteBranch = getRemoteBranch(gitRemoteUri1, 1, 0, Constants.MASTER);
		String newRefId1 = remoteBranch.getString(ProtocolConstants.KEY_ID);
		// an incoming commit
		assertFalse(refId1.equals(newRefId1));

		String gitHeadUri = remoteBranch.getString(GitConstants.KEY_HEAD);
		assertNotNull(gitHeadUri);

		// rebase
		JSONObject rebase = rebase(gitHeadUri, newRefId1);
		RebaseResult.Status rebaseResult = RebaseResult.Status.valueOf(rebase.getString(GitConstants.KEY_RESULT));
		assertEquals(RebaseResult.Status.FAST_FORWARD, rebaseResult);

		JSONObject testTxt1 = getChild(project1, "test.txt");
		request = getGetRequest(testTxt1.getString(ProtocolConstants.KEY_LOCATION));
		response = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
		assertEquals("incoming change", response.getText());
	}
}
 
Example #16
Source File: RebaseResponse.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public RebaseResponse(RebaseResult rebaseResult) {
    this.success = rebaseResult.getStatus().isSuccessful();
    this.status = Status.valueOf(rebaseResult.getStatus().name());
}