Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c92df6

Browse files
committed
diff: added __str__ method to diff class
IndexObject._mode_str_to_int: Now uses the 6 relevant bytes of the passed in octal string
1 parent e40b5f0 commit 9c92df6

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

lib/git/diff.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def __init__(self, repo, a_path, b_path, a_blob_id, b_blob_id, a_mode,
208208
self.a_blob = None
209209
else:
210210
self.a_blob = blob.Blob(repo, a_blob_id, mode=a_mode, path=a_path)
211-
if not b_blob_id:
211+
if b_blob_id is None:
212212
self.b_blob = None
213213
else:
214214
self.b_blob = blob.Blob(repo, b_blob_id, mode=b_mode, path=b_path)
@@ -244,6 +244,45 @@ def __ne__(self, other):
244244
def __hash__(self):
245245
return hash(tuple(getattr(self,n) for n in self.__slots__))
246246

247+
def __str__(self):
248+
h = "%s"
249+
if self.a_blob:
250+
h %= self.a_blob.path
251+
if self.b_blob:
252+
h %= self.b_blob.path
253+
254+
msg = ''
255+
l = None # temp line
256+
ll = 0 # line length
257+
for b,n in zip((self.a_blob, self.b_blob), ('lhs', 'rhs')):
258+
if b:
259+
l = "\n%s: %o | %s" % (n, b.mode, b.sha)
260+
else:
261+
l = "\n%s: None" % n
262+
# END if blob is not None
263+
ll = max(len(l), ll)
264+
msg += l
265+
# END for each blob
266+
267+
# add headline
268+
h += '\n' + '='*ll
269+
270+
if self.deleted_file:
271+
msg += '\nfile deleted in rhs'
272+
if self.new_file:
273+
msg += '\nfile added in rhs'
274+
if self.rename_from:
275+
msg += '\nfile renamed from %r' % self.rename_from
276+
if self.rename_to:
277+
msg += '\nfile renamed to %r' % self.rename_to
278+
if self.diff:
279+
msg += '\n---'
280+
msg += self.diff
281+
msg += '\n---'
282+
# END diff info
283+
284+
return h + msg
285+
247286
@property
248287
def renamed(self):
249288
"""

lib/git/objects/base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,16 @@ def _set_cache_(self, attr):
184184
def _mode_str_to_int(cls, modestr):
185185
"""
186186
``modestr``
187-
string like 755 or 644 or 100644 - only the last 3 chars will be used
187+
string like 755 or 644 or 100644 - only the last 6 chars will be used
188188
189189
Returns
190190
String identifying a mode compatible to the mode methods ids of the
191-
stat module regarding the rwx permissions for user, group and other
191+
stat module regarding the rwx permissions for user, group and other,
192+
special flags and file system flags, i.e. whether it is a symlink
193+
for example.
192194
"""
193195
mode = 0
194-
for iteration,char in enumerate(reversed(modestr[-3:])):
196+
for iteration,char in enumerate(reversed(modestr[-6:])):
195197
mode += int(char) << iteration*3
196198
# END for each char
197199
return mode

0 commit comments

Comments
 (0)