2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009-2010 Alejandro R. SedeƱo <asedeno@mit.edu>
6 # Permission is hereby granted, free of charge, to any person
7 # obtaining a copy of this software and associated documentation files
8 # (the "Software"), to deal in the Software without restriction,
9 # including without limitation the rights to use, copy, modify, merge,
10 # publish, distribute, sublicense, and/or sell copies of the Software,
11 # and to permit persons to whom the Software is furnished to do so,
12 # subject to the following conditions:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 # git svn keyword parsing, populating, and clearing.
28 from __future__ import with_statement
29 import errno, os, re, urllib
30 from ConfigParser import ConfigParser
31 from optparse import OptionParser
37 # Where we keep data in the repo.
39 return os.path.join(g.path, 'svn_keywords')
42 CONFIG = ConfigParser()
43 FILES = ConfigParser()
44 FILEINFO = ConfigParser()
52 svn_keywords = {'Date': ['Date', 'LastDateChanged'],
53 'Revision': ['Revision', 'LastChangedRevision', 'Rev'],
54 'Author': ['Author','LastChangedBy'],
55 'URL': ['HeadURL', 'URL'],
59 # Regular expressions we'll be using to smudge/clean; created as
62 def get_svn_keyword_re(s):
63 if not s in svn_keywords:
64 raise 'Invalid SVN Keyword'
65 if not s in svn_keywords_re:
66 svn_keywords_re[s] = re.compile('\$(' + ('|'.join(svn_keywords[s])) + ')[^$]*\$')
67 return svn_keywords_re[s]
69 def conf_right_version():
71 if CONFIG.has_option('core', 'version'):
72 ver = CONFIG.get('core', 'version')
76 if conf_right_version():
77 FILES.read(FILES_PATH)
79 def get_last_rev(path):
80 if not CONFIG.has_section(path):
81 CONFIG.add_section(path)
84 if conf_right_version() and CONFIG.has_option(path, 'lastrev'):
86 lastrev = CONFIG.getint(path, 'lastrev')
92 # Parse the unhandled log.
93 def _do_parse_unhandled(directory):
94 base = os.path.join(directory)
95 for d in os.listdir(base):
96 subent = os.path.join(base, d)
97 if (d == 'unhandled.log' and os.path.isfile(subent)):
99 strip_prefix = g.git.config('--get','svn-remote.svn.fetch').split(':')[0]
100 lastrev = get_last_rev(subent)
101 with open(subent, 'r') as f:
102 # Compile the regular expressions we'll be using here.
103 re_rev = re.compile("^r(\d+)$")
104 re_keywords = re.compile("^\s+[-+]file_prop: (\S+) svn:keywords ?(\S*)$")
107 m = re_rev.match(line)
112 if (lastrev >= int(rev)):
115 m = re_keywords.match(line)
117 path = urllib.unquote(m.group(1))
118 path = os.path.relpath(path, strip_prefix)
119 keywords = set(urllib.unquote(m.group(2)).split(' '))
120 if not FILES.has_section(path):
121 FILES.add_section(path)
122 FILES.set(path, rev, keywords)
124 lastrev = max(int(rev), lastrev)
125 CONFIG.set(subent, 'lastrev', lastrev)
126 elif (os.path.isdir(subent)):
127 _do_parse_unhandled(subent)
129 def parse_svn_unhandled(g):
133 if e.errno != errno.EEXIST:
136 _do_parse_unhandled(os.path.join(g.path, 'svn'))
137 CONFIG.set('core', 'version', VERSION)
139 with open(FILES_PATH, 'wb') as f:
142 with open(CONFIG_PATH, 'wb') as f:
145 def get_path_info(g, path):
148 # parse ls-tree output and get a blob id for path
150 blob = g.git.ls_tree('HEAD', path).split(' ')[2].split("\t")[0]
152 print "%s may be a stale file in your workspace." % path
155 # translate that to a commit id
156 if not CONFIG.has_option('BlobToCommit', blob):
157 CONFIG.set('BlobToCommit', blob, g.commits('HEAD', path, 1)[0].id)
159 commit = CONFIG.get('BlobToCommit', blob)
161 # tranlsate that into an svn revision id
162 if not CONFIG.has_option('CommitToRev', commit):
163 file_rev = g.git.svn('find-rev', commit)
165 file_rev = "%iM" % find_last_svn_rev('HEAD')
166 CONFIG.set('CommitToRev', commit, file_rev)
169 file_rev = CONFIG.get('CommitToRev', commit)
170 if file_rev[-1] is 'M':
171 # Rewrite old nnnnnnM commits if they're now available in SVN
172 commit = g.commits('HEAD', path, 1)[0].id
173 file_rev2 = g.git.svn('find-rev', commit)
175 CONFIG.set('BlobToCommit', blob, commit)
176 CONFIG.set('CommitToRev', commit, file_rev2)
180 # get information about that revision
183 if not CONFIG.has_option('RevInfo', file_rev):
184 for line in g.git.svn('info', path).split("\n"):
185 k, v = line.split(": ", 1)
186 if k == 'Last Changed Date':
187 info_dict['Date'] = v
188 elif k == 'Last Changed Author':
189 info_dict['Author'] = v
190 CONFIG.set('RevInfo', file_rev, info_dict)
193 info = CONFIG.get('RevInfo', file_rev)
194 info_dict.update(info if type(info) is dict else eval(info))
197 with open(CONFIG_PATH, 'wb') as f:
200 info_dict['Revision'] = file_rev
203 def find_last_svn_rev(treeish, parent=0):
204 svnRev = g.git.svn('find-rev', "%s~%i" % (treeish, parent))
208 return find_last_svn_rev(treeish, parent+1)
211 def smudge(g, options):
213 parse_svn_unhandled(g)
214 rev_head = find_last_svn_rev('HEAD')
215 url_base = g.git.svn('info', '--url')
217 FILES.read(FILES_PATH)
218 FILEINFO.read(FILEINFO_PATH)
221 with open(os.path.join(g.wd,'.git','info','exclude')) as f:
223 line = line.rstrip('\n')
224 if line and line[0] != '#':
225 ignores.append(re.compile(fnmatch.translate(line)))
227 paths = FILES.sections()
230 if not os.path.exists(path):
241 kw_rev = max(filter(lambda x: x <= rev_head, map(int, FILES.options(path))))
246 if not options.clean:
247 update_dict = get_path_info(g, path)
250 info_dict.update(update_dict)
251 info_dict['URL'] = '/'.join([url_base, path])
252 info_dict['Name'] = os.path.basename(path)
253 info_dict['Revision'] = str(max(kw_rev, info_dict['Revision']))
256 with open(os.path.join(g.wd, path), 'r') as f:
259 keywords = eval(FILES.get(path, str(kw_rev)))
261 for sk in svn_keywords:
262 if k in svn_keywords[sk]:
264 buf = get_svn_keyword_re(sk).sub('$\\1$', buf)
266 id_str = ' '.join([info_dict['Name'],
267 info_dict['Revision'],
269 info_dict['Author']])
270 buf = get_svn_keyword_re(sk).sub('$\\1: ' + id_str + ' $', buf)
272 buf = get_svn_keyword_re(sk).sub('$\\1: ' + info_dict[sk] + ' $', buf)
274 with open(os.path.join(g.wd, path), 'w') as f:
277 print path + ' [' + ', '.join(keywords) + '] [len: ' + str(len(buf)) +']'
279 if __name__ == '__main__':
281 parser = OptionParser(version="%prog "+str(VERSION))
282 parser.set_defaults(clean=None)
283 parser.add_option("-s", "--smudge",
284 action="store_false", dest="clean",
285 help="Populate svn:keywords.")
286 parser.add_option("-c", "--clean",
287 action="store_true", dest="clean",
288 help="Return svn:keywords to pristene state.")
289 parser.add_option("-v", "--verbose",
290 action="store_true", dest="verbose", default=False)
291 (options, args) = parser.parse_args()
293 if (options.clean is None):
299 except git.errors.InvalidGitRepositoryError:
300 print "You are not in a git repository or working directory."
304 print "This appears to be a bare git repository."
309 CONFIG_PATH = os.path.join(gsk(g), 'conf.ini')
310 FILES_PATH = os.path.join(gsk(g), 'files.ini')
311 FILEINFO_PATH = os.path.join(gsk(g), 'fileinfo.ini')
313 CONFIG.read(CONFIG_PATH)
314 for section in ['core','CommitToRev','BlobToCommit', 'RevInfo']:
315 if not CONFIG.has_section(section):
316 CONFIG.add_section(section)