Skip to content

Commit

Permalink
[WIP] git: Add support to list peeled refs
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
  • Loading branch information
pjbgf committed Apr 26, 2023
1 parent 0542a30 commit e535839
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
11 changes: 7 additions & 4 deletions _examples/ls-remote/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ import (
// Retrieve remote tags without cloning repository
func main() {

url := "https://github.com/aide/aide"

// Create the remote with repository URL
rem := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: "origin",
URLs: []string{"https://github.com/Zenika/MARCEL"},
URLs: []string{url},
})

log.Print("Fetching tags...")

// We can then use every Remote functions to retrieve wanted information
refs, err := rem.List(&git.ListOptions{})
refs, err := rem.List(&git.ListOptions{
PeelOption: git.PeelOnly,
})
if err != nil {
log.Fatal(err)
}
Expand All @@ -29,6 +33,7 @@ func main() {
var tags []string
for _, ref := range refs {
if ref.Name().IsTag() {
log.Printf("%s: %s\n", ref.Name(), ref.Hash())
tags = append(tags, ref.Name().Short())
}
}
Expand All @@ -37,6 +42,4 @@ func main() {
log.Println("No tags!")
return
}

log.Printf("Tags found: %v", tags)
}
14 changes: 14 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,22 @@ type ListOptions struct {
InsecureSkipTLS bool
// CABundle specify additional ca bundle with system cert pool
CABundle []byte

// PeelOption defines how peeled refs will be handled.
PeelOption PeelOption
}

type PeelOption uint8

const (
// PeelIgnore ignores all peeled refs. This is the default.
PeelIgnore PeelOption = 0
// PeelOnly returns only peeled refs and ignores the rest.
PeelOnly PeelOption = 1
// PeelBoth returns peeled refs together with the other refs.
PeelBoth PeelOption = 2
)

// CleanOptions describes how a clean should be performed.
type CleanOptions struct {
Dir bool
Expand Down
21 changes: 15 additions & 6 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -1282,13 +1282,22 @@ func (r *Remote) list(ctx context.Context, o *ListOptions) (rfs []*plumbing.Refe
}

var resultRefs []*plumbing.Reference
err = refs.ForEach(func(ref *plumbing.Reference) error {
resultRefs = append(resultRefs, ref)
return nil
})
if err != nil {
return nil, err
if o.PeelOption == PeelBoth || o.PeelOption == PeelIgnore {
err = refs.ForEach(func(ref *plumbing.Reference) error {
resultRefs = append(resultRefs, ref)
return nil
})
if err != nil {
return nil, err
}
}

if o.PeelOption == PeelBoth || o.PeelOption == PeelOnly {
for k, v := range ar.Peeled {
resultRefs = append(resultRefs, plumbing.NewReferenceFromStrings(k+"^{}", v.String()))
}
}

return resultRefs, nil
}

Expand Down

0 comments on commit e535839

Please sign in to comment.