Skip to content

Commit

Permalink
update check timer for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
changchaishi committed Jan 14, 2025
1 parent b26c356 commit e4e7766
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
16 changes: 10 additions & 6 deletions services/repository/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,25 +672,29 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_
// so at the moment, we first check the update time, then check whether the fork branch has base's head
diff, err := git.GetDivergingCommits(ctx, baseRepo.RepoPath(), baseGitBranch.CommitID, headGitBranch.CommitID)
if err != nil {
info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix
if headRepo.IsFork && info.BaseIsNewer {
return info, nil
}
// if the base's update time is before the fork, check whether the base's head is in the fork
baseGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, baseRepo)
if err != nil {
return nil, err
}
headGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, headRepo)
if err != nil {
return nil, err
}
baseCommitID, err := headGitRepo.ConvertToGitID(baseGitBranch.CommitID)
baseCommitID, err := baseGitRepo.ConvertToGitID(baseGitBranch.CommitID)
if err != nil {
return nil, err
}
headCommit, err := headGitRepo.GetCommit(headGitBranch.CommitID)
if err != nil {
return nil, err
}
hasPreviousCommit, err := headCommit.HasPreviousCommit(baseCommitID)
hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID)
info.BaseIsNewer = !hasPreviousCommit
// make update time as last resort for divergence comparison
if err != nil {
info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix
}
return info, nil
}

Expand Down
29 changes: 11 additions & 18 deletions tests/integration/repo_merge_upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package integration

import (
"encoding/base64"
"fmt"
"net/http"
"net/url"
Expand All @@ -13,6 +12,8 @@ import (
"time"

auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
Expand All @@ -36,7 +37,6 @@ func TestRepoMergeUpstream(t *testing.T) {
require.Equal(t, exp, resp.Body.String())
}

baseSession := loginUser(t, baseUser.Name)
session := loginUser(t, forkUser.Name)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)

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

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

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

t.Run("BaseChangeAfterHeadChange", func(t *testing.T) {
// update the files: base first, head later, and check the prompt
testAPINewFile(t, session, forkRepo.OwnerName, forkRepo.Name, "fork-branch", "new-file-other.txt", "test-content-other")
baseUserToken := getTokenForLoggedInUser(t, baseSession, auth_model.AccessTokenScopeWriteRepository)
req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", baseRepo.OwnerName, baseRepo.Name, "new-file.txt"), &api.UpdateFileOptions{
DeleteFileOptions: api.DeleteFileOptions{
FileOptions: api.FileOptions{
BranchName: "master",
NewBranchName: "master",
Message: "Update new-file.txt",
},
SHA: "a4007b6679563f949751ed31bb371fdfb3194446",
},
ContentBase64: base64.StdEncoding.EncodeToString([]byte("test-content-2")),
}).
AddTokenAuth(baseUserToken)
MakeRequest(t, req, http.StatusOK)
require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-2"))
require.NoError(t, createOrReplaceFileInBranch(forkUser, forkRepo, "new-file-other.txt", "fork-branch", "test-content-other"))

// make sure the base branch's update time is before the fork, to make it test the complete logic
baseBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: baseRepo.ID, Name: "master"})
forkBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: forkRepo.ID, Name: "fork-branch"})
_, err := db.GetEngine(db.DefaultContext).ID(forkBranch.ID).Update(&git_model.Branch{UpdatedUnix: baseBranch.UpdatedUnix + 1})
require.NoError(t, err)

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

0 comments on commit e4e7766

Please sign in to comment.