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

Show can't delete reason if has uncommitted changes #123

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,7 @@ func GetBranches(ctx context.Context, remote shared.Remote, connection shared.Co
return nil, err
}

var uncommittedChanges []UncommittedChange
if changes, err := connection.GetUncommittedChanges(ctx); err == nil {
uncommittedChanges = toUncommittedChange(SplitLines(changes))
} else {
return nil, err
}

branches = checkDeletion(branches, uncommittedChanges)
branches = checkDeletion(branches)

branches, err = switchToDefaultBranchIfDeleted(ctx, branches, defaultBranchName, connection, dryRun)
if err != nil {
Expand Down Expand Up @@ -106,6 +99,10 @@ func loadBranches(ctx context.Context, remote shared.Remote, defaultBranchName s
if err != nil {
return nil, err
}
branches, err = applyTrackedChanges(ctx, branches, connection)
if err != nil {
return nil, err
}
} else {
return nil, err
}
Expand Down Expand Up @@ -255,6 +252,33 @@ func applyCommits(ctx context.Context, remote shared.Remote, branches []shared.B
return results, nil
}

func applyTrackedChanges(ctx context.Context, branches []shared.Branch, connection shared.Connection) ([]shared.Branch, error) {
var uncommittedChanges []UncommittedChange
if changes, err := connection.GetUncommittedChanges(ctx); err == nil {
uncommittedChanges = toUncommittedChange(SplitLines(changes))
} else {
return nil, err
}

results := []shared.Branch{}
for _, branch := range branches {
if branch.Head {
hasTrackedChanges := false
for _, change := range uncommittedChanges {
if !change.IsUntracked() {
hasTrackedChanges = true
break
}
}
if hasTrackedChanges {
branch.HasTrackedChanges = true
}
}
results = append(results, branch)
}
return results, nil
}

func trimBranch(ctx context.Context, oids []string, remoteHeadOid string, isMerged bool,
branchName string, defaultBranchName string, connection shared.Connection) ([]string, error) {
results := []string{}
Expand Down Expand Up @@ -399,28 +423,21 @@ func toUncommittedChange(changes []string) []UncommittedChange {
return results
}

func checkDeletion(branches []shared.Branch, uncommittedChanges []UncommittedChange) []shared.Branch {
func checkDeletion(branches []shared.Branch) []shared.Branch {
results := []shared.Branch{}
for _, branch := range branches {
branch.State = getDeleteStatus(branch, uncommittedChanges)
branch.State = getDeleteStatus(branch)
results = append(results, branch)
}
return results
}

func getDeleteStatus(branch shared.Branch, uncommittedChanges []UncommittedChange) shared.BranchState {
func getDeleteStatus(branch shared.Branch) shared.BranchState {
if branch.IsProtected {
return shared.NotDeletable
}

hasTrackedChanges := false
for _, change := range uncommittedChanges {
if !change.IsUntracked() {
hasTrackedChanges = true
break
}
}
if branch.Head && hasTrackedChanges {
if branch.HasTrackedChanges {
return shared.NotDeletable
}

Expand Down
1 change: 1 addition & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ func Test_ReturnsErrorWhenGetPullRequestsFails(t *testing.T) {
{Oid: "6ebe3d30d23531af56bd23b5a098d3ccae2a534a", Filename: "main_issue1"},
}, nil, nil).
GetPullRequests("issue1Merged", ErrCommand, nil).
GetUncommittedChanges("", nil, nil).
GetConfig([]conn.ConfigStub{
{BranchName: "branch.main.gh-poi-protected", Filename: "empty"},
{BranchName: "branch.issue1.remote", Filename: "remote"},
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ func printBranches(branches []shared.Branch) {
if branch.IsProtected {
reason = "protected"
}
if branch.HasTrackedChanges {
reason = "uncommitted changes"
}
if reason == "" {
fmt.Fprintln(color.Output, "")
} else {
Expand Down
17 changes: 9 additions & 8 deletions shared/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ type (
BranchState int

Branch struct {
Head bool
Name string
IsMerged bool
IsProtected bool
RemoteHeadOid string
Commits []string
PullRequests []PullRequest
State BranchState
Head bool
Name string
IsMerged bool
IsProtected bool
HasTrackedChanges bool
RemoteHeadOid string
Commits []string
PullRequests []PullRequest
State BranchState
}
)

Expand Down