Skip to content

Commit 6b14297

Browse files
committed
Do not commit swap files
Eventually this list should be configurable.
1 parent 8c1348a commit 6b14297

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

common/commit.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,17 @@ func commit(repoPath string) error {
3232
continue
3333
}
3434

35+
ignore, err := ShouldIgnoreFile(repoPath, filePath)
36+
if err != nil {
37+
return tracerr.Wrap(err)
38+
}
39+
40+
if ignore {
41+
continue
42+
}
43+
3544
hasChanges = true
36-
_, err := w.Add(filePath)
45+
_, err = w.Add(filePath)
3746
if err != nil {
3847
return tracerr.Wrap(err)
3948
}

common/ignore.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"os"
5+
"path"
56
"path/filepath"
67
"strings"
78

@@ -10,8 +11,12 @@ import (
1011
"gopkg.in/src-d/go-git.v4/plumbing/format/gitignore"
1112
)
1213

13-
func ShouldIgnoreFile(repoPath string, fullFilePath string) (bool, error) {
14-
fileName := filepath.Base(fullFilePath)
14+
func ShouldIgnoreFile(repoPath string, filePath string) (bool, error) {
15+
if !filepath.IsAbs(filePath) {
16+
filePath = path.Join(repoPath, filePath)
17+
}
18+
19+
fileName := filepath.Base(filePath)
1520
var isTempFile = strings.HasSuffix(fileName, ".swp") || // vim
1621
strings.HasPrefix(fileName, "~") || // emacs
1722
strings.HasSuffix(fileName, "~") || // kate
@@ -23,20 +28,20 @@ func ShouldIgnoreFile(repoPath string, fullFilePath string) (bool, error) {
2328
return true, nil
2429
}
2530

26-
relativePath := fullFilePath[len(repoPath)+1:]
31+
relativePath := filePath[len(repoPath)+1:]
2732
if strings.HasPrefix(relativePath, ".git/") {
2833
return true, nil
2934
}
3035

31-
empty, err := isEmptyFile(fullFilePath)
36+
empty, err := isEmptyFile(filePath)
3237
if err != nil {
3338
return false, tracerr.Wrap(err)
3439
}
3540
if empty {
3641
return true, nil
3742
}
3843

39-
return isFileIgnoredByGit(repoPath, fullFilePath)
44+
return isFileIgnoredByGit(repoPath, filePath)
4045
}
4146

4247
func isFileIgnoredByGit(repoPath string, filePath string) (bool, error) {

0 commit comments

Comments
 (0)