def main(repo, directory, host: Host, owner, branch, separate_git_dir): """REPO can also be: - 'rii-mango/NIFTI-Reader-JS' - 'github.com/rii-mango/NIFTI-Reader-JS' - 'www.github.com/rii-mango/NIFTI-Reader-JS' - 'https://github.com/rii-mango/NIFTI-Reader-JS' - 'https://www.github.com/rii-mango/NIFTI-Reader-JS' """ # from igit_debug.loggr import Loggr # logger = Loggr() # logger.debug(f'repo: {repo}, ', f'directory: {directory}, ', f'host: {host}, ', f'owner: {owner}, ', f'branch: {branch}, ', f'separate_git_dir: {separate_git_dir}') cmd = 'git clone ' if branch != 'master': cmd += f'--branch={branch} ' if separate_git_dir: cmd += f'--separate-git-dir={separate_git_dir} ' cmd += build_url(repo, host, owner) if directory: cmd += f' {directory}' shell.run(cmd)
def main(name): btree = Branches() if name in btree: if not prompt.confirm(f'"{name}" already exists, check it out?'): sys.exit() shell.run(f'git checkout {name}') return git.pull() status = Status() stash = False if status: os.system('git status') answer = prompt.action('Uncommitted changes', 'stash → apply', 'just checkout', 'reset --hard', flowopts=True) if answer == 's': stash = True shell.run('git stash') elif answer == 'r': shell.run('git reset --hard') else: stash = False shell.run(f'git checkout -b {name}', f'git push --set-upstream origin {name}') if stash: shell.run('git stash apply') from .addcommitpush import main as addcommitpush addcommitpush(f'new branch: {name}')
def main(): status = Status() if status: sys.exit(colors.red('Uncommitted changes, aborting')) btree = Branches() versionbranch = btree.version if not versionbranch: print(colors.yellow("Couldn't find version branch")) if not prompt.confirm('Checkout master?'): sys.exit() branch = 'master' else: branch = versionbranch currbranch = btree.current if currbranch == branch: print(colors.yellow(f'Already on version branch: {branch}')) if not prompt.confirm('Pull?'): sys.exit() if git.pull() == 1: print(colors.yellow(f"git pull failed")) shell.run(f'git checkout {branch}') if git.pull() == 1: print(colors.yellow(f"git pull failed"))
def main(name): btree = Branches() if name not in btree: print(colors.yellow(f"didn't find {name} in branches")) name = btree.search(name) if name == btree.current: # TODO: gco - then continue sys.exit(colors.red(f'"{name}" is current branch')) if name == btree.version: if not prompt.confirm(f'{name} is version branch, continue?'): sys.exit() shell.run(f'git branch -D {name}', f'git push origin --delete {name}')
def split(self, verify_integrity=False) -> List['ExPath']: """If verify_integrity is `True`, and splits are bad, raises OSError""" existing_splits = self.getsplits() if existing_splits: raise FileExistsError(f"ExPath.split() was called for {self}, but existing_splits were found:\n\t{existing_splits}") shell.run(f'split -d --bytes 49000KB "{self}" "{self._split_prefix}"') splits = self.getsplits() if not verify_integrity: return splits splits_ok = self.splits_integrity_ok() if not splits_ok: raise OSError(f"ExPath.split() was called for {self} with verify_integrity=True. splits are bad.") return splits
def main(oldname, newname): status = Status() if status: sys.exit(colors.red('Uncommitted changes, aborting')) btree = Branches() if newname in btree: sys.exit(colors.red(f'"{newname}" already exists')) if oldname not in btree: os.system('git branch -l') sys.exit(colors.red(f'"{oldname}" doesnt exist')) shell.run(f'git branch -m {newname}', f'git push origin :{oldname} {newname}', f'git push origin -u {newname}')
def splits_integrity_ok(self) -> bool: """Removes the joined file after joining only if integrity is ok.""" existing_splits = self.getsplits() if not existing_splits: raise FileNotFoundError(f"ExPath.verify_splits_integrity() was called for {self}, but existing_splits were not found by split prefix: {self._split_prefix}") # todo: this doesn't work with shell.run for some reason os.system(f'cat "{self._split_prefix}"* > /tmp/joined') try: # diff returns 0 only if there's no difference shell.run(f'diff /tmp/joined "{self}"', raise_on_non_zero='short') except ChildProcessError: return False else: shell.run('rm /tmp/joined', raise_on_non_zero=True) return True
def main(paths): print(f'main({", ".join(paths)})') # TODO: see if exists in status if not paths: sys.exit(colors.red('no values! exiting')) cmds = [] gitignore = Gitignore() status = Status() existing_paths = [] for p in paths: breakpoint() path = status.search(p, noprompt=False) if not path: yellowprint(f"status.search({p}) return None, skipping") continue if not path.exists(): yellowprint(f"{p} does not exist, skipping") continue # exists if path.is_dir(): cmds.append(f'git rm -r --cached {path}') else: cmds.append(f'git rm --cached {path}') existing_paths.append(path) if not cmds: sys.exit(colors.red('no values to rm, exiting')) shell.run(*cmds, raiseexc=False) if prompt.confirm(f'try to ignore {len(existing_paths)} values?'): gitignore.write(existing_paths) commitmsg = f'Removed from cache: ' + ', '.join(map(str, paths)) answer = prompt.generic(f'commit and push?', f'yes, commit with "{commitmsg}"', 'custom commit message', flowopts='quit') if answer is not True: commitmsg = prompt.generic('commit msg:', free_input=True)[1] shell.run(f'git commit -am "{commitmsg}"') git.push()
def backup(self, confirm: bool): if confirm and not prompt.confirm(f'Backup .gitignore to .gitignore.backup?'): print('aborting') return False absolute = self.file.absolute() try: shell.run(f'cp {absolute} {absolute}.backup', raiseexc='summary') except Exception as e: if not prompt.confirm('Backup failed, overwrite .gitignore anyway?', flowopts='debug'): print('aborting') return False return True else: backup = ExPath(f'{absolute}.backup') if not backup.exists() and not prompt.confirm(f'Backup command completed without error, but {backup} doesnt exist. overwrite .gitignore anyway?', flowopts='debug'): print('aborting') return False return True
def main(commitmsg: str, dry_run: bool = False): status = Status() if not status.files: if prompt.confirm('No files in status, just push?'): if dry_run: misc.whiteprint('dry run, not pushing. returning') return return git.push() cwd = ExPath(os.getcwd()) largefiles: Dict[ExPath, float] = get_large_files_from_status(cwd, status) if largefiles: handle_large_files(cwd, largefiles, dry_run) if '.zsh_history' in status: pwd = misc.getsecret('general') with open('/home/gilad/.zsh_history', errors='backslashreplace') as f: history = f.readlines() for i, line in enumerate(history): if pwd in line: misc.redprint( f"password found in .zsh_history in line {i}. exiting") return if not commitmsg: if len(status.files) == 1: commitmsg = status.files[0] elif len(status.files) < 3: commitmsg = ', '.join([f.name for f in status.files]) else: os.system('git status -s') commitmsg = input('commit msg:\t') commitmsg = misc.unquote(commitmsg) if dry_run: misc.whiteprint('dry run. exiting') return shell.run('git add .', f'git commit -am "{commitmsg}"') git.push()
def main(branch): status = Status() if status: if not prompt.confirm('Uncommitted changes, checkout anyway?'): print('aborting') return btree = Branches() if branch not in btree: yellowprint(f'"{branch}" not in branches, searching...') branch = btree.search(branch) if btree.current == branch: yellowprint(f'Already on {branch}') return if not branch: redprint(f"Couldn't find branch") return shell.run(f'git checkout {branch}') if not prompt.confirm('git pull?'): print('aborting') return if git.pull() == 1: brightyellowprint(f"git pull failed")
def main(items: Tuple[str], exclude): # TODO: option to view in web like comparebranch exclude_exts = [] if exclude: for ex in map(misc.clean, exclude.split(' ')): exclude_exts.append(misc.quote(f':!{ex}')) cmd = 'git diff --color-moved=zebra --find-copies-harder --ignore-blank-lines --ignore-cr-at-eol --ignore-space-at-eol --ignore-space-change --ignore-all-space ' darkprint(f'diff.py main(items): {items}') if not items: # TODO: exclude shell.run(cmd, stdout=sys.stdout) btree = Branches() ctree = Commits() formatted = [] # if exclude_exts: gitignore = None status = Status() any_sha = False diff_files = set() for i, item in enumerate(map(misc.clean, items)): if regex.SHA_RE.fullmatch(item): # e.g. 'f5905f1' any_sha = True if i > 1: redprint( f'SHA1 items, if any, must be placed at the most in the first 2 arg slots' ) return if item in ctree: if i == 1 and formatted[0] not in ctree: redprint( f'When specifying two SHA1 args, both must belong to the same tree. 0th arg doesnt belong to ctree, 1st does' ) return formatted.append(item) continue if item in btree: if i == 1 and formatted[0] not in btree: redprint( f'When specifying two SHA1 args, both must belong to the same tree. 0th arg doesnt belong to btree, 1st does' ) return formatted.append(item) continue redprint( f'item is SHA1 but not in commits nor branches. item: {repr(item)}' ) return else: if any_sha: # all SHA items have already been appended, and current item is not a SHA numstat_cmd = f'git diff --numstat {" ".join(items[:i])}' else: # no SHA items at all, this is the first item numstat_cmd = f'git diff --numstat' filestats = shell.runquiet(numstat_cmd).splitlines() diff_files = set(line.rpartition(' ')[2] for line in filestats) # TODO (continue here): # gpdy SHA SHA REGEX -e REGEX # make -e filter out results if item in diff_files: with_quotes = misc.quote(item) formatted.append(with_quotes) darkprint(f'appended: {with_quotes}') else: brightwhiteprint( f'{item} is not in diff_files, searching within diff_files...') choice = search_and_prompt(item, diff_files) if choice: with_quotes = misc.quote(choice) formatted.append(with_quotes) darkprint(f'appended: {with_quotes}') # file = status.search(item, quiet=False) # if not file: # brightyellowprint(f'{item} is not in status, skipping') # continue # stripped = misc.unquote(file.strip()) # formatted.append(misc.quote(stripped)) # darkprint(f'appended: {repr(stripped)}') joined = " ".join(formatted) cmd += f'{joined} ' if exclude_exts: cmd += " ".join(exclude_exts) shell.run(cmd, stdout=sys.stdout)
def trash(self): shell.run(f'gio trash "{self}"')