From 485cf516787c0bcbb3c52bbc8b46c8d1254a4c4e Mon Sep 17 00:00:00 2001 From: julien040 <48369040+julien040@users.noreply.github.com> Date: Fri, 23 Jun 2023 13:26:09 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix:=20untracked=20files=20showi?= =?UTF-8?q?ng=20up=20in=20status=20A=20strange=20bug=20in=20go-git=20shows?= =?UTF-8?q?=20files=20as=20untracked=20while=20git=20simply=20ignore=20the?= =?UTF-8?q?m.=20I=20couldn't=20fix=20the=20bug=20so=20I=20built=20a=20work?= =?UTF-8?q?=20around.=20Now=20gut=20ignores=20untracked=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/executor/commit.go | 36 +++++++++++++++--------------------- src/executor/executor.go | 14 +++++++++----- src/executor/status.go | 21 ++++++++++++++++++--- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/executor/commit.go b/src/executor/commit.go index 07c87a4..feceac5 100644 --- a/src/executor/commit.go +++ b/src/executor/commit.go @@ -32,31 +32,25 @@ func AddAll(path string) error { // This is a workaround for the fact that go-git does not support .gitignore err := GitAddAll() - // If git add . is successful, return - // Otherwise, use go-git to add all files - if err == nil { - return nil - } - } - repo, err := OpenRepo(path) - if err != nil { - return err - } - w, err := repo.Worktree() - if err != nil { return err - } - // Replace the .gitignore file with the one in the repo - replaceGitIgnore(w, filepath.Join(path, ".gitignore")) + } else { + repo, err := OpenRepo(path) + if err != nil { + return err + } + w, err := repo.Worktree() + if err != nil { + return err + } + // Replace the .gitignore file with the one in the repo + replaceGitIgnore(w, filepath.Join(path, ".gitignore")) - // Add all files - err = w.AddWithOptions(&git.AddOptions{ - All: true, - }) - if err != nil { + // Add all files + err = w.AddWithOptions(&git.AddOptions{ + All: true, + }) return err } - return nil } func Commit(path string, message string) (CommitResult, error) { diff --git a/src/executor/executor.go b/src/executor/executor.go index d6d57e3..ee07f0d 100644 --- a/src/executor/executor.go +++ b/src/executor/executor.go @@ -1,8 +1,6 @@ package executor import ( - "path/filepath" - "github.com/go-git/go-git/v5" ) @@ -64,13 +62,19 @@ func IsWorkTreeClean(path string) (bool, error) { if err != nil { return false, err } - // To not track files listed in the .gitignore - replaceGitIgnore(worktree, filepath.Join(path, ".gitignore")) + AddAll(path) status, err := worktree.Status() if err != nil { return false, err } - return status.IsClean(), nil + + // See /src/executor/status.go:29 + for _, statusFile := range status { + if statusFile.Staging != git.Untracked { + return false, nil + } + } + return true, nil } func IsDetachedHead(path string) (bool, error) { diff --git a/src/executor/status.go b/src/executor/status.go index 87d46e4..e9c128e 100644 --- a/src/executor/status.go +++ b/src/executor/status.go @@ -1,5 +1,9 @@ package executor +import ( + "github.com/go-git/go-git/v5" +) + type FileStatus struct { Path string Status string @@ -10,20 +14,31 @@ func GetStatus(path string) ([]FileStatus, error) { if err != nil { return nil, err } + // Run git add . to add all files using git + // or use go-git to add all files AddAll(path) w, err := repo.Worktree() if err != nil { return nil, err } - // Add all files without one listed in gitignore status, err := w.Status() if err != nil { return nil, err } + /** + * Due to a bug in go-git, the status of untracked files is not returned. + * Sometimes, go-git returns an untracked status while git returns ignores the files + * This is a workaround to fix this issue. + * I thinks it's caused by the fact that go-git does not support nested .gitignore properly + */ + var fileStatuses []FileStatus - for file, status := range status { - fileStatuses = append(fileStatuses, FileStatus{Path: file, Status: string(status.Staging)}) + for file, statusFile := range status { + if statusFile.Staging == git.Untracked { + continue + } + fileStatuses = append(fileStatuses, FileStatus{Path: file, Status: string(statusFile.Staging)}) } return fileStatuses, nil