-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgit.sh
More file actions
96 lines (74 loc) · 3 KB
/
git.sh
File metadata and controls
96 lines (74 loc) · 3 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
function downstream_repo_lock () {
local DOWNSTREAM=$1
local DOWNSTREAM_REPO_DIR="${HOME}/.downstream_repo"
local DOWNSTREAM_SAFE=$(echo ${DOWNSTREAM} | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g)
local DOWNSTREAM_REPO_LOCK_DIR="${DOWNSTREAM_REPO_DIR}/${DOWNSTREAM_SAFE}.${CI_COMMIT_REF_SLUG}.lock"
local DOWNSTREAM_REPO_LOCK_RETRIES=1
local DOWNSTREAM_REPO_LOCK_RETRIES_MAX=60
local DOWNSTREAM_REPO_LOCK_SLEEP_TIME=5
mkdir -p ${DOWNSTREAM_REPO_DIR}
until mkdir "${DOWNSTREAM_REPO_LOCK_DIR}" || (( DOWNSTREAM_REPO_LOCK_RETRIES == DOWNSTREAM_REPO_LOCK_RETRIES_MAX )); do
echo "NOTICE: Acquiring lock failed on ${DOWNSTREAM_REPO_LOCK_DIR}, sleeping for ${DOWNSTREAM_REPO_LOCK_SLEEP_TIME}s"
let "DOWNSTREAM_REPO_LOCK_RETRIES++"
sleep ${DOWNSTREAM_REPO_LOCK_SLEEP_TIME}
done
if [ ${DOWNSTREAM_REPO_LOCK_RETRIES} -eq ${DOWNSTREAM_REPO_LOCK_RETRIES_MAX} ]; then
echo "ERROR: Cannot acquire lock after ${DOWNSTREAM_REPO_LOCK_RETRIES} retries, giving up on ${DOWNSTREAM_REPO_LOCK_DIR}"
exit 1
else
echo "NOTICE: Successfully acquired lock on ${DOWNSTREAM_REPO_LOCK_DIR}"
fi
}
function downstream_repo_unlock () {
local DOWNSTREAM=$1
local DOWNSTREAM_REPO_DIR="${HOME}/.downstream_repo"
local DOWNSTREAM_SAFE=$(echo ${DOWNSTREAM} | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g)
local DOWNSTREAM_REPO_LOCK_DIR="${DOWNSTREAM_REPO_DIR}/${DOWNSTREAM_SAFE}.${CI_COMMIT_REF_SLUG}.lock"
rm -rf "${DOWNSTREAM_REPO_LOCK_DIR}"
echo "NOTICE: Successfully removed lock on ${DOWNSTREAM_REPO_LOCK_DIR}"
}
function get_tmp_git_repo_dir () {
export GIT_REPO_DIR=$(mktemp -d -t git.XXXXXXXXXX)
}
function git_init_and_set_remote_to_downstream_repo () {
local DOWNSTREAM=$1
if [ -z "${GIT_REPO_DIR}" ]; then echo "ERROR: GIT_REPO_DIR var not set"; exit 1; fi
pushd ${GIT_REPO_DIR}
git init
git remote add downstream ${DOWNSTREAM}
git checkout -b ${CI_COMMIT_REF_NAME}
popd
}
function copy_to_repo_as_root_dir () {
local ADD="$1"
if [ -z "${GIT_REPO_DIR}" ]; then echo "ERROR: GIT_REPO_DIR var not set"; exit 1; fi
rsync -a ${ADD}/ ${GIT_REPO_DIR}/
}
function copy_file_to_repo () {
local ADD="$1"
if [ -z "${GIT_REPO_DIR}" ]; then echo "ERROR: GIT_REPO_DIR var not set"; exit 1; fi
cp ${ADD} ${GIT_REPO_DIR}
}
function git_add_submodule () {
local SUBM_NAME="$1"
local SUBM_BRANCH="$2"
local SUBM_URL="$3"
local SUBM_PATH="$4"
if [ -z "${GIT_REPO_DIR}" ]; then echo "ERROR: GIT_REPO_DIR var not set"; exit 1; fi
pushd ${GIT_REPO_DIR}
git submodule add --name ${SUBM_NAME} -b ${SUBM_BRANCH} -- ${SUBM_URL} ${SUBM_PATH}
popd
}
function git_force_push_and_rm_tmp_git_repo () {
local COMMIT_NAME="$1"
local COMMIT_EMAIL="$2"
local COMMIT_OPTS="$3"
if [ -z "${GIT_REPO_DIR}" ]; then echo "ERROR: GIT_REPO_DIR var not set"; exit 1; fi
pushd ${GIT_REPO_DIR}
git add -A
git -c "user.name=${COMMIT_NAME}" -c "user.email=${COMMIT_EMAIL}" commit -m "Force push from upstream repo"
bash -c "git push --set-upstream downstream ${CI_COMMIT_REF_NAME} -f ${COMMIT_OPTS}"
popd
rm -rf ${GIT_REPO_DIR}
}