def _move_root(forward, prefix=''): root = GitRoot.ROOT container = GitRoot.root_container() roots = sorted(File.each(path=container, select=GitRoot.select(prefix))) if roots: try: index = roots.index(root) except ValueError: print(roots[0] if forward else roots[-1]) return index += (1 if forward else -1) path = roots[index % len(roots)] sub = os.path.join(path, os.path.relpath(os.getcwd(), root)) print((sub if os.path.exists(sub) else path) or '.') else: print('.')
def get_format_string(name, user, context): def get_url(): full_path = os.getcwd() if name: path, f = os.path.split(name) full_path = os.path.join(full_path, path) if not os.path.exists(full_path): raise ValueError("Path %s doesn't exist." % full_path) if f: for p in os.listdir(full_path): if startswith(p, f): full_path = os.path.join(full_path, p) break else: raise ValueError("Can't find file matching " + name) context['path'] = os.path.relpath(full_path, GitRoot.ROOT) return _URL if not name: return get_url() if name.isdigit(): context['number'] = int(name) return PULL if 'commits'.startswith(name): return _COMMIT if 'diffs'.startswith(name): return _DIFF if 'pull'.startswith(name): return _PULL if 'pending'.startswith(name): if user == Settings.PROJECT_USER: return Pulls.pull_urls()[0] if user == Settings.USER: branch_name = '%s:%s' % (user, Git.branch()) for number, pull in Git.pulls().items(): if pull.branch == branch_name: context['number'] = number return PULL else: return _NEW_PULL raise ValueError("Can't pull for user %s." % user) if 'root'.startswith(name): name = GitRoot.root() return get_url()
def explode(): root = GitRoot.root() for line in Git.git("status", "--porcelain").splitlines(): mode, filename = line.split() try: if mode == "??": Git.git("add", filename, cwd=root) Git.git("commit", filename, "-m", "[fold] %s" % filename, cwd=root) except Exception as e: print("ERROR: couldn't commit filename %s." % filename) print("ERROR:", e)
def branches(prefix='', *args): root = GitRoot.root_container() pulls = Git.pull_branches() if not ARGS.expanded: fname = [''] def before(f): fname[0] = os.path.basename(f) def callback(data): parts = filter(None, data.splitlines()) for i, p in enumerate(parts): branch = p.split()[-1] if branch in pulls: branch += '(%s)' % pulls[branch] if p.startswith('*'): branch = '*' + branch parts[i] = branch print('%-12s %s' % (fname[0] + ':', ' '.join(parts))) Call.for_each_directory( BRANCH_COMMAND, path=root, select=GitRoot.select(prefix), before=before, callback=callback) else: def before(f): print('\n%s:' % os.path.basename(f)) Call.for_each_directory( BRANCH_COMMAND, path=root, select=GitRoot.select(prefix), before=before) if args: print('ERROR: arguments %s ignored' % ', '.join(args))
def version_commit(version_number=None, success=None, failure=None): root = GitRoot.root() files = Project.settings('version')['files'] old_version = get_version() if version_number == old_version: raise Exception('Version number is already %s' % old_version) if not version_number: version_number = semver.increment_string(old_version) if not CommandList.confirm('update version %s to %s' % (old_version, version_number)): return for f in files: File.subn(os.path.join(root, f), VERSION, version_number) if success or failure: ChangeLog.add_status_line(version_number, success, failure) Git.git('commit', '-am', 'Set version to %s' % version_number)
def clone(directory): settings = Project.settings("clone") branch = settings.get("base_branch", "develop") root = GitRoot.root(os.getcwd()) if root: directory = directory or os.path.basename(root) root = os.path.dirname(root) else: directory = directory or Settings.PROJECT root = os.getcwd() directory = File.next_version(os.path.join(root, directory)) settings.update(branch=branch, directory=directory, project=Settings.PROJECT, user=Settings.USER) # Call git clone. if Git.git(*_CLONE.format(**settings).split(), cwd=root): raise ValueError("Failed to start new directory") Remote.remote("all", cwd=directory) Remote.remote("upstream", Settings.PROJECT, directory) Git.git("pull", "upstream", branch) banner("Created", branch + ", directory", directory) return directory
def efind(*args): args = list(args) is_forward = '-' not in args if not is_forward: args.remove('-') if '+' in args: args.remove('+') if not args: raise ValueError('No file specified for find') if len(args) < 2: args.append(os.getcwd()) prefix, current = args current = current and os.path.abspath(current) root = GitRoot.root(current) if not root: raise ValueError('There is no git repository here.') settings = Project.settings('find') find_root = os.path.join(root, settings['find_root']) display_root = os.path.join(root, settings['display_root']) matches = match(find_root, prefix) if not matches: open('/tmp/error.txt', 'w').write('failed to find prefix ' + prefix + ' current ' + current) return if is_forward: index = bisect.bisect_right(matches, current) else: index = bisect.bisect_left(matches, current) - 1 print(matches[index % len(matches)])
#!/usr/bin/env python from __future__ import absolute_import, division, print_function, unicode_literals import os, sys from grit import GitRoot if __name__ == '__main__': print(GitRoot.root((sys.argv[1:] or [os.getcwd()])[0]))