/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to git_repository.py

Rewrite GitRepository._parse_rev, with unit tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
95
95
    def get_revisions(self, revisions):
96
96
        return [self.get_revision(r) for r in revisions]
97
97
 
98
 
    def _parse_rev(self, raw):
99
 
        # first field is the rev itself.
100
 
        # then its 'field value'
101
 
        # until the EOF??
 
98
    @classmethod
 
99
    def _parse_rev(klass, raw):
 
100
        """Parse a single git revision.
 
101
 
 
102
        * The first line is the git commit id.
 
103
        * Following lines conform to the 'name value' structure, until the
 
104
          first blank line.
 
105
        * All lines after the first blank line and until the NULL line have 4
 
106
          leading spaces and constitute the commit message.
 
107
 
 
108
        :param raw: sequence of newline-terminated strings, its last item is a
 
109
            single NULL character.
 
110
        :return: a `bzrlib.revision.Revision` object.
 
111
        """
102
112
        parents = []
103
 
        log = []
104
 
        in_log = False
105
 
        committer = None
 
113
        message_lines = []
 
114
        in_message = False
 
115
        committer_was_set = False
106
116
        revision_id = ids.convert_revision_id_git_to_bzr(raw[0][:-1])
107
 
        for field in raw[1:]:
108
 
            #if field.startswith('author '):
109
 
            #    committer = field[7:]
110
 
            if field.startswith('parent '):
111
 
                parents.append(
112
 
                    ids.convert_revision_id_git_to_bzr(field.split()[1]))
113
 
            elif field.startswith('committer '):
114
 
                commit_fields = field.split()
115
 
                if committer is None:
116
 
                    committer = ' '.join(commit_fields[1:-3])
117
 
                timestamp = commit_fields[-2]
118
 
                timezone = commit_fields[-1]
119
 
            elif field.startswith('tree '):
120
 
                tree_id = field.split()[1]
121
 
            elif in_log:
122
 
                log.append(field[4:])
123
 
            elif field == '\n':
124
 
                in_log = True
 
117
        rev = revision.Revision(revision_id)
 
118
        rev.inventory_sha1 = ""
 
119
        assert raw[-1] == '\x00', (
 
120
            "Last item of raw was not a single NULL character.")
 
121
        for line in raw[1:-1]:
 
122
            if in_message:
 
123
                assert line[:4] == '    ', (
 
124
                    "Unexpected line format in commit message: %r" % line)
 
125
                message_lines.append(line[4:])
 
126
                continue
 
127
            if line == '\n':
 
128
                in_message = True
 
129
                continue
 
130
            name, value = line[:-1].split(' ', 1)
 
131
            if name == 'parent':
 
132
                rev.parent_ids.append(
 
133
                    ids.convert_revision_id_git_to_bzr(value))
 
134
                continue
 
135
            if name == 'author':
 
136
                author, timestamp, timezone = value.rsplit(' ', 2)
 
137
                rev.properties['author'] = author
 
138
                rev.properties['git-author-timestamp'] = timestamp
 
139
                rev.properties['git-author-timezone'] = timezone
 
140
                if not committer_was_set:
 
141
                    rev.committer = author
 
142
                    rev.timestamp = float(timestamp)
 
143
                    rev.timezone = timezone
 
144
                continue
 
145
            if name == 'committer':
 
146
                committer_was_set = True
 
147
                committer, timestamp, timezone = value.rsplit(' ', 2)
 
148
                rev.committer = committer
 
149
                rev.timestamp = float(timestamp)
 
150
                rev.timezone = timezone
 
151
                continue
 
152
            if name == 'tree':
 
153
                rev.properties['git-tree-id'] = value
 
154
                continue
125
155
 
126
 
        log = ''.join(log)
127
 
        result = revision.Revision(revision_id)
128
 
        result.parent_ids = parents
129
 
        result.message = log
130
 
        result.inventory_sha1 = ""
131
 
        result.timezone = timezone and int(timezone)
132
 
        result.timestamp = float(timestamp)
133
 
        result.committer = committer
134
 
        result.properties['git-tree-id'] = tree_id
135
 
        return result
 
156
        rev.message = ''.join(message_lines)
 
157
        return rev
136
158
 
137
159
    def revision_trees(self, revids):
138
160
        for revid in revids: