Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » JGit: How to get branch from a commit id(How to find the branch from a particular commit id)
JGit: How to get branch from a commit id [message #784829] Sat, 28 January 2012 02:45 Go to next message
kamalakannan muralidharan is currently offline kamalakannan muralidharanFriend
Messages: 3
Registered: January 2012
Junior Member
Hi

I want the code snippet (using JGit), to get the branch on which a commit was made.
This code is needed because when a checkout happens from a commit ID, the normal api of getbranch returns the commit id (because it will be detached No_branch state). so i need to find the actual branch from commit ID


Regards
Kamal
Re: JGit: How to get branch from a commit id [message #786302 is a reply to message #784829] Mon, 30 January 2012 08:43 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 18
Registered: July 2009
Junior Member
I don't think that Git can tell that always. With the help of reflog you may find out for locally created versions (only for a certain period of time until old reflog entries are deleted), but not for versions which you have received from remote. With which native git commands would you get this information?
Re: JGit: How to get branch from a commit id [message #786758 is a reply to message #786302] Mon, 30 January 2012 19:49 Go to previous messageGo to next message
kamalakannan muralidharan is currently offline kamalakannan muralidharanFriend
Messages: 3
Registered: January 2012
Junior Member
Thanks for the info.

git branch --contains <commit>
Re: JGit: How to get branch from a commit id [message #786850 is a reply to message #786758] Mon, 30 January 2012 22:05 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 274
Registered: July 2009
Senior Member
'git branch --contains <commit>' will report every branch which contains this commit. In the standard push/fetch workflows for all commits you get from remote only "origin/master" is reported. And even for local commits: Imagine you have merged your feature branch back to master. Then this command will report also the master branch and all feature branches created after merging. Git simply doesn't store for a revision on which branch it was created.

After these warnings: a rough implementation of "git branch --conttains <commit>" could look like this:

		Repository repo = new FileRepository(args[0]);
		RevWalk walk = new RevWalk(repo);
		RevCommit commit = walk.parseCommit(repo.resolve(args[1] + "^0"));
		for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet())
			if (e.getKey().startsWith(Constants.R_HEADS))
				if (walk.isMergedInto(commit,
						walk.parseCommit(e.getValue().getObjectId())))
					System.out.println("Ref " + e.getValue().getName()
							+ " contains commit " + commit);




Ciao
Chris
Re: JGit: How to get branch from a commit id [message #786873 is a reply to message #786850] Mon, 30 January 2012 22:52 Go to previous message
kamalakannan muralidharan is currently offline kamalakannan muralidharanFriend
Messages: 3
Registered: January 2012
Junior Member
Thanks for information.
Previous Topic:History of selected resource broken?
Next Topic:strategy resolve resulted in: Failed
Goto Forum:
  


Current Time: Tue Apr 23 14:26:33 GMT 2024

Powered by FUDForum. Page generated in 0.03805 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top