Skip to content

Add RestoreStaged to Worktree that mimics the behaviour of git restore --staged <file>... #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Prev Previous commit
git: worktree, When comparing against the Files option in resetWorktr…
…ee the source file was just using the name instead of the full path. Fix problems with files in subdirs not working
  • Loading branch information
Ben Talbot committed Nov 5, 2021
commit 68f2675d318856bd452b924c53f5c0fd40cd554b
4 changes: 2 additions & 2 deletions worktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,9 @@ func (w *Worktree) resetWorktree(t *object.Tree, files []string) error {
if len(files) > 0 {
file := ""
if ch.From != nil {
file = ch.From.Name()
file = ch.From.String()
} else {
file = ch.To.Name()
file = ch.To.String()
}

contains := inFiles(files, file)
Expand Down
61 changes: 59 additions & 2 deletions worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2266,12 +2266,15 @@ func setupForRestore(c *C, s *WorktreeSuite) (fs billy.Filesystem, w *Worktree,
err := w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)

names = []string{"foo", "CHANGELOG", "LICENSE", "binary.jpg"}
names = []string{"foo", "CHANGELOG", "LICENSE", "binary.jpg", "json/short.json", "json/medium.json", "json/long.json"}
verifyStatus(c, "Checkout", w, names, []FileStatus{
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
})

// Touch of bunch of files including create a new file and delete an exsiting file
Expand All @@ -2289,6 +2292,9 @@ func setupForRestore(c *C, s *WorktreeSuite) (fs billy.Filesystem, w *Worktree,
{Worktree: Modified, Staging: Unmodified},
{Worktree: Modified, Staging: Unmodified},
{Worktree: Deleted, Staging: Unmodified},
{Worktree: Modified, Staging: Unmodified},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Modified, Staging: Unmodified},
})

// Stage all files and verify the updated status
Expand All @@ -2301,26 +2307,34 @@ func setupForRestore(c *C, s *WorktreeSuite) (fs billy.Filesystem, w *Worktree,
{Worktree: Unmodified, Staging: Modified},
{Worktree: Unmodified, Staging: Modified},
{Worktree: Unmodified, Staging: Deleted},
{Worktree: Unmodified, Staging: Modified},
{Worktree: Unmodified, Staging: Added},
{Worktree: Unmodified, Staging: Modified},
})

// Add secondary changes to a file to make sure we only restore the staged file
err = util.WriteFile(fs, names[1], []byte("Foo Bar:11"), 0755)
c.Assert(err, IsNil)
err = util.WriteFile(fs, names[2], []byte("Foo Bar:22"), 0755)
c.Assert(err, IsNil)
err = util.WriteFile(fs, names[4], []byte("Foo Bar:44"), 0755)
c.Assert(err, IsNil)

verifyStatus(c, "Secondary Edits", w, names, []FileStatus{
{Worktree: Unmodified, Staging: Added},
{Worktree: Modified, Staging: Modified},
{Worktree: Modified, Staging: Modified},
{Worktree: Unmodified, Staging: Deleted},
{Worktree: Modified, Staging: Modified},
{Worktree: Unmodified, Staging: Added},
{Worktree: Unmodified, Staging: Modified},
})

return
}

func verifyStatus(c *C, marker string, w *Worktree, files []string, statuses []FileStatus) {
c.Assert(len(files), Equals, len(statuses))
c.Assert(len(files), Equals, len(statuses), Commentf("%s - Incorrect number of statuses %d != %d", marker, len(files), len(statuses)))

status, err := w.Status()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -2350,6 +2364,9 @@ func (s *WorktreeSuite) TestRestoreStaged(c *C) {
{Worktree: Modified, Staging: Unmodified},
{Worktree: Modified, Staging: Modified},
{Worktree: Unmodified, Staging: Deleted},
{Worktree: Modified, Staging: Modified},
{Worktree: Unmodified, Staging: Added},
{Worktree: Unmodified, Staging: Modified},
})

//Make sure the restore didn't overwrite our secondary changes
Expand All @@ -2365,12 +2382,33 @@ func (s *WorktreeSuite) TestRestoreStaged(c *C) {
{Worktree: Modified, Staging: Unmodified},
{Worktree: Modified, Staging: Unmodified},
{Worktree: Deleted, Staging: Unmodified},
{Worktree: Modified, Staging: Modified},
{Worktree: Unmodified, Staging: Added},
{Worktree: Unmodified, Staging: Modified},
})

//Make sure the restore didn't overwrite our secondary changes
contents, err = util.ReadFile(fs, names[2])
c.Assert(err, IsNil)
c.Assert(string(contents), Equals, "Foo Bar:22")

opts.Files = []string{names[4], names[5], names[6]}
err = w.Restore(&opts)
c.Assert(err, IsNil)
verifyStatus(c, "Restored Third", w, names, []FileStatus{
{Worktree: Untracked, Staging: Untracked},
{Worktree: Modified, Staging: Unmodified},
{Worktree: Modified, Staging: Unmodified},
{Worktree: Deleted, Staging: Unmodified},
{Worktree: Modified, Staging: Unmodified},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Modified, Staging: Unmodified},
})

//Make sure the restore didn't overwrite our secondary changes
contents, err = util.ReadFile(fs, names[4])
c.Assert(err, IsNil)
c.Assert(string(contents), Equals, "Foo Bar:44")
}

func (s *WorktreeSuite) TestRestoreWorktree(c *C) {
Expand Down Expand Up @@ -2403,6 +2441,9 @@ func (s *WorktreeSuite) TestRestoreBoth(c *C) {
{Worktree: Untracked, Staging: Untracked},
{Worktree: Modified, Staging: Modified},
{Worktree: Unmodified, Staging: Deleted},
{Worktree: Modified, Staging: Modified},
{Worktree: Unmodified, Staging: Added},
{Worktree: Unmodified, Staging: Modified},
})

opts.Files = []string{names[2], names[3]}
Expand All @@ -2413,5 +2454,21 @@ func (s *WorktreeSuite) TestRestoreBoth(c *C) {
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Modified, Staging: Modified},
{Worktree: Unmodified, Staging: Added},
{Worktree: Unmodified, Staging: Modified},
})

opts.Files = []string{names[4], names[5], names[6]}
err = w.Restore(&opts)
c.Assert(err, IsNil)
verifyStatus(c, "Restored Third", w, names, []FileStatus{
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
{Worktree: Untracked, Staging: Untracked},
})
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy