Skip to content

Commit

Permalink
🐛 Fix: untracked files showing up in status
Browse files Browse the repository at this point in the history
A strange bug in go-git shows files as untracked while git simply ignore them.
I couldn't fix the bug so I built a work around. Now gut ignores untracked files
  • Loading branch information
julien040 committed Jun 23, 2023
1 parent e0c413f commit 485cf51
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
36 changes: 15 additions & 21 deletions src/executor/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions src/executor/executor.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package executor

import (
"path/filepath"

"github.com/go-git/go-git/v5"
)

Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 18 additions & 3 deletions src/executor/status.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package executor

import (
"github.com/go-git/go-git/v5"
)

type FileStatus struct {
Path string
Status string
Expand All @@ -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

Expand Down

0 comments on commit 485cf51

Please sign in to comment.