From 4ece12348bd9032f27f2169ec10537c644dbc020 Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Tue, 6 Apr 2021 13:11:52 +0200 Subject: [PATCH] Ignore broken symlinks and outside path, in commit Signed-off-by: Hidde Beydals --- internal/bootstrap/git/gogit/gogit.go | 31 +++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/internal/bootstrap/git/gogit/gogit.go b/internal/bootstrap/git/gogit/gogit.go index 2060f2f9..619dd552 100644 --- a/internal/bootstrap/git/gogit/gogit.go +++ b/internal/bootstrap/git/gogit/gogit.go @@ -20,6 +20,8 @@ import ( "context" "fmt" "io" + "os" + "path/filepath" "strings" "time" @@ -138,16 +140,37 @@ func (g *GoGit) Commit(message git.Commit) (string, error) { if err != nil { return "", err } - if status.IsClean() { + + // go-git has [a bug](https://github.com/go-git/go-git/issues/253) + // whereby it thinks broken symlinks to absolute paths are + // modified. There's no circumstance in which we want to commit a + // change to a broken symlink: so, detect and skip those. + var changed bool + for file, _ := range status { + abspath := filepath.Join(g.path, file) + info, err := os.Lstat(abspath) + if err != nil { + return "", fmt.Errorf("checking if %s is a symlink: %w", file, err) + } + if info.Mode()&os.ModeSymlink > 0 { + // symlinks are OK; broken symlinks are probably a result + // of the bug mentioned above, but not of interest in any + // case. + if _, err := os.Stat(abspath); os.IsNotExist(err) { + continue + } + } + _, _ = wt.Add(file) + changed = true + } + + if !changed { head, err := g.repository.Head() if err != nil { return "", err } return head.Hash().String(), git.ErrNoStagedFiles } - if _, err = wt.Add("."); err != nil { - return "", err - } commit, err := wt.Commit(message.Message, &gogit.CommitOptions{ Author: &object.Signature{