-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-clone-both
executable file
·64 lines (55 loc) · 1.63 KB
/
git-clone-both
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
#!/usr/bin/env python
def classify_args(args):
flags = []
others = []
for i, arg in enumerate(args):
if arg == "--":
others.extend(args[i + 1:])
break
elif arg.startswith("-"):
flags.append(arg)
else:
others.append(arg)
return flags, others
def guess_dir_name(repo):
import re
'''partly based on guess_dir_name in git/builtin/clone.c;
we try to be more strict however'''
m = re.search(r"([A-Za-z0-9._-]+)[/\\]?(\.git)?$", repo)
if not m:
return
return m.group(1)
def main(arg0, *args):
import os, subprocess, sys
prog = os.path.basename(arg0)
flags, others = classify_args(args)
try:
origin = others[0]
source = others[1]
except IndexError:
sys.stderr.write(USAGE.format(prog))
exit(1)
try:
dest = others[2]
except IndexError:
dest = guess_dir_name(origin)
if not dest:
sys.stderr.write("""
{0}: unable to guess destination directory
{0}: please specify DEST explicitly
"""[1:].format(prog))
exit(1)
subprocess.check_call(["git", "clone"] + flags +
["--", others[0]] + others[2:])
os.chdir(dest)
subprocess.check_call(["git", "remote", "add", "source", source])
subprocess.check_call(["git", "fetch", "--all"])
USAGE = "usage: {0} ORIGIN SOURCE [DEST] [FLAGS...]\n"
if __name__ == "__main__":
import subprocess, sys
try:
main(*sys.argv)
except KeyboardInterrupt:
exit(2)
except subprocess.CalledProcessError as e:
exit(e.returncode)