| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,8 +110,6 @@ Repo | |||
| 110 | 110 | * Figure out how to implement a proper merge API. It should be index based, but provide | |
| 111 | 111 | all necessary information to the ones willing to ask for it. The index implementation | |
| 112 | 112 | actually provides this already, but some real use-cases would be great to have a least. | |
| 113 | - * repo.checkout should be added that does everything HEAD.reset does, but in addition | ||
| 114 | - it allows to checkout heads beforehand, hence its more like a repo.head.reference = other_head. | ||
| 115 | 113 | ||
| 116 | 114 | ||
| 117 | 115 | Submodules | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -351,7 +351,10 @@ is the following one. | |||
| 351 | 351 | ||
| 352 | 352 | The previous approach would brutally overwrite the user's changes in the working copy | |
| 353 | 353 | and index though and is less sophisticated than a git-checkout for instance which | |
| 354 | - generally prevents you from destroying your work. | ||
| 354 | + generally prevents you from destroying your work. Use the safer approach as follows: | ||
| 355 | + | ||
| 356 | + >>> repo.heads.master.checkout() # checkout the branch using git-checkout | ||
| 357 | + >>> repo.heads.other_branch.checkout() | ||
| 355 | 358 | ||
| 356 | 359 | Using git directly | |
| 357 | 360 | ****************** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -517,7 +517,39 @@ def rename(self, new_path, force=False): | |||
| 517 | 517 | self.path = "%s/%s" % (self._common_path_default, new_path) | |
| 518 | 518 | return self | |
| 519 | 519 | ||
| 520 | - | ||
| 520 | + def checkout(self, force=False, **kwargs): | ||
| 521 | + """ | ||
| 522 | + Checkout this head by setting the HEAD to this reference, by updating the index | ||
| 523 | + to reflect the tree we point to and by updating the working tree to reflect | ||
| 524 | + the latest index. | ||
| 525 | + | ||
| 526 | + The command will fail if changed working tree files would be overwritten. | ||
| 527 | + | ||
| 528 | + ``force`` | ||
| 529 | + If True, changes to the index and the working tree will be discarded. | ||
| 530 | + If False, GitCommandError will be raised in that situation. | ||
| 531 | + | ||
| 532 | + ``**kwargs`` | ||
| 533 | + Additional keyword arguments to be passed to git checkout, i.e. | ||
| 534 | + b='new_branch' to create a new branch at the given spot. | ||
| 535 | + | ||
| 536 | + Returns | ||
| 537 | + The active branch after the checkout operation, usually self unless | ||
| 538 | + a new branch has been created. | ||
| 539 | + | ||
| 540 | + Note | ||
| 541 | + By default it is only allowed to checkout heads - everything else | ||
| 542 | + will leave the HEAD detached which is allowed and possible, but remains | ||
| 543 | + a special state that some tools might not be able to handle. | ||
| 544 | + """ | ||
| 545 | + args = list() | ||
| 546 | + kwargs['f'] = force | ||
| 547 | + if kwargs['f'] == False: | ||
| 548 | + kwargs.pop('f') | ||
| 549 | + | ||
| 550 | + self.repo.git.checkout(self, **kwargs) | ||
| 551 | + return self.repo.active_branch | ||
| 552 | + | ||
| 521 | 553 | ||
| 522 | 554 | class TagReference(Reference): | |
| 523 | 555 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -211,8 +211,29 @@ def test_head_reset(self, rw_repo): | |||
| 211 | 211 | assert not cur_head.is_detached | |
| 212 | 212 | assert head.commit == parent_commit | |
| 213 | 213 | ||
| 214 | + # test checkout | ||
| 215 | + active_branch = rw_repo.active_branch | ||
| 216 | + for head in rw_repo.heads: | ||
| 217 | + checked_out_head = head.checkout() | ||
| 218 | + assert checked_out_head == head | ||
| 219 | + # END for each head to checkout | ||
| 220 | + | ||
| 221 | + # checkout with branch creation | ||
| 222 | + new_head = active_branch.checkout(b="new_head") | ||
| 223 | + assert active_branch != rw_repo.active_branch | ||
| 224 | + assert new_head == rw_repo.active_branch | ||
| 225 | + | ||
| 226 | + # checkout with force has we have a change | ||
| 227 | + # clear file | ||
| 228 | + open(new_head.commit.tree.blobs[-1].abspath,'w').close() | ||
| 229 | + assert len(new_head.commit.diff(None)) == 1 | ||
| 230 | + | ||
| 231 | + # create a new branch that is likely to touch the file we changed | ||
| 232 | + far_away_head = rw_repo.create_head("far_head",'HEAD~100') | ||
| 233 | + self.failUnlessRaises(GitCommandError, far_away_head.checkout) | ||
| 234 | + assert active_branch == active_branch.checkout(force=True) | ||
| 235 | + | ||
| 214 | 236 | # test ref listing - assure we have packed refs | |
| 215 | 237 | rw_repo.git.pack_refs(all=True) | |
| 216 | 238 | assert rw_repo.heads | |
| 217 | 239 | assert rw_repo.tags | |
| 218 | - | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments