def sync_remote_list(directory): quoted_directory = shlex.quote(directory) commands = [] try: repo = pygit2.Repository(directory) except pygit2.GitError: commands.append("git init %s\n" % (quoted_directory, )) current_remotes = {} else: current_remotes = { remote.name: git_sort.RepoURL(remote.url) for remote in repo.remotes } commands.append("cd %s\n" % (quoted_directory, )) new_remotes = collections.OrderedDict((( transform(str(head.repo_url)), head.repo_url, ) for head in git_sort.remotes)) # modify existing remotes whose url has changed commands.extend([ "git remote set-url %s %s\n" % ( shlex.quote(name), shlex.quote(repr(repo_url)), ) for name, repo_url in new_remotes.items() if name in current_remotes and repo_url != current_remotes[name] ]) # add missing remotes current = set(current_remotes) new = set(new_remotes) mainline = str(git_sort.remotes[0].repo_url) def option(name): if name == mainline: return "" else: return " --no-tags" commands.extend([ "git remote add%s %s %s\n" % ( option(name), shlex.quote(name), shlex.quote(repr(new_remotes[name])), ) for name in new_remotes if name in new - current ]) # remove superfluous remotes commands.extend([ "git remote remove %s\n" % (shlex.quote(name), ) for name in sorted(current - new) ]) return commands
parser.add_argument( "-u", "--upstream", action="store_true", help="Move patches upstream between subsystem sections " "as appropriate. Default: false.") parser.add_argument("series", nargs="?", metavar="series.conf", help="series.conf file which will be modified in " "place. Default: if stdin is a terminal, " "\"series.conf\"; otherwise, read input from stdin.") args = parser.parse_args() repo_path = lib.repo_path() repo = pygit2.Repository(repo_path) index = git_sort.SortIndex(repo) filter_mode = False if args.series is None: if sys.stdin.isatty(): path = "series.conf" else: filter_mode = True else: path = args.series if filter_mode: f = sys.stdin else: try: f = open(path)
"(debugging).") args = parser.parse_args() try: path = os.environ["GIT_DIR"] except KeyError: try: # depending on the pygit2 version, discover_repository() will either # raise KeyError or return None if a repository is not found. path = pygit2.discover_repository(os.getcwd()) except KeyError: path = None if path is None: print("Error: Not a git repository", file=sys.stderr) sys.exit(1) repo = pygit2.Repository(path) if args.dump_heads: needs_rebuild = False try: with Cache() as cache: try: print("Cached heads (version %d):" % cache["version"]) except CKeyError: print("No usable cache") needs_rebuild = True else: try: history = cache["history"] except CUnsupported: print("Unsupported cache version")
def sequence_insert(series, rev, top): """ top is the top applied patch, None if none are applied. Caller must chdir to where the entries in series can be found. Returns the name of the new top patch and how many must be applied/popped. """ git_dir = repo_path() repo = pygit2.Repository(git_dir) index = git_sort.SortIndex(repo) try: commit = str(repo.revparse_single(rev).id) except ValueError: raise exc.KSError("\"%s\" is not a valid revision." % (rev, )) except KeyError: raise exc.KSError("Revision \"%s\" not found in \"%s\"." % ( rev, git_dir, )) marker = "# new commit" new_entry = InputEntry(marker) try: new_entry.dest = index.lookup(commit) except git_sort.GSKeyError: raise exc.KSError( "Commit %s not found in git-sort index. If it is from a " "repository and branch pair which is not listed in \"remotes\", " "please add it and submit a patch." % (commit, )) new_entry.dest_head = new_entry.dest.head try: before, inside, after = series_conf.split(series) except exc.KSNotFound as err: raise exc.KSError(err) before, after = map(series_conf.filter_series, ( before, after, )) current_patches = flatten( [before, series_conf.filter_series(inside), after]) if top is None: top_index = 0 else: top_index = current_patches.index(top) + 1 input_entries = parse_inside(index, inside, False) input_entries.append(new_entry) sorted_entries = series_sort(index, input_entries) new_patches = flatten([ before, [line.strip() for lines in sorted_entries.values() for line in lines], after, ]) commit_pos = new_patches.index(marker) if commit_pos == 0: # should be inserted first in series name = "" else: name = new_patches[commit_pos - 1] del new_patches[commit_pos] if new_patches != current_patches: raise exc.KSError("Subseries is not sorted. " "Please run scripts/series_sort.py.") return ( name, commit_pos - top_index, )