how to get the content of a commit?

I'm trying to enforce a script onto the contents of a commit, but I've yet to figure out how to get the actual commit contents. I see examples on how to obtain the name, JIRA tickets, and other stuff but how do I get the commit that is seen in git on bitbucket?

Thanks

5 answers

1 accepted

0 vote
Adam Ahmed Atlassian Team Jun 22, 2016

You have a couple options. If you really want the output of git log -p, you can run it directly:

MyThing(GitCommandBuilderFactory builderFactory) {
    this.builderFactory = builderFactory;
}
public String getItAll(Repository repo, String commitId) {
    return builderFactory.builder(repo).command("log").argument("-p").argument("-n").argument("1").argument(commitId).call(new StringOutputHandler());
}

Or you can run a few commands to get different parts into their respective business objects:

MyThing(CommitService commitService) {
    this.commitService = commitService;
}
public Map<String, Object> getItInParts(Repository repo, String commitId) {
    Commit c = commitService.getCommit(new CommitRequest.Builder(repo, commitId).build());
    PagedIterable<Change> changes = new PagedIterable(new PageProvider<Change>() {
        public Page<Change> get(PageRequest pageRequest) {
            return commitService.getChanges(new ChangesRequest.Builder(repo, commitId).build(), pageRequest);
        }
    }, 25);
    commitService.streamDiff(new DiffRequest.Builder(repo, commitId).build(), new DiffContentCallback() {
        // ... implement methods that are called as the diff is streamed.
        // Note that the content will be capped after 10k lines across the change. You can get diffs of individual paths if you need to.
    });    
}

Note that the changes and the diff might be redundant if you don't actually need all the information about the commit.

I haven't tested either of those snippets, so they probably don't actually work, but that's the gist of how to get the information - a good starting point at least.

0 vote
Adam Ahmed Atlassian Team Jun 21, 2016

By "content" do you mean the commit message, or the list of files changed, or the diffs of those files, or something else?

the original content of the commit, as in everything that was seen in git for the commit

0 vote
Adam Ahmed Atlassian Team Jun 22, 2016

That's still not really clarifying it for me. What does "seen in git" mean? Do you want the output of a particular git command?

I want the output of git log -p or git show basically

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jun 12, 2018 in Bitbucket

Do you use any Atlassian products for your personal projects?

After spinning my wheels trying to get organized enough to write a book for National Novel Writing Month (NaNoWriMo) I took my affinity for Atlassian products from my work life and decided to tr...

174 views 20 8
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you