def _herd(self, remote, ref, **kwargs): """Herd ref .. py:function:: _herd(remote, ref, depth=0, fetch=True, rebase=False) :param str remote: Remote name :param str ref: Git ref Keyword Args: depth (int): Git clone depth. 0 indicates full clone, otherwise must be a positive integer fetch (bool): Whether to fetch rebase (bool): Whether to use rebase instead of pulling latest changes """ depth = kwargs.get('depth', 0) fetch = kwargs.get('fetch', True) rebase = kwargs.get('rebase', False) if ref_type(ref) == 'tag': self.fetch(remote, depth=depth, ref=ref) self._checkout_tag(truncate_ref(ref)) return if ref_type(ref) == 'sha': self.fetch(remote, depth=depth, ref=ref) self._checkout_sha(ref) return branch = truncate_ref(ref) if not self.existing_local_branch(branch): self._create_branch_local_tracking(branch, remote, depth=depth, fetch=fetch) return self._herd_existing_local(remote, branch, depth=depth, rebase=rebase)
def sync(self, fork_remote, rebase=False): """Sync fork with upstream remote .. py:function:: sync(fork_remote, rebase=False) :param str fork_remote: Fork remote name :param Optional[bool] rebase: Whether to use rebase instead of pulling latest changes. """ self._print(' - Sync fork with upstream remote') if ref_type(self.default_ref) != 'branch': message = colored(' - Can only sync branches', 'red') self._print(message) self._exit(message) fork_remote_output = fmt.remote_string(fork_remote) branch_output = fmt.ref_string(truncate_ref(self.default_ref)) if rebase: self._rebase_remote_branch(self.remote, truncate_ref(self.default_ref)) else: self._pull(self.remote, truncate_ref(self.default_ref)) self._print(' - Push to ' + fork_remote_output + ' ' + branch_output) command = ['git', 'push', fork_remote, truncate_ref(self.default_ref)] try: execute_command(command, self.repo_path, print_output=self._print_output) except ClowderError: message = colored(' - Failed to push to ', 'red') + fork_remote_output + ' ' + branch_output self._print(message) self._print(fmt.command_failed_error(command)) self._exit(message)
def _herd_initial(self, url, depth=0): """Herd ref initial .. py:function:: _herd_initial(url, depth=0) :param str url: URL of repo :param Optional[int] depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer """ self._init_repo() self._create_remote(self.remote, url, remove_dir=True) if ref_type(self.default_ref) == 'branch': self._checkout_new_repo_branch(truncate_ref(self.default_ref), depth) elif ref_type(self.default_ref) == 'tag': self._checkout_new_repo_tag(truncate_ref(self.default_ref), self.remote, depth, remove_dir=True) elif ref_type(self.default_ref) == 'sha': self._checkout_new_repo_commit(self.default_ref, self.remote, depth)
def reset(self, depth=0): """Reset branch to upstream or checkout tag/sha as detached HEAD .. py:function:: reset(depth=0) :param Optional[int] depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer """ if ref_type(self.default_ref) == 'tag': self.fetch(self.remote, ref=self.default_ref, depth=depth) self._checkout_tag(truncate_ref(self.default_ref)) return if ref_type(self.default_ref) == 'sha': self.fetch(self.remote, ref=self.default_ref, depth=depth) self._checkout_sha(self.default_ref) return branch = truncate_ref(self.default_ref) if not self.existing_local_branch(branch): self._create_branch_local_tracking(branch, self.remote, depth=depth, fetch=True) return self._checkout_branch(branch) branch_output = fmt.ref_string(branch) remote_output = fmt.remote_string(self.remote) if not self.existing_remote_branch(branch, self.remote): message = colored(' - No existing remote branch ', 'red') + remote_output + ' ' + branch_output self._print(message) self._exit(message) self.fetch(self.remote, ref=self.default_ref, depth=depth) self._print(' - Reset branch ' + branch_output + ' to ' + remote_output + ' ' + branch_output) remote_branch = self.remote + '/' + branch self._reset_head(branch=remote_branch)
def test_ref_type_unknown(self): """Test ref_type() function for unknown ref type""" self.assertEqual(ref_type('42'), 'unknown')
def test_ref_type_tag(self): """Test ref_type() function for tag ref""" self.assertEqual(ref_type(self.tag_ref), 'tag')
def test_ref_type_sha(self): """Test ref_type() function for sha ref""" self.assertEqual(ref_type(self.sha_ref), 'sha')
def test_ref_type_branch(self): """Test ref_type() function for branch ref""" self.assertEqual(ref_type(self.branch_ref), 'branch')