1

I'm writing my program in java using Class Repository from JGit.

This class has the method resolve(String revstr).

Basically this methoad parses a git revision string and returns an object id. There are specific combinations of these operators which are supported:

HEAD, MERGE_HEAD, FETCH_HEAD
SHA-1: a complete or abbreviated SHA-1
refs/...: a complete reference name
short-name: a short reference name under refs/heads, refs/tags, or refs/remotes namespace
tag-NN-gABBREV: output from describe, parsed by treating ABBREV as an abbreviated SHA-1.
id^: first parent of commit id, this is the same as id^1
id^0: ensure id is a commit
id^n: n-th parent of commit id
id~n: n-th historical ancestor of id, by first parent. id~3 is equivalent to id^1^1^1 or id^^^.
id:path: Lookup path under tree named by id
id^{commit}: ensure id is a commit
id^{tree}: ensure id is a tree
id^{tag}: ensure id is a tag
id^{blob}: ensure id is a blob

I want to use this method

Repository repo;
ObjectId commit = repo.resolve("cnfuwfxmiazsdixfnsdiufsdhfiusfhsfisfh^{tree}");
System.out.println(commit );

Expected Ouput:

cnfuwfxmiazsdixfnsdiufsdhfiusfhsfisfh

Output:

sjakfshdofcsmdfocsdfjdofdjgdhgfdgfhgf
//Output is another commit in project.

I want to have the commit which I've inserted, but it shows me another commit. I think because it is the first parent of the commit id.

Is there any way to get the same commit id which I put into the method resolve()?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
thuyanh
  • 23
  • 8

1 Answers1

1

The expression that you pass to resolve() asks for the tree id of cnf... (BTW, Git object ids consists of hexadecimal characters only). And this is most likely what you get, the tree id of the commit that you specified.

In order to convert a string into a JGit ObjectId, use

ObjectId objectId = ObjectId.fromString( "..." );

See also: How to obtain the RevCommit or ObjectId from a SHA1 ID string with JGit?

Community
  • 1
  • 1
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Thank you very much. I'm reading your JGit cookbook and would like to understand your code in the command 'Show diff of changes to all files between two commits'. The next steps will be instantiating a reader to read the data from the Git database and creation of the tree iterator for each commit. I've tried to test it, but it doesn't work. Can you please help me? – thuyanh Jun 01 '16 at 15:16
  • I didn't write the _JGit cookbook_ and I'm not sure what you are talking about. If you have a different question, please do thorough research if there is no solution to be found, then write a new SO post. – Rüdiger Herrmann Jun 01 '16 at 15:24
  • Thank you. I thought you wrote the JGit cookbook. I will write a new SO post. – thuyanh Jun 01 '16 at 15:29
  • Don't mind. I wrote several blog posts about JGit (see here: http://www.codeaffine.com/?s=jgit) but not the JGit cookbook. – Rüdiger Herrmann Jun 02 '16 at 08:21
  • I read all your blog posts about JGit before and they helped me a lot. Thank you ;) – thuyanh Jun 02 '16 at 08:25