Skip to content

Commit cbad455

Browse files
authored
Merge pull request #205 from thockin/git-cat-file
Make revIsHash a bit safer
2 parents 97a5488 + 1b295ad commit cbad455

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

cmd/git-sync/main.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,21 @@ func hashForRev(ctx context.Context, rev, gitRoot string) (string, error) {
534534
}
535535

536536
func revIsHash(ctx context.Context, rev, gitRoot string) (bool, error) {
537-
// If rev is a tag name or HEAD, rev-parse will produce the git hash. If
538-
// rev is already a git hash, the output will be the same hash. Of course, a
539-
// user could specify "abc" and match "abcdef12345678", so we just do a
540-
// prefix match.
541-
output, err := hashForRev(ctx, rev, gitRoot)
537+
// If git doesn't identify rev as a commit, we're done.
538+
output, err := runCommand(ctx, gitRoot, *flGitCmd, "cat-file", "-t", rev)
539+
if err != nil {
540+
return false, err
541+
}
542+
o := strings.Trim(string(output), "\n")
543+
if o != "commit" {
544+
return false, nil
545+
}
546+
547+
// `git cat-file -t` also returns "commit" for tags. If rev is already a git
548+
// hash, the output will be the same hash as the input. Of course, a user
549+
// could specify "abc" and match "abcdef12345678", so we just do a prefix
550+
// match.
551+
output, err = hashForRev(ctx, rev, gitRoot)
542552
if err != nil {
543553
return false, err
544554
}

0 commit comments

Comments
 (0)