Example usage for org.eclipse.jgit.api Git Git

List of usage examples for org.eclipse.jgit.api Git Git

Introduction

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

Prototype

public Git(Repository repo) 

Source Link

Document

Construct a new org.eclipse.jgit.api.Git object which can interact with the specified git repository.

Usage

From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java

License:Open Source License

/**
 * Writes a new {@link Review} based on the given task data.
 * @return the new review's hash./*  w  ww .ja  v  a  2 s  .c o m*/
 */
public String createReview(String reviewCommitHash, Review review) throws GitClientException {
    // Sync to minimize the chances of non-linear merges.
    syncCommentsAndReviews();

    // Push the code under review, or the user won't be able to access the commit with the
    // notes.
    try (Git git = new Git(repo)) {
        assert !"master".equals(review.getReviewRef());
        RefSpec reviewRefSpec = new RefSpec(review.getReviewRef());
        PushCommand pushCommand = git.push();
        pushCommand.setRefSpecs(reviewRefSpec);
        try {
            pushCommand.call();
        } catch (Exception e) {
            throw new GitClientException("Error pushing review commit(s) to origin", e);
        }
    }

    // Commit.
    commitReviewNote(reviewCommitHash, review);

    // Push.
    try {
        pushCommentsAndReviews();
    } catch (Exception e) {
        throw new GitClientException("Error pushing, review is " + reviewCommitHash, e);
    }

    return reviewCommitHash;
}

From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java

License:Open Source License

/**
 * Pushes the local comments and reviews back to the origin.
 *//*from w  w w.  j av  a2  s .c  o  m*/
private void pushCommentsAndReviews() throws Exception {
    try (Git git = new Git(repo)) {
        RefSpec spec = new RefSpec(DEVTOOLS_PUSH_REFSPEC);
        PushCommand pushCommand = git.push();
        pushCommand.setRefSpecs(spec);
        pushCommand.call();
    }
}

From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java

License:Open Source License

/**
 * Gets the diff entries associated with a specific review commit.
 * The review commit is the commit hash at which the review was requested.
 * Subsequent commits on that review can be inferred from the append-only comments.
 *//* ww  w  . ja  v  a2  s.co  m*/
public List<DiffEntry> getDiff(String requestCommitHash)
        throws GitClientException, IOException, GitAPIException {
    Review review = getReview(requestCommitHash);

    // If the target ref is missing or the corresponding branch does not exist,
    // the review is bogus.
    if (review.getTargetRef() == null || review.getTargetRef().isEmpty()) {
        throw new GitClientException("Review target ref not set: " + requestCommitHash);
    }

    try (Git git = new Git(repo)) {
        if (!isBranchExists(review.getTargetRef())) {
            throw new GitClientException(
                    "Review target ref does not exist: " + requestCommitHash + ", " + review.getTargetRef());
        }

        if (review.getReviewRef() == null || review.getReviewRef().isEmpty()) {
            // If there is no review ref, then show the diff from the single commit.
            RevCommit revCommit = resolveRevCommit(requestCommitHash);
            return calculateCommitDiffs(git, resolveParentRevCommit(revCommit), revCommit);
        } else if (isBranchExists(review.getReviewRef())
                && !areAncestorDescendent(review.getReviewRef(), review.getTargetRef())) {
            // If the review ref branch exists and is not already submitted,
            // then show the diff between review ref and target ref.
            return calculateBranchDiffs(git, review.getTargetRef(), review.getReviewRef());
        } else {
            // If the review ref points to a non-existent branch, the review is over, so read the
            // comments and diff between the parent and the "last" (chronologically) one.
            Map<String, ReviewComment> comments = listCommentsForReview(git, requestCommitHash);
            RevCommit revCommit = resolveRevCommit(requestCommitHash);
            RevCommit parent = resolveParentRevCommit(revCommit);
            RevCommit last = findLastCommitInComments(comments.values(), revCommit);
            return calculateCommitDiffs(git, parent, last);
        }
    }
}

From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java

License:Open Source License

/**
 * Fetches review and comment git notes and updates the local refs, performing
 * merges if necessary.// w  ww.  j av  a  2 s . co  m
 */
public void syncCommentsAndReviews() throws GitClientException {
    RevWalk revWalk = null;
    try (Git git = new Git(repo)) {
        revWalk = new RevWalk(repo);

        // Fetch the latest.
        RefSpec spec = new RefSpec(DEVTOOLS_PULL_REFSPEC);
        git.fetch().setRefSpecs(spec).call();

        syncNotes(revWalk, COMMENTS_REF, COMMENTS_ORIGIN_REF);
        revWalk.reset();
        syncNotes(revWalk, REVIEWS_REF, REVIEWS_ORIGIN_REF);
    } catch (Exception e) {
        throw new GitClientException("Error syncing notes", e);
    } finally {
        if (revWalk != null) {
            revWalk.close();
        }
    }
}

From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java

License:Open Source License

/**
 * Gets all the comments for a specific review hash, by comment id.
 * The comment id is conventionally the SHA-1 hash of its JSON string.
 *///from  www.  j a  v  a  2s.  co m
public Map<String, ReviewComment> listCommentsForReview(String requestCommitHash) throws GitClientException {
    try (Git git = new Git(repo)) {
        return listCommentsForReview(git, requestCommitHash);
    }
}

From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java

License:Open Source License

/**
 * Gets the diff between heads on two branches.
 */// w  w  w . ja  v a  2s . co m
public List<DiffEntry> calculateBranchDiffs(String targetRef, String reviewRef) throws GitClientException {
    try (Git git = new Git(repo)) {
        return calculateBranchDiffs(git, targetRef, reviewRef);
    } catch (Exception e) {
        throw new GitClientException("Error loading branch diffs for " + reviewRef + " and " + targetRef, e);
    }
}

From source file:com.google.appraise.eclipse.ui.editor.AppraiseReviewTaskActivationListener.java

License:Open Source License

/**
 * Asks the user if they want to switch to the review branch, and performs
 * the switch if so./*from   w  ww . ja  v a 2 s.co m*/
 */
private void promptSwitchToReviewBranch(TaskRepository taskRepository, String reviewBranch) {
    MessageDialog dialog = new MessageDialog(null, "Appraise Review", null,
            "Do you want to switch to the review branch (" + reviewBranch + ")", MessageDialog.QUESTION,
            new String[] { "Yes", "No" }, 0);
    int result = dialog.open();
    if (result == 0) {
        Repository repo = AppraisePluginUtils.getGitRepoForRepository(taskRepository);
        try (Git git = new Git(repo)) {
            previousBranch = repo.getFullBranch();
            git.checkout().setName(reviewBranch).call();
        } catch (RefNotFoundException rnfe) {
            MessageDialog alert = new MessageDialog(null, "Oops", null, "Branch " + reviewBranch + " not found",
                    MessageDialog.INFORMATION, new String[] { "OK" }, 0);
            alert.open();
        } catch (Exception e) {
            AppraiseUiPlugin.logError("Unable to switch to review branch: " + reviewBranch, e);
        }
    }
}

From source file:com.google.appraise.eclipse.ui.editor.AppraiseReviewTaskActivationListener.java

License:Open Source License

@Override
public void taskDeactivated(ITask task) {
    if (task == null) {
        return;//ww w .  j av  a 2 s. c o  m
    }

    TaskData taskData = loadTaskData(task);
    TaskRepository taskRepository = TasksUi.getRepositoryManager()
            .getRepository(AppraiseConnectorPlugin.CONNECTOR_KIND, taskData.getRepositoryUrl());

    if (previousBranch != null) {
        Repository repo = AppraisePluginUtils.getGitRepoForRepository(taskRepository);
        try (Git git = new Git(repo)) {
            git.checkout().setName(previousBranch).call();
        } catch (Exception e) {
            AppraiseUiPlugin.logError("Unable to switch to previous branch: " + previousBranch, e);
        }
    }

    int depth = IResource.DEPTH_INFINITE;
    IProject project = AppraisePluginUtils.getProjectForRepository(taskRepository);
    try {
        project.deleteMarkers(AppraiseUiPlugin.REVIEW_TASK_MARKER_ID, true, depth);
    } catch (CoreException e) {
        AppraiseUiPlugin.logError("Error deleting review markers for task " + task.getTaskId(), e);
    }
}

From source file:com.google.gct.idea.appengine.synchronization.SampleSyncTaskTest.java

License:Apache License

@Override
public void setUp() throws Exception {
    super.setUp();

    mockAndroidRepoPath = System.getProperty("java.io.tmpdir") + "/android/mockAndroidRepo";
    String mockGitHubRepoGitPath = db.getDirectory().getPath();
    mockGitHubRepoPath = mockGitHubRepoGitPath.substring(0, mockGitHubRepoGitPath.lastIndexOf('/'));

    // Configure the mock github repo
    StoredConfig targetConfig = db.getConfig();
    targetConfig.setString("branch", "master", "remote", "origin");
    targetConfig.setString("branch", "master", "merge", "refs/heads/master");

    RemoteConfig config = new RemoteConfig(targetConfig, "origin");
    config.addURI(new URIish(mockGitHubRepoGitPath));
    config.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
    config.update(targetConfig);//from   ww  w .  ja  v a 2  s. c o  m

    targetConfig.save();

    mockGitHubRepo = new Git(db);

    // commit something
    writeTrashFile("Test.txt", "Hello world");
    mockGitHubRepo.add().addFilepattern("Test.txt").call();
    mockGitHubRepo.commit().setMessage("Initial commit").call();
    mockGitHubRepo.tag().setName("tag-initial").setMessage("Tag initial").call();

}

From source file:com.google.gerrit.acceptance.api.change.SubmitTypeRuleIT.java

License:Apache License

private List<RevCommit> log(String commitish, int n) throws Exception {
    try (Repository repo = repoManager.openRepository(project); Git git = new Git(repo)) {
        ObjectId id = repo.resolve(commitish);
        assertThat(id).isNotNull();//from w w w. j  a  va 2  s.co m
        return ImmutableList.copyOf(git.log().add(id).setMaxCount(n).call());
    }
}