Skip to content

Commit

Permalink
git: align tests and code with commit fmt change
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Dec 13, 2022
1 parent 761b254 commit 6e2b4a4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
19 changes: 9 additions & 10 deletions git/gogit/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ func (g *Client) cloneBranch(ctx context.Context, url, branch string, opts repos
if head != "" && head == opts.LastObservedCommit {
// Construct a non-concrete commit with the existing information.
// Split the revision and take the last part as the hash.
// Example revision: main/43d7eb9c49cdd49b2494efd481aea1166fc22b67
// Example revision: main@sha1:43d7eb9c49cdd49b2494efd481aea1166fc22b67
var hash git.Hash
ss := strings.Split(head, "/")
ss := strings.Split(head, "@")
if len(ss) > 1 {
hash = git.Hash(ss[len(ss)-1])
hash = git.Hash(strings.TrimPrefix(ss[len(ss)-1], "sha1:"))
} else {
hash = git.Hash(ss[0])
hash = git.Hash(strings.TrimPrefix(ss[0], "sha1:"))
}
c := &git.Commit{
Hash: hash,
Expand Down Expand Up @@ -148,13 +148,13 @@ func (g *Client) cloneTag(ctx context.Context, url, tag string, opts repository.
if head != "" && head == opts.LastObservedCommit {
// Construct a non-concrete commit with the existing information.
// Split the revision and take the last part as the hash.
// Example revision: 6.1.4/bf09377bfd5d3bcac1e895fa8ce52dc76695c060
// Example revision: 6.1.4@sha1:bf09377bfd5d3bcac1e895fa8ce52dc76695c060
var hash git.Hash
ss := strings.Split(head, "/")
ss := strings.Split(head, "@")
if len(ss) > 1 {
hash = git.Hash(ss[len(ss)-1])
hash = git.Hash(strings.TrimPrefix(ss[len(ss)-1], "sha1:"))
} else {
hash = git.Hash(ss[0])
hash = git.Hash(strings.TrimPrefix(ss[0], "sha1:"))
}
c := &git.Commit{
Hash: hash,
Expand Down Expand Up @@ -406,10 +406,9 @@ func getRemoteHEAD(ctx context.Context, url string, ref plumbing.ReferenceName,
func filterRefs(refs []*plumbing.Reference, currentRef plumbing.ReferenceName) string {
for _, ref := range refs {
if ref.Name().String() == currentRef.String() {
return fmt.Sprintf("%s/%s", currentRef.Short(), ref.Hash().String())
return fmt.Sprintf("%s@sha1:%s", currentRef.Short(), ref.Hash().String())
}
}

return ""
}

Expand Down
20 changes: 10 additions & 10 deletions git/gogit/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ func TestClone_cloneBranch(t *testing.T) {
name: "skip clone if LastRevision hasn't changed",
branch: "master",
filesCreated: map[string]string{"branch": "init"},
lastRevision: fmt.Sprintf("master/%s", firstCommit.String()),
lastRevision: fmt.Sprintf("master@sha1:%s", firstCommit.String()),
expectedCommit: firstCommit.String(),
expectedConcreteCommit: false,
},
{
name: "Other branch - revision has changed",
branch: "test",
filesCreated: map[string]string{"branch": "second"},
lastRevision: fmt.Sprintf("master/%s", firstCommit.String()),
lastRevision: fmt.Sprintf("master@sha1:%s", firstCommit.String()),
expectedCommit: secondCommit.String(),
expectedConcreteCommit: true,
},
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestClone_cloneBranch(t *testing.T) {
}

g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc.String()).To(Equal(tt.branch + "/" + tt.expectedCommit))
g.Expect(cc.String()).To(Equal(tt.branch + "@sha1:" + tt.expectedCommit))
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(tt.expectedConcreteCommit))

if tt.expectedConcreteCommit {
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestClone_cloneTag(t *testing.T) {
// If last revision is provided, configure it.
if tt.lastRevTag != "" {
lc := tagCommits[tt.lastRevTag]
opts.LastObservedCommit = fmt.Sprintf("%s/%s", tt.lastRevTag, lc)
opts.LastObservedCommit = fmt.Sprintf("%s@sha1:%s", tt.lastRevTag, lc)
}

cc, err := ggc.Clone(context.TODO(), path, opts)
Expand All @@ -274,7 +274,7 @@ func TestClone_cloneTag(t *testing.T) {
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(tt.expectConcreteCommit))
targetTagHash := tagCommits[tt.checkoutTag]
g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc.String()).To(Equal(tt.checkoutTag + "/" + targetTagHash))
g.Expect(cc.String()).To(Equal(tt.checkoutTag + "@sha1:" + targetTagHash))

// Check file content only when there's an actual checkout.
if tt.lastRevTag != tt.checkoutTag {
Expand Down Expand Up @@ -314,14 +314,14 @@ func TestClone_cloneCommit(t *testing.T) {
{
name: "Commit",
commit: firstCommit.String(),
expectCommit: "HEAD/" + firstCommit.String(),
expectCommit: "sha1:" + firstCommit.String(),
expectFile: "init",
},
{
name: "Commit in specific branch",
commit: secondCommit.String(),
branch: "other-branch",
expectCommit: "other-branch/" + secondCommit.String(),
expectCommit: "other-branch@sha1:" + secondCommit.String(),
expectFile: "second",
},
{
Expand Down Expand Up @@ -470,7 +470,7 @@ func TestClone_cloneSemVer(t *testing.T) {
}

g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc.String()).To(Equal(tt.expectTag + "/" + refs[tt.expectTag]))
g.Expect(cc.String()).To(Equal(tt.expectTag + "@sha1:" + refs[tt.expectTag]))
g.Expect(filepath.Join(tmpDir, "tag")).To(BeARegularFile())
g.Expect(os.ReadFile(filepath.Join(tmpDir, "tag"))).To(BeEquivalentTo(tt.expectTag))
})
Expand Down Expand Up @@ -970,7 +970,7 @@ func Test_getRemoteHEAD(t *testing.T) {
ref := plumbing.NewBranchReferenceName(git.DefaultBranch)
head, err := getRemoteHEAD(context.TODO(), path, ref, &git.AuthOptions{}, nil)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(head).To(Equal(fmt.Sprintf("%s/%s", git.DefaultBranch, cc)))
g.Expect(head).To(Equal(fmt.Sprintf("%s@sha1:%s", git.DefaultBranch, cc)))

cc, err = commitFile(repo, "test", "testing current head tag", time.Now())
g.Expect(err).ToNot(HaveOccurred())
Expand All @@ -980,7 +980,7 @@ func Test_getRemoteHEAD(t *testing.T) {
ref = plumbing.NewTagReferenceName("v0.1.0")
head, err = getRemoteHEAD(context.TODO(), path, ref, &git.AuthOptions{}, nil)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(head).To(Equal(fmt.Sprintf("%s/%s", "v0.1.0", cc)))
g.Expect(head).To(Equal(fmt.Sprintf("%s@sha1:%s", "v0.1.0", cc)))
}

func TestClone_CredentialsOverHttp(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions git/libgit2/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (l *Client) cloneBranch(ctx context.Context, url, branch string, opts repos
}
if len(heads) > 0 {
hash := heads[0].Id.String()
remoteHead := fmt.Sprintf("%s/%s", branch, hash)
remoteHead := fmt.Sprintf("%s@sha1:%s", branch, hash)
if remoteHead == opts.LastObservedCommit {
// Construct a non-concrete commit with the existing information.
c := &git.Commit{
Expand Down Expand Up @@ -173,13 +173,13 @@ func (l *Client) cloneTag(ctx context.Context, url, tag string, opts repository.
}
if len(heads) > 0 {
hash := heads[0].Id.String()
remoteHEAD := fmt.Sprintf("%s/%s", tag, hash)
remoteHEAD := fmt.Sprintf("%s@sha1:%s", tag, hash)
var same bool
if remoteHEAD == opts.LastObservedCommit {
same = true
} else if len(heads) > 1 {
hash = heads[1].Id.String()
remoteAnnotatedHEAD := fmt.Sprintf("%s/%s", tag, hash)
remoteAnnotatedHEAD := fmt.Sprintf("%s@sha1:%s", tag, hash)
if remoteAnnotatedHEAD == opts.LastObservedCommit {
same = true
}
Expand Down
14 changes: 7 additions & 7 deletions git/libgit2/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ func TestClone_cloneBranch(t *testing.T) {
name: "skip clone - lastRevision hasn't changed",
branch: defaultBranch,
filesCreated: map[string]string{"branch": "second"},
lastRevision: fmt.Sprintf("%s/%s", defaultBranch, secondCommit.String()),
lastRevision: fmt.Sprintf("%s@sha1:%s", defaultBranch, secondCommit.String()),
expectedCommit: secondCommit.String(),
expectedConcreteCommit: false,
},
{
name: "lastRevision is different",
branch: defaultBranch,
filesCreated: map[string]string{"branch": "second"},
lastRevision: fmt.Sprintf("%s/%s", defaultBranch, firstCommit.String()),
lastRevision: fmt.Sprintf("%s@sha1:%s", defaultBranch, firstCommit.String()),
expectedCommit: secondCommit.String(),
expectedConcreteCommit: true,
},
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestClone_cloneBranch(t *testing.T) {
return
}
g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc.String()).To(Equal(tt.branch + "/" + tt.expectedCommit))
g.Expect(cc.String()).To(Equal(tt.branch + "@sha1:" + tt.expectedCommit))
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(tt.expectedConcreteCommit))

if tt.expectedConcreteCommit {
Expand Down Expand Up @@ -272,7 +272,7 @@ func TestClone_cloneTag(t *testing.T) {
// If last revision is provided, configure it.
if tt.lastRevTag != "" {
lc := tagCommits[tt.lastRevTag]
cloneOpts.LastObservedCommit = fmt.Sprintf("%s/%s", tt.lastRevTag, lc.Id().String())
cloneOpts.LastObservedCommit = fmt.Sprintf("%s@sha1:%s", tt.lastRevTag, lc.Id().String())
}

cc, err := lgc.Clone(context.TODO(), repoURL, cloneOpts)
Expand All @@ -286,7 +286,7 @@ func TestClone_cloneTag(t *testing.T) {
// Check successful checkout results.
targetTagCommit := tagCommits[tt.checkoutTag]
g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc.String()).To(Equal(tt.checkoutTag + "/" + targetTagCommit.Id().String()))
g.Expect(cc.String()).To(Equal(tt.checkoutTag + "@sha1:" + targetTagCommit.Id().String()))
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(tt.expectConcreteCommit))

// Check file content only when there's an actual checkout.
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestClone_cloneCommit(t *testing.T) {
})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc).ToNot(BeNil())
g.Expect(cc.String()).To(Equal("HEAD/" + c.String()))
g.Expect(cc.String()).To(Equal("sha1:" + c.String()))
g.Expect(filepath.Join(tmpDir, "commit")).To(BeARegularFile())
g.Expect(os.ReadFile(filepath.Join(tmpDir, "commit"))).To(BeEquivalentTo("init"))

Expand Down Expand Up @@ -500,7 +500,7 @@ func TestClone_cloneSemVer(t *testing.T) {
}

g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc.String()).To(Equal(tt.expectTag + "/" + refs[tt.expectTag]))
g.Expect(cc.String()).To(Equal(tt.expectTag + "@sha1:" + refs[tt.expectTag]))
g.Expect(filepath.Join(tmpDir, "tag")).To(BeARegularFile())
g.Expect(os.ReadFile(filepath.Join(tmpDir, "tag"))).To(BeEquivalentTo(tt.expectTag))
})
Expand Down

0 comments on commit 6e2b4a4

Please sign in to comment.