This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgit-sync
executable file
·107 lines (97 loc) · 3 KB
/
git-sync
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
#!/usr/bin/env bash
set -e
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo "usage: git sync [remote-name]"
echo
echo "Sync's the current repository to another remote host, using git,"
echo "but not using this repository."
echo
echo "You must set sync.default.repo (the path to store the repository)"
echo "and git.default.remote (the remote repository to push to). Example:"
echo " $ git config --add sync.default.repo .remote-repo"
echo " $ git config --add sync.default.remote git@hostname:/path.git"
echo "And optionally:"
echo " $ git config --add sync.default.branch gh-pages"
exit
fi
if [ -z "$1" ] ; then
dest=default
else
dest="$1"
fi
repo_location="$(git config --get --path sync.${dest}.repo || true)"
if [ -z "$repo_location" ] ; then
repo_location="$(git config --get --path sync.repo || true)"
if [ -z "$repo_location" ] ; then
repo_location=/tmp/repos
fi
repo_location="${repo_location}/$(basename $(pwd))"
fi
remote_branch="$(git config --get sync.${dest}.branch || true)"
if [ ! -e $repo_location ] ; then
remote="$(git config --get sync.${dest}.remote || true)"
if [ -z "$remote" ] ; then
echo "You must set sync.${dest}.remote"
echo "Like:"
echo " git config --add sync.${dest}.remote git@host.com:/path.git"
exit 2
fi
if [ -z "$remote_branch" ] ; then
git_op=""
git_msg=""
else
git_op="-b $remote_branch"
git_msg=" from branch $remote_branch"
fi
echo "making git repo in $repo_location$git_msg"
mkdir -p "$(basename $repo_location)"
echo git clone $git_op $remote $repo_location
git clone $git_op $remote $repo_location
else
# pull from master to ensure we're uptodate
(
cd $repo_location
if [ -z "$remote_branch" ] ; then
git_op=""
else
git_op="$remote_branch:$remote_branch"
fi
echo cd $repo_location "&&" git pull origin $git_op
git pull origin $git_op
)
fi
if [ -e .syncignore ] ; then
rsync_option="--exclude-from=.syncignore"
else
rsync_option=""
fi
## FIXME: should I exclude untracked files? Seems like it
# per discussion over build tools issue, rsync everything
rsync --recursive --delete --exclude .git --exclude $repo_location . $repo_location
if [ -e .syncignore ] ; then
cat .syncignore >> $repo_location/.gitignore
fi
build_command="$(git config --get sync.${dest}.build || true)"
if [ -n "$build_command" ] ; then
echo "Running $build_command"
(
cd $repo_location
$build_command
)
fi
version="$(git describe --always --dirty)"
(
cd $repo_location
adds="$(git status -s | awk '/^\?\?/ {print $2}')"
if [ -n "$adds" ] ; then
git add $adds
fi
git commit -a -m "deployment $version"
if [ -z "$remote_branch" ] ; then
git_op=""
else
git_op="$remote_branch:$remote_branch"
fi
echo cd $repo_location "&&" git push origin $git_op
git push origin $git_op
)