Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pr): Add cleanup subcommand to delete merged local branches #7322

Draft
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 44 additions & 2 deletions git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (c *Client) UncommittedChangeCount(ctx context.Context) (int, error) {
if err != nil {
return 0, err
}
lines := strings.Split(string(out), "\n")
lines := outputLines(out)
count := 0
for _, l := range lines {
if l != "" {
Expand Down Expand Up @@ -274,6 +274,7 @@ func (c *Client) lookupCommit(ctx context.Context, sha, format string) ([]byte,
}

// ReadBranchConfig parses the `branch.BRANCH.(remote|merge)` part of git config.
// This is the upstream associated with a branch.
func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (cfg BranchConfig) {
prefix := regexp.QuoteMeta(fmt.Sprintf("branch.%s.", branch))
args := []string{"config", "--get-regexp", fmt.Sprintf("^%s(remote|merge)$", prefix)}
Expand Down Expand Up @@ -354,6 +355,47 @@ func (c *Client) HasLocalBranch(ctx context.Context, branch string) bool {
return err == nil
}

// LocalBranches returns all local branches.
func (c *Client) LocalBranches(ctx context.Context) []Branch {
args := []string{"branch", "--format", "%(objectname) %(refname) %(upstream)"}
cmd, err := c.Command(ctx, args...)
if err != nil {
return nil
}
output, err := cmd.Output()
if err != nil {
return nil
}
lines := outputLines(output)

var branches []Branch
for _, line := range lines {
sections := strings.Split(line, " ")
objectname := sections[0]
refname := sections[1]
upstream := sections[2]
branch := Branch{
Local: Ref{
Hash: objectname,
Name: strings.TrimPrefix(refname, "refs/heads/"),
},
}
if upstream != "" {
trimmed := strings.TrimPrefix(sections[2], "refs/remotes/")
remoteSections := strings.SplitN(trimmed, "/", 2)
branch.Upstream = TrackingRef{
RemoteName: remoteSections[0],
BranchName: remoteSections[1],
}
}
branches = append(branches, branch)
}

return branches
}

// TrackingBranchNames returns the names of all remote branches of all remotes,
// optionally filtered to branch names that match a prefix.
func (c *Client) TrackingBranchNames(ctx context.Context, prefix string) []string {
args := []string{"branch", "-r", "--format", "%(refname:strip=3)"}
if prefix != "" {
Expand All @@ -367,7 +409,7 @@ func (c *Client) TrackingBranchNames(ctx context.Context, prefix string) []strin
if err != nil {
return nil
}
return strings.Split(string(output), "\n")
return outputLines(output)
}

// ToplevelDir returns the top-level directory path of the current repository.
Expand Down
8 changes: 8 additions & 0 deletions git/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func (r TrackingRef) String() string {
return "refs/remotes/" + r.RemoteName + "/" + r.BranchName
}

// A Branch is a local git branch with a possible upstream remote tracking
// branch. If the branch has no upstream, then TrackingRef will be its zero
// value.
type Branch struct {
Local Ref
Upstream TrackingRef
}

type Commit struct {
Sha string
Title string
Expand Down