Skip to content

Commit

Permalink
Merge pull request #248 from ajoberstar/abbrev
Browse files Browse the repository at this point in the history
Ensure abbreviated commit IDs are unique
  • Loading branch information
ajoberstar authored Jul 4, 2018
2 parents 867b8be + 21c5331 commit 0775a63
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 13 deletions.
10 changes: 8 additions & 2 deletions src/main/groovy/org/ajoberstar/grgit/Commit.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Commit {
*/
String id

/**
* The abbreviated hash of the commit.
*/
String abbreviatedId

/**
* Hashes of any parent commits.
*/
Expand Down Expand Up @@ -67,9 +72,10 @@ class Commit {
/**
* The first {@code length} characters of the commit hash.
* @param length the number of characters to abbreviate the
* hash to (defaults to 7)
* hash.
*/
String getAbbreviatedId(int length = 7) {
@Deprecated
String getAbbreviatedId(int length) {
return id[0..(length - 1)]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ class CommitOp implements Callable<Commit> {
if (all) { cmd.all = all }
cmd.amend = amend
RevCommit commit = cmd.call()
return JGitUtil.convertCommit(commit)
return JGitUtil.convertCommit(repo, commit)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ class LogOp implements Callable<List<Commit>> {
}
cmd.skip = skipCommits
cmd.maxCount = maxCommits
return cmd.call().collect { JGitUtil.convertCommit(it) }.asImmutable()
return cmd.call().collect { JGitUtil.convertCommit(repo, it) }.asImmutable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ class RevertOp implements Callable<Commit> {
if (cmd.failingResult) {
throw new IllegalStateException("Could not merge reverted commits (conflicting files can be retrieved with a call to grgit.status()): ${cmd.failingResult}")
}
return JGitUtil.convertCommit(commit)
return JGitUtil.convertCommit(repo, commit)
}
}
7 changes: 4 additions & 3 deletions src/main/groovy/org/ajoberstar/grgit/util/JGitUtil.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,18 @@ class JGitUtil {
*/
static Commit resolveCommit(Repository repo, ObjectId id) {
RevWalk walk = new RevWalk(repo.jgit.repository)
return convertCommit(walk.parseCommit(id))
return convertCommit(repo, walk.parseCommit(id))
}

/**
* Converts a JGit commit to a Grgit commit.
* @param rev the JGit commit to convert
* @return a corresponding Grgit commit
*/
static Commit convertCommit(RevCommit rev) {
static Commit convertCommit(Repository repo, RevCommit rev) {
Map props = [:]
props.id = ObjectId.toString(rev)
props.abbreviatedId = repo.jgit.repository.newObjectReader().abbreviate(rev).name()
PersonIdent committer = rev.committerIdent
props.committer = new Person(committer.name, committer.emailAddress)
PersonIdent author = rev.authorIdent
Expand Down Expand Up @@ -144,7 +145,7 @@ class JGitUtil {
RevTag rev = walk.parseTag(ref.objectId)
RevObject target = walk.peel(rev)
walk.parseBody(rev.object)
props.commit = convertCommit(target)
props.commit = convertCommit(repo, target)
PersonIdent tagger = rev.taggerIdent
props.tagger = new Person(tagger.name, tagger.emailAddress)
props.fullMessage = rev.fullMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LogOpSpec extends SimpleGitOpSpec {
grgit.checkout(branch: 'master')
def jgitId = JGitUtil.resolveObject(grgit.repository, commits[2].id)
def mergeCommit = grgit.repository.jgit.merge().include(jgitId).setStrategy(MergeStrategy.OURS).call().newHead
commits << JGitUtil.convertCommit(mergeCommit)
commits << JGitUtil.convertCommit(grgit.repository, mergeCommit)

testFile1 << '4'
grgit.add(patterns: ['.'])
Expand Down
9 changes: 5 additions & 4 deletions src/test/groovy/org/ajoberstar/grgit/util/JGitUtilSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class JGitUtilSpec extends Specification {
ZonedDateTime commitTime = ZonedDateTime.ofInstant(instant, zone)
Commit expectedCommit = new Commit(
ObjectId.toString(commits[1]),
ObjectId.toString(commits[1])[0..6],
[ObjectId.toString(commits[0])],
person,
person,
Expand All @@ -105,7 +106,7 @@ class JGitUtilSpec extends Specification {
'second commit'
)
expect:
def result = JGitUtil.convertCommit(commits[1])
def result = JGitUtil.convertCommit(repo, commits[1])
result == expectedCommit
result.date.toInstant() == commitTime.toInstant()
}
Expand All @@ -119,7 +120,7 @@ class JGitUtilSpec extends Specification {
and:
ZonedDateTime after = ZonedDateTime.now().plusSeconds(2)
then:
tag.commit == JGitUtil.convertCommit(commits[0])
tag.commit == JGitUtil.convertCommit(repo, commits[0])
tag.tagger == person
tag.fullName == 'refs/tags/v1.0.0'
tag.fullMessage == 'first tag\ntesting'
Expand All @@ -137,7 +138,7 @@ class JGitUtilSpec extends Specification {
and:
ZonedDateTime after = ZonedDateTime.now().plusSeconds(2)
then:
tag.commit == JGitUtil.convertCommit(commits[0])
tag.commit == JGitUtil.convertCommit(repo, commits[0])
tag.tagger == null
tag.fullName == 'refs/tags/v2.0.0'
tag.fullMessage == null
Expand All @@ -154,7 +155,7 @@ class JGitUtilSpec extends Specification {
and:
ZonedDateTime after = ZonedDateTime.now().plusSeconds(2)
then:
tag.commit == JGitUtil.convertCommit(commits[0])
tag.commit == JGitUtil.convertCommit(repo, commits[0])
tag.tagger == person
tag.fullName == 'refs/tags/v1.1.0'
tag.fullMessage == 'testing'
Expand Down

0 comments on commit 0775a63

Please sign in to comment.