Debian Codegraph @

test/common/git-utils.sh

raw
1 # shellcheck shell=sh
2 # doesn't do anything on its own. must be sourced.
3
4 # The script which sources this script must set the following variables:
5 # GITHUB_REPO = the relative repo name of the submodule on github
6 # SUBDIR = the location in the working tree where the submodule goes
7 # We also expect `set -eu`, but set them ourselves for shellcheck.
8 set -eu
9 [ -n "${GITHUB_REPO}" ]
10 [ -n "${SUBDIR}" ]
11
12 # Set by git-rebase for spawned actions
13 unset GIT_DIR GIT_EXEC_PATH GIT_PREFIX GIT_REFLOG_ACTION GIT_WORK_TREE
14
15 GITHUB_BASE="${GITHUB_BASE:-cockpit-project/cockpit}"
16 GITHUB_REPOSITORY="${GITHUB_BASE%/*}/${GITHUB_REPO}"
17 HTTPS_REMOTE="https://github.com/${GITHUB_REPOSITORY}"
18 # shellcheck disable=SC2034 # used in other scripts
19 SSH_REMOTE="[email protected]:${GITHUB_REPOSITORY}"
20
21 CACHE_DIR="${XDG_CACHE_HOME-${HOME}/.cache}/cockpit-dev/${GITHUB_REPOSITORY}.git"
22
23 if [ "${V-}" = 0 ]; then
24 message() { printf " %-8s %s\n" "$1" "$2" >&2; }
25 quiet='--quiet'
26 else
27 message() { :; }
28 quiet=''
29 fi
30
31 init_cache() {
32 if [ ! -d "${CACHE_DIR}" ]; then
33 message INIT "${CACHE_DIR}"
34 mkdir -p "${CACHE_DIR}"
35 git init --bare --template='' ${quiet} "${CACHE_DIR}"
36 git --git-dir "${CACHE_DIR}" remote add origin "${HTTPS_REMOTE}"
37 fi
38 }
39
40 # runs a git command on the cache dir
41 git_cache() {
42 init_cache
43 git --git-dir "${CACHE_DIR}" "$@"
44 }
45
46 # reads the named gitlink from the current state of the index
47 # returns (ie: prints) a 40-character commit ID
48 get_index_gitlink() {
49 if ! git ls-files -s "$1" | grep -E -o '\<[[:xdigit:]]{40}\>'; then
50 echo "*** couldn't read gitlink for file $1 from the index" >&2
51 exit 1
52 fi
53 }
54
55 # This checks if the given argument "$1" (already) exists in the repository
56 # we use git rev-list --objects to to avoid problems with incomplete fetches:
57 # we want to make sure the complete commit is there
58 check_ref() {
59 git_cache rev-list --quiet --objects "$1" -- 2>/dev/null
60 }
61
62 # Fetch a specific commit ID into the cache
63 # Either we have this commit available locally (in which case this function
64 # does nothing), or we need to fetch it. There's no chance that the object
65 # changed on the server, because we define it by its checksum.
66 fetch_sha_to_cache() {
67 sha="$1"
68
69 # No "offline mode" here: we either have the commit, or we don't
70 if ! check_ref "${sha}"; then
71 message FETCH "${SUBDIR} [ref: ${sha}]"
72 git_cache fetch --no-tags ${quiet} origin "${sha}"
73 # tag it to keep it from being GC'd.
74 git_cache tag "sha-${sha}" "${sha}" "--no-sign"
75 fi
76 }
77
78 # General purpose "fetch" function to be used with tags, refs, or nothing at
79 # all (to fetch everything). This checks the server for updates, because all
80 # of those things might change at any given time. Supports an "offline" mode
81 # to skip the fetch and use the possibly-stale local version, if we have it.
82 fetch_to_cache() {
83 # We're fetching a named ref (or all refs), which means:
84 # - we should always do the fetch because it might have changed. but
85 # - we might be able to skip updating in case we already have it
86 if [ -z "${OFFLINE-}" ]; then
87 for retry in $(seq 3); do
88 message FETCH "${SUBDIR} ${1+[ref: $*]}"
89 if git_cache fetch --prune ${quiet} origin "$@"; then
90 return
91 fi
92 sleep $((retry * retry * 5))
93 done
94 echo "repeated git fetch failure, giving up" >&2
95 exit 1
96 fi
97 }
98
99 # Get the content of "$2" from cache commit "$1"
100 cat_from_cache() {
101 git_cache cat-file blob "$1:$2"
102 }
103
104 # Consistency checking: for a given cache commit "$1", check if it contains a
105 # file "$2" which is equal to the file "$3" present in the working tree.
106 cmp_from_cache() {
107 cat_from_cache "$1" "$2" | cmp "$3"
108 }
109
110 # Like `git clone` except that it uses the original origin url and supports
111 # checking out commit IDs as detached heads. The target directory must either
112 # be empty, or not exist.
113 clone_from_cache() {
114 message CLONE "${SUBDIR} [ref: $1]"
115 [ ! -e "${SUBDIR}" ] || rmdir "${SUBDIR}"
116 mkdir "${SUBDIR}"
117 cp -a --reflink=auto "${CACHE_DIR}" "${SUBDIR}/.git"
118 git --git-dir "${SUBDIR}/.git" config --unset core.bare
119 git -c advice.detachedHead=false -C "${SUBDIR}" checkout ${quiet} "$1"
120 }
121
122 # This stores a .tar file from stdin into the cache as a tree object.
123 # Returns the ID. Opposite of `git archive`, basically.
124 tar_to_cache() {
125 # Need to do this before we set the GIT_* variables
126 init_cache
127
128 # Use a sub-shell to enable cleanup of the temporary directory
129 (
130 tmpdir="$(mktemp --tmpdir --directory cockpit-tar-to-git.XXXXXX)"
131 # shellcheck disable=SC2064 # we want ${tmpdir} expanded now
132 trap "rm -r '${tmpdir}'" EXIT
133
134 export GIT_INDEX_FILE="${tmpdir}/tmp-index"
135 export GIT_WORK_TREE="${tmpdir}/work"
136
137 mkdir "${GIT_WORK_TREE}"
138 cd "${GIT_WORK_TREE}"
139
140 tar --extract --exclude '.git*'
141 message INDEX "${SUBDIR}"
142 git_cache add --all
143 git_cache write-tree
144 )
145 }
146
147 # Small helper to run a git command on the cache directory
148 cmd_git() {
149 git_cache "$@"
150 }