Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Provide a git_stash_apply() API #2636

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
swisspol opened this issue Oct 23, 2014 · 5 comments
Closed

Provide a git_stash_apply() API #2636

swisspol opened this issue Oct 23, 2014 · 5 comments

Comments

@swisspol
Copy link
Contributor

I'm trying to reproduce the behavior of git stash -u followed by git stash apply in libgit2. I can create the stash properly in libgit2 and applying it with the git CLT works. However, if I try to apply it with libgit2, the untracked files are not restored.

git_checkout_options options = GIT_CHECKOUT_OPTIONS_INIT;
  options.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_DONT_UPDATE_INDEX;
  CALL_LIBGIT2_FUNCTION_RETURN(NO, git_checkout_tree, self.private, (git_object*)stash.private, &options);

(stash.private is actually a git_commit* retrieved with git_commit_lookup() after creating the stash)

What am I doing wrong?

@swisspol
Copy link
Contributor Author

Side question: when I look up at the tree corresponding to the stash (whether or not it's created using CLT or libgit2), I don't see my untracked file "text.txt" - why is that?

$ git cat-file -p stash@{0}^{tree}
100644 blob 90e5d6690018fc5a9c2d6b6ba61dde10d4e2acd6    hello_world.txt

$ git stash pop
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   hello_world.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test.txt

@swisspol
Copy link
Contributor Author

Looking at the source code for git_stash_save() I see it is indeed creating 2 extra commits for the index and untracked files, so I understand where the topology observed in git objects is coming from.

Then the stash commit has 3 parents: these 2 commits and the base / HEAD commit. How are you supposed to reply these commits?

PS: Any plans to add a git_stash_apply() API?

@swisspol swisspol changed the title Applying a stash doesn't restore untracked files Provide a git_stash_apply() API Oct 24, 2014
@swisspol
Copy link
Contributor Author

I got this to work but I have the feeling I'm hacking this around and I'm not sure I'm doing this right:

  1. Check if the stash commit has 3 parents and if so assume the 3rd one is the commit for the untracked files
  2. If it exists, retrieve the tree and call git_checkout_tree()
  3. Finally call git_checkout_tree() on the stash commit itself

@swisspol
Copy link
Contributor Author

The actual implementation of git apply [stash] is here:
https://github.com/git/git/blob/master/git-stash.sh#L440

@swisspol
Copy link
Contributor Author

See PR #2705.

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