@@ -310,7 +310,12 @@ var errBranchInvalidMerge = errors.New("branch config: invalid merge")
310310
311311// Returns the current directory git commit hash if possible
312312// If we are not in a git repo it will return an empty string
313- func gracefulGitRepoHead (path string ) (* HeadCommit , error ) {
313+ func gracefulGitRepoHead (path string , logger * zerolog.Logger ) (* HeadCommit , error ) {
314+ if logger == nil {
315+ l := zerolog .Nop ()
316+ logger = & l
317+ }
318+
314319 repo , err := git .PlainOpenWithOptions (path , & git.PlainOpenOptions {
315320 // walk up the directory tree until we find a git repo
316321 DetectDotGit : true ,
@@ -329,6 +334,15 @@ func gracefulGitRepoHead(path string) (*HeadCommit, error) {
329334 return nil , nil
330335 }
331336
337+ // Mitigate https://github.com/go-git/go-git/issues/331
338+ // This error is not exposed by go-git so we do a string comparison.
339+ // The upcoming go-git v6 no longer returns this error, so this
340+ // workaround can be removed after upgrading.
341+ if isBranchInvalidMergeError (err ) {
342+ logger .Warn ().Err (err ).Msg ("branch is invalid merge, skipping commit" )
343+ return nil , nil
344+ }
345+
332346 return nil , fmt .Errorf ("opening repository: %w" , err )
333347 }
334348
@@ -361,7 +375,8 @@ func gracefulGitRepoHead(path string) (*HeadCommit, error) {
361375 // we do not care about that use-case, so we ignore the error
362376 // we compare by error string because go-git does not expose the error type
363377 // and errors.Is require the same instance of the error
364- if err .Error () == errBranchInvalidMerge .Error () {
378+ if isBranchInvalidMergeError (err ) {
379+ logger .Warn ().Err (err ).Msg ("branch is invalid merge, skipping remotes" )
365380 return c , nil
366381 }
367382
@@ -387,6 +402,10 @@ func gracefulGitRepoHead(path string) (*HeadCommit, error) {
387402 return c , nil
388403}
389404
405+ func isBranchInvalidMergeError (err error ) bool {
406+ return err .Error () == errBranchInvalidMerge .Error ()
407+ }
408+
390409// Clear any basic auth credentials from the remote URL
391410func sanitizeRemoteURL (remoteURL string ) (string , error ) {
392411 uri , err := url .Parse (remoteURL )
@@ -409,7 +428,7 @@ func initialCraftingState(cwd string, opts *InitOpts) (*api.CraftingState, error
409428 return nil , errors .New ("required init options not provided" )
410429 }
411430 // Get git commit hash
412- headCommit , err := gracefulGitRepoHead (cwd )
431+ headCommit , err := gracefulGitRepoHead (cwd , opts . Logger )
413432 if err != nil {
414433 return nil , fmt .Errorf ("getting git commit hash: %w" , err )
415434 }
0 commit comments