| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,15 +60,19 @@ def diff(self, other=None, paths=None, create_patch=False, **kwargs): | |||
| 60 | 60 | else: | |
| 61 | 61 | args.append("--raw") | |
| 62 | 62 | ||
| 63 | - paths = paths or [] | ||
| 64 | - if paths: | ||
| 65 | - paths.insert(0, "--") | ||
| 63 | + if paths is not None and not isinstance(paths, (tuple,list)): | ||
| 64 | + paths = [ paths ] | ||
| 66 | 65 | ||
| 67 | 66 | if other is not None: | |
| 68 | 67 | args.insert(0, other) | |
| 69 | 68 | ||
| 70 | 69 | args.insert(0,self) | |
| 71 | - args.extend(paths) | ||
| 70 | + | ||
| 71 | + # paths is list here or None | ||
| 72 | + if paths: | ||
| 73 | + args.append("--") | ||
| 74 | + args.extend(paths) | ||
| 75 | + # END paths handling | ||
| 72 | 76 | ||
| 73 | 77 | kwargs['as_process'] = True | |
| 74 | 78 | proc = self.repo.git.diff(*args, **kwargs) | |
@@ -79,6 +83,16 @@ def diff(self, other=None, paths=None, create_patch=False, **kwargs): | |||
| 79 | 83 | return diff_method(self.repo, proc.stdout) | |
| 80 | 84 | ||
| 81 | 85 | ||
| 86 | + class DiffIndex(list): | ||
| 87 | + """ | ||
| 88 | + Implements an Index for diffs, allowing a list of Diffs to be queried by | ||
| 89 | + the diff properties. | ||
| 90 | + | ||
| 91 | + The class improves the diff handling convenience | ||
| 92 | + """ | ||
| 93 | + | ||
| 94 | + | ||
| 95 | + | ||
| 82 | 96 | class Diff(object): | |
| 83 | 97 | """ | |
| 84 | 98 | A Diff contains diff information between two Trees. | |
@@ -99,7 +113,7 @@ class Diff(object): | |||
| 99 | 113 | ``Deleted File``:: | |
| 100 | 114 | ||
| 101 | 115 | b_mode is None | |
| 102 | - b_blob is NOne | ||
| 116 | + b_blob is None | ||
| 103 | 117 | """ | |
| 104 | 118 | ||
| 105 | 119 | # precompiled regex | |
@@ -160,7 +174,7 @@ def _index_from_patch_format(cls, repo, stream): | |||
| 160 | 174 | """ | |
| 161 | 175 | # for now, we have to bake the stream | |
| 162 | 176 | text = stream.read() | |
| 163 | - diffs = [] | ||
| 177 | + index = DiffIndex() | ||
| 164 | 178 | ||
| 165 | 179 | diff_header = cls.re_header.match | |
| 166 | 180 | for diff in ('\n' + text).split('\ndiff --git')[1:]: | |
@@ -171,19 +185,49 @@ def _index_from_patch_format(cls, repo, stream): | |||
| 171 | 185 | a_blob_id, b_blob_id, b_mode = header.groups() | |
| 172 | 186 | new_file, deleted_file = bool(new_file_mode), bool(deleted_file_mode) | |
| 173 | 187 | ||
| 174 | - diffs.append(Diff(repo, a_path, b_path, a_blob_id, b_blob_id, | ||
| 188 | + index.append(Diff(repo, a_path, b_path, a_blob_id, b_blob_id, | ||
| 175 | 189 | old_mode or deleted_file_mode, new_mode or new_file_mode or b_mode, | |
| 176 | 190 | new_file, deleted_file, rename_from, rename_to, diff[header.end():])) | |
| 177 | 191 | ||
| 178 | - return diffs | ||
| 192 | + return index | ||
| 179 | 193 | ||
| 180 | 194 | @classmethod | |
| 181 | 195 | def _index_from_raw_format(cls, repo, stream): | |
| 182 | 196 | """ | |
| 183 | 197 | Create a new DiffIndex from the given stream which must be in raw format. | |
| 184 | 198 | ||
| 199 | + NOTE: | ||
| 200 | + This format is inherently incapable of detecting renames, hence we only | ||
| 201 | + modify, delete and add files | ||
| 202 | + | ||
| 185 | 203 | Returns | |
| 186 | 204 | git.DiffIndex | |
| 187 | 205 | """ | |
| 188 | - raise NotImplementedError("") | ||
| 206 | + # handles | ||
| 207 | + # :100644 100644 6870991011cc8d9853a7a8a6f02061512c6a8190 37c5e30c879213e9ae83b21e9d11e55fc20c54b7 M .gitignore | ||
| 208 | + index = DiffIndex() | ||
| 209 | + for line in stream: | ||
| 210 | + if not line.startswith(":"): | ||
| 211 | + continue | ||
| 212 | + # END its not a valid diff line | ||
| 213 | + old_mode, new_mode, a_blob_id, b_blob_id, modification_id, path = line[1:].split() | ||
| 214 | + a_path = path | ||
| 215 | + b_path = path | ||
| 216 | + deleted_file = False | ||
| 217 | + new_file = False | ||
| 218 | + if modification_id == 'D': | ||
| 219 | + b_path = None | ||
| 220 | + deleted_file = True | ||
| 221 | + elif modification_id == 'A': | ||
| 222 | + a_path = None | ||
| 223 | + new_file = True | ||
| 224 | + # END add/remove handling | ||
| 225 | + | ||
| 226 | + | ||
| 227 | + diff = Diff(repo, a_path, b_path, a_blob_id, b_blob_id, old_mode, new_mode, | ||
| 228 | + new_file, deleted_file, None, None, '') | ||
| 229 | + index.append(diff) | ||
| 230 | + # END for each line | ||
| 231 | + | ||
| 232 | + return index | ||
| 189 | 233 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,11 +52,12 @@ def test_diff_interface(self): | |||
| 52 | 52 | for paths in (None, "CHANGES", ("CHANGES", "lib")): | |
| 53 | 53 | for create_patch in range(2): | |
| 54 | 54 | diff_index = diff_item.diff(other, paths, create_patch) | |
| 55 | - assert diff_index | ||
| 55 | + assert isinstance(diff_index, DiffIndex) | ||
| 56 | + | ||
| 57 | + # TODO: test diff index | ||
| 56 | 58 | # END for each patch option | |
| 57 | 59 | # END for each path option | |
| 58 | 60 | # END for each other side | |
| 59 | 61 | # END for each commit | |
| 60 | 62 | ||
| 61 | - | ||
| 62 | 63 | self.fail( "TODO: Test full diff interface on commits, trees, index, patch and non-patch" ) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments