From 5e5d76697bafba33777355d3955def0e549eefdd Mon Sep 17 00:00:00 2001 From: Hans Ulrich Niedermann Date: Sun, 8 Aug 2010 17:04:37 +0200 Subject: Consistently use git.* and pyfedpkg.* functions Also - remove -n/--dry-run argument - fix branch sorting order --- src/pyfedpkg/initial_merge.py | 115 ++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 67 deletions(-) diff --git a/src/pyfedpkg/initial_merge.py b/src/pyfedpkg/initial_merge.py index 5bc119f..dd5e7a9 100755 --- a/src/pyfedpkg/initial_merge.py +++ b/src/pyfedpkg/initial_merge.py @@ -15,14 +15,12 @@ the branches will be a lot easier: Easier to follow in the dependency graph, and easier to perform without conflicts. """ -# TODO: Use more pyfedpkg.* functions and/or git.* functions instead -# of direct calls to git. - import argparse import sys import os import subprocess import pyfedpkg +import git __all__ = [ @@ -32,47 +30,48 @@ __all__ = [ ] -global global_dry_run -global_dry_run = False -# global_dry_run = True - - -def run_cmd_out(cmd, dry_run=None, shell=False): - if dry_run==None: - global global_dry_run - dry_run = global_dry_run - if dry_run: - print 'RUN-', cmd +def str_numsplit(s): + assert(not s[0].isdigit()) + if not s[-1].isdigit(): + return s + i = len(s) + while i>0: + i = i - 1 + if not s[i].isdigit(): + break + prefix, numstr = s[0:i+1], s[i+1:] + return (prefix, int(numstr)) + + +def cmp_relbranch(a, b): + asplit = str_numsplit(a) + bsplit = str_numsplit(b) + if type(asplit) == str and type(bsplit) == str: + return cmp(asplit,bsplit) + elif type(asplit) == str and type(bsplit) == tuple: + return 1 + elif type(asplit) == tuple and type(bsplit) == str: + return -1 + elif type(asplit) == tuple and type(bsplit) == tuple: + if asplit[0] in ('f','fc') and bsplit[0] in ('f','fc'): + return cmp(asplit[1], bsplit[1]) + else: + return cmp(asplit,bsplit) else: - print 'RUN:', cmd - if dry_run: - return (0,'','') - proc = subprocess.Popen(cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - shell=shell) - stdout, stderr = proc.communicate() - return (proc.returncode,stdout,stderr) - - -def run_cmd(cmd, dry_run=None, shell=False): - retcode, stdout, stderr = run_cmd_out(cmd, dry_run=dry_run, shell=shell) - sys.stdout.write(stdout) - sys.stdout.write(stderr) - return retcode + return cmp(asplit,bsplit) def cmp_Branch(a, b): if a.sha == b.sha: - return cmp(a.localbranch,b.localbranch) + return cmp_relbranch(a.localbranch,b.localbranch) else: return cmp(a.sha,b.sha) class Branch: - def __init__(self, string): - self.sha, self.origbranch = string.split() + def __init__(self, sha, origbranch): + self.sha, self.origbranch = sha, origbranch a = self.origbranch.split('/') assert(a[0] == 'origin') assert(a[-1] == 'master') @@ -87,15 +86,15 @@ def do_initial_merge(into, to_merge): print "######## Merging", [ x.localbranch for x in to_merge ], \ "into", into.localbranch, "########" pyfedpkg.switch_branch(into.localbranch) - ret = run_cmd(['git', 'merge', - '-m', '"Initial peudo merge for dist-git setup"', - '-s', 'ours'] + - [ x.origbranch for x in to_merge]) - assert(ret == 0) + repo = git.Repo() + print "Merging", [ x.origbranch for x in to_merge], "into", into.localbranch + repo.git.merge(*(['-m', '"Initial peudo merge for dist-git setup"', + '-s', 'ours'] + + [ x.origbranch for x in to_merge])) for t in to_merge: pyfedpkg.switch_branch(t.localbranch) - ret = run_cmd(['git', 'merge', into.localbranch]) - assert(ret == 0) + print "Merging", into.localbranch, "into", t.localbranch + repo.git.merge(into.localbranch) pyfedpkg.switch_branch(into.localbranch) @@ -137,25 +136,17 @@ class Consumer: def handle_curdir(): - git_branch_cmd = """for branch in $(git branch -r | grep -v '/HEAD'); do echo "$(git rev-parse "$branch^{tree}") $branch"; done""" - retcode, stdout, stderr = run_cmd_out(git_branch_cmd, - dry_run=False, shell=True) - assert(retcode == 0) - assert(stderr == "") - # print "RESULT:" - # sys.stdout.write(stdout) - - a = stdout.splitlines() - a = [ Branch(s) for s in a ] - a.sort(cmp_Branch) - # print "SORTED:" - for x in a: print " ", x - - # print "ITERATING:" + repo = git.Repo() + _locals, remotes = pyfedpkg._list_branches(repo=repo) + aa = [ Branch(repo.git.rev_parse('%s^{tree}' % b), b) for b in remotes ] + aa.sort(cmp_Branch) + print "Branches sorted by tree sha:" + for x in aa: print " ", x + n = 0 c = Consumer() - while n < len(a): - c.eat(a[n]) + while n < len(aa): + c.eat(aa[n]) n = n + 1 c.finish() @@ -188,10 +179,6 @@ class Action(argparse.Action): def fedpkg_command(args): - global global_dry_run - global_dry_run = args.dry_run - if global_dry_run: - print "NOTE: Running in dry-run mode, i.e. we will not actually perform any merges." for repo in args.repos: handle_path(repo) @@ -204,12 +191,6 @@ def get_parser(subparsers): help = 'git merge to join branches with identical trees', formatter_class=argparse.RawDescriptionHelpFormatter, description = _module_doc) - sp.add_argument('-n', '--dry-run', - dest='dry_run', - action='store_const', const=True, - default=False, - help = ('Whether to actually perform the merges. '+ - 'Local tracking branches will be created anyway.')) sp.add_argument('repos', metavar='repo-path', nargs='*', default=['.'], action=Action, -- cgit