-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgitinitremote
executable file
·178 lines (151 loc) · 3.79 KB
/
gitinitremote
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh
#
# gitinitremote
# turns a local directory into a new remote git repository,
# with the local directory being a clone of the remote
#
# e.g. gitinitremote ssh://mikelward.com/home/mikelward/git/uni/520/nimsys.git
#
# Mikel Ward <[email protected]>
scriptname=gitinitremote
die()
{
error "$*"
exit 1
}
error()
{
echo "$scriptname: $*" 1>&2
}
notice()
{
echo "$scriptname: $*" 1>&2
}
warn()
{
echo "$scriptname: $*" 1>&2
}
run()
{
if $simulate; then
echo "Would run $*" 1>&2
else
"$@"
fi
}
usage()
{
cat <<EOF 1>&2
Usage: $scriptname [-hn] [-r <remote name>] <new remote URL>
EOF
}
# defaults
remotename=origin # what to call the remote
refspec=master # refspec ("branch") to push - can't be changed yet
local=$PWD # local directory to create the repository out of
simulate=false
while getopts ":hnr:" option
do
case $option in
h)
usage
exit 0
;;
n)
simulate=true
;;
r)
remotename=$OPTARG
;;
':')
error "Missing argument to -$OPTARG"
usage
exit 2
;;
'?')
error "Invalid option -$OPTARG"
usage
exit 2
;;
*)
error "The -$option option is not supported yet"
usage
exit 2
;;
esac
done
shift $((OPTIND - 1))
remote=$1
if test -z "$remote"; then
error "Remote URL is required"
usage
exit 2
fi
case $remote in
ssh:*)
remoteproto=ssh
case $remote in
*.git)
:
;;
*)
warn "Adding .git to end of remote URL"
remote="$remote".git
;;
esac
badformatmessage="SSH remote is not in the expected ssh://hostname/dir format"
remotenoproto=${remote#ssh://}
test "$remotenoproto" = "$remote" && die "$badformatmessage"
remotehost=${remotenoproto%%/*}
test "$remotehost" = "$remotenoproto" && die "$badformatmessage"
remotedir=${remotenoproto#*/}
test "$remotedir" = "$remotenoproto" && die "$badformatmessage"
test -z "$remotedir" || test "$remotedir" = ".git" && die "$badformatmessage" # don't allow root directory
remotedir=/"$remotedir" # re-add the leading slash that was stripped above
;;
*)
die "only ssh remotes are supported at the moment"
;;
esac
test "$local" = "$PWD" || die "must currently be run from the local git directory"
if git status >/dev/null 2>&1; then
localalreadyrepo=true
else
localalreadyrepo=false
fi
if ! $localalreadyrepo; then
run git init --quiet
fi
case $remoteproto in
ssh)
ssh $remotehost true || die "Cannot connect to $remotehost via ssh"
ssh $remotehost test -d "'$remotedir'" && die "$remotedir already exists on $remotehost"
ssh $remotehost git --help >/dev/null 2>&1 || die "git is not installed on $remotehost"
run ssh $remotehost git init --quiet --bare "'$remotedir'" || die "Cannot initialize remote repository"
# git remote does not accept --quiet option
run git remote add "$remotename" "$remote" || die "git remote add failed"
;;
*)
die "only ssh remotes are supported at the moment"
;;
esac
if ! $localalreadyrepo; then
# git <= 1.7 requires we have a non-empty directory for commit to work
# (avoid "error: pathspec '' did not match any file(s) known to git.")
dummyfile="$local/.gitignore"
if ! test -f "$dummyfile"; then
run touch "$dummyfile"
fi
# git add does not accept --quiet option
run git add "$local" || die "git add failed"
run git commit --quiet --message "Initial commit" "$local" || die "git commit failed"
fi
# git push does not accept --quiet option
run git push "$remotename" "$refspec" || die "git push failed"
# make the new remote the "upstream" (make "pull" without args work)
# in git >= 1.7, this can be done by adding the "-u" flag to "push"
run git config branch.master.remote "$remotename"
run git config branch.master.merge refs/heads/"$refspec"
notice "Successfully created remote $remote as $remotename"
exit 0
# vim: set ts=4 sw=4 tw=0 noet: