Skip to content

Commit

Permalink
fix: deleted files are now detected with git-type: go (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell authored Aug 12, 2022
1 parent ad5bb77 commit b21509c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/git/gogit/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ func (g *Git) Commit(commitAuthor *internalgit.CommitAuthor, commitMessage strin
return err
}

status, err := w.Status()
if err != nil {
return err
}

// This is a workaround for a bug in go-git where "add all" does not add deleted files
// If https://github.com/go-git/go-git/issues/223 is fixed, this can be removed
for file, s := range status {
if s.Worktree == git.Deleted {
_, err = w.Add(file)
if err != nil {
return err
}
}
}

// Get the current hash to be able to diff it with the committed changes later
oldHead, err := g.repo.Head()
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions tests/scripts/remover/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"os"
)

func main() {
os.Remove("test_file")
}
28 changes: 28 additions & 0 deletions tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,34 @@ Repositories with a successful run:
assert.True(t, vcMock.PullRequests[0].Draft)
},
},

{
name: "remove files",
vcCreate: func(t *testing.T) *vcmock.VersionController {
repo := createRepo(t, "owner", "should-delete", "i like apples")
addFile(t, repo.Path, "test_file", "some content", "added test_file")
return &vcmock.VersionController{
Repositories: []vcmock.Repository{
repo,
},
}
},
args: []string{
"run",
"--author-name", "Test Author",
"--author-email", "[email protected]",
"-B", "custom-branch-name",
"-m", "custom message",
fmt.Sprintf("go run %s", filepath.ToSlash(filepath.Join(workingDir, "scripts/remover/main.go"))),
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 1)

changeBranch(t, vcMock.Repositories[0].Path, "custom-branch-name", false)

assert.False(t, fileExist(t, vcMock.Repositories[0].Path, "test_file"))
},
},
}

for _, gitBackend := range gitBackends {
Expand Down

0 comments on commit b21509c

Please sign in to comment.