-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclone.py
140 lines (118 loc) · 4.52 KB
/
clone.py
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
#!/usr/bin/env python3
from core import *
from fetch import fetch
import shutil
emailRgx = r'\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'
class clone(Command):
"""Clone a TFS repository."""
def __init__(self):
self.fetch = fetch()
Command.__init__(self)
def _initArgParser(self, parser):
parser.addVerbose()
parser.addNoChecks()
ver = parser.add_mutually_exclusive_group()
ver.add_argument('-V', '--version', default='',
help='first changeset. Defaults to the latest.')
ver.add_argument('-A', '--all', action='store_true',
help='Fetch the entire history. Slow.')
ArgParser.addNumber(ver, 'Number of changesets to fetch')
parser.add_argument('-e', '--email',
help='email for TFS')
def _checkRepositoryExists(self):
if git('status -s', errorValue='', allowedExitCodes=[1]):
print('A Git repository already exists')
if git.getChangesetNumber():
print('If you tried to clone but it failed, try to continue by calling pull or fetch')
printIndented('$ git tf pull')
fail()
def _checkDirectory(self):
print('Determining the TFS workspace and folder mapping...')
workfold = tf('workfold .')
pwd = os.path.abspath('.')
folderMaps = re.findall('^\s*(\$[^:]+): (\S+)$', workfold, re.M)
if any(pwd == l for s, l in folderMaps):
return
print('You must be in a mapped local folder to use "git tf clone"')
for serverRoot, localRoot in folderMaps:
if os.path.commonprefix((localRoot, pwd)) == localRoot:
printIndented(localRoot)
fail()
def _setupEmail(self):
def checkEmail(email):
if not re.match(emailRgx, email, flags=re.I):
fail('Malformed email: ' + email)
email = self.args.email
if email:
checkEmail(email)
git('config user.email ' + email)
git('config user.name ' + email.split('@', 1)[0])
else:
email = git('config user.email', errorMsg='Email is not specified')
checkEmail(email)
if self.args.verbose:
print('Email is not specified, so using ', email)
def _configure(self):
autocrlf = git('config tf.clone.autocrlf') or 'true'
git('config core.autocrlf ' + autocrlf)
# rewrite all tf* notes on rebase, namely tf.wi
git('config notes.rewrite.rebase true')
git('config notes.rewriteRef refs/notes/tf*')
def _setupBranches(self):
git('commit --allow-empty -m root-commit')
git('branch -f tfs')
git('branch --set-upstream master tfs')
git('checkout tfs')
def _determineHistoryToFetch(self):
# History
if self.args.all:
print('Requesting for the entire TFS history...')
return tf.history()
elif self.args.number:
print('Requesting for TFS history...')
return tf.history(stopAfter=self.args.number)
else:
print('Determining the latest version...')
history = tf.history(stopAfter=1)
if not history:
return history
latest = history[0]
version = self.args.version
if version:
print('Requesting for TFS history since', version)
return tf.history(version=(version, latest.id))
else:
print('Version is not specified, so using the latest version...')
return [latest]
def _fetch(self):
history = self._determineHistoryToFetch()
if not history:
print('Nothing to fetch')
return
history.reverse()
# Fetch
try:
self.args.force = True
self.fetch.args = self.args
self.fetch.doFetch(history)
finally:
git('checkout master')
git('reset --hard tfs')
def _run(self):
self._checkRepositoryExists()
self._checkDirectory()
self.checkStatus(checkGit=False)
git('init')
try:
self._configure()
self._setupEmail()
self._setupBranches()
self._fetch()
except:
if git('notes show', errorValue=False) is False:
shutil.rmtree('.git')
raise
print()
print('Cloning is completed. Try "git tf log" to see the change history.')
if __name__ == '__main__':
clone().run()