with open(os.path.join(self.git_dir, 'FETCH_HEAD'), 'w') as fhead:
fhead.write('0000000000000000000000000000000000000000\n')
+ def force_checkout(self, commitish):
+ """Checkout commitish"""
+ self._git_command("checkout", ['--force', commitish])
+
@classmethod
def clone(cls, path, url, bare=False):
"""Create a mirrored clone"""
# Resolve commit-ish to sha-1 and set HEAD (and working copy) to it
try:
sha = self.repo.rev_parse(commitish)
- self.repo.set_branch(sha)
+ self.repo.force_checkout(sha)
except GitRepositoryError as err:
raise CachedRepoError("Unknown ref '%s': %s" % (commitish, err))
self.repo.force_head(sha, hard=True)
with assert_raises(CachedRepoError):
sha = repo.update_working_copy('foo/bar')
+ def test_update_dirty_index(self):
+ """Test situation where index is out-of-sync with HEAD"""
+
+ self.update_repository_file(self.orig_repo, 'foo.txt', 'more data\n')
+ shas = [self.orig_repo.rev_parse('HEAD~2'),
+ self.orig_repo.rev_parse('HEAD~1'),
+ self.orig_repo.rev_parse('HEAD')]
+ repo = CachedRepo(self.orig_repo.path)
+ repo.update_working_copy(shas[-1])
+ del repo
+
+ # Change upstream, after this index cached repo will be out-of-sync
+ # from orig HEAD
+ self.orig_repo.set_branch('HEAD~1')
+ repo = CachedRepo(self.orig_repo.path)
+ assert repo.update_working_copy(shas[0]) == shas[0]
+
def test_update_bare(self):
"""Test update for bare repository"""
repo = CachedRepo(self.orig_repo.path, bare=True)