annotate oldgit/hooks/push-to-checkout.sample @ 2:a3b158471bd3 draft

planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
author fubar
date Sun, 04 Aug 2024 00:06:43 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
1 #!/bin/sh
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
2
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
3 # An example hook script to update a checked-out tree on a git push.
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
4 #
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
5 # This hook is invoked by git-receive-pack(1) when it reacts to git
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
6 # push and updates reference(s) in its repository, and when the push
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
7 # tries to update the branch that is currently checked out and the
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
8 # receive.denyCurrentBranch configuration variable is set to
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
9 # updateInstead.
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
10 #
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
11 # By default, such a push is refused if the working tree and the index
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
12 # of the remote repository has any difference from the currently
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
13 # checked out commit; when both the working tree and the index match
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
14 # the current commit, they are updated to match the newly pushed tip
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
15 # of the branch. This hook is to be used to override the default
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
16 # behaviour; however the code below reimplements the default behaviour
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
17 # as a starting point for convenient modification.
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
18 #
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
19 # The hook receives the commit with which the tip of the current
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
20 # branch is going to be updated:
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
21 commit=$1
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
22
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
23 # It can exit with a non-zero status to refuse the push (when it does
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
24 # so, it must not modify the index or the working tree).
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
25 die () {
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
26 echo >&2 "$*"
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
27 exit 1
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
28 }
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
29
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
30 # Or it can make any necessary changes to the working tree and to the
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
31 # index to bring them to the desired state when the tip of the current
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
32 # branch is updated to the new commit, and exit with a zero status.
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
33 #
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
34 # For example, the hook can simply run git read-tree -u -m HEAD "$1"
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
35 # in order to emulate git fetch that is run in the reverse direction
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
36 # with git push, as the two-tree form of git read-tree -u -m is
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
37 # essentially the same as git switch or git checkout that switches
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
38 # branches while keeping the local changes in the working tree that do
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
39 # not interfere with the difference between the branches.
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
40
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
41 # The below is a more-or-less exact translation to shell of the C code
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
42 # for the default behaviour for git's push-to-checkout hook defined in
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
43 # the push_to_deploy() function in builtin/receive-pack.c.
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
44 #
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
45 # Note that the hook will be executed from the repository directory,
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
46 # not from the working tree, so if you want to perform operations on
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
47 # the working tree, you will have to adapt your code accordingly, e.g.
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
48 # by adding "cd .." or using relative paths.
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
49
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
50 if ! git update-index -q --ignore-submodules --refresh
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
51 then
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
52 die "Up-to-date check failed"
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
53 fi
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
54
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
55 if ! git diff-files --quiet --ignore-submodules --
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
56 then
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
57 die "Working directory has unstaged changes"
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
58 fi
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
59
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
60 # This is a rough translation of:
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
61 #
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
62 # head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
63 if git cat-file -e HEAD 2>/dev/null
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
64 then
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
65 head=HEAD
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
66 else
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
67 head=$(git hash-object -t tree --stdin </dev/null)
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
68 fi
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
69
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
70 if ! git diff-index --quiet --cached --ignore-submodules $head --
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
71 then
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
72 die "Working directory has staged changes"
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
73 fi
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
74
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
75 if ! git read-tree -u -m "$commit"
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
76 then
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
77 die "Could not update working tree to new HEAD"
a3b158471bd3 planemo upload for repository https://github.com/ncbi/egapx commit 98875ef7eda9323fc9991970103954e9097d9e73
fubar
parents:
diff changeset
78 fi