-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgit-shallow
executable file
·67 lines (61 loc) · 2.58 KB
/
git-shallow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
set -euo pipefail
set -x
TMP_GIT_SHALLOW=$(mktemp -d -t yplatform.XXXXXXXXXX)
function on_exit() {
rm -rf ${TMP_GIT_SHALLOW}
}
trap on_exit EXIT
GIT_BRANCH=$(git -C $1 rev-parse --abbrev-ref HEAD)
GIT_BRANCH_TRACKING_REF=
GIT_TAG=
if [[ -n "${GIT_BRANCH#HEAD}" ]]; then
GIT_BRANCH_TRACKING_REF=$(git -C $1 for-each-ref --format="%(upstream)" "$(git -C $1 symbolic-ref -q HEAD)")
else
GIT_TAG=$(git -C $1 tag --points-at HEAD | head -1)
GIT_BRANCH="${GIT_TAG}"
fi
[[ -n "${GIT_BRANCH#HEAD}" ]] || {
# attach temporarily
>&2 echo "$(date +"%H:%M:%S")" "[INFO] Creating temporary branch yp-git-shallow."
git -C $1 checkout -B yp-git-shallow
GIT_BRANCH=$(git -C $1 rev-parse --abbrev-ref HEAD)
[[ "${GIT_BRANCH}" = "yp-git-shallow" ]]
}
>&2 echo "$(date +"%H:%M:%S")" "[INFO] Shallow-cloning branch/tag ${GIT_BRANCH}."
# file:/// or else no shallow clone. see https://stackoverflow.com/a/40383231/465684
git clone \
--depth 1 \
--branch ${GIT_BRANCH} \
--recurse-submodules \
--shallow-submodules \
file:///$1 ${TMP_GIT_SHALLOW}
git -C ${TMP_GIT_SHALLOW} branch --unset-upstream
git -C ${TMP_GIT_SHALLOW} config --unset remote.origin.fetch
git -C ${TMP_GIT_SHALLOW} remote set-url origin "$(git -C $1 remote get-url origin)"
git -C ${TMP_GIT_SHALLOW} pack-refs
rm -rf ${TMP_GIT_SHALLOW}/.git/refs/remotes
rm -rf ${1}/.git
mv ${TMP_GIT_SHALLOW}/.git ${1}/
if [[ "${GIT_BRANCH}" != "yp-git-shallow" ]]; then
if [[ -n "${GIT_TAG}" ]]; then
>&2 echo "$(date +"%H:%M:%S")" "[INFO] Set up fetching tag ${GIT_TAG}."
git -C $1 config remote.origin.fetch "+refs/tags/${GIT_TAG}:refs/tags/${GIT_TAG}"
elif [[ -n "${GIT_BRANCH_TRACKING_REF}" ]]; then
if [[ "${GIT_BRANCH_TRACKING_REF#refs/remotes/origin/}" = "${GIT_BRANCH_TRACKING_REF}" ]]; then
>&2 echo "$(date +"%H:%M:%S")" "[SKIP] Set up fetching branch ${GIT_BRANCH} to ${GIT_BRANCH_TRACKING_REF}."
else
>&2 echo "$(date +"%H:%M:%S")" "[INFO] Set up fetching branch ${GIT_BRANCH} to ${GIT_BRANCH_TRACKING_REF}."
git -C $1 config remote.origin.fetch "+refs/heads/${GIT_BRANCH}:${GIT_BRANCH_TRACKING_REF}"
fi
fi
else
>&2 echo "$(date +"%H:%M:%S")" "[INFO] Set up fetching all branches."
git -C $1 config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
# detach again
>&2 echo "$(date +"%H:%M:%S")" "[INFO] Detach from yp-git-shallow."
GIT_BRANCH=yp-git-shallow
git -C $1 checkout $(git -C $1 rev-parse HEAD)
git -C $1 branch -D ${GIT_BRANCH}
git -C $1 branch -r -D origin/${GIT_BRANCH}
fi