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

Commit ad02bf0

Browse files
authored
Merge pull request #444 from silvertern/gitignore-ch2
Fixes checkout not possible with (untracked) files under gitignore
2 parents 4046ad9 + 81dbc6a commit ad02bf0

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

worktree_status.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ func (w *Worktree) status(commit plumbing.Hash) (Status, error) {
6868
return nil, err
6969
}
7070

71-
right = w.excludeIgnoredChanges(right)
72-
7371
for _, ch := range right {
7472
a, err := ch.Action()
7573
if err != nil {
@@ -117,7 +115,11 @@ func (w *Worktree) diffStagingWithWorktree() (merkletrie.Changes, error) {
117115
}
118116

119117
to := filesystem.NewRootNode(w.fs, submodules)
120-
return merkletrie.DiffTree(from, to, diffTreeIsEquals)
118+
res, err := merkletrie.DiffTree(from, to, diffTreeIsEquals)
119+
if err == nil {
120+
res = w.excludeIgnoredChanges(res)
121+
}
122+
return res, err
121123
}
122124

123125
func (w *Worktree) excludeIgnoredChanges(changes merkletrie.Changes) merkletrie.Changes {

worktree_test.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,35 @@ func (s *WorktreeSuite) testCheckoutBisect(c *C, url string) {
307307
})
308308
}
309309

310+
func (s *WorktreeSuite) TestCheckoutWithGitignore(c *C) {
311+
fs := memfs.New()
312+
w := &Worktree{
313+
r: s.Repository,
314+
fs: fs,
315+
}
316+
317+
err := w.Checkout(&CheckoutOptions{})
318+
c.Assert(err, IsNil)
319+
320+
f, _ := fs.Create("file")
321+
f.Close()
322+
323+
err = w.Checkout(&CheckoutOptions{})
324+
c.Assert(err.Error(), Equals, "worktree contains unstagged changes")
325+
326+
f, _ = fs.Create(".gitignore")
327+
f.Write([]byte("file"))
328+
f.Close()
329+
330+
err = w.Checkout(&CheckoutOptions{})
331+
c.Assert(err.Error(), Equals, "worktree contains unstagged changes")
332+
333+
w.Add(".gitignore")
334+
335+
err = w.Checkout(&CheckoutOptions{})
336+
c.Assert(err, IsNil)
337+
}
338+
310339
func (s *WorktreeSuite) TestStatus(c *C) {
311340
fs := memfs.New()
312341
w := &Worktree{
@@ -458,10 +487,7 @@ func (s *WorktreeSuite) TestStatusModified(c *C) {
458487
}
459488

460489
func (s *WorktreeSuite) TestStatusIgnored(c *C) {
461-
dir, _ := ioutil.TempDir("", "status")
462-
defer os.RemoveAll(dir)
463-
464-
fs := osfs.New(filepath.Join(dir, "worktree"))
490+
fs := memfs.New()
465491
w := &Worktree{
466492
r: s.Repository,
467493
fs: fs,

0 commit comments

Comments
 (0)