-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-simple-subsplit.sh
executable file
·105 lines (88 loc) · 2.36 KB
/
git-simple-subsplit.sh
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
#!/bin/sh
# simple and fast way of creating one-way subsplits of git repositories
#
usage()
{
echo ""
echo "USAGE:"
echo 'git-simple-subsplit.sh subpath git@github.com:user/subrepo.git [--tags "tag1 tag2 ..."] [--branches "branch1 branch2 ..."]'
echo ""
echo "will sync all branches and all tags by default."
exit 1
}
if [ $# -lt 2 ] ; then
echo "unexpected number of parameters."
usage
fi
while [ $# -gt 0 ]; do
opt="$1"
shift
case "$opt" in
--branches) BRANCHES="$1"; shift ;;
--tags) TAGS="$1"; shift ;;
*)
if [ -z "$SUBPATH" ] ; then
SUBPATH=$opt
else
if [ -z "$REMOTE" ] ; then
REMOTE=$opt
else
echo "unexpected number of parameters."
usage
fi
fi
;;
esac
done
if [ -z "$BRANCHES" ]
then
BRANCHES="$(git ls-remote origin 2>/dev/null | grep "refs/heads/" | cut -f3- -d/)"
fi
if [ -z "$TAGS" ]
then
TAGS="$(git ls-remote origin 2>/dev/null | grep -v "\^{}" | grep "refs/tags/" | cut -f3 -d/)"
fi
echo "syncing branches..."
for BRANCH in $BRANCHES
do
echo ""
echo " syncing branch $BRANCH..."
git checkout "$BRANCH" > /dev/null
# create and checkout tracking branch
(git branch | grep "subsplit-track/$SUBPATH/$BRANCH") > /dev/null || git checkout -b "subsplit-track/$SUBPATH/$BRANCH" > /dev/null
git checkout "subsplit-track/$SUBPATH/$BRANCH" > /dev/null
# merge new changes
git merge "$BRANCH" --no-edit > /dev/null
if [ $? -eq 0 ] ; then
echo " creating/updating subtree split..."
git subtree split --prefix="$SUBPATH" --branch="subsplit/$SUBPATH/$BRANCH" --rejoin "subsplit-track/$SUBPATH/$BRANCH"
echo " pushing changes to remote..."
git push -q --force "$REMOTE" "subsplit/$SUBPATH/$BRANCH:$BRANCH"
else
echo " failed to merge changes for branch $BRANCH!"
fi
# go back to branch
git checkout "$BRANCH" > /dev/null
done
echo "done."
echo "syncing tags..."
for TAG in $TAGS
do
LOCALTAG="subsplit-tags/$SUBPATH/$TAG"
if git branch | grep "$LOCALTAG$" >/dev/null
then
echo " skipping tag '${TAG}' (already synced)"
continue
fi
echo " syncing tag $TAG..."
git branch -D "$LOCALTAG" >/dev/null 2>&1
echo " creating subtree split for tag $TAG"
git subtree split --prefix="$SUBPATH" --branch="$LOCALTAG" "$TAG" > /dev/null
if [ $? -eq 0 ]
then
git push -q --force "$REMOTE" "$LOCALTAG:refs/tags/$TAG"
else
echo " failed to create subsplit for tag $TAG!"
fi
done
echo "done."