Installer: Get rid of superfluous emit commands for simplicity
[msysgit.git] / share / msysGit / post-checkout-hook
blob6aca7a1bdf56c4b43a3f6fbb5b33a9d5f60e4a81
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 diff --ignore-submodules --name-only -z "$1".."$2" |
34 xargs -0r git ls-files --modified)"
35 test -z "$MODIFIED" && exit
36 cd /share/msysGit || exit 0 # called from tmproot
38 mkdir -p tmproot/bin tmproot/etc &&
39 echo "$MODIFIED" > tmproot/etc/modifiedFiles &&
40 make create-shortcut.exe > /dev/null &&
41 cp /bin/{sh.exe,msys-1.0.dll,git.exe,cat.exe,rm.exe,xargs.exe} \
42 /mingw/bin/{libcurl-4.dll,libiconv2.dll} tmproot/bin/ &&
43 cat > tmproot/etc/profile << \EOF &&
44 # cd to msysGit root
45 cd "$(pwd -W)"/../../../..
47 # add original msysGit PATH
48 export PATH=/bin:"$(pwd)/bin:$(pwd)/mingw/bin"
50 prompt_and_exit () {
51 echo "<Press Enter to close this window>" >&2
52 read line
53 exit "$1"
56 die () {
57 printf '\n' >&2
58 echo "$*" >&2
59 prompt_and_exit 1
62 # Try to update files marked 'modified'
63 test -s /etc/modifiedFiles && {
64 echo "Trying to finish checkout" >&2
65 cat /etc/modifiedFiles |
66 tr "\n" "\0" |
67 xargs -0r git checkout -- ||
68 die "Please close _all_ msysGit instances and run this shortcut again"
69 test -z "$(cat /etc/modifiedFiles |
70 tr "\n" "\0" |
71 xargs -0r git diff --ignore-submodules --name-only)" ||
72 die "At least one file could not be updated; is msysGit still running?"
75 echo "Everything up-to-date"
76 test -f /etc/linkfile && rm -f "$(cat /etc/linkfile)"
77 prompt_and_exit 0
78 EOF
79 echo "$USERPROFILE/Desktop/finish-checkout.lnk" > tmproot/etc/linkfile &&
80 command=sh.exe &&
81 arguments="--login -i" &&
82 if test -n "$COMMONPROGRAMW6432"
83 then
84 arguments="//c $command $arguments" &&
85 command="$WINDIR"\\SysWOW64\\cmd.exe && echo "$arguments"
86 fi &&
87 ./create-shortcut --work-dir "$(cd tmproot/bin && pwd -W)" \
88 --icon-file "$(cd ../resources && pwd -W)/msysgitlogo.ico" \
89 --arguments "$arguments" \
90 "$command" \
91 "$(cat tmproot/etc/linkfile)" &&
92 cat << EOF ||
94 WARNING!
95 --------
97 You appear to have modified files after switching branches. As Windows has
98 problems updating files that are in use, the most likely reason is that a
99 .dll or a .exe file needs to be updated, but that was refused due to a process
100 locking it.
102 A shortcut 'finish-checkout' was created on your desktop to help you: quit
103 all msysGit programs, launch that shortcut, and all should be fixed.
105 die "Could not set up temporary MSys root"