Mercurial > hg
annotate hgext/convert/git.py @ 4768:f52bfe566583
convert: import all branches from git repositories
author | Brendan Cully <brendan@kublai.com> |
---|---|
date | Sun, 01 Jul 2007 22:00:25 -0700 |
parents | 2d0a823cbba5 |
children | 15a3cbfc6568 |
rev | line source |
---|---|
4536
cc9b79216a76
Split convert extension into common and repository type modules
Brendan Cully <brendan@kublai.com>
parents:
4532
diff
changeset
|
1 # git support for the convert extension |
3821
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
2 |
4536
cc9b79216a76
Split convert extension into common and repository type modules
Brendan Cully <brendan@kublai.com>
parents:
4532
diff
changeset
|
3 import os |
3938
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3917
diff
changeset
|
4 |
4536
cc9b79216a76
Split convert extension into common and repository type modules
Brendan Cully <brendan@kublai.com>
parents:
4532
diff
changeset
|
5 from common import NoRepo, commit, converter_source |
3954
9af4b853ed4d
convert-repo: add CVS branch support
Matt Mackall <mpm@selenic.com>
parents:
3953
diff
changeset
|
6 |
4448
af013ae3ca10
use documented convert-repo interface
Daniel Holth <dholth@fastmail.fm>
parents:
4447
diff
changeset
|
7 class convert_git(converter_source): |
4767
2d0a823cbba5
convert: gitcmd wrapper for os.popen
Brendan Cully <brendan@kublai.com>
parents:
4760
diff
changeset
|
8 def gitcmd(self, s): |
2d0a823cbba5
convert: gitcmd wrapper for os.popen
Brendan Cully <brendan@kublai.com>
parents:
4760
diff
changeset
|
9 return os.popen('GIT_DIR=%s %s' % (self.path, s)) |
2d0a823cbba5
convert: gitcmd wrapper for os.popen
Brendan Cully <brendan@kublai.com>
parents:
4760
diff
changeset
|
10 |
4760
07efcce17d28
convert: add -r argument specifying latest revision to convert
Brendan Cully <brendan@kublai.com>
parents:
4759
diff
changeset
|
11 def __init__(self, ui, path, rev=None): |
3938
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3917
diff
changeset
|
12 if os.path.isdir(path + "/.git"): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3917
diff
changeset
|
13 path += "/.git" |
4759
20ec5cc02f18
convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents:
4721
diff
changeset
|
14 if not os.path.exists(path + "/objects"): |
20ec5cc02f18
convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents:
4721
diff
changeset
|
15 raise NoRepo("couldn't open GIT repo %s" % path) |
20ec5cc02f18
convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents:
4721
diff
changeset
|
16 |
316 | 17 self.path = path |
4513
ac2fe196ac9b
Turns convert.py into a real extension
Edouard Gomez <ed.gomez@free.fr>
parents:
4512
diff
changeset
|
18 self.ui = ui |
4760
07efcce17d28
convert: add -r argument specifying latest revision to convert
Brendan Cully <brendan@kublai.com>
parents:
4759
diff
changeset
|
19 self.rev = rev |
4759
20ec5cc02f18
convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents:
4721
diff
changeset
|
20 self.encoding = 'utf-8' |
316 | 21 |
22 def getheads(self): | |
4768
f52bfe566583
convert: import all branches from git repositories
Brendan Cully <brendan@kublai.com>
parents:
4767
diff
changeset
|
23 if not self.rev: |
f52bfe566583
convert: import all branches from git repositories
Brendan Cully <brendan@kublai.com>
parents:
4767
diff
changeset
|
24 return self.gitcmd('git-rev-parse --branches').read().splitlines() |
f52bfe566583
convert: import all branches from git repositories
Brendan Cully <brendan@kublai.com>
parents:
4767
diff
changeset
|
25 else: |
f52bfe566583
convert: import all branches from git repositories
Brendan Cully <brendan@kublai.com>
parents:
4767
diff
changeset
|
26 fh = self.gitcmd("git-rev-parse --verify %s" % self.rev) |
f52bfe566583
convert: import all branches from git repositories
Brendan Cully <brendan@kublai.com>
parents:
4767
diff
changeset
|
27 return [fh.read()[:-1]] |
316 | 28 |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
29 def catfile(self, rev, type): |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
30 if rev == "0" * 40: raise IOError() |
4767
2d0a823cbba5
convert: gitcmd wrapper for os.popen
Brendan Cully <brendan@kublai.com>
parents:
4760
diff
changeset
|
31 fh = self.gitcmd("git-cat-file %s %s 2>/dev/null" % (type, rev)) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
32 return fh.read() |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
33 |
316 | 34 def getfile(self, name, rev): |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
35 return self.catfile(rev, "blob") |
316 | 36 |
3956
558f52943cd2
convert-repo: add CVS exec bit support
Matt Mackall <mpm@selenic.com>
parents:
3954
diff
changeset
|
37 def getmode(self, name, rev): |
558f52943cd2
convert-repo: add CVS exec bit support
Matt Mackall <mpm@selenic.com>
parents:
3954
diff
changeset
|
38 return self.modecache[(name, rev)] |
558f52943cd2
convert-repo: add CVS exec bit support
Matt Mackall <mpm@selenic.com>
parents:
3954
diff
changeset
|
39 |
316 | 40 def getchanges(self, version): |
3956
558f52943cd2
convert-repo: add CVS exec bit support
Matt Mackall <mpm@selenic.com>
parents:
3954
diff
changeset
|
41 self.modecache = {} |
4767
2d0a823cbba5
convert: gitcmd wrapper for os.popen
Brendan Cully <brendan@kublai.com>
parents:
4760
diff
changeset
|
42 fh = self.gitcmd("git-diff-tree --root -m -r %s" % version) |
316 | 43 changes = [] |
44 for l in fh: | |
45 if "\t" not in l: continue | |
46 m, f = l[:-1].split("\t") | |
47 m = m.split() | |
48 h = m[3] | |
49 p = (m[1] == "100755") | |
4082
6b2909e84203
convert-repo converts symlinks from git
Daniel Holth <dholth@fastmail.fm>
parents:
4062
diff
changeset
|
50 s = (m[1] == "120000") |
6b2909e84203
convert-repo converts symlinks from git
Daniel Holth <dholth@fastmail.fm>
parents:
4062
diff
changeset
|
51 self.modecache[(f, h)] = (p and "x") or (s and "l") or "" |
3956
558f52943cd2
convert-repo: add CVS exec bit support
Matt Mackall <mpm@selenic.com>
parents:
3954
diff
changeset
|
52 changes.append((f, h)) |
316 | 53 return changes |
54 | |
55 def getcommit(self, version): | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
56 c = self.catfile(version, "commit") # read the commit hash |
316 | 57 end = c.find("\n\n") |
58 message = c[end+2:] | |
4759
20ec5cc02f18
convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents:
4721
diff
changeset
|
59 message = self.recode(message) |
316 | 60 l = c[:end].splitlines() |
61 manifest = l[0].split()[1] | |
62 parents = [] | |
63 for e in l[1:]: | |
4532
c3a78a49d7f0
Some small cleanups for convert extension:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4521
diff
changeset
|
64 n, v = e.split(" ", 1) |
316 | 65 if n == "author": |
66 p = v.split() | |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
67 tm, tz = p[-2:] |
316 | 68 author = " ".join(p[:-2]) |
69 if author[0] == "<": author = author[1:-1] | |
4759
20ec5cc02f18
convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents:
4721
diff
changeset
|
70 author = self.recode(author) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
71 if n == "committer": |
431 | 72 p = v.split() |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
73 tm, tz = p[-2:] |
431 | 74 committer = " ".join(p[:-2]) |
75 if committer[0] == "<": committer = committer[1:-1] | |
4759
20ec5cc02f18
convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents:
4721
diff
changeset
|
76 committer = self.recode(committer) |
3910
4bc5a2405b12
convert-repo: fix recoding of committer
Matt Mackall <mpm@selenic.com>
parents:
3821
diff
changeset
|
77 message += "\ncommitter: %s\n" % committer |
316 | 78 if n == "parent": parents.append(v) |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
79 |
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
80 tzs, tzh, tzm = tz[-5:-4] + "1", tz[-4:-2], tz[-2:] |
2093
5cc414722587
convert-repo: fix reversed time zone offset
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1715
diff
changeset
|
81 tz = -int(tzs) * (int(tzh) * 3600 + int(tzm)) |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
82 date = tm + " " + str(tz) |
4721
96614af3c679
convert: "unknown" is a string
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4684
diff
changeset
|
83 author = author or "unknown" |
3954
9af4b853ed4d
convert-repo: add CVS branch support
Matt Mackall <mpm@selenic.com>
parents:
3953
diff
changeset
|
84 |
9af4b853ed4d
convert-repo: add CVS branch support
Matt Mackall <mpm@selenic.com>
parents:
3953
diff
changeset
|
85 c = commit(parents=parents, date=date, author=author, desc=message) |
9af4b853ed4d
convert-repo: add CVS branch support
Matt Mackall <mpm@selenic.com>
parents:
3953
diff
changeset
|
86 return c |
316 | 87 |
694 | 88 def gettags(self): |
89 tags = {} | |
4767
2d0a823cbba5
convert: gitcmd wrapper for os.popen
Brendan Cully <brendan@kublai.com>
parents:
4760
diff
changeset
|
90 fh = self.gitcmd('git-ls-remote --tags "%s" 2>/dev/null' % self.path) |
4062
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
91 prefix = 'refs/tags/' |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
92 for line in fh: |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
93 line = line.strip() |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
94 if not line.endswith("^{}"): |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
95 continue |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
96 node, tag = line.split(None, 1) |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
97 if not tag.startswith(prefix): |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
98 continue |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
99 tag = tag[len(prefix):-3] |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
100 tags[tag] = node |
516f883e3d79
convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4047
diff
changeset
|
101 |
694 | 102 return tags |