-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Move git config/remote to gitrepo package and add global lock to resolve possible conflict when updating repository git config file #35151
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
Open
lunny
wants to merge
10
commits into
go-gitea:main
Choose a base branch
from
lunny:lunny/fix_git_config_conflict
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a38ff9a
Move git config/remote to gitrepo package and add global lock to reso…
lunny 2e69ad3
remove unnecessary change
lunny 2f89a04
Fix bug
lunny 183cde5
fix
wxiaoguang 70ef126
fix
wxiaoguang 98bc0df
some improvements
lunny 976df17
Merge branch 'lunny/fix_git_config_conflict' of github.com:lunny/gite…
lunny 7a8fdc2
improvements
lunny 869f3ae
improvements
lunny 6501cff
Merge branch 'main' into lunny/fix_git_config_conflict
lunny 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,132 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gitrepo | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/git" | ||
giturl "code.gitea.io/gitea/modules/git/url" | ||
"code.gitea.io/gitea/modules/globallock" | ||
) | ||
|
||
func GetGitConfig(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 | ||
} | ||
if len(result) > 0 { | ||
result = result[:len(result)-1] // remove trailing newline | ||
} | ||
return result, nil | ||
} | ||
|
||
func getRepoConfigLockKey(repoStoragePath string) string { | ||
return "repo-config:" + repoStoragePath | ||
} | ||
|
||
// AddGitConfig add a git configuration key to a specific value for the given repository. | ||
func AddGitConfig(ctx context.Context, repo Repository, key, value string) error { | ||
releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath())) | ||
if err != nil { | ||
return err | ||
} | ||
defer releaser() | ||
|
||
_, _, err = git.NewCommand("config", "--add"). | ||
AddDynamicArguments(key, value). | ||
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) | ||
return err | ||
} | ||
|
||
// UpdateGitConfig 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 UpdateGitConfig(ctx context.Context, repo Repository, key, value string) (string, error) { | ||
releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath())) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer releaser() | ||
|
||
value, _, err1 := git.NewCommand("config"). | ||
AddDynamicArguments(key, value). | ||
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) | ||
return value, err1 | ||
} | ||
|
||
func AddGitRemote(ctx context.Context, repo Repository, remoteName, remoteURL string, options ...string) error { | ||
releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath())) | ||
if err != nil { | ||
return err | ||
} | ||
defer releaser() | ||
|
||
cmd := git.NewCommand("remote", "add") | ||
if len(options) > 0 { | ||
cmd.AddDynamicArguments(options...) | ||
} | ||
_, _, err = cmd. | ||
AddDynamicArguments(remoteName, remoteURL). | ||
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) | ||
return err | ||
} | ||
|
||
func RemoveGitRemote(ctx context.Context, repo Repository, remoteName string) error { | ||
releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath())) | ||
if err != nil { | ||
return err | ||
} | ||
defer releaser() | ||
|
||
cmd := git.NewCommand("remote", "rm").AddDynamicArguments(remoteName) | ||
_, _, err = cmd.RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) | ||
return err | ||
} | ||
|
||
// GetRemoteURL returns the url of a specific remote of the repository. | ||
func GetRemoteURL(ctx context.Context, repo Repository, remoteName string) (*giturl.GitURL, error) { | ||
addr, err := git.GetRemoteAddress(ctx, repoPath(repo), remoteName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return giturl.ParseGitURL(addr) | ||
} | ||
|
||
// PruneRemote prunes the remote branches that no longer exist in the remote repository. | ||
func PruneRemote(ctx context.Context, repo Repository, remoteName string, timeout time.Duration, stdout, stderr io.Writer) error { | ||
releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath())) | ||
if err != nil { | ||
return err | ||
} | ||
defer releaser() | ||
|
||
return git.NewCommand("remote", "prune").AddDynamicArguments(remoteName). | ||
Run(ctx, &git.RunOpts{ | ||
Timeout: timeout, | ||
Dir: repoPath(repo), | ||
Stdout: stdout, | ||
Stderr: stderr, | ||
}) | ||
} | ||
|
||
func UpdateRemotePrune(ctx context.Context, repo Repository, remoteName string, timeout time.Duration, stdout, stderr io.Writer) error { | ||
releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath())) | ||
if err != nil { | ||
return err | ||
} | ||
defer releaser() | ||
|
||
return git.NewCommand("remote", "update", "--prune").AddDynamicArguments(remoteName). | ||
Run(ctx, &git.RunOpts{ | ||
Timeout: timeout, | ||
Dir: repoPath(repo), | ||
Stdout: stdout, | ||
Stderr: stderr, | ||
}) | ||
} |
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
Oops, something went wrong.
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.