@@ -5,12 +5,8 @@ package gitrepo
55
66import (
77 "context"
8- "errors"
9- "io"
10- "time"
118
129 "code.gitea.io/gitea/modules/git"
13- giturl "code.gitea.io/gitea/modules/git/url"
1410 "code.gitea.io/gitea/modules/globallock"
1511)
1612
@@ -33,123 +29,22 @@ func getRepoConfigLockKey(repoStoragePath string) string {
3329
3430// GitConfigAdd add a git configuration key to a specific value for the given repository.
3531func GitConfigAdd (ctx context.Context , repo Repository , key , value string ) error {
36- releaser , err := globallock .Lock (ctx , getRepoConfigLockKey (repo .RelativePath ()))
37- if err != nil {
32+ return globallock .LockAndDo (ctx , getRepoConfigLockKey (repo .RelativePath ()), func (ctx context.Context ) error {
33+ _ , _ , err := git .NewCommand ("config" , "--add" ).
34+ AddDynamicArguments (key , value ).
35+ RunStdString (ctx , & git.RunOpts {Dir : repoPath (repo )})
3836 return err
39- }
40- defer releaser ()
41-
42- _ , _ , err = git .NewCommand ("config" , "--add" ).
43- AddDynamicArguments (key , value ).
44- RunStdString (ctx , & git.RunOpts {Dir : repoPath (repo )})
45- return err
37+ })
4638}
4739
4840// GitConfigSet updates a git configuration key to a specific value for the given repository.
4941// If the key does not exist, it will be created.
5042// If the key exists, it will be updated to the new value.
5143func GitConfigSet (ctx context.Context , repo Repository , key , value string ) error {
52- releaser , err := globallock .Lock (ctx , getRepoConfigLockKey (repo .RelativePath ()))
53- if err != nil {
54- return err
55- }
56- defer releaser ()
57-
58- _ , _ , err = git .NewCommand ("config" ).
59- AddDynamicArguments (key , value ).
60- RunStdString (ctx , & git.RunOpts {Dir : repoPath (repo )})
61- return err
62- }
63-
64- type RemoteOption string
65-
66- const (
67- RemoteOptionMirrorPush RemoteOption = "--mirror=push"
68- RemoteOptionMirrorFetch RemoteOption = "--mirror=fetch"
69- )
70-
71- func GitRemoteAdd (ctx context.Context , repo Repository , remoteName , remoteURL string , options ... RemoteOption ) error {
72- releaser , err := globallock .Lock (ctx , getRepoConfigLockKey (repo .RelativePath ()))
73- if err != nil {
74- return err
75- }
76- defer releaser ()
77-
78- cmd := git .NewCommand ("remote" , "add" )
79- if len (options ) > 0 {
80- switch options [0 ] {
81- case RemoteOptionMirrorPush :
82- cmd .AddArguments ("--mirror=push" )
83- case RemoteOptionMirrorFetch :
84- cmd .AddArguments ("--mirror=fetch" )
85- default :
86- return errors .New ("unknown remote option: " + string (options [0 ]))
87- }
88- }
89- _ , _ , err = cmd .
90- AddDynamicArguments (remoteName , remoteURL ).
91- RunStdString (ctx , & git.RunOpts {Dir : repoPath (repo )})
92- return err
93- }
94-
95- func GitRemoteRemove (ctx context.Context , repo Repository , remoteName string ) error {
96- releaser , err := globallock .Lock (ctx , getRepoConfigLockKey (repo .RelativePath ()))
97- if err != nil {
98- return err
99- }
100- defer releaser ()
101-
102- cmd := git .NewCommand ("remote" , "rm" ).AddDynamicArguments (remoteName )
103- _ , _ , err = cmd .RunStdString (ctx , & git.RunOpts {Dir : repoPath (repo )})
104- return err
105- }
106-
107- // GitRemoteGetURL returns the url of a specific remote of the repository.
108- func GitRemoteGetURL (ctx context.Context , repo Repository , remoteName string ) (* giturl.GitURL , error ) {
109- addr , err := git .GetRemoteAddress (ctx , repoPath (repo ), remoteName )
110- if err != nil {
111- return nil , err
112- }
113- if addr == "" {
114- return nil , nil
115- }
116- return giturl .ParseGitURL (addr )
117- }
118-
119- func SetRemoteURL (ctx context.Context , repo Repository , remoteName , remoteURL string ) error {
120- releaser , err := globallock .Lock (ctx , getRepoConfigLockKey (repo .RelativePath ()))
121- if err != nil {
44+ return globallock .LockAndDo (ctx , getRepoConfigLockKey (repo .RelativePath ()), func (ctx context.Context ) error {
45+ _ , _ , err := git .NewCommand ("config" ).
46+ AddDynamicArguments (key , value ).
47+ RunStdString (ctx , & git.RunOpts {Dir : repoPath (repo )})
12248 return err
123- }
124- defer releaser ()
125-
126- cmd := git .NewCommand ("remote" , "set-url" ).AddDynamicArguments (remoteName , remoteURL )
127- _ , _ , err = cmd .RunStdString (ctx , & git.RunOpts {Dir : repoPath (repo )})
128- return err
129- }
130-
131- // GitRemotePrune prunes the remote branches that no longer exist in the remote repository.
132- // No lock is needed because the remote remoteName will be checked before invoking this function.
133- // Then it will not update the remote automatically if the remote does not exist.
134- func GitRemotePrune (ctx context.Context , repo Repository , remoteName string , timeout time.Duration , stdout , stderr io.Writer ) error {
135- return git .NewCommand ("remote" , "prune" ).AddDynamicArguments (remoteName ).
136- Run (ctx , & git.RunOpts {
137- Timeout : timeout ,
138- Dir : repoPath (repo ),
139- Stdout : stdout ,
140- Stderr : stderr ,
141- })
142- }
143-
144- // GitRemoteUpdatePrune updates the remote branches and prunes the ones that no longer exist in the remote repository.
145- // No lock is needed because the remote remoteName will be checked before invoking this function.
146- // Then it will not update the remote automatically if the remote does not exist.
147- func GitRemoteUpdatePrune (ctx context.Context , repo Repository , remoteName string , timeout time.Duration , stdout , stderr io.Writer ) error {
148- return git .NewCommand ("remote" , "update" , "--prune" ).AddDynamicArguments (remoteName ).
149- Run (ctx , & git.RunOpts {
150- Timeout : timeout ,
151- Dir : repoPath (repo ),
152- Stdout : stdout ,
153- Stderr : stderr ,
154- })
49+ })
15550}
0 commit comments