25
25
from difflib import SequenceMatcher
30
29
from dulwich.objects import (
35
34
def write_commit_patch(f, commit, contents, progress, version=None):
50
50
p = subprocess.Popen(["diffstat"], stdout=subprocess.PIPE,
51
51
stdin=subprocess.PIPE)
52
except (ImportError, OSError), e:
53
53
pass # diffstat not available?
55
55
(diffstat, _) = p.communicate(contents)
106
def write_object_diff(f, store, (old_path, old_mode, old_id),
107
(new_path, new_mode, new_id)):
108
"""Write the diff for an object.
110
:param f: File-like object to write to
111
:param store: Store to retrieve objects from, if necessary
112
:param (old_path, old_mode, old_hexsha): Old file
113
:param (new_path, new_mode, new_hexsha): New file
115
:note: the tuple elements should be None for nonexistant files
122
def lines(mode, hexsha):
125
elif S_ISGITLINK(mode):
126
return ["Submodule commit " + hexsha + "\n"]
128
return store[hexsha].data.splitlines(True)
130
old_path = "/dev/null"
132
old_path = "a/%s" % old_path
134
new_path = "/dev/null"
136
new_path = "b/%s" % new_path
137
f.write("diff --git %s %s\n" % (old_path, new_path))
138
if old_mode != new_mode:
139
if new_mode is not None:
140
if old_mode is not None:
141
f.write("old mode %o\n" % old_mode)
142
f.write("new mode %o\n" % new_mode)
144
f.write("deleted mode %o\n" % old_mode)
145
f.write("index %s..%s" % (shortid(old_id), shortid(new_id)))
146
if new_mode is not None:
147
f.write(" %o" % new_mode)
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,
106
155
def write_blob_diff(f, (old_path, old_mode, old_blob),
107
156
(new_path, new_mode, new_blob)):
108
157
"""Write diff file header.
110
159
:param f: File-like object to write to
111
160
:param (old_path, old_mode, old_blob): Previous file (None if nonexisting)
112
161
:param (new_path, new_mode, new_blob): New file (None if nonexisting)
163
:note: The use of write_object_diff is recommended over this function.
114
165
def blob_id(blob):
157
208
changes = store.tree_changes(old_tree, new_tree)
158
209
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
160
old_blob = Blob.from_string("")
162
old_blob = store[oldsha]
164
new_blob = Blob.from_string("")
166
new_blob = store[newsha]
167
write_blob_diff(f, (oldpath, oldmode, old_blob),
168
(newpath, newmode, new_blob))
210
write_object_diff(f, store, (oldpath, oldmode, oldsha),
211
(newpath, newmode, newsha))
171
214
def git_am_patch_split(f):