# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
-# git svn keyword parsing and populating
+# git svn keyword parsing, populating, and clearing.
-import os
+from __future__ import with_statement
+import ConfigParser, errno, os, re, urllib
+import git
-if 'GIT_DIR' not in os.environ:
- print "This is a git post-checkout hook and must be run in the context of git."
- exit(1)
+VERSION = 0
+def parse_svn_unhandled(g):
+ gsk = g.path + '/svn_keywords'
+ try:
+ os.mkdir(gsk)
+ except os.error, e:
+ if e.errno != errno.EEXIST:
+ raise
+
+ config = ConfigParser.ConfigParser()
+ config_path = gsk + '/conf.ini'
+ config.read(config_path)
+
+ files = ConfigParser.ConfigParser()
+ files_path = gsk + '/files.ini'
+
+ if not config.has_section('core'):
+ config.add_section('core')
+ if config.has_option('core', 'version'):
+ ver = config.getint('core', 'ver')
+
+ lastrev = None
+ if ver == VERSION:
+ files.read(files_path)
+ if config.has_option('core', 'lastrev'):
+ lastrev = config.getint('core', 'lastrev')
+
+ with open(g.path + '/svn/git-svn/unhandled.log', 'r') as f:
+ # Compile the regular expressions we'll be using here.
+ re_rev = re.compile("^r(\d+)$")
+ re_keywords = re.compile("^\s+[-+]file_prop: (\S+) svn:keywords ?(\S*)$")
+
+ rev = None
+ for line in f:
+ m = re_rev.match(line)
+ if m:
+ rev = m.group(1)
+ continue
+
+ if (lastrev >= int(rev)):
+ continue
+
+ m = re_keywords.match(line)
+ if m:
+ path = urllib.unquote(m.group(1))
+ keywords = set(urllib.unquote(m.group(2)).split(' '))
+ if not files.has_section(path):
+ files.add_section(path)
+ files.set(path, rev, keywords)
+
+ lastrev = max(int(rev), lastrev)
+ config.set('core', 'lastrev', lastrev)
+ config.set('core', 'version', VERSION)
+
+ with open(files_path, 'wb') as f:
+ files.write(f)
+
+ with open(config_path, 'wb') as f:
+ config.write(f)
+
+def smudge(g):
+ return 0
+
+def clean(g):
+ return 0
+
+if __name__ == '__main__':
+ try:
+ g = git.Repo()
+ except git.errors.InvalidGitRepositoryError:
+ print "You are not in a git repository or working directory."
+ exit(1)
+ parse_svn_unhandled(g)