Skip to content

Commit

Permalink
git: change Commit#String format
Browse files Browse the repository at this point in the history
This changes the string format of `Commit` to use an `@` separator
instead of `/`, and drops the usage of "HEAD" as a virtual named
reference for commits without a named pointer (e.g. a Git branch and/or
tag). Matching the (revision) format proposed in RFC-0005.

Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Dec 13, 2022
1 parent 0b80dfb commit 761b254
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ type Commit struct {
// for a "tag-1" tag.
func (c *Commit) String() string {
if short := strings.SplitAfterN(c.Reference, "/", 3); len(short) == 3 {
return fmt.Sprintf("%s/%s", short[2], c.Hash)
return fmt.Sprintf("%s@sha1:%s", short[2], c.Hash)
}
return fmt.Sprintf("HEAD/%s", c.Hash)
return fmt.Sprintf("sha1:%s", c.Hash)
}

// Verify the Signature of the commit with the given key rings.
Expand Down Expand Up @@ -121,8 +121,8 @@ var (
)

// IsConcreteCommit returns if a given commit is a concrete commit. Concrete
// commits have most of commit metadata and commit content. In contrast, a
// partial commit may only have some metadata and no commit content.
// commits have most of the commit metadata and content. In contrast, a partial
// commit may only have some metadata and no commit content.
func IsConcreteCommit(c Commit) bool {
if c.Hash != nil && c.Encoded != nil {
return true
Expand Down
8 changes: 4 additions & 4 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,22 @@ func TestCommit_String(t *testing.T) {
Hash: []byte("commit"),
Reference: "refs/heads/main",
},
want: "main/commit",
want: "main@sha1:commit",
},
{
name: "Reference with slash and commit",
commit: &Commit{
Hash: []byte("commit"),
Reference: "refs/heads/feature/branch",
},
want: "feature/branch/commit",
want: "feature/branch@sha1:commit",
},
{
name: "No reference",
name: "No name reference",
commit: &Commit{
Hash: []byte("commit"),
},
want: "HEAD/commit",
want: "sha1:commit",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 761b254

Please sign in to comment.