forked from DarkStarSword/junk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-push-wd.sh
executable file
·57 lines (44 loc) · 1.51 KB
/
git-push-wd.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
#!/bin/sh
cred="[1;31;40m"
cyellow="[1;33;40m"
cgreen="[1;32;40m"
crst="[0;37;40m"
die()
{
echo "${cred}$@${crst}"
exit 1
}
usage_and_die()
{
die "Usage: $0 remote_target [ --ignore-untracked ]"
}
deploy_commit="last-deployed"
remote="$1"; shift
[ -z "$remote" ] && usage_and_die
if [ "$1" = "--ignore-untracked" ]; then
ignore_untracked=1
elif [ -n "$1" ]; then
usage_and_die
fi
host=$(git remote -v|grep "^$remote\\s.*(push)"|perl -pe 's|^.*ssh://(.*?)/(.*) .*$|\1|')
rdir=/$(git remote -v|grep "^$remote\\s.*(push)"|perl -pe 's|^.*ssh://(.*?)/(.*) .*$|\2|')
last_commit=$(git rev-parse HEAD)
git commit -m git_remote_build_index_state
index_state=$(git rev-parse HEAD)
if [ "$ignore_untracked" = 1 ]; then
git add -u $(git rev-parse --show-toplevel) # Otherwise it's only subdirectories
else
git add -A $(git rev-parse --show-toplevel) # Otherwise it's only subdirectories
fi
git commit -m git_remote_build_working_tree_state
working_tree_state=$(git rev-parse HEAD)
git branch -f "$deploy_commit" "$working_tree_state"
git reset --mixed $index_state
git reset --soft $last_commit
git push --force ${remote} ${deploy_commit} || die push failed
ssh ${host} "cd ${rdir} && git checkout -f ${deploy_commit} && git reset --hard" || die remote build/reset failed
echo "------------------------------------------------------------"
echo "| HEAD: $last_commit |"
echo "| Index: $index_state |"
echo "| Working Tree: $working_tree_state |"
echo "------------------------------------------------------------"