Skip to content

Commit e4e7766

Browse files
committed
update check timer for tests
1 parent b26c356 commit e4e7766

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

services/repository/branch.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -672,25 +672,29 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_
672672
// so at the moment, we first check the update time, then check whether the fork branch has base's head
673673
diff, err := git.GetDivergingCommits(ctx, baseRepo.RepoPath(), baseGitBranch.CommitID, headGitBranch.CommitID)
674674
if err != nil {
675+
info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix
676+
if headRepo.IsFork && info.BaseIsNewer {
677+
return info, nil
678+
}
675679
// if the base's update time is before the fork, check whether the base's head is in the fork
680+
baseGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, baseRepo)
681+
if err != nil {
682+
return nil, err
683+
}
676684
headGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, headRepo)
677685
if err != nil {
678686
return nil, err
679687
}
680-
baseCommitID, err := headGitRepo.ConvertToGitID(baseGitBranch.CommitID)
688+
baseCommitID, err := baseGitRepo.ConvertToGitID(baseGitBranch.CommitID)
681689
if err != nil {
682690
return nil, err
683691
}
684692
headCommit, err := headGitRepo.GetCommit(headGitBranch.CommitID)
685693
if err != nil {
686694
return nil, err
687695
}
688-
hasPreviousCommit, err := headCommit.HasPreviousCommit(baseCommitID)
696+
hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID)
689697
info.BaseIsNewer = !hasPreviousCommit
690-
// make update time as last resort for divergence comparison
691-
if err != nil {
692-
info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix
693-
}
694698
return info, nil
695699
}
696700

tests/integration/repo_merge_upstream_test.go

+11-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package integration
55

66
import (
7-
"encoding/base64"
87
"fmt"
98
"net/http"
109
"net/url"
@@ -13,6 +12,8 @@ import (
1312
"time"
1413

1514
auth_model "code.gitea.io/gitea/models/auth"
15+
"code.gitea.io/gitea/models/db"
16+
git_model "code.gitea.io/gitea/models/git"
1617
repo_model "code.gitea.io/gitea/models/repo"
1718
"code.gitea.io/gitea/models/unittest"
1819
user_model "code.gitea.io/gitea/models/user"
@@ -36,7 +37,6 @@ func TestRepoMergeUpstream(t *testing.T) {
3637
require.Equal(t, exp, resp.Body.String())
3738
}
3839

39-
baseSession := loginUser(t, baseUser.Name)
4040
session := loginUser(t, forkUser.Name)
4141
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
4242

@@ -60,7 +60,7 @@ func TestRepoMergeUpstream(t *testing.T) {
6060

6161
t.Run("HeadBeforeBase", func(t *testing.T) {
6262
// add a file in base repo
63-
testAPINewFile(t, baseSession, baseRepo.OwnerName, baseRepo.Name, "master", "new-file.txt", "test-content-1")
63+
require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-1"))
6464

6565
// the repo shows a prompt to "sync fork"
6666
var mergeUpstreamLink string
@@ -83,21 +83,14 @@ func TestRepoMergeUpstream(t *testing.T) {
8383

8484
t.Run("BaseChangeAfterHeadChange", func(t *testing.T) {
8585
// update the files: base first, head later, and check the prompt
86-
testAPINewFile(t, session, forkRepo.OwnerName, forkRepo.Name, "fork-branch", "new-file-other.txt", "test-content-other")
87-
baseUserToken := getTokenForLoggedInUser(t, baseSession, auth_model.AccessTokenScopeWriteRepository)
88-
req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", baseRepo.OwnerName, baseRepo.Name, "new-file.txt"), &api.UpdateFileOptions{
89-
DeleteFileOptions: api.DeleteFileOptions{
90-
FileOptions: api.FileOptions{
91-
BranchName: "master",
92-
NewBranchName: "master",
93-
Message: "Update new-file.txt",
94-
},
95-
SHA: "a4007b6679563f949751ed31bb371fdfb3194446",
96-
},
97-
ContentBase64: base64.StdEncoding.EncodeToString([]byte("test-content-2")),
98-
}).
99-
AddTokenAuth(baseUserToken)
100-
MakeRequest(t, req, http.StatusOK)
86+
require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-2"))
87+
require.NoError(t, createOrReplaceFileInBranch(forkUser, forkRepo, "new-file-other.txt", "fork-branch", "test-content-other"))
88+
89+
// make sure the base branch's update time is before the fork, to make it test the complete logic
90+
baseBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: baseRepo.ID, Name: "master"})
91+
forkBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: forkRepo.ID, Name: "fork-branch"})
92+
_, err := db.GetEngine(db.DefaultContext).ID(forkBranch.ID).Update(&git_model.Branch{UpdatedUnix: baseBranch.UpdatedUnix + 1})
93+
require.NoError(t, err)
10194

10295
// the repo shows a prompt to "sync fork"
10396
require.Eventually(t, func() bool {

0 commit comments

Comments
 (0)