-1

I have a SHA-1 of a newly cloned repository. I want the author indent of this SHA-1.

So I need to use RevWalk and iterate the whole repository? Or is there a findXX method or other code I can use to get the RevCommit or another object that has the PersonIdent?

What I tried:

public void authorInfoOf(Repository repo, AnyObjectId head) {
    try {
        try (RevWalk walk = new RevWalk(repo)) {
            ObjectDatabase db = repo.getObjectDatabase();
            ObjectLoader k = repo.newObjectReader().open(head);
            ObjectReader s;
            // repo.newObjectReader().open(head);
            ObjectStream st = k.openStream();
            // RevWalk rw2 = new RevWalk(k);
            RevCommit commit = null;// walk.parseCommit(ref.getObjectId());

            PersonIdent authorIndent = commit.getAuthorIdent();
            System.out.println("\nCommit-Message: " + commit.getFullMessage() + " " + authorIndent.getEmailAddress());
        }
    } catch (Exception e) {
        System.out.println("Authir info of Anybject id Err " + e);
        e.printStackTrace();
    }
}
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
tgkprog
  • 4,493
  • 4
  • 41
  • 70
  • Possible duplicate of [How to obtain the RevCommit or ObjectId from a SHA1 ID string with JGit?](http://stackoverflow.com/questions/25755475/how-to-obtain-the-revcommit-or-objectid-from-a-sha1-id-string-with-jgit) – Rüdiger Herrmann Feb 01 '17 at 16:01

2 Answers2

2

The RevCommit represents a particular commit in a Git repository. Use RevWalk::parseCommit() to obtain the RevCommit for a specific object id/SHA-1.

For example:

try( RevWalk walk = new RevWalk( repository ) ) {
  RevCommit commit = walk.parseCommit( ref.getObjectId() );
}

parseCommit returns the matching commit object for the given ObjectId.

In order to convert a SHA-1 (string) into an ObjectId, use ObjectId::fromString():

ObjectId commitId = ObjectId.fromString( "ab434..." );

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

In the above example, a Ref was used to reference the object id. Refs represent named references to object ids like branches, tags, or special refs like HEAD. Repository::exactRef() can be used to resolve a string to a Ref object.

For example:

Ref headRef = repository.exactRef( "HEAD" );
Community
  • 1
  • 1
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Yes know how to get for head,but can I use it to get a Ref to a Commit by Sha-1 like Ref r1 = repo.exactRef("0375381d0f467d55c832b50aca54ad16858baa8b"); - gives me a big fat null. How to get a ref using a SHA? – tgkprog Feb 01 '17 at 15:20
0

This worked for me:

public RevCommit getCommit(String idString, Repository repository, Git git){
    try{

        ObjectId o1 = repository.resolve(idString);
        if(o1 != null){
            try( RevWalk walk = new RevWalk( repository ) ) {
                  RevCommit commit = walk.parseCommit( o1 );
                  PersonIdent ai = commit.getAuthorIdent();
                  System.out.println("bbb >>" + ai.getEmailAddress() + "|" + ai.getTimeZone()  + "|" + ai.getWhen() + ", ref :" + idString);
                  return commit;
                }
        }else{
            System.err.println("Could not get commit with SHA :" + idString);
        }
    }catch (Exception e) {
        System.out.println("err :" + e);
        e.printStackTrace();
    }
    return null;
}
tgkprog
  • 4,493
  • 4
  • 41
  • 70