def sync(self, fork_remote, rebase=False): """Sync fork with upstream remote""" self._print(' - Sync fork with upstream remote') if self.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(self.truncate_ref(self.default_ref)) if rebase: self._rebase_remote_branch(self.remote, self.truncate_ref(self.default_ref)) else: self._pull(self.remote, self.truncate_ref(self.default_ref)) self._print(' - Push to ' + fork_remote_output + ' ' + branch_output) command = [ 'git', 'push', fork_remote, self.truncate_ref(self.default_ref) ] return_code = execute_command(command, self.repo_path, print_output=self.print_output) if return_code != 0: 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 _set_tracking_branch(self, remote, branch, remove_dir=False): """Set tracking branch""" branch_output = fmt.ref_string(branch) remote_output = fmt.remote_string(remote) origin = self._remote(remote) try: local_branch = self.repo.heads[branch] remote_branch = origin.refs[branch] self._print(' - Set tracking branch ' + branch_output + ' -> ' + remote_output + ' ' + branch_output) local_branch.set_tracking_branch(remote_branch) return 0 except GitError as err: message = colored(' - Failed to set tracking branch ', 'red') + branch_output if remove_dir: remove_directory(self.repo_path) self._print(message) self._print(fmt.error(err)) self._exit(message) except (KeyboardInterrupt, SystemExit): if remove_dir: remove_directory(self.repo_path) self._exit()
def _set_tracking_branch(self, remote, branch, remove_dir=False): """Set tracking branch .. py:function:: _set_tracking_branch(remote, branch, remove_dir=False) :param str remote: Remote name :param str branch: Branch name :param Optional[bool] remove_dir: Whether to remove the directory if commands fail """ branch_output = fmt.ref_string(branch) remote_output = fmt.remote_string(remote) origin = self._remote(remote) try: local_branch = self.repo.heads[branch] remote_branch = origin.refs[branch] self._print(' - Set tracking branch ' + branch_output + ' -> ' + remote_output + ' ' + branch_output) local_branch.set_tracking_branch(remote_branch) except GitError as err: message = colored(' - Failed to set tracking branch ', 'red') + branch_output if remove_dir: remove_directory(self.repo_path) self._print(message) self._print(fmt.error(err)) self._exit(message) except (KeyboardInterrupt, SystemExit): if remove_dir: remove_directory(self.repo_path) self._exit()
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 _create_remote(self, remote, url, remove_dir=False): """Create new remote""" remote_names = [r.name for r in self.repo.remotes] if remote in remote_names: return 0 remote_output = fmt.remote_string(remote) try: self._print(' - Create remote ' + remote_output) self.repo.create_remote(remote, url) return 0 except GitError as err: message = colored(' - Failed to create remote ', 'red') if remove_dir: remove_directory(self.repo_path) self._print(message + remote_output) self._print(fmt.error(err)) self._exit( fmt.parallel_exception_error(self.repo_path, message, remote_output)) except (KeyboardInterrupt, SystemExit): if remove_dir: remove_directory(self.repo_path) self._exit()
def _create_remote(self, remote, url, remove_dir=False): """Create new remote .. py:function:: _create_remote(remote, url, remove_dir=False) :param str remote: Remote name :param str url: URL of repo :param Optional[bool] remove_dir: Whether to remove the directory if commands fail """ remote_names = [r.name for r in self.repo.remotes] if remote in remote_names: return 0 remote_output = fmt.remote_string(remote) try: self._print(' - Create remote ' + remote_output) self.repo.create_remote(remote, url) return 0 except GitError as err: message = colored(' - Failed to create remote ', 'red') if remove_dir: remove_directory(self.repo_path) self._print(message + remote_output) self._print(fmt.error(err)) self._exit( fmt.parallel_exception_error(self.repo_path, message, remote_output)) except (KeyboardInterrupt, SystemExit): if remove_dir: remove_directory(self.repo_path) self._exit()
def herd_branch(self, url, branch, **kwargs): """Herd branch .. py:function:: herd_branch(url, branch, depth=0, fork_remote=None, rebase=False) :param str url: URL of repo :param str branch: Branch name Keyword Args: depth (int): Git clone depth. 0 indicates full clone, otherwise must be a positive integer fork_remote (str): Fork remote name rebase (bool): Whether to use rebase instead of pulling latest changes """ depth = kwargs.get('depth', 0) rebase = kwargs.get('rebase', False) fork_remote = kwargs.get('fork_remote', None) if not existing_git_repository(self.repo_path): self._herd_branch_initial(url, branch, depth=depth) return branch_output = fmt.ref_string(branch) branch_ref = 'refs/heads/' + branch if self.existing_local_branch(branch): self._herd_branch_existing_local(branch, depth=depth, rebase=rebase, fork_remote=fork_remote) return self.fetch(self.remote, depth=depth, ref=branch_ref, allow_failure=True) if self.existing_remote_branch(branch, self.remote): self._herd(self.remote, branch_ref, depth=depth, fetch=False, rebase=rebase) return remote_output = fmt.remote_string(self.remote) self._print(' - No existing remote branch ' + remote_output + ' ' + branch_output) if fork_remote: self.fetch(fork_remote, depth=depth, ref=branch_ref) if self.existing_remote_branch(branch, fork_remote): self._herd(fork_remote, branch_ref, depth=depth, fetch=False, rebase=rebase) return remote_output = fmt.remote_string(fork_remote) self._print(' - No existing remote branch ' + remote_output + ' ' + branch_output) fetch = depth != 0 self.herd(url, depth=depth, fetch=fetch, rebase=rebase)
def _rename_remote(self, remote_from, remote_to): """Rename remote""" remote_output_f = fmt.remote_string(remote_from) remote_output_t = fmt.remote_string(remote_to) self._print(' - Rename remote ' + remote_output_f + ' to ' + remote_output_t) try: self.repo.git.remote('rename', remote_from, remote_to) except GitError as err: message = colored( ' - Failed to rename remote from ', 'red') + remote_output_f + ' to ' + remote_output_t self._print(message) self._print(fmt.error(err)) self._exit(message) except (KeyboardInterrupt, SystemExit): self._exit()
def _remote(self, remote, remove_dir=False): """Get remote""" remote_output = fmt.remote_string(remote) try: return self.repo.remotes[remote] except GitError as err: message = colored(' - No existing remote ', 'red') + remote_output if remove_dir: remove_directory(self.repo_path) self._print(message) self._print(fmt.error(err)) self._exit(message) except (KeyboardInterrupt, SystemExit): self._exit()
def _herd_branch_initial(self, url, branch, depth=0): """Herd branch initial""" self._init_repo() self._create_remote(self.remote, url, remove_dir=True) self.fetch(self.remote, depth=depth, ref=branch) if not self.existing_remote_branch(branch, self.remote): remote_output = fmt.remote_string(self.remote) self._print(' - No existing remote branch ' + remote_output + ' ' + fmt.ref_string(branch)) self._herd_initial(url, depth=depth) return self._create_branch_local_tracking(branch, self.remote, depth=depth, fetch=False, remove_dir=True)
def fetch(self, remote, **kwargs): """Fetch from a specific remote ref .. py:function:: fetch(remote, ref=None, depth=0, remove_dir=False, allow_failure=False) :param str remote: Remote name Keyword Args: ref (str): Ref to fetch depth (int): Git clone depth. 0 indicates full clone, otherwise must be a positive integer remove_dir (bool): Whether to remove the directory if commands fail allow_failure (bool): Whether to allow failure """ ref = kwargs.get('ref', None) depth = kwargs.get('depth', 0) remove_dir = kwargs.get('remove_dir', False) allow_failure = kwargs.get('allow_failure', False) remote_output = fmt.remote_string(remote) if depth == 0: self._print(' - Fetch from ' + remote_output) message = colored(' - Failed to fetch from ', 'red') error = message + remote_output command = ['git fetch', remote, '--prune --tags'] elif ref is None: command = ['git fetch', remote, '--depth', str(depth), '--prune --tags'] message = colored(' - Failed to fetch remote ', 'red') error = message + remote_output else: ref_output = fmt.ref_string(truncate_ref(ref)) self._print(' - Fetch from ' + remote_output + ' ' + ref_output) message = colored(' - Failed to fetch from ', 'red') error = message + remote_output + ' ' + ref_output command = ['git fetch', remote, truncate_ref(ref), '--depth', str(depth), '--prune --tags'] try: execute_command(command, self.repo_path, print_output=self._print_output) except ClowderError: if remove_dir: remove_directory(self.repo_path) if not allow_failure: self._print(error) self._exit(error)
def _herd_branch_initial(self, url, branch, depth=0): """Herd branch initial .. py:function:: _herd_branch_initial(url, branch, depth=0) :param str url: URL of repo :param str branch: Branch name to attempt to herd :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) self.fetch(self.remote, depth=depth, ref=branch) if not self.existing_remote_branch(branch, self.remote): remote_output = fmt.remote_string(self.remote) self._print(' - No existing remote branch ' + remote_output + ' ' + fmt.ref_string(branch)) self._herd_initial(url, depth=depth) return self._create_branch_local_tracking(branch, self.remote, depth=depth, fetch=False, remove_dir=True)
def _checkout_new_repo_branch(self, branch, depth): """Checkout remote branch or fail and delete repo if it doesn't exist""" branch_output = fmt.ref_string(branch) remote_output = fmt.remote_string(self.remote) self._remote(self.remote, remove_dir=True) self.fetch(self.remote, depth=depth, ref=branch, remove_dir=True) if not self.existing_remote_branch(branch, self.remote): remove_directory(self.repo_path) message = colored(' - No existing remote branch ', 'red') + remote_output + ' ' + branch_output self._print(message) self._exit(fmt.parallel_exception_error(self.repo_path, message)) self._create_branch_local_tracking(branch, self.remote, depth=depth, fetch=False, remove_dir=True)
def _pull(self, remote, branch): """Pull from remote branch""" if self.repo.head.is_detached: self._print(' - HEAD is detached') return branch_output = fmt.ref_string(branch) remote_output = fmt.remote_string(remote) self._print(' - Pull from ' + remote_output + ' ' + branch_output) command = ['git pull', remote, branch] return_code = execute_command(command, self.repo_path, print_output=self.print_output) if return_code != 0: message = colored(' - Failed to pull from ', 'red') + remote_output + ' ' + branch_output self._print(message) self._exit(message)
def _pull(self, remote, branch): """Pull from remote branch :param str remote: Remote name :param str branch: Branch name """ branch_output = fmt.ref_string(branch) remote_output = fmt.remote_string(remote) self._print(' - Pull from ' + remote_output + ' ' + branch_output) command = ['git pull', remote, branch] try: execute_command(command, self.repo_path, print_output=self._print_output) except ClowderError: message = colored(' - Failed to pull from ', 'red') + remote_output + ' ' + branch_output self._print(message) self._exit(message)
def _rebase_remote_branch(self, remote, branch): """Rebase from remote branch""" if self.repo.head.is_detached: self._print(' - HEAD is detached') return branch_output = fmt.ref_string(branch) remote_output = fmt.remote_string(remote) self._print(' - Rebase onto ' + remote_output + ' ' + branch_output) command = ['git pull --rebase', remote, branch] return_code = execute_command(command, self.repo_path, print_output=self.print_output) if return_code != 0: message = colored(' - Failed to rebase onto ', 'red') + remote_output + ' ' + branch_output self._print(message) self._print(fmt.command_failed_error(command)) self._exit(message)
def _rebase_remote_branch(self, remote, branch): """Rebase onto remote branch :param str remote: Remote name :param str branch: Branch name """ branch_output = fmt.ref_string(branch) remote_output = fmt.remote_string(remote) self._print(' - Rebase onto ' + remote_output + ' ' + branch_output) command = ['git pull --rebase', remote, branch] try: execute_command(command, self.repo_path, print_output=self._print_output) except ClowderError: message = colored(' - Failed to rebase onto ', 'red') + remote_output + ' ' + branch_output self._print(message) self._print(fmt.command_failed_error(command)) self._exit(message)
def reset(self, depth=0): """Reset branch to upstream or checkout tag/sha as detached HEAD""" if self.ref_type(self.default_ref) == 'branch': branch = self.truncate_ref(self.default_ref) branch_output = fmt.ref_string(branch) if not self.existing_local_branch(branch): return_code = self._create_branch_local_tracking(branch, self.remote, depth=depth, fetch=True) if return_code != 0: message = colored(' - Failed to create tracking branch ', 'red') + branch_output self._print(message) self._exit(message) return elif self._is_branch_checked_out(branch): self._print(' - Branch ' + branch_output + ' already checked out') else: self._checkout_branch_local(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) elif self.ref_type(self.default_ref) == 'tag': self.fetch(self.remote, ref=self.default_ref, depth=depth) self._checkout_tag(self.truncate_ref(self.default_ref)) elif self.ref_type(self.default_ref) == 'sha': self.fetch(self.remote, ref=self.default_ref, depth=depth) self._checkout_sha(self.default_ref)
def _remote(self, remote, remove_dir=False): """Get GitPython Remote instance .. py:function:: _remote(remote, remove_dir=False) :param str remote: Remote name :param Optional[bool] remove_dir: Whether to remove the directory if commands fail :return: GitPython Remote instance :rtype: Remote """ remote_output = fmt.remote_string(remote) try: return self.repo.remotes[remote] except GitError as err: message = colored(' - No existing remote ', 'red') + remote_output if remove_dir: remove_directory(self.repo_path) self._print(message) self._print(fmt.error(err)) self._exit(message) except (KeyboardInterrupt, SystemExit): self._exit()
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 _checkout_new_repo_branch(self, branch, depth): """Checkout remote branch or fail and delete repo if it doesn't exist :param str branch: Branch name :param int depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer """ branch_output = fmt.ref_string(branch) remote_output = fmt.remote_string(self.remote) self._remote(self.remote, remove_dir=True) self.fetch(self.remote, depth=depth, ref=branch, remove_dir=True) if not self.existing_remote_branch(branch, self.remote): remove_directory(self.repo_path) message = colored(' - No existing remote branch ', 'red') + remote_output + ' ' + branch_output self._print(message) self._exit(fmt.parallel_exception_error(self.repo_path, message)) self._create_branch_local_tracking(branch, self.remote, depth=depth, fetch=False, remove_dir=True)
def fetch(self, remote, ref=None, depth=0, remove_dir=False): """Fetch from a specific remote ref""" remote_output = fmt.remote_string(remote) if depth == 0: self._print(' - Fetch from ' + remote_output) message = colored(' - Failed to fetch from ', 'red') error = message + remote_output command = ['git fetch', remote, '--prune --tags'] elif ref is None: command = [ 'git fetch', remote, '--depth', str(depth), '--prune --tags' ] message = colored(' - Failed to fetch remote ', 'red') error = message + remote_output else: ref_output = fmt.ref_string(GitRepo.truncate_ref(ref)) self._print(' - Fetch from ' + remote_output + ' ' + ref_output) message = colored(' - Failed to fetch from ', 'red') error = message + remote_output + ' ' + ref_output command = [ 'git fetch', remote, GitRepo.truncate_ref(ref), '--depth', str(depth), '--prune --tags' ] return_code = execute_command(command, self.repo_path, print_output=self.print_output) if return_code != 0: if remove_dir: remove_directory(self.repo_path) self._print(error) self._exit(error) return return_code
def herd_branch(self, url, branch, depth=0, rebase=False, fork_remote=None): """Herd branch""" if not self.existing_git_repository(self.repo_path): self._herd_branch_initial(url, branch, depth=depth) return branch_output = fmt.ref_string(branch) branch_ref = 'refs/heads/' + branch if self.existing_local_branch(branch): if self._is_branch_checked_out(branch): self._print(' - Branch ' + branch_output + ' already checked out') else: self._checkout_branch_local(branch) self.fetch(self.remote, depth=depth, ref=branch_ref) if self.existing_remote_branch(branch, self.remote): self._herd_remote_branch(self.remote, branch, depth=depth, rebase=rebase) return if fork_remote: self.fetch(fork_remote, depth=depth, ref=branch_ref) if self.existing_remote_branch(branch, fork_remote): self._herd_remote_branch(fork_remote, branch, depth=depth, rebase=rebase) return self.fetch(self.remote, depth=depth, ref=branch_ref) if self.existing_remote_branch(branch, self.remote): self._herd(self.remote, branch_ref, depth=depth, fetch=False, rebase=rebase) return else: remote_output = fmt.remote_string(self.remote) self._print(' - No existing remote branch ' + remote_output + ' ' + branch_output) if fork_remote: self.fetch(fork_remote, depth=depth, ref=branch_ref) if self.existing_remote_branch(branch, fork_remote): self._herd(fork_remote, branch_ref, depth=depth, fetch=False, rebase=rebase) return else: remote_output = fmt.remote_string(fork_remote) self._print(' - No existing remote branch ' + remote_output + ' ' + branch_output) fetch = depth != 0 self.herd(url, depth=depth, fetch=fetch, rebase=rebase)