Skip to content
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

Diff Staging with Worktree #700

Open
ghost opened this issue Mar 4, 2023 · 5 comments
Open

Diff Staging with Worktree #700

ghost opened this issue Mar 4, 2023 · 5 comments

Comments

@ghost
Copy link

ghost commented Mar 4, 2023

I want to Diff Staging with Worktree. I found diffStagingWithWorktree:

func (w *Worktree) diffStagingWithWorktree(reverse bool) (merkletrie.Changes, error) {

but it returns a merkletrie.Changes. To make use of that, it would need to be converted to object.Changes. I found function newChanges:

func newChanges(src merkletrie.Changes) (Changes, error) {

but it is not public. I tried just copying the function into my code, but that does work as it calls another private function newChange:

func newChange(c merkletrie.Change) (*Change, error) {

which calls another private function newChangeEntry:

func newChangeEntry(p noder.Path) (ChangeEntry, error) {

which calls a private struct treeNoder:

type treeNoder struct {

after that I gave up.

@ghost
Copy link
Author

ghost commented Mar 5, 2023

looks like what I want is the Encode method:

https://godocs.io/github.com/go-git/go-git/v5/plumbing/format/diff#UnifiedEncoder.Encode

which requires the Patch interface:

https://godocs.io/github.com/go-git/go-git/v5/plumbing/format/diff#Patch

which is implemented by object.Patch:

https://godocs.io/github.com/go-git/go-git/v5/plumbing/object#Patch

assuming the following functions were exported, we could call diffStagingWithWorktree:

func (w *Worktree) diffStagingWithWorktree(reverse bool) (merkletrie.Changes, error) {

then call newChanges:

func newChanges(src merkletrie.Changes) (Changes, error) {

then call Patch:

func (c Changes) Patch() (*Patch, error) {

until those functions are exported, the Patch interface has to be manually implemented. I found some such code here:

https://github.com/fhs/gig/blob/dd59dc92/cli/diff.go#L181-L187

@headyj
Copy link

headyj commented Sep 5, 2023

@1268 did you manage to make it work like this? I'm also struggling with the same case: I basically want to execute the equivalent of git diff --cached to get an output of the differences between staging and worktree

@ghost
Copy link
Author

ghost commented Sep 5, 2023

@headyj yeah I found a solution, but:

I basically want to execute the equivalent of git diff --cached to get an output of the differences between staging and worktree

is not true. staging to worktree is git diff. HEAD to staging is git diff --cached

@headyj
Copy link

headyj commented Sep 6, 2023

True @1268 , but I guess I can use the same principle for my case and it should work. Did you just do what you wrote on your first post or did you add something else to make it work?

@ghost
Copy link
Author

ghost commented Sep 6, 2023

you should clarify what you are wanting, either staging to worktree or HEAD to staging. those aren't the same thing and they have very different solutions.

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

No branches or pull requests

1 participant