Repository navigation
Expand file tree
/
Copy pathgit-cms-cherry-pick
More file actions
executable file
·109 lines (94 loc) · 2.8 KB
/
Copy pathgit-cms-cherry-pick
File metadata and controls
executable file
·109 lines (94 loc) · 2.8 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
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash -e
function usage() {
cat <<@EOF
Usage: git cms-cherry-pick <commit1>[..<commit2>] [-r|--remote <remote>] [(-c|--create)|(-C|--force-create) <branch>] [<cherry-pick optional arguments>]
Description:
Cherry pick commit(s), ensuring modified subpackages are added before the commits are applied.
The remote containing the commit(s) can be specified if not already present locally.
A new branch can be created before the cherry-picking takes place.
@EOF
exit $1
}
# disambiguate the first argument when the user
# requests help instead of specifying a commit
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage 0
fi
# Initialize variables
COMMITS=""
REMOTE=""
OPTIONS=()
# Iterate through all arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage 1
;;
-r|--remote)
REMOTE="$2"
shift; shift;
;;
-c|--create)
BRANCH="$2"
shift; shift;
;;
-C|--force-create)
FORCE_BRANCH="$2"
shift; shift;
;;
[a-zA-Z0-9][a-zA-Z0-9\/\.\-][a-zA-Z0-9\/\.\-][a-zA-Z0-9\/\.\-][a-zA-Z0-9\/\.\-][a-zA-Z0-9\/\.\-][a-zA-Z0-9\/\.\-]*)
COMMITS="$1"
shift
;;
*)
OPTIONS+=("$1")
shift
;;
esac
done
# mandatory commit(s) to cherry-pick
[ "$COMMITS" ] || usage 1
# go to release
[ "$CMSSW_BASE" ] && cd "$CMSSW_BASE/src"
# add repository where commits to be cherry-picked can be found
if [ -n "$REMOTE" ]; then
git cms-remote add "$REMOTE"
git fetch "$REMOTE"
fi
# check for erroneous flag duplication
if [ -n "$BRANCH" ] && [ -n "$FORCE_BRANCH" ]; then
echo "fatal: options '-c' and '-C' cannot be used together"
exit 1
fi
# create trap to delete new branches if anything fails
function branch_cleanup() {
if [ -n "$BRANCH" ] || [ -n "$FORCE_BRANCH" ] ; then
git switch - # Switch back to the previous branch
if [ -n "$BRANCH" ] ; then
git branch -D "$BRANCH" 2>/dev/null || true
elif [ -n "$FORCE_BRANCH" ] ; then
git branch -D "$FORCE_BRANCH" 2>/dev/null || true
fi
fi
}
trap branch_cleanup ERR
# create a new branch before cherry-picking
if [ -n "$BRANCH" ]; then
git switch -c "$BRANCH"
elif [ -n "$FORCE_BRANCH" ]; then
git switch -C "$FORCE_BRANCH"
fi
# download modified subpackages
if [[ "$COMMITS" == *..* ]]; then
DIFF_RANGE="$COMMITS"
else
# single commit: diff against its (mainline) parent only
DIFF_RANGE="${COMMITS}^..${COMMITS}"
fi
git diff "$DIFF_RANGE" --name-only --no-renames | cut -d/ -f-2 | sort -u | xargs -r git cms-addpkg
# cherry-pick with user arguments
git cherry-pick "$COMMITS" "${OPTIONS[@]}"
# check dependencies
git cms-checkdeps -Aa
# remove the trap if everything succeeds
trap - ERR