~ubuntu-branches/ubuntu/wily/dulwich/wily

« back to all changes in this revision

Viewing changes to dulwich/patch.py

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2013-05-31 00:58:42 UTC
  • mfrom: (1.5.1) (31.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130531005842-whdxa8wnmeqqfidr
Tags: 0.9.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    S_ISGITLINK,
32
32
    )
33
33
 
 
34
FIRST_FEW_BYTES = 8000
 
35
 
 
36
 
34
37
def write_commit_patch(f, commit, contents, progress, version=None):
35
38
    """Write a individual file patch.
36
39
 
103
106
                    yield '+' + line
104
107
 
105
108
 
 
109
def is_binary(content):
 
110
    """See if the first few bytes contain any null characters.
 
111
 
 
112
    :param content: Bytestring to check for binary content
 
113
    """
 
114
    return '\0' in content[:FIRST_FEW_BYTES]
 
115
 
 
116
 
106
117
def write_object_diff(f, store, (old_path, old_mode, old_id),
107
 
                                (new_path, new_mode, new_id)):
 
118
                                (new_path, new_mode, new_id),
 
119
                                diff_binary=False):
108
120
    """Write the diff for an object.
109
121
 
110
122
    :param f: File-like object to write to
111
123
    :param store: Store to retrieve objects from, if necessary
112
124
    :param (old_path, old_mode, old_hexsha): Old file
113
125
    :param (new_path, new_mode, new_hexsha): New file
 
126
    :param diff_binary: Whether to diff files even if they
 
127
        are considered binary files by is_binary().
114
128
 
115
129
    :note: the tuple elements should be None for nonexistant files
116
130
    """
119
133
            return "0" * 7
120
134
        else:
121
135
            return hexsha[:7]
122
 
    def lines(mode, hexsha):
 
136
 
 
137
    def content(mode, hexsha):
123
138
        if hexsha is None:
124
 
            return []
 
139
            return ''
125
140
        elif S_ISGITLINK(mode):
126
 
            return ["Submodule commit " + hexsha + "\n"]
127
 
        else:
128
 
            return store[hexsha].data.splitlines(True)
 
141
            return "Submodule commit " + hexsha + "\n"
 
142
        else:
 
143
            return store[hexsha].data
 
144
 
 
145
    def lines(content):
 
146
        if not content:
 
147
            return []
 
148
        else:
 
149
            return content.splitlines(True)
 
150
 
129
151
    if old_path is None:
130
152
        old_path = "/dev/null"
131
153
    else:
146
168
    if new_mode is not None:
147
169
        f.write(" %o" % new_mode)
148
170
    f.write("\n")
149
 
    old_contents = lines(old_mode, old_id)
150
 
    new_contents = lines(new_mode, new_id)
151
 
    f.writelines(unified_diff(old_contents, new_contents,
152
 
        old_path, new_path))
 
171
    old_content = content(old_mode, old_id)
 
172
    new_content = content(new_mode, new_id)
 
173
    if not diff_binary and (is_binary(old_content) or is_binary(new_content)):
 
174
        f.write("Binary files %s and %s differ\n" % (old_path, new_path))
 
175
    else:
 
176
        f.writelines(unified_diff(lines(old_content), lines(new_content),
 
177
            old_path, new_path))
153
178
 
154
179
 
155
180
def write_blob_diff(f, (old_path, old_mode, old_blob),
198
223
        old_path, new_path))
199
224
 
200
225
 
201
 
def write_tree_diff(f, store, old_tree, new_tree):
 
226
def write_tree_diff(f, store, old_tree, new_tree, diff_binary=False):
202
227
    """Write tree diff.
203
228
 
204
229
    :param f: File-like object to write to.
205
230
    :param old_tree: Old tree id
206
231
    :param new_tree: New tree id
 
232
    :param diff_binary: Whether to diff files even if they
 
233
        are considered binary files by is_binary().
207
234
    """
208
235
    changes = store.tree_changes(old_tree, new_tree)
209
236
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
210
237
        write_object_diff(f, store, (oldpath, oldmode, oldsha),
211
 
                                    (newpath, newmode, newsha))
 
238
                                    (newpath, newmode, newsha),
 
239
                                    diff_binary=diff_binary)
212
240
 
213
241
 
214
242
def git_am_patch_split(f):