Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit a33a60d

Browse files
grunenwflorianmcuadros
authored andcommitted
Worktree.Add: Support Add deleted files, fixes #571 (#577)
1 parent f9879dd commit a33a60d

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

worktree_commit_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,42 @@ func (s *WorktreeSuite) TestCommitAll(c *C) {
9999
assertStorageStatus(c, s.Repository, 13, 11, 10, expected)
100100
}
101101

102+
func (s *WorktreeSuite) TestRemoveAndCommitAll(c *C) {
103+
expected := plumbing.NewHash("907cd576c6ced2ecd3dab34a72bf9cf65944b9a9")
104+
105+
fs := memfs.New()
106+
w := &Worktree{
107+
r: s.Repository,
108+
Filesystem: fs,
109+
}
110+
111+
err := w.Checkout(&CheckoutOptions{})
112+
c.Assert(err, IsNil)
113+
114+
util.WriteFile(fs, "foo", []byte("foo"), 0644)
115+
_, err = w.Add("foo")
116+
c.Assert(err, IsNil)
117+
118+
_, errFirst := w.Commit("Add in Repo\n", &CommitOptions{
119+
Author: defaultSignature(),
120+
})
121+
c.Assert(errFirst, IsNil)
122+
123+
errRemove := fs.Remove("foo")
124+
c.Assert(errRemove, IsNil)
125+
126+
hash, errSecond := w.Commit("Remove foo\n", &CommitOptions{
127+
All: true,
128+
Author: defaultSignature(),
129+
})
130+
c.Assert(errSecond, IsNil)
131+
132+
c.Assert(hash, Equals, expected)
133+
c.Assert(err, IsNil)
134+
135+
assertStorageStatus(c, s.Repository, 13, 11, 11, expected)
136+
}
137+
102138
func assertStorageStatus(
103139
c *C, r *Repository,
104140
treesCount, blobCount, commitCount int, head plumbing.Hash,

worktree_status.go

+3
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) {
252252

253253
h, err := w.copyFileToStorage(path)
254254
if err != nil {
255+
if os.IsNotExist(err) {
256+
h, err = w.deleteFromIndex(path)
257+
}
255258
return h, err
256259
}
257260

worktree_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,11 @@ func (s *WorktreeSuite) TestFilenameNormalization(c *C) {
372372

373373
modFilename := norm.Form(norm.NFKD).String(filename)
374374
util.WriteFile(w.Filesystem, modFilename, []byte("foo"), 0755)
375+
375376
_, err = w.Add(filename)
377+
c.Assert(err, IsNil)
378+
_, err = w.Add(modFilename)
379+
c.Assert(err, IsNil)
376380

377381
status, err = w.Status()
378382
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)