5

I'm only new to git and I'm looking at the post-checkout hook.
As I understand it the following command will restore "filename" from the latest commit:

git checkout -- filename

When I get control in post-checkout I get 3 arguments, namely 2 SHAs and a type.
Both the SHAs are the same, namely the SHA of the last commit and type has a value of 0 which means a checkout of a file occurred.
But home do I find out what file was checked out?

1615903
  • 32,635
  • 12
  • 70
  • 99
GOVarney
  • 1,027
  • 1
  • 7
  • 9
  • *How do I find out what file was checked out?* Basically, you don't. You can (on some OSes) poke around in the set of processes running and see if you can find a `git checkout [commit] -- path1 path2 ... pathN` command that's running your hook; if so, those would be the files that were copied from some specific commit into the index and then on into the work-tree. – torek Jun 08 '17 at 05:56

1 Answers1

1

The documentation says:

The hook is given three parameters:

  • the ref of the previous HEAD,
  • the ref of the new HEAD (which may or may not have changed), and
  • a flag indicating whether the checkout was a branch checkout (changing branches, flag=1) or a file checkout (retrieving a file from the index, flag=0).

In your case, your HEAD has not changed. You might have some modified files initialized back to the HEAD content, but other than that, all files already checked out are still checked out:

git show --pretty="" --name-only HEAD

Since you have not changed branches, the flag is set to 0, even if all files were already checked out. So there is no easy way to see which ones were modified and reset to HEAD content.


The key is: that post-checkout hook cannot affect the outcome of 'git checkout'.
On other words, the index and working tree already reflect their final state (and don't remember what was there before)

If you need a list of files reset by a git checkout ., it is best to do first a git stash.
Then a git checkout: your post-checkout hook can then use git stash show in order to list the files that were present before the checkout.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 4
    You have to wonder why you get an indicator saying that a file is being checked out and then not giving you a way to find out the name of the file. – GOVarney Jun 08 '17 at 13:46
  • @GOVarney Agreed, but that is the nature of that post-checkout hook: that checkout has already happen, the files are already reset and that information (list of files reset) is lost. I have edited the answer to propose first (before the checkout) a `git stash`. – VonC Jun 08 '17 at 18:30
  • The requirement I have is "whenever a file in the working directory is changed by git I need to process it". When a branch is checked out I assume all files in the working directory have changed. It may not be true, but it could be so I have to process all files in the directory. It is expensive to do but a necessary evil. I was hoping that if just files were checked out I would only have to process the checked out files. But it seems I can't get that list. Is this a valid reason for having a pre-checkout hook??? Then I could track what changed myself. :-) – GOVarney Jun 11 '17 at 09:41
  • @GOVarney Yes, a pre-checkout hook might help, although there isn't one (http://git.661346.n2.nabble.com/Why-there-is-no-pre-checkout-hook-td5638042.html), and git stash is often mentioned in its place (https://stackoverflow.com/a/40725474/6309) – VonC Jun 11 '17 at 09:47