| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1496979 commit ea33fe8
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,6 +58,15 @@ Index | |||
| 58 | 58 | creating several tree objects, so in the end it might be slower. | |
| 59 | 59 | Hmm, probably its okay to use the command unless we go c(++) | |
| 60 | 60 | ||
| 61 | + | ||
| 62 | + Head.reset | ||
| 63 | + ---------- | ||
| 64 | + * Should better be an instance method. Problem was that there is no class specifying | ||
| 65 | + the HEAD - in a way reset would always effect the active branch. | ||
| 66 | + Probably it would be okay to have a special type called SymbolicReference | ||
| 67 | + which represents items like HEAD. These could naturally carry the reset | ||
| 68 | + instance method. | ||
| 69 | + | ||
| 61 | 70 | Remote | |
| 62 | 71 | ------ | |
| 63 | 72 | * 'push' method needs a test, a true test repository is required though, a fork | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,13 +18,18 @@ class Diffable(object): | |||
| 18 | 18 | """ | |
| 19 | 19 | __slots__ = tuple() | |
| 20 | 20 | ||
| 21 | - # subclasses provide additional arguments to the git-diff comamnd by supplynig | ||
| 22 | - # them in this tuple | ||
| 23 | - _diff_args = tuple() | ||
| 24 | - | ||
| 25 | - # Temporary standin for Index type until we have a real index type | ||
| 21 | + # standin indicating you want to diff against the index | ||
| 26 | 22 | class Index(object): | |
| 27 | 23 | pass | |
| 24 | + | ||
| 25 | + def _process_diff_args(self, args): | ||
| 26 | + """ | ||
| 27 | + Returns | ||
| 28 | + possibly altered version of the given args list. | ||
| 29 | + Method is called right before git command execution. | ||
| 30 | + Subclasses can use it to alter the behaviour of the superclass | ||
| 31 | + """ | ||
| 32 | + return args | ||
| 28 | 33 | ||
| 29 | 34 | def diff(self, other=Index, paths=None, create_patch=False, **kwargs): | |
| 30 | 35 | """ | |
@@ -60,13 +65,13 @@ def diff(self, other=Index, paths=None, create_patch=False, **kwargs): | |||
| 60 | 65 | On a bare repository, 'other' needs to be provided as Index or as | |
| 61 | 66 | as Tree/Commit, or a git command error will occour | |
| 62 | 67 | """ | |
| 63 | - args = list(self._diff_args[:]) | ||
| 68 | + args = list() | ||
| 64 | 69 | args.append( "--abbrev=40" ) # we need full shas | |
| 65 | 70 | args.append( "--full-index" ) # get full index paths, not only filenames | |
| 66 | 71 | ||
| 67 | 72 | if create_patch: | |
| 68 | 73 | args.append("-p") | |
| 69 | - args.append("-M") # check for renames | ||
| 74 | + args.append("-M") # check for renames | ||
| 70 | 75 | else: | |
| 71 | 76 | args.append("--raw") | |
| 72 | 77 | ||
@@ -87,7 +92,7 @@ def diff(self, other=Index, paths=None, create_patch=False, **kwargs): | |||
| 87 | 92 | # END paths handling | |
| 88 | 93 | ||
| 89 | 94 | kwargs['as_process'] = True | |
| 90 | - proc = self.repo.git.diff(*args, **kwargs) | ||
| 95 | + proc = self.repo.git.diff(*self._process_diff_args(args), **kwargs) | ||
| 91 | 96 | ||
| 92 | 97 | diff_method = Diff._index_from_raw_format | |
| 93 | 98 | if create_patch: | |
@@ -96,7 +101,7 @@ def diff(self, other=Index, paths=None, create_patch=False, **kwargs): | |||
| 96 | 101 | ||
| 97 | 102 | status = proc.wait() | |
| 98 | 103 | if status != 0: | |
| 99 | - raise GitCommandError("git-diff", status, proc.stderr ) | ||
| 104 | + raise GitCommandError(("git diff",)+tuple(args), status, proc.stderr.read()) | ||
| 100 | 105 | ||
| 101 | 106 | return index | |
| 102 | 107 | ||
@@ -207,6 +212,20 @@ def __init__(self, repo, a_path, b_path, a_blob_id, b_blob_id, a_mode, | |||
| 207 | 212 | ||
| 208 | 213 | self.diff = diff | |
| 209 | 214 | ||
| 215 | + | ||
| 216 | + def __eq__(self, other): | ||
| 217 | + for name in self.__slots__: | ||
| 218 | + if getattr(self, name) != getattr(other, name): | ||
| 219 | + return False | ||
| 220 | + # END for each name | ||
| 221 | + return True | ||
| 222 | + | ||
| 223 | + def __ne__(self, other): | ||
| 224 | + return not ( self == other ) | ||
| 225 | + | ||
| 226 | + def __hash__(self): | ||
| 227 | + return hash(tuple(getattr(self,n) for n in self.__slots__)) | ||
| 228 | + | ||
| 210 | 229 | @property | |
| 211 | 230 | def renamed(self): | |
| 212 | 231 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,8 +25,8 @@ def __init__(self, command, status, stderr=None): | |||
| 25 | 25 | self.stderr = stderr | |
| 26 | 26 | self.status = status | |
| 27 | 27 | self.command = command | |
| 28 | - | ||
| 28 | + | ||
| 29 | 29 | def __str__(self): | |
| 30 | - return repr("'%s' returned exit status %d: %r" % | ||
| 31 | - (' '.join(self.command), self.status, str(self.stderr))) | ||
| 30 | + return ("'%s' returned exit status %i: %s" % | ||
| 31 | + (' '.join(str(i) for i in self.command), self.status, self.stderr)) | ||
| 32 | 32 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,9 +14,11 @@ | |||
| 14 | 14 | import tempfile | |
| 15 | 15 | import os | |
| 16 | 16 | import stat | |
| 17 | - from git.objects import Blob, Tree | ||
| 17 | + import git.diff as diff | ||
| 18 | + | ||
| 19 | + from git.objects import Blob, Tree, Object | ||
| 18 | 20 | from git.utils import SHA1Writer, LazyMixin, ConcurrentWriteOperation | |
| 19 | - from git.diff import Diffable | ||
| 21 | + | ||
| 20 | 22 | ||
| 21 | 23 | class _TemporaryFileSwap(object): | |
| 22 | 24 | """ | |
@@ -140,7 +142,7 @@ def from_blob(cls, blob): | |||
| 140 | 142 | return IndexEntry((time, time, 0, 0, blob.mode, 0, 0, blob.size, blob.id, 0, blob.path)) | |
| 141 | 143 | ||
| 142 | 144 | ||
| 143 | - class Index(LazyMixin): | ||
| 145 | + class Index(LazyMixin, diff.Diffable): | ||
| 144 | 146 | """ | |
| 145 | 147 | Implements an Index that can be manipulated using a native implementation in | |
| 146 | 148 | order to save git command function calls wherever possible. | |
@@ -154,7 +156,7 @@ class Index(LazyMixin): | |||
| 154 | 156 | The index contains an entries dict whose keys are tuples of type IndexEntry | |
| 155 | 157 | to facilitate access. | |
| 156 | 158 | """ | |
| 157 | - __slots__ = ( "repo", "version", "entries", "_extension_data" ) | ||
| 159 | + __slots__ = ( "repo", "version", "entries", "_extension_data", "_is_default_index" ) | ||
| 158 | 160 | _VERSION = 2 # latest version we support | |
| 159 | 161 | S_IFGITLINK = 0160000 | |
| 160 | 162 | ||
@@ -168,10 +170,13 @@ def __init__(self, repo, stream = None): | |||
| 168 | 170 | self.repo = repo | |
| 169 | 171 | self.version = self._VERSION | |
| 170 | 172 | self._extension_data = '' | |
| 173 | + self._is_default_index = True | ||
| 171 | 174 | if stream is not None: | |
| 175 | + self._is_default_index = False | ||
| 172 | 176 | self._read_from_stream(stream) | |
| 173 | 177 | # END read from stream immediatly | |
| 174 | 178 | ||
| 179 | + | ||
| 175 | 180 | def _set_cache_(self, attr): | |
| 176 | 181 | if attr == "entries": | |
| 177 | 182 | # read the current index | |
@@ -187,6 +192,18 @@ def _set_cache_(self, attr): | |||
| 187 | 192 | def _index_path(self): | |
| 188 | 193 | return os.path.join(self.repo.path, "index") | |
| 189 | 194 | ||
| 195 | + | ||
| 196 | + @property | ||
| 197 | + def path(self): | ||
| 198 | + """ | ||
| 199 | + Returns | ||
| 200 | + Path to the index file we are representing or None if we are | ||
| 201 | + a loose index that was read from a stream. | ||
| 202 | + """ | ||
| 203 | + if self._is_default_index: | ||
| 204 | + return self._index_path() | ||
| 205 | + return None | ||
| 206 | + | ||
| 190 | 207 | @classmethod | |
| 191 | 208 | def _read_entry(cls, stream): | |
| 192 | 209 | """Return: One entry of the given stream""" | |
@@ -535,4 +552,51 @@ def write_tree(self): | |||
| 535 | 552 | # END write tree handling | |
| 536 | 553 | return Tree(self.repo, tree_sha, 0, '') | |
| 537 | 554 | ||
| 555 | + | ||
| 556 | + def _process_diff_args(self, args): | ||
| 557 | + try: | ||
| 558 | + args.pop(args.index(self)) | ||
| 559 | + except IndexError: | ||
| 560 | + pass | ||
| 561 | + # END remove self | ||
| 562 | + return args | ||
| 563 | + | ||
| 564 | + def diff(self, other=diff.Diffable.Index, paths=None, create_patch=False, **kwargs): | ||
| 565 | + """ | ||
| 566 | + Diff this index against the working copy or a Tree or Commit object | ||
| 567 | + | ||
| 568 | + For a documentation of the parameters and return values, see | ||
| 569 | + Diffable.diff | ||
| 570 | + | ||
| 571 | + Note | ||
| 572 | + Will only work with indices that represent the default git index as | ||
| 573 | + they have not been initialized with a stream. | ||
| 574 | + """ | ||
| 575 | + if not self._is_default_index: | ||
| 576 | + raise AssertionError( "Cannot diff custom indices as they do not represent the default git index" ) | ||
| 577 | + | ||
| 578 | + # index against index is always empty | ||
| 579 | + if other is self.Index: | ||
| 580 | + return diff.DiffIndex() | ||
| 581 | + | ||
| 582 | + # index against anything but None is a reverse diff with the respective | ||
| 583 | + # item. Handle existing -R flags properly. Transform strings to the object | ||
| 584 | + # so that we can call diff on it | ||
| 585 | + if isinstance(other, basestring): | ||
| 586 | + other = Object.new(self.repo, other) | ||
| 587 | + # END object conversion | ||
| 588 | + | ||
| 589 | + if isinstance(other, Object): | ||
| 590 | + # invert the existing R flag | ||
| 591 | + cur_val = kwargs.get('R', False) | ||
| 592 | + kwargs['R'] = not cur_val | ||
| 593 | + return other.diff(self.Index, paths, create_patch, **kwargs) | ||
| 594 | + # END diff against other item handlin | ||
| 595 | + | ||
| 596 | + # if other is not None here, something is wrong | ||
| 597 | + if other is not None: | ||
| 598 | + raise ValueError( "other must be None, Diffable.Index, a Tree or Commit, was %r" % other ) | ||
| 599 | + | ||
| 600 | + # diff against working copy - can be handled by superclass natively | ||
| 601 | + return super(Index, self).diff(other, paths, create_patch, **kwargs) | ||
| 538 | 602 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | Import all submodules main classes into the package space | |
| 3 | 3 | """ | |
| 4 | 4 | import inspect | |
| 5 | + from base import * | ||
| 5 | 6 | from tag import * | |
| 6 | 7 | from blob import * | |
| 7 | 8 | from tree import * | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,6 +59,14 @@ def test_diff_interface(self): | |||
| 59 | 59 | assertion_map.setdefault(key, 0) | |
| 60 | 60 | assertion_map[key] = assertion_map[key]+len(list(diff_index.iter_change_type(ct))) | |
| 61 | 61 | # END for each changetype | |
| 62 | + | ||
| 63 | + # check entries | ||
| 64 | + diff_set = set() | ||
| 65 | + diff_set.add(diff_index[0]) | ||
| 66 | + diff_set.add(diff_index[0]) | ||
| 67 | + assert len(diff_set) == 1 | ||
| 68 | + assert diff_index[0] == diff_index[0] | ||
| 69 | + assert not (diff_index[0] != diff_index[0]) | ||
| 62 | 70 | # END diff index checking | |
| 63 | 71 | # END for each patch option | |
| 64 | 72 | # END for each path option | |
@@ -71,7 +79,4 @@ def test_diff_interface(self): | |||
| 71 | 79 | for key,value in assertion_map.items(): | |
| 72 | 80 | assert value, "Did not find diff for %s" % key | |
| 73 | 81 | # END for each iteration type | |
| 74 | - | ||
| 75 | - def test_diff_index_working_tree(self): | ||
| 76 | - self.fail("""Find a good way to diff an index against the working tree | ||
| 77 | - which is not possible with the current interface""") | ||
| 82 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -102,12 +102,45 @@ def test_from_tree(self): | |||
| 102 | 102 | assert num_blobs == len(three_way_index.entries) | |
| 103 | 103 | ||
| 104 | 104 | @with_rw_repo('0.1.6') | |
| 105 | - def test_from_index(self, rw_repo): | ||
| 105 | + def test_from_index_and_diff(self, rw_repo): | ||
| 106 | 106 | # default Index instance points to our index | |
| 107 | 107 | index = Index(rw_repo) | |
| 108 | + assert index.path is not None | ||
| 108 | 109 | assert len(index.entries) | |
| 109 | 110 | ||
| 110 | 111 | # write the file back | |
| 111 | 112 | index.write() | |
| 112 | 113 | ||
| 113 | 114 | # could sha it, or check stats | |
| 115 | + | ||
| 116 | + # test diff | ||
| 117 | + # resetting the head will leave the index in a different state, and the | ||
| 118 | + # diff will yield a few changes | ||
| 119 | + cur_head_commit = rw_repo.head.commit | ||
| 120 | + ref = rw_repo.head.reset(rw_repo, 'HEAD~6', index=True, working_tree=False) | ||
| 121 | + | ||
| 122 | + # diff against same index is 0 | ||
| 123 | + diff = index.diff() | ||
| 124 | + assert len(diff) == 0 | ||
| 125 | + | ||
| 126 | + # against HEAD as string, must be the same as it matches index | ||
| 127 | + diff = index.diff('HEAD') | ||
| 128 | + assert len(diff) == 0 | ||
| 129 | + | ||
| 130 | + # against previous head, there must be a difference | ||
| 131 | + diff = index.diff(cur_head_commit) | ||
| 132 | + assert len(diff) | ||
| 133 | + | ||
| 134 | + # we reverse the result | ||
| 135 | + adiff = index.diff(str(cur_head_commit), R=True) | ||
| 136 | + odiff = index.diff(cur_head_commit, R=False) # now its not reversed anymore | ||
| 137 | + assert adiff != odiff | ||
| 138 | + assert odiff == diff # both unreversed diffs against HEAD | ||
| 139 | + | ||
| 140 | + # against working copy - its still at cur_commit | ||
| 141 | + wdiff = index.diff(None) | ||
| 142 | + assert wdiff != adiff | ||
| 143 | + assert wdiff != odiff | ||
| 144 | + | ||
| 145 | + # against something unusual | ||
| 146 | + self.failUnlessRaises(ValueError, index.diff, int) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments