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

Improve performance when commit logs are long #35

Merged
merged 2 commits into from
Feb 27, 2022
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
2 changes: 1 addition & 1 deletion conn/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (conn *Connection) GetBranchNames() (string, error) {

func (conn *Connection) GetLog(branchName string) (string, error) {
args := []string{
"log", "--first-parent", "--format=%H", branchName,
"log", "--first-parent", "--max-count=30", "--format=%H", branchName,
}
return run("git", args)
}
Expand Down
8 changes: 8 additions & 0 deletions conn/fixtures/gh/pr_notFound.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"search": {
"issueCount": 0,
"edges": []
}
}
}
3 changes: 3 additions & 0 deletions conn/fixtures/git/log_issue1ManyCommits.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
62d5d8280031f607f1db058da959a97f6a8e6d90
b8a2645298053fb62ea03e27feea6c483d3fd27e
d787669ee4a103fe0b361fe31c10ea037c72f27c
7 changes: 5 additions & 2 deletions poi.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func applyCommits(branches []Branch, defaultBranchName string, conn Connection)
return nil, err
}

trimmedOids, err := trimBranch(splitLines(oids), branch.Name, conn)
trimmedOids, err := trimBranch(splitLines(oids), branch.Name, defaultBranchName, conn)
if err != nil {
return nil, err
}
Expand All @@ -222,7 +222,7 @@ func applyCommits(branches []Branch, defaultBranchName string, conn Connection)
return results, nil
}

func trimBranch(oids []string, branchName string, conn Connection) ([]string, error) {
func trimBranch(oids []string, branchName string, defaultBranchName string, conn Connection) ([]string, error) {
results := []string{}
childNames := []string{}

Expand All @@ -235,6 +235,9 @@ func trimBranch(oids []string, branchName string, conn Connection) ([]string, er

if i == 0 {
for _, name := range names {
if name == defaultBranchName {
return []string{}, nil
}
if name != branchName {
childNames = append(childNames, name)
}
Expand Down
79 changes: 79 additions & 0 deletions poi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,85 @@ func Test_ShouldNotDeletableWhenDefaultBranchAssociatedWithMergedPR(t *testing.T
}, actual)
}

func Test_BranchesAndPRsAreNotAssociatedWhenManyLocalCommitsAreAhead(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

s := conn.Setup(ctrl).
CheckRepos(nil, nil).
GetRemoteNames("origin", nil, nil).
GetRepoNames("origin", nil, nil).
GetBranchNames("@main_issue1", nil, nil).
GetLog([]conn.LogStub{
{"main", "main"},
{"issue1", "issue1ManyCommits"}, // return with '--max-count=3'
}, nil, nil).
GetAssociatedRefNames([]conn.AssociatedBranchNamesStub{
{"62d5d8280031f607f1db058da959a97f6a8e6d90", "issue1"},
{"b8a2645298053fb62ea03e27feea6c483d3fd27e", "issue1"},
{"d787669ee4a103fe0b361fe31c10ea037c72f27c", "issue1"},
}, nil, nil).
GetPullRequests("notFound", nil, nil).
GetUncommittedChanges("", nil, nil)

actual, _ := GetBranches(s.Conn, false)

assert.Equal(t, []Branch{
{
false, "issue1",
[]string{
"62d5d8280031f607f1db058da959a97f6a8e6d90",
"b8a2645298053fb62ea03e27feea6c483d3fd27e",
"d787669ee4a103fe0b361fe31c10ea037c72f27c",
},
[]PullRequest{},
NotDeletable,
},
{
true, "main",
[]string{},
[]PullRequest{},
NotDeletable,
},
}, actual)
}

func Test_ShouldBeNoCommitHistoryWhenTheFirstCommitOfATopicBranchIsAssociatedWithTheDefaultBranch(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

s := conn.Setup(ctrl).
CheckRepos(nil, nil).
GetRemoteNames("origin", nil, nil).
GetRepoNames("origin", nil, nil).
GetBranchNames("@main_issue1", nil, nil).
GetLog([]conn.LogStub{
{"main", "main"}, {"issue1", "main"},
}, nil, nil).
GetAssociatedRefNames([]conn.AssociatedBranchNamesStub{
{"6ebe3d30d23531af56bd23b5a098d3ccae2a534a", "main_issue1"},
}, nil, nil).
GetPullRequests("notFound", nil, nil).
GetUncommittedChanges("", nil, nil)

actual, _ := GetBranches(s.Conn, false)

assert.Equal(t, []Branch{
{
false, "issue1",
[]string{},
[]PullRequest{},
NotDeletable,
},
{
true, "main",
[]string{},
[]PullRequest{},
NotDeletable,
},
}, actual)
}

func Test_ReturnsAnErrorWhenGetRemoteNamesFails(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down