Merge branch 'garden-shears'
[msysgit.git] / share / msysGit / post-checkout-hook
blob3ac6a16a8ea829291c8b6bb2b62450d8bc2e7d57
1 #!/bin/sh
3 # This post-checkout hook is meant to be installed in msysGit's root repository.
5 # The problem:
6 # Windows does not allow deleting or overwriting files that are in use.
7 # Concretely, it does not allow msysGit's shell to call Git to update
8 # msys-1.0.dll, because the shell still uses it.
10 # The symptoms:
11 # Directly after a checkout (as is the case when this hook is called),
12 # some files that should have been changed (i.e. that are in the output
13 # of 'git diff --name-only $old..$new') are marked modified, i.e. the
14 # command 'git diff-files --quiet -- $files' fails.
16 # The solution:
17 # We copy a fresh /bin/sh.exe and /bin/msys-1.0.dll into a temporary
18 # directory, add a script to that directory that will add the PATH back
19 # so we can use the Git commands (which are unaffected by the issue, as
20 # they are neither MSys programs nor are they tracked), and then fixes
21 # the issue.
23 # Finally, we add a shortcut on the desktop so that it is easy for the
24 # user to start after closing all msysGit windows.
26 die () {
27 echo "$*" >&2
28 exit 1
31 test "$3" != 1 && exit
32 test "$1" = "$2" && exit
33 MODIFIED="$(cd ./$(git rev-parse --show-cdup) &&
34 git diff --ignore-submodules --name-only -z "$1".."$2" |
35 xargs -0r git ls-files --modified)"
36 test -z "$MODIFIED" && exit
37 cd /share/msysGit || exit 0 # called from tmproot
39 mkdir -p tmproot/bin tmproot/etc &&
40 echo "$MODIFIED" > tmproot/etc/modifiedFiles &&
41 make create-shortcut.exe > /dev/null &&
42 cp /bin/{sh.exe,msys-1.0.dll,git.exe,cat.exe,rm.exe,xargs.exe} \
43 /mingw/bin/{libcurl-4.dll,libiconv2.dll} tmproot/bin/ &&
44 cat > tmproot/etc/profile << \EOF &&
45 # cd to msysGit root
46 cd "$(pwd -W)"/../../../..
48 # add original msysGit PATH
49 export PATH=/bin:"$(pwd)/bin:$(pwd)/mingw/bin"
51 prompt_and_exit () {
52 echo "<Press Enter to close this window>" >&2
53 read line
54 exit "$1"
57 die () {
58 printf '\n' >&2
59 echo "$*" >&2
60 prompt_and_exit 1
63 # Try to update files marked 'modified'
64 test -s /etc/modifiedFiles && {
65 echo "Trying to finish checkout" >&2
66 cat /etc/modifiedFiles |
67 tr "\n" "\0" |
68 xargs -0r git checkout -- ||
69 die "Please close _all_ msysGit instances and run this shortcut again"
70 test -z "$(cat /etc/modifiedFiles |
71 tr "\n" "\0" |
72 xargs -0r git diff --ignore-submodules --name-only)" ||
73 die "At least one file could not be updated; is msysGit still running?"
76 echo "Everything up-to-date"
77 test -f /etc/linkfile && rm -f "$(cat /etc/linkfile)"
78 prompt_and_exit 0
79 EOF
80 echo "$USERPROFILE/Desktop/finish-checkout.lnk" > tmproot/etc/linkfile &&
81 command=sh.exe &&
82 arguments="--login -i" &&
83 if test -n "$COMMONPROGRAMW6432"
84 then
85 arguments="//c $command $arguments" &&
86 command="$WINDIR"\\SysWOW64\\cmd.exe && echo "$arguments"
87 fi &&
88 ./create-shortcut --work-dir "$(cd tmproot/bin && pwd -W)" \
89 --icon-file "$(cd ../resources && pwd -W)/msysgitlogo.ico" \
90 --arguments "$arguments" \
91 "$command" \
92 "$(cat tmproot/etc/linkfile)" &&
93 cat << EOF ||
95 WARNING!
96 --------
98 You appear to have modified files after switching branches. As Windows has
99 problems updating files that are in use, the most likely reason is that a
100 .dll or a .exe file needs to be updated, but that was refused due to a process
101 locking it.
103 A shortcut 'finish-checkout' was created on your desktop to help you: quit
104 all msysGit programs, launch that shortcut, and all should be fixed.
106 die "Could not set up temporary MSys root"