hgext/convert/git.py
author Augie Fackler <durin42@gmail.com>
Sat, 24 Mar 2012 22:13:17 -0500
branchstable
changeset 16292 101c8a1befb1
parent 14945 11aad09a6370
child 16295 ba42eb722bb3
permissions -rw-r--r--
convert/git: abort if git submodules are detected (issue2150) This improves the error message when convert encounters a git submodule. Now, instead of a git-cat-file error, we'll directly report the lack of support for git submodules.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8250
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 7875
diff changeset
     1
# git.py - git support for the convert extension
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 7875
diff changeset
     2
#
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 7875
diff changeset
     3
#  Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 7875
diff changeset
     4
#
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 7875
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 8456
diff changeset
     6
# GNU General Public License version 2 or any later version.
3821
158fce02dc40 Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents: 2657
diff changeset
     7
4536
cc9b79216a76 Split convert extension into common and repository type modules
Brendan Cully <brendan@kublai.com>
parents: 4532
diff changeset
     8
import os
5220
9d7052f17d77 convert: fix /dev/null redirections under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5217
diff changeset
     9
from mercurial import util
12144
be9c4131a8f4 clone, patch, convert: use hex(nullid) instead of '0'*40
Martin Geisler <mg@lazybytes.net>
parents: 11134
diff changeset
    10
from mercurial.node import hex, nullid
10939
9f6731b03906 convert: mark strings for translation
Martin Geisler <mg@lazybytes.net>
parents: 10938
diff changeset
    11
from mercurial.i18n import _
3938
0fab73b3f453 convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents: 3917
diff changeset
    12
5497
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5404
diff changeset
    13
from common import NoRepo, commit, converter_source, checktool
3954
9af4b853ed4d convert-repo: add CVS branch support
Matt Mackall <mpm@selenic.com>
parents: 3953
diff changeset
    14
4448
af013ae3ca10 use documented convert-repo interface
Daniel Holth <dholth@fastmail.fm>
parents: 4447
diff changeset
    15
class convert_git(converter_source):
5217
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    16
    # Windows does not support GIT_DIR= construct while other systems
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    17
    # cannot remove environment variable. Just assume none have
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    18
    # both issues.
14945
11aad09a6370 hgext: replace uses of hasattr with util.safehasattr
Augie Fackler <durin42@gmail.com>
parents: 14735
diff changeset
    19
    if util.safehasattr(os, 'unsetenv'):
13756
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    20
        def gitopen(self, s, noerr=False):
5217
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    21
            prevgitdir = os.environ.get('GIT_DIR')
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    22
            os.environ['GIT_DIR'] = self.path
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    23
            try:
13756
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    24
                if noerr:
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    25
                    (stdin, stdout, stderr) = util.popen3(s)
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    26
                    return stdout
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    27
                else:
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    28
                    return util.popen(s, 'rb')
5217
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    29
            finally:
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    30
                if prevgitdir is None:
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    31
                    del os.environ['GIT_DIR']
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    32
                else:
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    33
                    os.environ['GIT_DIR'] = prevgitdir
149742a628fd convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5216
diff changeset
    34
    else:
13756
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    35
        def gitopen(self, s, noerr=False):
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    36
            if noerr:
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    37
                (sin, so, se) = util.popen3('GIT_DIR=%s %s' % (self.path, s))
14177
fc004d16633f convert: fix error in git solaris code
timeless <timeless@mozdev.org>
parents: 13756
diff changeset
    38
                return so
13756
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
    39
            else:
14735
d297ee0b2d94 convert: fix git convert on solaris - it cannot remove environment variables
Mads Kiilerich <mads@kiilerich.com>
parents: 14177
diff changeset
    40
                return util.popen('GIT_DIR=%s %s' % (self.path, s), 'rb')
4767
2d0a823cbba5 convert: gitcmd wrapper for os.popen
Brendan Cully <brendan@kublai.com>
parents: 4760
diff changeset
    41
10986
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    42
    def gitread(self, s):
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    43
        fh = self.gitopen(s)
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    44
        data = fh.read()
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    45
        return data, fh.close()
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    46
4760
07efcce17d28 convert: add -r argument specifying latest revision to convert
Brendan Cully <brendan@kublai.com>
parents: 4759
diff changeset
    47
    def __init__(self, ui, path, rev=None):
4807
15a3cbfc6568 convert: call superclass init from engine init functions
Brendan Cully <brendan@kublai.com>
parents: 4768
diff changeset
    48
        super(convert_git, self).__init__(ui, path, rev=rev)
15a3cbfc6568 convert: call superclass init from engine init functions
Brendan Cully <brendan@kublai.com>
parents: 4768
diff changeset
    49
3938
0fab73b3f453 convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents: 3917
diff changeset
    50
        if os.path.isdir(path + "/.git"):
0fab73b3f453 convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents: 3917
diff changeset
    51
            path += "/.git"
4102
06d65498f73b convert-repo: use .git/objects/ rather than .git/HEAD to detect git repos
Matt Mackall <mpm@selenic.com>
parents: 4082
diff changeset
    52
        if not os.path.exists(path + "/objects"):
10939
9f6731b03906 convert: mark strings for translation
Martin Geisler <mg@lazybytes.net>
parents: 10938
diff changeset
    53
            raise NoRepo(_("%s does not look like a Git repository") % path)
5497
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5404
diff changeset
    54
6837
e30c56f337b1 convert: use git executable only, with subcommands
Dhruva Krishnamurthy <dhruvakm@gmail.com>
parents: 6001
diff changeset
    55
        checktool('git', 'git')
5497
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5404
diff changeset
    56
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    57
        self.path = path
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    58
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    59
    def getheads(self):
4768
f52bfe566583 convert: import all branches from git repositories
Brendan Cully <brendan@kublai.com>
parents: 4767
diff changeset
    60
        if not self.rev:
10986
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    61
            heads, ret = self.gitread('git rev-parse --branches --remotes')
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    62
            heads = heads.splitlines()
4768
f52bfe566583 convert: import all branches from git repositories
Brendan Cully <brendan@kublai.com>
parents: 4767
diff changeset
    63
        else:
10986
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    64
            heads, ret = self.gitread("git rev-parse --verify %s" % self.rev)
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    65
            heads = [heads[:-1]]
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    66
        if ret:
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    67
            raise util.Abort(_('cannot retrieve git heads'))
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    68
        return heads
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    69
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    70
    def catfile(self, rev, type):
12144
be9c4131a8f4 clone, patch, convert: use hex(nullid) instead of '0'*40
Martin Geisler <mg@lazybytes.net>
parents: 11134
diff changeset
    71
        if rev == hex(nullid):
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    72
            raise IOError()
10986
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    73
        data, ret = self.gitread("git cat-file %s %s" % (type, rev))
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    74
        if ret:
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    75
            raise util.Abort(_('cannot read %r object at %s') % (type, rev))
610f047326b9 convert/git: check status when reading the whole output
Patrick Mezard <pmezard@gmail.com>
parents: 10985
diff changeset
    76
        return data
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    77
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    78
    def getfile(self, name, rev):
11134
33010ff1fd6f convert: merge sources getmode() into getfile()
Patrick Mezard <pmezard@gmail.com>
parents: 10987
diff changeset
    79
        data = self.catfile(rev, "blob")
33010ff1fd6f convert: merge sources getmode() into getfile()
Patrick Mezard <pmezard@gmail.com>
parents: 10987
diff changeset
    80
        mode = self.modecache[(name, rev)]
33010ff1fd6f convert: merge sources getmode() into getfile()
Patrick Mezard <pmezard@gmail.com>
parents: 10987
diff changeset
    81
        return data, mode
3956
558f52943cd2 convert-repo: add CVS exec bit support
Matt Mackall <mpm@selenic.com>
parents: 3954
diff changeset
    82
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    83
    def getchanges(self, version):
3956
558f52943cd2 convert-repo: add CVS exec bit support
Matt Mackall <mpm@selenic.com>
parents: 3954
diff changeset
    84
        self.modecache = {}
10985
7fab6ae3f688 convert/git: rename gitcmd() into gitopen() for readability
Patrick Mezard <pmezard@gmail.com>
parents: 10939
diff changeset
    85
        fh = self.gitopen("git diff-tree -z --root -m -r %s" % version)
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    86
        changes = []
8456
e9e2a2c9b294 convert: use set instead of dict
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8271
diff changeset
    87
        seen = set()
7242
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    88
        entry = None
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    89
        for l in fh.read().split('\x00'):
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    90
            if not entry:
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    91
                if not l.startswith(':'):
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    92
                    continue
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    93
                entry = l
5335
88e931f74e8b convert_git: avoid returning two entries for the same file in getchanges
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5220
diff changeset
    94
                continue
7242
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    95
            f = l
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    96
            if f not in seen:
8456
e9e2a2c9b294 convert: use set instead of dict
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8271
diff changeset
    97
                seen.add(f)
7242
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    98
                entry = entry.split()
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
    99
                h = entry[3]
16292
101c8a1befb1 convert/git: abort if git submodules are detected (issue2150)
Augie Fackler <durin42@gmail.com>
parents: 14945
diff changeset
   100
                if entry[1] == '160000':
101c8a1befb1 convert/git: abort if git submodules are detected (issue2150)
Augie Fackler <durin42@gmail.com>
parents: 14945
diff changeset
   101
                    raise util.Abort('git submodules are not supported!')
7242
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
   102
                p = (entry[1] == "100755")
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
   103
                s = (entry[1] == "120000")
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
   104
                self.modecache[(f, h)] = (p and "x") or (s and "l") or ""
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
   105
                changes.append((f, h))
d1dff8c492dd convert: fix non-ASCII filenames retrieval from git sources (issue 1360)
Patrick Mezard <pmezard@gmail.com>
parents: 7222
diff changeset
   106
            entry = None
10987
b3af02b1f19f convert/git: check status when reading output stream
Patrick Mezard <pmezard@gmail.com>
parents: 10986
diff changeset
   107
        if fh.close():
b3af02b1f19f convert/git: check status when reading output stream
Patrick Mezard <pmezard@gmail.com>
parents: 10986
diff changeset
   108
            raise util.Abort(_('cannot read changes in %s') % version)
5121
ef338e34a906 convert: look up copies in getchanges instead of getcommit
Brendan Cully <brendan@kublai.com>
parents: 4873
diff changeset
   109
        return (changes, {})
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   110
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   111
    def getcommit(self, version):
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   112
        c = self.catfile(version, "commit") # read the commit hash
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   113
        end = c.find("\n\n")
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   114
        message = c[end + 2:]
4759
20ec5cc02f18 convert: ove recode method into converter_source
Brendan Cully <brendan@kublai.com>
parents: 4721
diff changeset
   115
        message = self.recode(message)
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   116
        l = c[:end].splitlines()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   117
        parents = []
8271
e3d3dad805f9 Add committer tag only when needed in git conversion
Richard Quirk <richard.quirk@gmail.com>
parents: 8250
diff changeset
   118
        author = committer = None
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   119
        for e in l[1:]:
4532
c3a78a49d7f0 Some small cleanups for convert extension:
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4521
diff changeset
   120
            n, v = e.split(" ", 1)
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   121
            if n == "author":
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   122
                p = v.split()
1385
adb3de56635b convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents: 1335
diff changeset
   123
                tm, tz = p[-2:]
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   124
                author = " ".join(p[:-2])
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   125
                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
   126
                author = self.recode(author)
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   127
            if n == "committer":
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   128
                p = v.split()
1385
adb3de56635b convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents: 1335
diff changeset
   129
                tm, tz = p[-2:]
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   130
                committer = " ".join(p[:-2])
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   131
                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
   132
                committer = self.recode(committer)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   133
            if n == "parent":
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   134
                parents.append(v)
1385
adb3de56635b convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents: 1335
diff changeset
   135
8271
e3d3dad805f9 Add committer tag only when needed in git conversion
Richard Quirk <richard.quirk@gmail.com>
parents: 8250
diff changeset
   136
        if committer and committer != author:
e3d3dad805f9 Add committer tag only when needed in git conversion
Richard Quirk <richard.quirk@gmail.com>
parents: 8250
diff changeset
   137
            message += "\ncommitter: %s\n" % committer
1385
adb3de56635b convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents: 1335
diff changeset
   138
        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
   139
        tz = -int(tzs) * (int(tzh) * 3600 + int(tzm))
1385
adb3de56635b convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents: 1335
diff changeset
   140
        date = tm + " " + str(tz)
3954
9af4b853ed4d convert-repo: add CVS branch support
Matt Mackall <mpm@selenic.com>
parents: 3953
diff changeset
   141
4873
28b23b9073a8 convert: record the source revision in the changelog
Brendan Cully <brendan@kublai.com>
parents: 4810
diff changeset
   142
        c = commit(parents=parents, date=date, author=author, desc=message,
28b23b9073a8 convert: record the source revision in the changelog
Brendan Cully <brendan@kublai.com>
parents: 4810
diff changeset
   143
                   rev=version)
3954
9af4b853ed4d convert-repo: add CVS branch support
Matt Mackall <mpm@selenic.com>
parents: 3953
diff changeset
   144
        return c
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   145
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   146
    def gettags(self):
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   147
        tags = {}
10985
7fab6ae3f688 convert/git: rename gitcmd() into gitopen() for readability
Patrick Mezard <pmezard@gmail.com>
parents: 10939
diff changeset
   148
        fh = self.gitopen('git ls-remote --tags "%s"' % self.path)
4062
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   149
        prefix = 'refs/tags/'
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   150
        for line in fh:
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   151
            line = line.strip()
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   152
            if not line.endswith("^{}"):
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   153
                continue
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   154
            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
   155
            if not tag.startswith(prefix):
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   156
                continue
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   157
            tag = tag[len(prefix):-3]
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   158
            tags[tag] = node
10987
b3af02b1f19f convert/git: check status when reading output stream
Patrick Mezard <pmezard@gmail.com>
parents: 10986
diff changeset
   159
        if fh.close():
b3af02b1f19f convert/git: check status when reading output stream
Patrick Mezard <pmezard@gmail.com>
parents: 10986
diff changeset
   160
            raise util.Abort(_('cannot read tags from %s') % self.path)
4062
516f883e3d79 convert-repo: handle packed git tags
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4047
diff changeset
   161
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   162
        return tags
5380
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   163
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   164
    def getchangedfiles(self, version, i):
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   165
        changes = []
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   166
        if i is None:
10985
7fab6ae3f688 convert/git: rename gitcmd() into gitopen() for readability
Patrick Mezard <pmezard@gmail.com>
parents: 10939
diff changeset
   167
            fh = self.gitopen("git diff-tree --root -m -r %s" % version)
5380
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   168
            for l in fh:
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   169
                if "\t" not in l:
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   170
                    continue
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   171
                m, f = l[:-1].split("\t")
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   172
                changes.append(f)
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   173
        else:
10985
7fab6ae3f688 convert/git: rename gitcmd() into gitopen() for readability
Patrick Mezard <pmezard@gmail.com>
parents: 10939
diff changeset
   174
            fh = self.gitopen('git diff-tree --name-only --root -r %s "%s^%s" --'
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   175
                             % (version, version, i + 1))
5380
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   176
            changes = [f.rstrip('\n') for f in fh]
10987
b3af02b1f19f convert/git: check status when reading output stream
Patrick Mezard <pmezard@gmail.com>
parents: 10986
diff changeset
   177
        if fh.close():
b3af02b1f19f convert/git: check status when reading output stream
Patrick Mezard <pmezard@gmail.com>
parents: 10986
diff changeset
   178
            raise util.Abort(_('cannot read changes in %s') % version)
5380
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   179
a5a7f7fd5554 convert_git: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5336
diff changeset
   180
        return changes
13756
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   181
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   182
    def getbookmarks(self):
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   183
        bookmarks = {}
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   184
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   185
        # Interesting references in git are prefixed
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   186
        prefix = 'refs/heads/'
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   187
        prefixlen = len(prefix)
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   188
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   189
        # factor two commands
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   190
        gitcmd = { 'remote/': 'git ls-remote --heads origin',
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   191
                          '': 'git show-ref'}
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   192
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   193
        # Origin heads
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   194
        for reftype in gitcmd:
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   195
            try:
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   196
                fh = self.gitopen(gitcmd[reftype], noerr=True)
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   197
                for line in fh:
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   198
                    line = line.strip()
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   199
                    rev, name = line.split(None, 1)
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   200
                    if not name.startswith(prefix):
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   201
                        continue
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   202
                    name = '%s%s' % (reftype, name[prefixlen:])
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   203
                    bookmarks[name] = rev
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   204
            except:
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   205
                pass
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   206
6b7077df4aa5 convert: add bookmarks reading support to git backend
Edouard Gomez <ed.gomez@free.fr>
parents: 12144
diff changeset
   207
        return bookmarks