10000 Add RestoreStaged to Worktree that mimics the behaviour of git restore --staged <file>... by ben-tbotlabs · Pull Request #343 · go-git/go-git · GitHub
[go: up one dir, main page]

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

Conversation

ben-tbotlabs
Copy link

git: worktree, add RestoreStaged which works like the "git restore --staged ... Fixes #342

worktree.go Outdated
@@ -262,6 +262,16 @@ func (w *Worktree) setHEADToBranch(branch plumbing.ReferenceName, commit plumbin
return w.r.Storer.SetReference(head)
}

// unstages a set of files -- equivalent to "git restore --staged <file>..."
func (w *Worktree) RestoreStaged(files ...string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rather have the method Restore, with a RestoreOptions with Staged and Files as the fields.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10000

I can definitely make a change to the signature. I had files as a non-optional field because it is non-optional in the actual git command

git restore --staged
fatal: you must specify path(s) to restore

In the scenario where no files are provided should it just discard all staged changes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The options should have a validation, in case the options are invalid, must return an error

Copy link
Author
@ben-tbotlabs ben-tbotlabs Jul 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have converted to

type RestoreOptions struct {
	// Marks to restore the content in the index
	Staged bool
	// Marks to restore the content of the working tree
	Worktree bool
	// List of file paths that will be restored
	Files []string
}

func (w *Worktree) Restore(o *RestoreOptions) error 

It will throw the same error string as git restore if no files are provided (ErrNoRestorePaths)
If only Worktree: true in the RestoreOptions then Restore will currently throw an error (ErrRestoreWorktreeeOnlyNotSupported) as I haven't had a chance to properly apply changes to the worktree without adjusting the Staged files

@ben-tbotlabs ben-tbotlabs requested a review from mcuadros July 5, 2021 21:17
…le options ( CheckoutOptions, CreateTagOptions, ...)
@@ -22,6 +23,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"

"github.com/go-git/go-billy/v5"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test helper function

func setupForRestore(c *C, s *WorktreeSuite) (fs billy.Filesystem, w *Worktree, names []string)  

Returns a billy.Filesystem, so I needed to get at the interface.

@ben-tbotlabs ben-tbotlabs requested a review from mcuadros July 14, 2021 20:13
@ben-tbotlabs
Copy link
Author
ben-tbotlabs commented Jul 26, 2021

I am confused as to how my change is causing these test failures. Is there some internal state dependency I am not aware of between some of the tests? The latest commit seems to be passing so unless there is some transient build issues it must be something in my change. Any pointers would be appreciated to help track this down

@ben-tbotlabs
Copy link
Author

@mcuadros Would you be able to spare some time to help me track this down?

…ee the source file was just using the name instead of the full path. Fix problems with files in subdirs not working
@linuxerwang
Copy link
Contributor

Any progress of this API? The capability is very useful.

@ben-tbotlabs
Copy link
Author

I was having problems with seemingly unrelated unit tests that I didn't figure out. I will bring this PR back up to head and try getting it resubmitted

@ben-tbotlabs
Copy link
Author

Closing this pull request because it has been replaced by #493 against updated master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Ability to unstage an individual file
3 participants
0