Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb372e26

Browse files
committed
added Diffable interface to objects.base, its used by Commit and Tree objects.
Diff class has been prepared to process raw input, but its not yet more than a frame
1 parentbb24f67 commitb372e26

File tree

5 files changed

+95
-62
lines changed

5 files changed

+95
-62
lines changed

‎CHANGES‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ General
2727
terms are used everywhere, such as "Reference" ( ref ) and "Revision" ( rev ).
2828
Prevously multiple terms where used making it harder to know which type was allowed
2929
or not.
30+
* Unified diff interface to allow easy diffing between trees, trees and index, trees
31+
and working tree, index and working tree, trees and index. This closely follows
32+
the git-diff capabilities.
3033

3134

3235
Item Iteration

‎lib/git/diff.py‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
classDiff(object):
1111
"""
12-
A Diff contains diff information between twocommits.
12+
A Diff contains diff information between twoTrees.
1313
1414
It contains two sides a and b of the diff, members are prefixed with
1515
"a" and "b" respectively to inidcate that.
@@ -74,18 +74,20 @@ def __init__(self, repo, a_path, b_path, a_blob_id, b_blob_id, a_mode,
7474
self.diff=diff
7575

7676
@classmethod
77-
def_list_from_string(cls,repo,text):
77+
def_index_from_patch_format(cls,repo,stream):
7878
"""
79-
Create a newdiff objectfrom the given text
79+
Create a newDiffIndexfrom the given text which must be in patch format
8080
``repo``
8181
is the repository we are operating on - it is required
8282
83-
``text``
84-
result of 'git diff'between two commits or one commit and the index
83+
``stream``
84+
result of 'git diff'as a stream (supporting file protocol)
8585
8686
Returns
87-
git.Diff[]
87+
git.DiffIndex
8888
"""
89+
# for now, we have to bake the stream
90+
text=stream.read()
8991
diffs= []
9092

9193
diff_header=cls.re_header.match
@@ -102,4 +104,14 @@ def _list_from_string(cls, repo, text):
102104
new_file,deleted_file,rename_from,rename_to,diff[header.end():]))
103105

104106
returndiffs
107+
108+
@classmethod
109+
def_index_from_raw_format(cls,repo,stream):
110+
"""
111+
Create a new DiffIndex from the given stream which must be in raw format.
112+
113+
Returns
114+
git.DiffIndex
115+
"""
116+
raiseNotImplementedError("")
105117

‎lib/git/objects/base.py‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,74 @@ def _mode_str_to_int(cls, modestr):
172172
returnmode
173173

174174

175+
classDiffable(object):
176+
"""
177+
Common interface for all object that can be diffed against another object of compatible type.
178+
179+
NOTE:
180+
Subclasses require a repo member as it is the case for Object instances, for practical
181+
reasons we do not derive from Object.
182+
"""
183+
__slots__=tuple()
184+
185+
# subclasses provide additional arguments to the git-diff comamnd by supplynig
186+
# them in this tuple
187+
_diff_args=tuple()
188+
189+
defdiff(self,other=None,paths=None,create_patch=False,**kwargs):
190+
"""
191+
Creates diffs between two items being trees, trees and index or an
192+
index and the working tree.
193+
194+
``other``
195+
Is the item to compare us with.
196+
If None, we will be compared to the working tree.
197+
198+
``paths``
199+
is a list of paths or a single path to limit the diff to.
200+
It will only include at least one of the givne path or paths.
201+
202+
``create_patch``
203+
If True, the returned Diff contains a detailed patch that if applied
204+
makes the self to other. Patches are somwhat costly as blobs have to be read
205+
and diffed.
206+
207+
``kwargs``
208+
Additional arguments passed to git-diff, such as
209+
R=True to swap both sides of the diff.
210+
211+
Returns
212+
git.DiffIndex
213+
214+
Note
215+
Rename detection will only work if create_patch is True
216+
"""
217+
args=list(self._diff_args[:])
218+
args.append("--abbrev=40" )# we need full shas
219+
args.append("--full-index" )# get full index paths, not only filenames
220+
221+
ifcreate_patch:
222+
args.append("-p")
223+
args.append("-M")# check for renames
224+
else:
225+
args.append("--raw")
226+
227+
paths=pathsor []
228+
ifpaths:
229+
paths.insert(0,"--")
230+
231+
ifotherisnotNone:
232+
args.insert(0,other)
233+
234+
args.insert(0,self)
235+
args.extend(paths)
236+
237+
kwargs['as_process']=True
238+
proc=self.repo.git.diff(*args,**kwargs)
239+
240+
diff_method=diff.Diff._index_from_raw_format
241+
ifcreate_patch:
242+
diff_method=diff.Diff._index_from_patch_format(self.repo,proc.stdout)
243+
returndiff_method(self.repo,proc.stdout)
244+
245+

‎lib/git/objects/commit.py‎

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
importbase
1212
importutils
1313

14-
classCommit(base.Object,Iterable):
14+
classCommit(base.Object,Iterable,base.Diffable):
1515
"""
1616
Wraps a git Commit object.
1717
@@ -176,60 +176,6 @@ def iter_parents(self, paths='', **kwargs):
176176

177177
returnself.iter_items(self.repo,self,paths,**kwargs )
178178

179-
@classmethod
180-
defdiff(cls,repo,a,b=None,paths=None):
181-
"""
182-
Creates diffs between a tree and the index or between two trees:
183-
184-
``repo``
185-
is the Repo
186-
187-
``a``
188-
is a named commit
189-
190-
``b``
191-
is an optional named commit. Passing a list assumes you
192-
wish to omit the second named commit and limit the diff to the
193-
given paths.
194-
195-
``paths``
196-
is a list of paths to limit the diff to.
197-
198-
Returns
199-
git.Diff[]::
200-
201-
between tree and the index if only a is given
202-
between two trees if a and b are given and are commits
203-
"""
204-
paths=pathsor []
205-
206-
ifisinstance(b,list):
207-
paths=b
208-
b=None
209-
210-
ifpaths:
211-
paths.insert(0,"--")
212-
213-
ifb:
214-
paths.insert(0,b)
215-
paths.insert(0,a)
216-
text=repo.git.diff('-M',full_index=True,*paths)
217-
returndiff.Diff._list_from_string(repo,text)
218-
219-
@property
220-
defdiffs(self):
221-
"""
222-
Returns
223-
git.Diff[]
224-
Diffs between this commit and its first parent or all changes if this
225-
commit is the first commit and has no parent.
226-
"""
227-
ifnotself.parents:
228-
d=self.repo.git.show(self.id,'-M',full_index=True,pretty='raw')
229-
returndiff.Diff._list_from_string(self.repo,d)
230-
else:
231-
returnself.diff(self.repo,self.parents[0].id,self.id)
232-
233179
@property
234180
defstats(self):
235181
"""

‎lib/git/objects/tree.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def sha_to_hex(sha):
1515
assertlen(hexsha)==40,"Incorrect length of sha1 string: %d"%hexsha
1616
returnhexsha
1717

18-
classTree(base.IndexObject):
18+
classTree(base.IndexObject,base.Diffable):
1919
"""
2020
Tress represent a ordered list of Blobs and other Trees. Hence it can be
2121
accessed like a list.
@@ -169,6 +169,7 @@ def _iter_recursive(cls, repo, tree, cur_depth, max_depth, predicate ):
169169
deftraverse(self,max_depth=-1,predicate=lambdai:True):
170170
"""
171171
Returns
172+
172173
Iterator to traverse the tree recursively up to the given level.
173174
The iterator returns Blob and Tree objects
174175

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp