def formatted_row(self) -> List[str]: return [ self.name, self.get_file_type().formatted_value, Format.size(self.size), Format.bool(self.downloaded) ]
def formatted_value(self) -> str: if self is FileType.DIR: return Format.blue(self.value) elif self is FileType.FILE: return self.value else: raise Exception('Unknown file type')
def tracking_branches(cls, output: str) -> Tuple[str, Optional[str]]: # Expected output format: # > git rev-parse --symbolic-full-name git-old@{upstream} # refs/heads/git # > git rev-parse --symbolic-full-name git-old@{upstream} # refs/remotes/origin/git if output.startswith('refs/heads'): remote = None branch = Format.remove_prefix('refs/heads', output) elif output.startswith('refs/remotes'): components = Format.remove_prefix('refs/remotes', output).split('/') remote = components[0] branch = components[1] else: raise Exception('Failed to parse tracking branch output') return branch, remote
def get_default_branch(cls, url: str) -> Optional[str]: """Get default branch from remote repo""" command = f'git ls-remote --symref {url} {HEAD}' output = cmd.get_stdout(command) if output is None: return None output_list = output.split() branch = [Format.remove_prefix(chunk, 'refs/heads/') for chunk in output_list if chunk.startswith('refs/heads/')] return branch[0]
def shas(cls, output: str, prefix: str) -> Dict[str, str]: # TODO: Add expected output example lines = output.strip().splitlines() items = {} for line in lines: components = line.split() sha = components[0].strip() name = Format.remove_prefix(components[1], prefix) items[name] = sha return items
def formatted_ref(self) -> str: """Formatted project repo ref""" if self.is_detached: return Format.Git.ref(Format.escape(f'[HEAD @ {self.sha()}]')) current_branch_output = Format.Git.ref( Format.escape(f'[{self.current_branch}]')) local_commits_count = GitOffline.new_commits_count(self.path) no_local_commits = local_commits_count == 0 # TODO: Specify correct remote upstream_commits_count = GitOffline.new_commits_count(self.path, upstream=True) no_upstream_commits = upstream_commits_count == 0 if no_local_commits and no_upstream_commits: return current_branch_output local_commits_output = Format.yellow(f'+{local_commits_count}') upstream_commits_output = Format.red(f'-{upstream_commits_count}') return f'{current_branch_output}({local_commits_output}/{upstream_commits_output})'
def remote_branches(cls, output: str, remote: str) -> Tuple[List[str], Optional[str]]: # TODO: Add expected output example lines = output.strip().splitlines() branches = [] default_branch = None for line in lines: components = line.split() if components[0] == 'warning:': continue if len(components) == 1: name = Format.remove_prefix(components[0].strip(), f'{remote}/') branches.append(name) elif len(components) == 3 and components[1] == '->': name = Format.remove_prefix(components[2].strip(), f'{remote}/') default_branch = name else: raise Exception('Wrong number of components for remote branch') return branches, default_branch
def diff_index(cls, output: str) -> Dict[str, List[Dict[str, str]]]: # Expected output format # :000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A something/something.txt # :100644 100644 375928ea14a5a1a7e14f14a36cb0a31c417d1ca1 0000000000000000000000000000000000000000 M README.md # :100644 000000 375928ea14a5a1a7e14f14a36cb0a31c417d1ca1 0000000000000000000000000000000000000000 D README.md changes = {'added': [], 'modified': [], 'deleted': []} if output is None: return changes def diff_type(value: str) -> str: if value == 'A': return 'added' elif value == 'M': return 'modified' elif value == 'D': return 'deleted' else: raise Exception('Unknown diff type') lines = output.strip().splitlines() for line in lines: old_permissions = Format.remove_prefix(line[0].strip(), ':') new_permissions = line[1].strip() old_sha = line[2].strip() new_sha = line[3].strip() change_type = line[4].strip() file_path = line[5].strip() change = { 'old_permissions': old_permissions, 'new_permissions': new_permissions, 'old_sha': old_sha, 'new_sha': new_sha, 'path': file_path } changes[diff_type(change_type)].append(change) return changes