@@ -10,10 +10,12 @@ import (
1010
1111 "github.com/go-git/go-git/v5"
1212
13+ "github.com/werf/lockgate"
1314 "github.com/werf/logboek"
1415 "github.com/werf/logboek/pkg/types"
1516 "github.com/werf/werf/pkg/git_repo/repo_handle"
1617 "github.com/werf/werf/pkg/path_matcher"
18+ "github.com/werf/werf/pkg/telemetry"
1719 "github.com/werf/werf/pkg/true_git"
1820 "github.com/werf/werf/pkg/true_git/status"
1921 "github.com/werf/werf/pkg/util"
@@ -156,29 +158,74 @@ func (repo *Local) SyncWithOrigin(ctx context.Context) error {
156158 })
157159}
158160
159- func (repo * Local ) FetchOrigin (ctx context.Context ) error {
161+ func (repo * Local ) acquireFetchLock (ctx context.Context ) (lockgate.LockHandle , error ) {
162+ _ , lock , err := werf .AcquireHostLock (ctx , fmt .Sprintf ("local_git_repo.fetch.%s" , repo .GitDir ), lockgate.AcquireOptions {})
163+ return lock , err
164+ }
165+
166+ func (repo * Local ) Unshallow (ctx context.Context ) error {
167+ if lock , err := repo .acquireFetchLock (ctx ); err != nil {
168+ return fmt .Errorf ("unable to acquire fetch lock: %w" , err )
169+ } else {
170+ defer werf .ReleaseHostLock (lock )
171+ }
172+
160173 isShallow , err := repo .IsShallowClone (ctx )
161174 if err != nil {
162175 return fmt .Errorf ("check shallow clone failed: %w" , err )
163176 }
177+ if ! isShallow {
178+ return nil
179+ }
164180
165- remoteOriginUrl , err : = repo .RemoteOriginUrl (ctx )
181+ err = repo .doFetchOrigin (ctx , true )
166182 if err != nil {
167- return fmt .Errorf ("get remote origin failed : %w" , err )
183+ return fmt .Errorf ("unable to fetch origin : %w" , err )
168184 }
169185
170- if remoteOriginUrl == "" {
171- return fmt .Errorf ("git remote origin was not detected" )
186+ return nil
187+ }
188+
189+ func (repo * Local ) FetchOrigin (ctx context.Context , opts FetchOptions ) error {
190+ if lock , err := repo .acquireFetchLock (ctx ); err != nil {
191+ return fmt .Errorf ("unable to acquire fetch lock: %w" , err )
192+ } else {
193+ defer werf .ReleaseHostLock (lock )
172194 }
173195
196+ var unshallow bool
197+ if opts .Unshallow {
198+ isShallow , err := repo .IsShallowClone (ctx )
199+ if err != nil {
200+ return fmt .Errorf ("check shallow clone failed: %w" , err )
201+ }
202+ unshallow = isShallow
203+ }
204+
205+ return repo .doFetchOrigin (ctx , unshallow )
206+ }
207+
208+ func (repo * Local ) doFetchOrigin (ctx context.Context , unshallow bool ) error {
174209 return logboek .Context (ctx ).Default ().LogProcess ("Fetching origin" ).DoError (func () error {
210+ remoteOriginUrl , err := repo .RemoteOriginUrl (ctx )
211+ if err != nil {
212+ return fmt .Errorf ("get remote origin failed: %w" , err )
213+ }
214+
215+ if remoteOriginUrl == "" {
216+ return fmt .Errorf ("git remote origin was not detected" )
217+ }
218+
175219 fetchOptions := true_git.FetchOptions {
176- Unshallow : isShallow ,
220+ Unshallow : unshallow ,
177221 RefSpecs : map [string ]string {"origin" : "+refs/heads/*:refs/remotes/origin/*" },
178222 }
179223
180224 if err := true_git .Fetch (ctx , repo .WorkTreeDir , fetchOptions ); err != nil {
181- return fmt .Errorf ("fetch failed: %w" , err )
225+ if true_git .IsShallowFileChangedSinceWeReadIt (err ) {
226+ telemetry .GetTelemetryWerfIO ().UnshallowFailed (ctx , err )
227+ }
228+ return err
182229 }
183230
184231 return nil
0 commit comments