This repository has been archived by the owner on Jul 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-receive
executable file
·70 lines (53 loc) · 2.6 KB
/
pre-receive
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
#!/usr/bin/env bash
### CONFIGURATION
TARGET_REPO=$(git config sit.target)
targetbranch=$(git config sit.targetbranch)
TARGET_BRANCH=${targetbranch:-master}
###
while read -r oldrev newrev refname; do
if [ "${newrev}" == "0000000000000000000000000000000000000000" ]; then
# Branch deletion, proceed
exit 0
fi
if [ "${oldrev}" != "0000000000000000000000000000000000000000" ]; then
echo "Only new branches permitted"
exit 1
fi
echo "Cloning target repo"
tempdir=$(mktemp -d)
target_branch="check-${newrev}"
env -u GIT_QUARANTINE_PATH -u GIT_DIR -u GIT_OBJECT_DIRECTORY -u GIT_ALTERNATE_OBJECT_DIRECTORIES git clone ${TARGET_REPO} "${tempdir}" || exit 1
# Figure out if we're using .sit/items or deprecated .sit/issues
if [ -d "${tempdir}/.sit/items" ]; then
target=item
target_dir=.sit/items
fi
if [ -d "${tempdir}/.sit/issues" ]; then
target=issue
target_dir=.sit/issues
fi
target_dir_len=${#target_dir}
env -u GIT_QUARANTINE_PATH git push "${tempdir}" "${newrev}":"refs/heads/${target_branch}" || exit 1
cd "${tempdir}" && GIT_DIR=${tempdir}/.git env -u GIT_QUARANTINE_PATH -u GIT_OBJECT_DIRECTORY -u GIT_ALTERNATE_OBJECT_DIRECTORIES git -c user.name="sit-git-hook" -c user.email="[email protected]" rebase "${TARGET_BRANCH}" "${target_branch}"
cd "${tempdir}" && GIT_DIR=${tempdir}/.git env -u GIT_QUARANTINE_PATH -u GIT_OBJECT_DIRECTORY -u GIT_ALTERNATE_OBJECT_DIRECTORIES git checkout "${TARGET_BRANCH}"
files=$(GIT_DIR=${tempdir}/.git env -u GIT_QUARANTINE_PATH -u GIT_OBJECT_DIRECTORY -u GIT_ALTERNATE_OBJECT_DIRECTORIES git diff --name-only "origin/${TARGET_BRANCH}" "${target_branch}")
for file in ${files}; do
if [ "${file:0:${target_dir_len}}" != "${target_dir}" ]; then
echo "${file}" is outside of "${target_dir}"
exit 1
fi
item=$(echo "${file}" | cut -d'/' -f 3)
record=$(echo "${file}" | cut -d'/' -f 4)
record_path=$(echo "${file}" | cut -d'/' -f 1-4)
if [ -d "${tempdir}/${record_path}" ]; then
echo "Record ${item}/${record} already exists in the target repository"
exit 1
fi
if [ -f "${tempdir}/${file}" ]; then
echo "File ${file} already exists in the target repository"
exit 1
fi
done
echo "Pushing ${refname} to target"
cd "${tempdir}" && GIT_DIR=${tempdir}/.git env -u GIT_QUARANTINE_PATH -u GIT_OBJECT_DIRECTORY -u GIT_ALTERNATE_OBJECT_DIRECTORIES git push "${TARGET_REPO}" "${target_branch}:master" || exit 1
done