| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ | |||
| 15 | 15 | import os | |
| 16 | 16 | import stat | |
| 17 | 17 | from git.objects import Blob, Tree | |
| 18 | - from git.utils import SHA1Writer, LazyMixin | ||
| 18 | + from git.utils import SHA1Writer, LazyMixin, ConcurrentWriteOperation | ||
| 19 | 19 | from git.diff import Diffable | |
| 20 | 20 | ||
| 21 | 21 | class _TemporaryFileSwap(object): | |
@@ -175,7 +175,7 @@ def __init__(self, repo, stream = None): | |||
| 175 | 175 | def _set_cache_(self, attr): | |
| 176 | 176 | if attr == "entries": | |
| 177 | 177 | # read the current index | |
| 178 | - fp = open(os.path.join(self.repo.path, "index"), "r") | ||
| 178 | + fp = open(self._index_path(), "r") | ||
| 179 | 179 | try: | |
| 180 | 180 | self._read_from_stream(fp) | |
| 181 | 181 | finally: | |
@@ -184,6 +184,9 @@ def _set_cache_(self, attr): | |||
| 184 | 184 | else: | |
| 185 | 185 | super(Index, self)._set_cache_(attr) | |
| 186 | 186 | ||
| 187 | + def _index_path(self): | ||
| 188 | + return os.path.join(self.repo.path, "index") | ||
| 189 | + | ||
| 187 | 190 | @classmethod | |
| 188 | 191 | def _read_entry(cls, stream): | |
| 189 | 192 | """Return: One entry of the given stream""" | |
@@ -306,19 +309,28 @@ def _write_cache_entry(cls, stream, entry): | |||
| 306 | 309 | real_size = ((stream.tell() - beginoffset + 8) & ~7) | |
| 307 | 310 | stream.write("\0" * ((beginoffset + real_size) - stream.tell())) | |
| 308 | 311 | ||
| 309 | - def write(self, stream): | ||
| 312 | + def write(self, stream=None): | ||
| 310 | 313 | """ | |
| 311 | - Write the current state to the given stream | ||
| 314 | + Write the current state to the given stream or to the default repository | ||
| 315 | + index. | ||
| 312 | 316 | ||
| 313 | 317 | ``stream`` | |
| 314 | - File-like object. | ||
| 318 | + File-like object or None. | ||
| 319 | + If None, the default repository index will be overwritten. | ||
| 315 | 320 | ||
| 316 | 321 | Returns | |
| 317 | 322 | self | |
| 318 | 323 | ||
| 319 | 324 | Note | |
| 320 | 325 | Index writing based on the dulwich implementation | |
| 321 | 326 | """ | |
| 327 | + write_op = None | ||
| 328 | + if stream is None: | ||
| 329 | + write_op = ConcurrentWriteOperation(self._index_path()) | ||
| 330 | + stream = write_op._begin_writing() | ||
| 331 | + # stream = open(self._index_path() | ||
| 332 | + # END stream handling | ||
| 333 | + | ||
| 322 | 334 | stream = SHA1Writer(stream) | |
| 323 | 335 | ||
| 324 | 336 | # header | |
@@ -338,6 +350,9 @@ def write(self, stream): | |||
| 338 | 350 | # write the sha over the content | |
| 339 | 351 | stream.write_sha() | |
| 340 | 352 | ||
| 353 | + if write_op is not None: | ||
| 354 | + write_op._end_writing() | ||
| 355 | + | ||
| 341 | 356 | ||
| 342 | 357 | @classmethod | |
| 343 | 358 | def from_tree(cls, repo, *treeish, **kwargs): | |
@@ -504,7 +519,7 @@ def write_tree(self): | |||
| 504 | 519 | Returns | |
| 505 | 520 | Tree object representing this index | |
| 506 | 521 | """ | |
| 507 | - index_path = os.path.join(self.repo.path, "index") | ||
| 522 | + index_path = self._index_path() | ||
| 508 | 523 | tmp_index_mover = _TemporaryFileSwap(index_path) | |
| 509 | 524 | ||
| 510 | 525 | self.to_file(self, index_path) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -201,7 +201,9 @@ def _end_writing(self, successful=True): | |||
| 201 | 201 | if self._temp_write_fp is None: | |
| 202 | 202 | return | |
| 203 | 203 | ||
| 204 | - self._temp_write_fp.close() | ||
| 204 | + if not self._temp_write_fp.closed: | ||
| 205 | + self._temp_write_fp.close() | ||
| 206 | + | ||
| 205 | 207 | if successful: | |
| 206 | 208 | # on windows, rename does not silently overwrite the existing one | |
| 207 | 209 | if sys.platform == "win32": | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -101,7 +101,13 @@ def test_from_tree(self): | |||
| 101 | 101 | # END for each blob | |
| 102 | 102 | assert num_blobs == len(three_way_index.entries) | |
| 103 | 103 | ||
| 104 | - def test_from_index(self): | ||
| 104 | + @with_rw_repo('0.1.6') | ||
| 105 | + def test_from_index(self, rw_repo): | ||
| 105 | 106 | # default Index instance points to our index | |
| 106 | - index = Index(self.rorepo) | ||
| 107 | + index = Index(rw_repo) | ||
| 107 | 108 | assert len(index.entries) | |
| 109 | + | ||
| 110 | + # write the file back | ||
| 111 | + index.write() | ||
| 112 | + | ||
| 113 | + # could sha it, or check stats | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments