-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Move GetDiverging functions to gitrepo #35524
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c33ac14
Move GetDiverging functions to gitrepo
lunny b26ecc2
Fix bug
lunny a66e32d
Fix bug
lunny d4ae2eb
Merge branch 'main' into lunny/move_diverging
lunny 2c70eb9
make the code simpler
lunny b821257
Merge branch 'lunny/move_diverging' of github.com:lunny/gitea into lu…
lunny 768c70c
remove unnecessary change
lunny 3d67efd
follow wxiaoguang's suggestion
lunny c858a19
improvements
lunny c397f49
fix
wxiaoguang 47a6bbd
fix
wxiaoguang 3d554b6
update comment
wxiaoguang f76938d
fix
wxiaoguang 21e2f94
fix
wxiaoguang 81b1b04
fix
wxiaoguang 5b6ca87
improvements
lunny 86bdf96
Fix test
lunny c04fb86
improvements
lunny d7bda6b
Revert "improvements"
lunny 22090f2
fix transaction pr status check
wxiaoguang 5b5a5d5
fix test
wxiaoguang 06fa8dc
Merge branch 'main' into lunny/move_diverging
techknowlogick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gitrepo | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/git/gitcmd" | ||
) | ||
|
||
// DivergeObject represents commit count diverging commits | ||
type DivergeObject struct { | ||
Ahead int | ||
Behind int | ||
} | ||
|
||
// GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch | ||
func GetDivergingCommits(ctx context.Context, repo Repository, baseBranch, targetBranch string) (*DivergeObject, error) { | ||
cmd := gitcmd.NewCommand("rev-list", "--count", "--left-right"). | ||
AddDynamicArguments(baseBranch + "..." + targetBranch).AddArguments("--") | ||
stdout, _, err1 := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)}) | ||
if err1 != nil { | ||
return nil, err1 | ||
} | ||
|
||
left, right, found := strings.Cut(strings.Trim(stdout, "\n"), "\t") | ||
if !found { | ||
return nil, fmt.Errorf("git rev-list output is missing a tab: %q", stdout) | ||
} | ||
|
||
behind, err := strconv.Atoi(left) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ahead, err := strconv.Atoi(right) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DivergeObject{Ahead: ahead, Behind: behind}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gitrepo | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockRepository struct { | ||
path string | ||
} | ||
|
||
func (r *mockRepository) RelativePath() string { | ||
return r.path | ||
} | ||
|
||
func TestRepoGetDivergingCommits(t *testing.T) { | ||
repo := &mockRepository{path: "repo1_bare"} | ||
do, err := GetDivergingCommits(t.Context(), repo, "master", "branch2") | ||
assert.NoError(t, err) | ||
assert.Equal(t, &DivergeObject{ | ||
Ahead: 1, | ||
Behind: 5, | ||
}, do) | ||
|
||
do, err = GetDivergingCommits(t.Context(), repo, "master", "master") | ||
assert.NoError(t, err) | ||
assert.Equal(t, &DivergeObject{ | ||
Ahead: 0, | ||
Behind: 0, | ||
}, do) | ||
|
||
do, err = GetDivergingCommits(t.Context(), repo, "master", "test") | ||
assert.NoError(t, err) | ||
assert.Equal(t, &DivergeObject{ | ||
Ahead: 0, | ||
Behind: 2, | ||
}, do) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gitrepo | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/tempdir" | ||
"code.gitea.io/gitea/modules/test" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home") | ||
if err != nil { | ||
log.Fatal("Unable to create temp dir: %v", err) | ||
} | ||
defer cleanup() | ||
|
||
// resolve repository path relative to the test directory | ||
testRootDir := test.SetupGiteaRoot() | ||
repoPath = func(repo Repository) string { | ||
return filepath.Join(testRootDir, "/modules/git/tests/repos", repo.RelativePath()) | ||
} | ||
|
||
setting.Git.HomePath = gitHomePath | ||
os.Exit(m.Run()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.