mirror of https://github.com/go-gitea/gitea.git
Browse Source
Partially fix #32018 `git config` and `git remote` write operations create a temporary file named `config.lock`. Since these operations are not atomic, they must not be run in parallel. If two requests attempt to modify the same repository concurrently—such as during a compare operation—one may fail due to the presence of an existing `config.lock` file. In cases where `config.lock` is left behind due to an unexpected program exit, a global lock mechanism could allow us to safely remove the stale lock file when a related error is detected. While this behavior is not yet implemented in this PR, it is planned for a future enhancement. --------- Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>pull/35240/head
19 changed files with 312 additions and 225 deletions
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package gitrepo |
||||
|
||||
import ( |
||||
"context" |
||||
"strings" |
||||
|
||||
"code.gitea.io/gitea/modules/git" |
||||
"code.gitea.io/gitea/modules/globallock" |
||||
) |
||||
|
||||
func GitConfigGet(ctx context.Context, repo Repository, key string) (string, error) { |
||||
result, _, err := git.NewCommand("config", "--get"). |
||||
AddDynamicArguments(key). |
||||
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) |
||||
if err != nil { |
||||
return "", err |
||||
} |
||||
return strings.TrimSpace(result), nil |
||||
} |
||||
|
||||
func getRepoConfigLockKey(repoStoragePath string) string { |
||||
return "repo-config:" + repoStoragePath |
||||
} |
||||
|
||||
// GitConfigAdd add a git configuration key to a specific value for the given repository.
|
||||
func GitConfigAdd(ctx context.Context, repo Repository, key, value string) error { |
||||
return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { |
||||
_, _, err := git.NewCommand("config", "--add"). |
||||
AddDynamicArguments(key, value). |
||||
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) |
||||
return err |
||||
}) |
||||
} |
||||
|
||||
// GitConfigSet updates a git configuration key to a specific value for the given repository.
|
||||
// If the key does not exist, it will be created.
|
||||
// If the key exists, it will be updated to the new value.
|
||||
func GitConfigSet(ctx context.Context, repo Repository, key, value string) error { |
||||
return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { |
||||
_, _, err := git.NewCommand("config"). |
||||
AddDynamicArguments(key, value). |
||||
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) |
||||
return err |
||||
}) |
||||
} |
||||
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package gitrepo |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"io" |
||||
"time" |
||||
|
||||
"code.gitea.io/gitea/modules/git" |
||||
giturl "code.gitea.io/gitea/modules/git/url" |
||||
"code.gitea.io/gitea/modules/globallock" |
||||
"code.gitea.io/gitea/modules/util" |
||||
) |
||||
|
||||
type RemoteOption string |
||||
|
||||
const ( |
||||
RemoteOptionMirrorPush RemoteOption = "--mirror=push" |
||||
RemoteOptionMirrorFetch RemoteOption = "--mirror=fetch" |
||||
) |
||||
|
||||
func GitRemoteAdd(ctx context.Context, repo Repository, remoteName, remoteURL string, options ...RemoteOption) error { |
||||
return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { |
||||
cmd := git.NewCommand("remote", "add") |
||||
if len(options) > 0 { |
||||
switch options[0] { |
||||
case RemoteOptionMirrorPush: |
||||
cmd.AddArguments("--mirror=push") |
||||
case RemoteOptionMirrorFetch: |
||||
cmd.AddArguments("--mirror=fetch") |
||||
default: |
||||
return errors.New("unknown remote option: " + string(options[0])) |
||||
} |
||||
} |
||||
_, _, err := cmd. |
||||
AddDynamicArguments(remoteName, remoteURL). |
||||
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) |
||||
return err |
||||
}) |
||||
} |
||||
|
||||
func GitRemoteRemove(ctx context.Context, repo Repository, remoteName string) error { |
||||
return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { |
||||
cmd := git.NewCommand("remote", "rm").AddDynamicArguments(remoteName) |
||||
_, _, err := cmd.RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) |
||||
return err |
||||
}) |
||||
} |
||||
|
||||
// GitRemoteGetURL returns the url of a specific remote of the repository.
|
||||
func GitRemoteGetURL(ctx context.Context, repo Repository, remoteName string) (*giturl.GitURL, error) { |
||||
addr, err := git.GetRemoteAddress(ctx, repoPath(repo), remoteName) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if addr == "" { |
||||
return nil, util.NewNotExistErrorf("remote '%s' does not exist", remoteName) |
||||
} |
||||
return giturl.ParseGitURL(addr) |
||||
} |
||||
|
||||
// GitRemotePrune prunes the remote branches that no longer exist in the remote repository.
|
||||
func GitRemotePrune(ctx context.Context, repo Repository, remoteName string, timeout time.Duration, stdout, stderr io.Writer) error { |
||||
return git.NewCommand("remote", "prune").AddDynamicArguments(remoteName). |
||||
Run(ctx, &git.RunOpts{ |
||||
Timeout: timeout, |
||||
Dir: repoPath(repo), |
||||
Stdout: stdout, |
||||
Stderr: stderr, |
||||
}) |
||||
} |
||||
|
||||
// GitRemoteUpdatePrune updates the remote branches and prunes the ones that no longer exist in the remote repository.
|
||||
func GitRemoteUpdatePrune(ctx context.Context, repo Repository, remoteName string, timeout time.Duration, stdout, stderr io.Writer) error { |
||||
return git.NewCommand("remote", "update", "--prune").AddDynamicArguments(remoteName). |
||||
Run(ctx, &git.RunOpts{ |
||||
Timeout: timeout, |
||||
Dir: repoPath(repo), |
||||
Stdout: stdout, |
||||
Stderr: stderr, |
||||
}) |
||||
} |
||||
@ -0,0 +1,95 @@
@@ -0,0 +1,95 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package pull |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"strconv" |
||||
"time" |
||||
|
||||
repo_model "code.gitea.io/gitea/models/repo" |
||||
"code.gitea.io/gitea/modules/git" |
||||
"code.gitea.io/gitea/modules/gitrepo" |
||||
"code.gitea.io/gitea/modules/graceful" |
||||
logger "code.gitea.io/gitea/modules/log" |
||||
) |
||||
|
||||
// CompareInfo represents needed information for comparing references.
|
||||
type CompareInfo struct { |
||||
MergeBase string |
||||
BaseCommitID string |
||||
HeadCommitID string |
||||
Commits []*git.Commit |
||||
NumFiles int |
||||
} |
||||
|
||||
// GetCompareInfo generates and returns compare information between base and head branches of repositories.
|
||||
func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Repository, headGitRepo *git.Repository, baseBranch, headBranch string, directComparison, fileOnly bool) (_ *CompareInfo, err error) { |
||||
var ( |
||||
remoteBranch string |
||||
tmpRemote string |
||||
) |
||||
|
||||
// We don't need a temporary remote for same repository.
|
||||
if headGitRepo.Path != baseRepo.RepoPath() { |
||||
// Add a temporary remote
|
||||
tmpRemote = strconv.FormatInt(time.Now().UnixNano(), 10) |
||||
if err = gitrepo.GitRemoteAdd(ctx, headRepo, tmpRemote, baseRepo.RepoPath()); err != nil { |
||||
return nil, fmt.Errorf("GitRemoteAdd: %w", err) |
||||
} |
||||
defer func() { |
||||
if err := gitrepo.GitRemoteRemove(graceful.GetManager().ShutdownContext(), headRepo, tmpRemote); err != nil { |
||||
logger.Error("GetPullRequestInfo: GitRemoteRemove: %v", err) |
||||
} |
||||
}() |
||||
} |
||||
|
||||
compareInfo := new(CompareInfo) |
||||
|
||||
compareInfo.HeadCommitID, err = git.GetFullCommitID(ctx, headGitRepo.Path, headBranch) |
||||
if err != nil { |
||||
compareInfo.HeadCommitID = headBranch |
||||
} |
||||
|
||||
compareInfo.MergeBase, remoteBranch, err = headGitRepo.GetMergeBase(tmpRemote, baseBranch, headBranch) |
||||
if err == nil { |
||||
compareInfo.BaseCommitID, err = git.GetFullCommitID(ctx, headGitRepo.Path, remoteBranch) |
||||
if err != nil { |
||||
compareInfo.BaseCommitID = remoteBranch |
||||
} |
||||
separator := "..." |
||||
baseCommitID := compareInfo.MergeBase |
||||
if directComparison { |
||||
separator = ".." |
||||
baseCommitID = compareInfo.BaseCommitID |
||||
} |
||||
|
||||
// We have a common base - therefore we know that ... should work
|
||||
if !fileOnly { |
||||
compareInfo.Commits, err = headGitRepo.ShowPrettyFormatLogToList(ctx, baseCommitID+separator+headBranch) |
||||
if err != nil { |
||||
return nil, fmt.Errorf("ShowPrettyFormatLogToList: %w", err) |
||||
} |
||||
} else { |
||||
compareInfo.Commits = []*git.Commit{} |
||||
} |
||||
} else { |
||||
compareInfo.Commits = []*git.Commit{} |
||||
compareInfo.MergeBase, err = git.GetFullCommitID(ctx, headGitRepo.Path, remoteBranch) |
||||
if err != nil { |
||||
compareInfo.MergeBase = remoteBranch |
||||
} |
||||
compareInfo.BaseCommitID = compareInfo.MergeBase |
||||
} |
||||
|
||||
// Count number of changed files.
|
||||
// This probably should be removed as we need to use shortstat elsewhere
|
||||
// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
|
||||
compareInfo.NumFiles, err = headGitRepo.GetDiffNumChangedFiles(remoteBranch, headBranch, directComparison) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return compareInfo, nil |
||||
} |
||||
Loading…
Reference in new issue