-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgit-autopush.sh
executable file
·61 lines (55 loc) · 1.16 KB
/
git-autopush.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
#!/bin/sh
usage()
{
cat << EOF
usage $0 [<options>] [-r <remoterepo>]
Will add a post-commit function in the git repo which will automatically push
to the default or remote repo.
OPTIONS:
-o Overwrite any existing post-commit hook
-r Remote repo
-a pushes all branches
EOF
}
params="$@"
OVERWRITE=0
ALL=""
while getopts "hor:a" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
o)
OVERWRITE=1
;;
r)
REMOTEREPO="$OPTARG"
;;
a)
ALL="--all"
;;
esac
done
shift $(( $OPTIND - 1 ))
HOOKS_FOLDER=.git/hooks
POST_COMMIT=$HOOKS_FOLDER/post-commit
if [ -d $HOOKS_FOLDER ]; then
if [ -f $POST_COMMIT ] && [ $OVERWRITE -eq 0 ]; then
echo "Post commit hook already exits, please add 'git push' manually in .git/hooks/post-commit"
exit 1
fi
if [ $OVERWRITE -eq 1 ]; then
mv $POST_COMMIT "$POST_COMMIT.bak"
echo "moved old hook to $POST_COMMIT.bak"
fi
echo "git push $REMOTEREPO $ALL" > $POST_COMMIT
chmod 755 $POST_COMMIT
REPOSITORY_BASENAME=$(basename "$PWD")
echo "added auto commit to $REPOSITORY_BASENAME"
exit 0
else
echo "This command must be run in the root of a Git repository."
exit 1
fi