- 
          
- 
        Couldn't load subscription status. 
- Fork 6.2k
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
          
     Merged
      
      
    
  
     Merged
                    Changes from 21 commits
      Commits
    
    
            Show all changes
          
          
            30 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 ae3c338
              
                Follow @wxiaoguang's suggestion
              
              
                lunny 00666bd
              
                Merge branch 'lunny/fix_git_config_conflict' of github.com:lunny/gite…
              
              
                lunny 38c19d2
              
                update comment
              
              
                lunny a9633fd
              
                follow wxiaoguang's suggestion
              
              
                lunny 578d82d
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                lunny 8fffb1b
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                lunny 661fa4f
              
                Merge branch 'lunny/fix_git_config_conflict' of github.com:lunny/gite…
              
              
                lunny 0b82431
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                lunny 5ec1088
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                lunny da34762
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                lunny 2e77047
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                6543 04ca695
              
                Update services/pull/compare.go
              
              
                wxiaoguang b40e8fd
              
                rename function
              
              
                lunny fb2b02f
              
                follow wxiaoguang's suggestion
              
              
                lunny 60bcbe8
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                lunny 308bae0
              
                Merge branch 'lunny/fix_git_config_conflict' of github.com:lunny/gite…
              
              
                lunny cd9d095
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                lunny 7a25938
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                GiteaBot 0effe51
              
                Merge branch 'main' into lunny/fix_git_config_conflict
              
              
                lunny d3dcab6
              
                Merge branch 'lunny/fix_git_config_conflict' of github.com:lunny/gite…
              
              
                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,50 @@ | ||
| // Copyright 2025 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|  | ||
| package gitrepo | ||
|  | ||
| import ( | ||
| "context" | ||
|  | ||
| "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 | ||
| } | ||
| if len(result) > 0 { | ||
| result = result[:len(result)-1] // remove trailing newline | ||
| } | ||
| return 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 | ||
| }) | ||
| } | ||
  
    
      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,97 @@ | ||
| // 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) | ||
| } | ||
|  | ||
| func SetRemoteURL(ctx context.Context, repo Repository, remoteName, remoteURL string) error { | ||
|         
                  wxiaoguang marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { | ||
| cmd := git.NewCommand("remote", "set-url").AddDynamicArguments(remoteName, remoteURL) | ||
| _, _, err := cmd.RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)}) | ||
| return err | ||
| }) | ||
| } | ||
|  | ||
| // GitRemotePrune prunes the remote branches that no longer exist in the remote repository. | ||
| // No lock is needed because the remote remoteName will be checked before invoking this function. | ||
|         
                  wxiaoguang marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| // Then it will not update the remote automatically if the remote does not exist. | ||
| 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. | ||
| // No lock is needed because the remote remoteName will be checked before invoking this function. | ||
|         
                  wxiaoguang marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| // Then it will not update the remote automatically if the remote does not exist. | ||
| 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, | ||
| }) | ||
| } | ||
  
    
      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.