summaryrefslogtreecommitdiff
path: root/scripts/combo-layer
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/combo-layer')
-rwxr-xr-xscripts/combo-layer914
1 files changed, 846 insertions, 68 deletions
diff --git a/scripts/combo-layer b/scripts/combo-layer
index ae97471d6d..d04d88b070 100755
--- a/scripts/combo-layer
+++ b/scripts/combo-layer
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
@@ -20,12 +20,20 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+import fnmatch
import os, sys
import optparse
import logging
import subprocess
-import ConfigParser
+import tempfile
+import configparser
import re
+import copy
+import pipes
+import shutil
+from collections import OrderedDict
+from string import Template
+from functools import reduce
__version__ = "0.2.1"
@@ -67,17 +75,34 @@ class Configuration(object):
if value.startswith("@"):
self.repos[repo][name] = eval(value.strip("@"))
else:
+ # Apply special type transformations for some properties.
+ # Type matches the RawConfigParser.get*() methods.
+ types = {'signoff': 'boolean', 'update': 'boolean', 'history': 'boolean'}
+ if name in types:
+ value = getattr(parser, 'get' + types[name])(section, name)
self.repos[repo][name] = value
+ def readglobalsection(parser, section):
+ for (name, value) in parser.items(section):
+ if name == "commit_msg":
+ self.commit_msg_template = value
+
logger.debug("Loading config file %s" % self.conffile)
- self.parser = ConfigParser.ConfigParser()
+ self.parser = configparser.ConfigParser()
with open(self.conffile) as f:
self.parser.readfp(f)
+ # initialize default values
+ self.commit_msg_template = "Automatic commit to update last_revision"
+
self.repos = {}
for repo in self.parser.sections():
- self.repos[repo] = {}
- readsection(self.parser, repo, repo)
+ if repo == "combo-layer-settings":
+ # special handling for global settings
+ readglobalsection(self.parser, repo)
+ else:
+ self.repos[repo] = {}
+ readsection(self.parser, repo, repo)
# Load local configuration, if available
self.localconffile = None
@@ -92,7 +117,7 @@ class Configuration(object):
self.localconffile = lcfile
logger.debug("Loading local config file %s" % self.localconffile)
- self.localparser = ConfigParser.ConfigParser()
+ self.localparser = configparser.ConfigParser()
with open(self.localconffile) as f:
self.localparser.readfp(f)
@@ -108,7 +133,9 @@ class Configuration(object):
readsection(self.localparser, section, repo)
def update(self, repo, option, value, initmode=False):
- if self.localparser:
+ # If the main config has the option already, that is what we
+ # are expected to modify.
+ if self.localparser and not self.parser.has_option(repo, option):
parser = self.localparser
section = "%s|%s" % (repo, self.combobranch)
conffile = self.localconffile
@@ -121,6 +148,7 @@ class Configuration(object):
parser.set(section, option, value)
with open(conffile, "w") as f:
parser.write(f)
+ self.repos[repo][option] = value
def sanity_check(self, initmode=False):
required_options=["src_uri", "local_repo_dir", "dest_dir", "last_revision"]
@@ -133,6 +161,12 @@ class Configuration(object):
if option not in self.repos[name]:
msg = "%s\nOption %s is not defined for component %s" %(msg, option, name)
missing_options.append(option)
+ # Sanitize dest_dir so that we do not have to deal with edge cases
+ # (unset, empty string, double slashes) in the rest of the code.
+ # It not being set will still be flagged as error because it is
+ # listed as required option above; that could be changed now.
+ dest_dir = os.path.normpath(self.repos[name].get("dest_dir", "."))
+ self.repos[name]["dest_dir"] = "." if not dest_dir else dest_dir
if msg != "":
logger.error("configuration file %s has the following error: %s" % (self.conffile,msg))
if self.localconffile and 'last_revision' in missing_options:
@@ -144,24 +178,28 @@ class Configuration(object):
logger.error("ERROR: patchutils package is missing, please install it (e.g. # apt-get install patchutils)")
sys.exit(1)
-def runcmd(cmd,destdir=None,printerr=True):
+def runcmd(cmd,destdir=None,printerr=True,out=None,env=None):
"""
execute command, raise CalledProcessError if fail
return output if succeed
"""
logger.debug("run cmd '%s' in %s" % (cmd, os.getcwd() if destdir is None else destdir))
- out = os.tmpfile()
+ if not out:
+ out = tempfile.TemporaryFile()
+ err = out
+ else:
+ err = tempfile.TemporaryFile()
try:
- subprocess.check_call(cmd, stdout=out, stderr=out, cwd=destdir, shell=True)
- except subprocess.CalledProcessError,e:
- out.seek(0)
+ subprocess.check_call(cmd, stdout=out, stderr=err, cwd=destdir, shell=isinstance(cmd, str), env=env or os.environ)
+ except subprocess.CalledProcessError as e:
+ err.seek(0)
if printerr:
- logger.error("%s" % out.read())
+ logger.error("%s" % err.read())
raise e
- out.seek(0)
- output = out.read()
- logger.debug("output: %s" % output )
+ err.seek(0)
+ output = err.read().decode('utf-8')
+ logger.debug("output: %s" % output.replace(chr(0), '\\0'))
return output
def action_init(conf, args):
@@ -176,6 +214,11 @@ def action_init(conf, args):
subprocess.check_call("git clone %s %s" % (conf.repos[name]['src_uri'], ldir), shell=True)
if not os.path.exists(".git"):
runcmd("git init")
+ if conf.history:
+ # Need a common ref for all trees.
+ runcmd('git commit -m "initial empty commit" --allow-empty')
+ startrev = runcmd('git rev-parse master').strip()
+
for name in conf.repos:
repo = conf.repos[name]
ldir = repo['local_repo_dir']
@@ -191,18 +234,227 @@ def action_init(conf, args):
lastrev = None
initialrev = branch
logger.info("Copying data from %s..." % name)
+ # Sanity check initialrev and turn it into hash (required for copying history,
+ # because resolving a name ref only works in the component repo).
+ rev = runcmd('git rev-parse %s' % initialrev, ldir).strip()
+ if rev != initialrev:
+ try:
+ refs = runcmd('git show-ref -s %s' % initialrev, ldir).split('\n')
+ if len(set(refs)) > 1:
+ # Happens for example when configured to track
+ # "master" and there is a refs/heads/master. The
+ # traditional behavior from "git archive" (preserved
+ # here) it to choose the first one. This might not be
+ # intended, so at least warn about it.
+ logger.warn("%s: initial revision '%s' not unique, picking result of rev-parse = %s" %
+ (name, initialrev, refs[0]))
+ initialrev = rev
+ except:
+ # show-ref fails for hashes. Skip the sanity warning in that case.
+ pass
+ initialrev = rev
dest_dir = repo['dest_dir']
- if dest_dir and dest_dir != ".":
+ if dest_dir != ".":
extract_dir = os.path.join(os.getcwd(), dest_dir)
- os.makedirs(extract_dir)
+ if not os.path.exists(extract_dir):
+ os.makedirs(extract_dir)
else:
extract_dir = os.getcwd()
file_filter = repo.get('file_filter', "")
- runcmd("git archive %s | tar -x -C %s %s" % (initialrev, extract_dir, file_filter), ldir)
+ exclude_patterns = repo.get('file_exclude', '').split()
+ def copy_selected_files(initialrev, extract_dir, file_filter, exclude_patterns, ldir,
+ subdir=""):
+ # When working inside a filtered branch which had the
+ # files already moved, we need to prepend the
+ # subdirectory to all filters, otherwise they would
+ # not match.
+ if subdir == '.':
+ subdir = ''
+ elif subdir:
+ subdir = os.path.normpath(subdir)
+ file_filter = ' '.join([subdir + '/' + x for x in file_filter.split()])
+ exclude_patterns = [subdir + '/' + x for x in exclude_patterns]
+ # To handle both cases, we cd into the target
+ # directory and optionally tell tar to strip the path
+ # prefix when the files were already moved.
+ subdir_components = len(subdir.split(os.path.sep)) if subdir else 0
+ strip=('--strip-components=%d' % subdir_components) if subdir else ''
+ # TODO: file_filter wild cards do not work (and haven't worked before either), because
+ # a) GNU tar requires a --wildcards parameter before turning on wild card matching.
+ # b) The semantic is not as intendend (src/*.c also matches src/foo/bar.c,
+ # in contrast to the other use of file_filter as parameter of "git archive"
+ # where it only matches .c files directly in src).
+ files = runcmd("git archive %s %s | tar -x -v %s -C %s %s" %
+ (initialrev, subdir,
+ strip, extract_dir, file_filter),
+ ldir)
+ if exclude_patterns:
+ # Implement file removal by letting tar create the
+ # file and then deleting it in the file system
+ # again. Uses the list of files created by tar (easier
+ # than walking the tree).
+ for file in files.split('\n'):
+ if file.endswith(os.path.sep):
+ continue
+ for pattern in exclude_patterns:
+ if fnmatch.fnmatch(file, pattern):
+ os.unlink(os.path.join(*([extract_dir] + ['..'] * subdir_components + [file])))
+ break
+
+ if not conf.history:
+ copy_selected_files(initialrev, extract_dir, file_filter, exclude_patterns, ldir)
+ else:
+ # First fetch remote history into local repository.
+ # We need a ref for that, so ensure that there is one.
+ refname = "combo-layer-init-%s" % name
+ runcmd("git branch -f %s %s" % (refname, initialrev), ldir)
+ runcmd("git fetch %s %s" % (ldir, refname))
+ runcmd("git branch -D %s" % refname, ldir)
+ # Make that the head revision.
+ runcmd("git checkout -b %s %s" % (name, initialrev))
+ # Optional: cut the history by replacing the given
+ # start point(s) with commits providing the same
+ # content (aka tree), but with commit information that
+ # makes it clear that this is an artifically created
+ # commit and nothing the original authors had anything
+ # to do with.
+ since_rev = repo.get('since_revision', '')
+ if since_rev:
+ committer = runcmd('git var GIT_AUTHOR_IDENT').strip()
+ # Same time stamp, no name.
+ author = re.sub('.* (\d+ [+-]\d+)', r'unknown <unknown> \1', committer)
+ logger.info('author %s' % author)
+ for rev in since_rev.split():
+ # Resolve in component repo...
+ rev = runcmd('git log --oneline --no-abbrev-commit -n1 %s' % rev, ldir).split()[0]
+ # ... and then get the tree in current
+ # one. The commit should be in both repos with
+ # the same tree, but better check here.
+ tree = runcmd('git show -s --pretty=format:%%T %s' % rev).strip()
+ with tempfile.NamedTemporaryFile(mode='wt') as editor:
+ editor.write('''cat >$1 <<EOF
+tree %s
+author %s
+committer %s
+
+%s: squashed import of component
+
+This commit copies the entire set of files as found in
+%s %s
+
+For more information about previous commits, see the
+upstream repository.
+
+Commit created by combo-layer.
+EOF
+''' % (tree, author, committer, name, name, since_rev))
+ editor.flush()
+ os.environ['GIT_EDITOR'] = 'sh %s' % editor.name
+ runcmd('git replace --edit %s' % rev)
+
+ # Optional: rewrite history to change commit messages or to move files.
+ if 'hook' in repo or dest_dir != ".":
+ filter_branch = ['git', 'filter-branch', '--force']
+ with tempfile.NamedTemporaryFile(mode='wt') as hookwrapper:
+ if 'hook' in repo:
+ # Create a shell script wrapper around the original hook that
+ # can be used by git filter-branch. Hook may or may not have
+ # an absolute path.
+ hook = repo['hook']
+ hook = os.path.join(os.path.dirname(conf.conffile), '..', hook)
+ # The wrappers turns the commit message
+ # from stdin into a fake patch header.
+ # This is good enough for changing Subject
+ # and commit msg body with normal
+ # combo-layer hooks.
+ hookwrapper.write('''set -e
+tmpname=$(mktemp)
+trap "rm $tmpname" EXIT
+echo -n 'Subject: [PATCH] ' >>$tmpname
+cat >>$tmpname
+if ! [ $(tail -c 1 $tmpname | od -A n -t x1) == '0a' ]; then
+ echo >>$tmpname
+fi
+echo '---' >>$tmpname
+%s $tmpname $GIT_COMMIT %s
+tail -c +18 $tmpname | head -c -4
+''' % (hook, name))
+ hookwrapper.flush()
+ filter_branch.extend(['--msg-filter', 'bash %s' % hookwrapper.name])
+ if dest_dir != ".":
+ parent = os.path.dirname(dest_dir)
+ if not parent:
+ parent = '.'
+ # May run outside of the current directory, so do not assume that .git exists.
+ filter_branch.extend(['--tree-filter', 'mkdir -p .git/tmptree && find . -mindepth 1 -maxdepth 1 ! -name .git -print0 | xargs -0 -I SOURCE mv SOURCE .git/tmptree && mkdir -p %s && mv .git/tmptree %s' % (parent, dest_dir)])
+ filter_branch.append('HEAD')
+ runcmd(filter_branch)
+ runcmd('git update-ref -d refs/original/refs/heads/%s' % name)
+ repo['rewritten_revision'] = runcmd('git rev-parse HEAD').strip()
+ repo['stripped_revision'] = repo['rewritten_revision']
+ # Optional filter files: remove everything and re-populate using the normal filtering code.
+ # Override any potential .gitignore.
+ if file_filter or exclude_patterns:
+ runcmd('git rm -rf .')
+ if not os.path.exists(extract_dir):
+ os.makedirs(extract_dir)
+ copy_selected_files('HEAD', extract_dir, file_filter, exclude_patterns, '.',
+ subdir=dest_dir)
+ runcmd('git add --all --force .')
+ if runcmd('git status --porcelain'):
+ # Something to commit.
+ runcmd(['git', 'commit', '-m',
+ '''%s: select file subset
+
+Files from the component repository were chosen based on
+the following filters:
+file_filter = %s
+file_exclude = %s''' % (name, file_filter or '<empty>', repo.get('file_exclude', '<empty>'))])
+ repo['stripped_revision'] = runcmd('git rev-parse HEAD').strip()
+
if not lastrev:
- lastrev = runcmd("git rev-parse %s" % initialrev, ldir).strip()
+ lastrev = runcmd('git rev-parse %s' % initialrev, ldir).strip()
conf.update(name, "last_revision", lastrev, initmode=True)
- runcmd("git add .")
+
+ if not conf.history:
+ runcmd("git add .")
+ else:
+ # Create Octopus merge commit according to http://stackoverflow.com/questions/10874149/git-octopus-merge-with-unrelated-repositoies
+ runcmd('git checkout master')
+ merge = ['git', 'merge', '--no-commit']
+ for name in conf.repos:
+ repo = conf.repos[name]
+ # Use branch created earlier.
+ merge.append(name)
+ # Root all commits which have no parent in the common
+ # ancestor in the new repository.
+ for start in runcmd('git log --pretty=format:%%H --max-parents=0 %s --' % name).split('\n'):
+ runcmd('git replace --graft %s %s' % (start, startrev))
+ try:
+ runcmd(merge)
+ except Exception as error:
+ logger.info('''Merging component repository history failed, perhaps because of merge conflicts.
+It may be possible to commit anyway after resolving these conflicts.
+
+%s''' % error)
+ # Create MERGE_HEAD and MERGE_MSG. "git merge" itself
+ # does not create MERGE_HEAD in case of a (harmless) failure,
+ # and we want certain auto-generated information in the
+ # commit message for future reference and/or automation.
+ with open('.git/MERGE_HEAD', 'w') as head:
+ with open('.git/MERGE_MSG', 'w') as msg:
+ msg.write('repo: initial import of components\n\n')
+ # head.write('%s\n' % startrev)
+ for name in conf.repos:
+ repo = conf.repos[name]
+ # <upstream ref> <rewritten ref> <rewritten + files removed>
+ msg.write('combo-layer-%s: %s %s %s\n' % (name,
+ repo['last_revision'],
+ repo['rewritten_revision'],
+ repo['stripped_revision']))
+ rev = runcmd('git rev-parse %s' % name).strip()
+ head.write('%s\n' % rev)
+
if conf.localconffile:
localadded = True
try:
@@ -232,32 +484,32 @@ def check_repo_clean(repodir):
sys.exit(1)
def check_patch(patchfile):
- f = open(patchfile)
+ f = open(patchfile, 'rb')
ln = f.readline()
of = None
in_patch = False
beyond_msg = False
- pre_buf = ''
+ pre_buf = b''
while ln:
if not beyond_msg:
- if ln == '---\n':
+ if ln == b'---\n':
if not of:
break
in_patch = False
beyond_msg = True
- elif ln.startswith('--- '):
+ elif ln.startswith(b'--- '):
# We have a diff in the commit message
in_patch = True
if not of:
print('WARNING: %s contains a diff in its commit message, indenting to avoid failure during apply' % patchfile)
- of = open(patchfile + '.tmp', 'w')
+ of = open(patchfile + '.tmp', 'wb')
of.write(pre_buf)
- pre_buf = ''
- elif in_patch and not ln[0] in '+-@ \n\r':
+ pre_buf = b''
+ elif in_patch and not ln[0] in b'+-@ \n\r':
in_patch = False
if of:
if in_patch:
- of.write(' ' + ln)
+ of.write(b' ' + ln)
else:
of.write(ln)
else:
@@ -269,6 +521,10 @@ def check_patch(patchfile):
os.rename(patchfile + '.tmp', patchfile)
def drop_to_shell(workdir=None):
+ if not sys.stdin.isatty():
+ print("Not a TTY so can't drop to shell for resolution, exiting.")
+ return False
+
shell = os.environ.get('SHELL', 'bash')
print('Dropping to shell "%s"\n' \
'When you are finished, run the following to continue:\n' \
@@ -276,7 +532,7 @@ def drop_to_shell(workdir=None):
' exit 1 -- abort\n' % shell);
ret = subprocess.call([shell], cwd=workdir)
if ret != 0:
- print "Aborting"
+ print("Aborting")
return False
else:
return True
@@ -304,21 +560,20 @@ def check_rev_branch(component, repodir, rev, branch):
return False
return True
-def get_repos(conf, args):
+def get_repos(conf, repo_names):
repos = []
- if len(args) > 1:
- for arg in args[1:]:
- if arg.startswith('-'):
- break
- else:
- repos.append(arg)
- for repo in repos:
- if not repo in conf.repos:
- logger.error("Specified component '%s' not found in configuration" % repo)
- sys.exit(0)
+ for name in repo_names:
+ if name.startswith('-'):
+ break
+ else:
+ repos.append(name)
+ for repo in repos:
+ if not repo in conf.repos:
+ logger.error("Specified component '%s' not found in configuration" % repo)
+ sys.exit(1)
if not repos:
- repos = conf.repos
+ repos = [ repo for repo in conf.repos if conf.repos[repo].get("update", True) ]
return repos
@@ -326,7 +581,7 @@ def action_pull(conf, args):
"""
update the component repos only
"""
- repos = get_repos(conf, args)
+ repos = get_repos(conf, args[1:])
# make sure all repos are clean
for name in repos:
@@ -336,33 +591,85 @@ def action_pull(conf, args):
repo = conf.repos[name]
ldir = repo['local_repo_dir']
branch = repo.get('branch', "master")
- runcmd("git checkout %s" % branch, ldir)
- logger.info("git pull for component repo %s in %s ..." % (name, ldir))
- output=runcmd("git pull", ldir)
- logger.info(output)
+ logger.info("update branch %s of component repo %s in %s ..." % (branch, name, ldir))
+ if not conf.hard_reset:
+ # Try to pull only the configured branch. Beware that this may fail
+ # when the branch is currently unknown (for example, after reconfiguring
+ # combo-layer). In that case we need to fetch everything and try the check out
+ # and pull again.
+ try:
+ runcmd("git checkout %s" % branch, ldir, printerr=False)
+ except subprocess.CalledProcessError:
+ output=runcmd("git fetch", ldir)
+ logger.info(output)
+ runcmd("git checkout %s" % branch, ldir)
+ runcmd("git pull --ff-only", ldir)
+ else:
+ output=runcmd("git pull --ff-only", ldir)
+ logger.info(output)
+ else:
+ output=runcmd("git fetch", ldir)
+ logger.info(output)
+ runcmd("git checkout %s" % branch, ldir)
+ runcmd("git reset --hard FETCH_HEAD", ldir)
def action_update(conf, args):
"""
update the component repos
- generate the patch list
- apply the generated patches
+ either:
+ generate the patch list
+ apply the generated patches
+ or:
+ re-creates the entire component history and merges them
+ into the current branch with a merge commit
"""
- repos = get_repos(conf, args)
+ components = [arg.split(':')[0] for arg in args[1:]]
+ revisions = {}
+ for arg in args[1:]:
+ if ':' in arg:
+ a = arg.split(':', 1)
+ revisions[a[0]] = a[1]
+ repos = get_repos(conf, components)
# make sure combo repo is clean
check_repo_clean(os.getcwd())
- import uuid
- patch_dir = "patch-%s" % uuid.uuid4()
- os.mkdir(patch_dir)
+ # Check whether we keep the component histories. Must be
+ # set either via --history command line parameter or consistently
+ # in combo-layer.conf. Mixing modes is (currently, and probably
+ # permanently because it would be complicated) not supported.
+ if conf.history:
+ history = True
+ else:
+ history = None
+ for name in repos:
+ repo = conf.repos[name]
+ repo_history = repo.get('history', False)
+ if history is None:
+ history = repo_history
+ elif history != repo_history:
+ logger.error("'history' property is set inconsistently")
+ sys.exit(1)
# Step 1: update the component repos
if conf.nopull:
logger.info("Skipping pull (-n)")
else:
- action_pull(conf, args)
+ action_pull(conf, ['arg0'] + components)
+
+ if history:
+ update_with_history(conf, components, revisions, repos)
+ else:
+ update_with_patches(conf, components, revisions, repos)
+
+def update_with_patches(conf, components, revisions, repos):
+ import uuid
+ patch_dir = "patch-%s" % uuid.uuid4()
+ if not os.path.exists(patch_dir):
+ os.mkdir(patch_dir)
for name in repos:
+ revision = revisions.get(name, None)
repo = conf.repos[name]
ldir = repo['local_repo_dir']
dest_dir = repo['dest_dir']
@@ -371,21 +678,31 @@ def action_update(conf, args):
# Step 2: generate the patch list and store to patch dir
logger.info("Generating patches from %s..." % name)
+ top_revision = revision or branch
+ if not check_rev_branch(name, ldir, top_revision, branch):
+ sys.exit(1)
if dest_dir != ".":
prefix = "--src-prefix=a/%s/ --dst-prefix=b/%s/" % (dest_dir, dest_dir)
else:
prefix = ""
if repo['last_revision'] == "":
logger.info("Warning: last_revision of component %s is not set, starting from the first commit" % name)
- patch_cmd_range = "--root %s" % branch
- rev_cmd_range = branch
+ patch_cmd_range = "--root %s" % top_revision
+ rev_cmd_range = top_revision
else:
if not check_rev_branch(name, ldir, repo['last_revision'], branch):
sys.exit(1)
- patch_cmd_range = "%s..%s" % (repo['last_revision'], branch)
+ patch_cmd_range = "%s..%s" % (repo['last_revision'], top_revision)
rev_cmd_range = patch_cmd_range
- file_filter = repo.get('file_filter',"")
+ file_filter = repo.get('file_filter',".")
+
+ # Filter out unwanted files
+ exclude = repo.get('file_exclude', '')
+ if exclude:
+ for path in exclude.split():
+ p = "%s/%s" % (dest_dir, path) if dest_dir != '.' else path
+ file_filter += " ':!%s'" % p
patch_cmd = "git format-patch -N %s --output-directory %s %s -- %s" % \
(prefix,repo_patch_dir, patch_cmd_range, file_filter)
@@ -393,7 +710,7 @@ def action_update(conf, args):
logger.debug("generated patch set:\n%s" % output)
patchlist = output.splitlines()
- rev_cmd = 'git rev-list --no-merges ' + rev_cmd_range
+ rev_cmd = "git rev-list --no-merges %s -- %s" % (rev_cmd_range, file_filter)
revlist = runcmd(rev_cmd, ldir).splitlines()
# Step 3: Call repo specific hook to adjust patch
@@ -420,13 +737,28 @@ def action_update(conf, args):
print('You may now edit the patch and patch list in %s\n' \
'For example, you can remove unwanted patch entries from patchlist-*, so that they will be not applied later' % patch_dir);
if not drop_to_shell(patch_dir):
- sys.exit(0)
+ sys.exit(1)
# Step 6: apply the generated and revised patch
apply_patchlist(conf, repos)
runcmd("rm -rf %s" % patch_dir)
# Step 7: commit the updated config file if it's being tracked
+ commit_conf_file(conf, components)
+
+def conf_commit_msg(conf, components):
+ # create the "components" string
+ component_str = "all components"
+ if len(components) > 0:
+ # otherwise tell which components were actually changed
+ component_str = ", ".join(components)
+
+ # expand the template with known values
+ template = Template(conf.commit_msg_template)
+ msg = template.substitute(components = component_str)
+ return msg
+
+def commit_conf_file(conf, components, commit=True):
relpath = os.path.relpath(conf.conffile)
try:
output = runcmd("git status --porcelain %s" % relpath, printerr=False)
@@ -434,9 +766,15 @@ def action_update(conf, args):
# Outside the repository
output = None
if output:
- logger.info("Committing updated configuration file")
if output.lstrip().startswith("M"):
- runcmd('git commit -m "Automatic commit to update last_revision" %s' % relpath)
+ logger.info("Committing updated configuration file")
+ if commit:
+ msg = conf_commit_msg(conf, components)
+ runcmd('git commit -m'.split() + [msg, relpath])
+ else:
+ runcmd('git add %s' % relpath)
+ return True
+ return False
def apply_patchlist(conf, repos):
"""
@@ -458,6 +796,10 @@ def apply_patchlist(conf, repos):
if line:
patchlist.append(line)
+ ldir = conf.repos[name]['local_repo_dir']
+ branch = conf.repos[name].get('branch', "master")
+ branchrev = runcmd("git rev-parse %s" % branch, ldir).strip()
+
if patchlist:
logger.info("Applying patches from %s..." % name)
linecount = len(patchlist)
@@ -469,7 +811,7 @@ def apply_patchlist(conf, repos):
if os.path.getsize(patchfile) == 0:
logger.info("(skipping %d/%d %s - no changes)" % (i, linecount, patchdisp))
else:
- cmd = "git am --keep-cr -s -p1 %s" % patchfile
+ cmd = "git am --keep-cr %s-p1 %s" % ('-s ' if repo.get('signoff', True) else '', patchfile)
logger.info("Applying %d/%d: %s" % (i, linecount, patchdisp))
try:
runcmd(cmd)
@@ -482,14 +824,31 @@ def apply_patchlist(conf, repos):
if not drop_to_shell():
if prevrev != repo['last_revision']:
conf.update(name, "last_revision", prevrev)
- sys.exit(0)
+ sys.exit(1)
prevrev = lastrev
i += 1
+ # Once all patches are applied, we should update
+ # last_revision to the branch head instead of the last
+ # applied patch. The two are not necessarily the same when
+ # the last commit is a merge commit or when the patches at
+ # the branch head were intentionally excluded.
+ #
+ # If we do not do that for a merge commit, the next
+ # combo-layer run will only exclude patches reachable from
+ # one of the merged branches and try to re-apply patches
+ # from other branches even though they were already
+ # copied.
+ #
+ # If patches were intentionally excluded, the next run will
+ # present them again instead of skipping over them. This
+ # may or may not be intended, so the code here is conservative
+ # and only addresses the "head is merge commit" case.
+ if lastrev != branchrev and \
+ len(runcmd("git show --pretty=format:%%P --no-patch %s" % branch, ldir).split()) > 1:
+ lastrev = branchrev
else:
logger.info("No patches to apply from %s" % name)
- ldir = conf.repos[name]['local_repo_dir']
- branch = conf.repos[name].get('branch', "master")
- lastrev = runcmd("git rev-parse %s" % branch, ldir).strip()
+ lastrev = branchrev
if lastrev != repo['last_revision']:
conf.update(name, "last_revision", lastrev)
@@ -533,6 +892,418 @@ def action_splitpatch(conf, args):
else:
logger.info(patch_filename)
+def update_with_history(conf, components, revisions, repos):
+ '''Update all components with full history.
+
+ Works by importing all commits reachable from a component's
+ current head revision. If those commits are rooted in an already
+ imported commit, their content gets mixed with the content of the
+ combined repo of that commit (new or modified files overwritten,
+ removed files removed).
+
+ The last commit is an artificial merge commit that merges all the
+ updated components into the combined repository.
+
+ The HEAD ref only gets updated at the very end. All intermediate work
+ happens in a worktree which will get garbage collected by git eventually
+ after a failure.
+ '''
+ # Remember current HEAD and what we need to add to it.
+ head = runcmd("git rev-parse HEAD").strip()
+ additional_heads = {}
+
+ # Track the mapping between original commit and commit in the
+ # combined repo. We do not have to distinguish between components,
+ # because commit hashes are different anyway. Often we can
+ # skip find_revs() entirely (for example, when all new commits
+ # are derived from the last imported revision).
+ #
+ # Using "head" (typically the merge commit) instead of the actual
+ # commit for the component leads to a nicer history in the combined
+ # repo.
+ old2new_revs = {}
+ for name in repos:
+ repo = conf.repos[name]
+ revision = repo['last_revision']
+ if revision:
+ old2new_revs[revision] = head
+
+ def add_p(parents):
+ '''Insert -p before each entry.'''
+ parameters = []
+ for p in parents:
+ parameters.append('-p')
+ parameters.append(p)
+ return parameters
+
+ # Do all intermediate work with a separate work dir and index,
+ # chosen via env variables (can't use "git worktree", it is too
+ # new). This is useful (no changes to current work tree unless the
+ # update succeeds) and required (otherwise we end up temporarily
+ # removing the combo-layer hooks that we currently use when
+ # importing a new component).
+ #
+ # Not cleaned up after a failure at the moment.
+ wdir = os.path.join(os.getcwd(), ".git", "combo-layer")
+ windex = wdir + ".index"
+ if os.path.isdir(wdir):
+ shutil.rmtree(wdir)
+ os.mkdir(wdir)
+ wenv = copy.deepcopy(os.environ)
+ wenv["GIT_WORK_TREE"] = wdir
+ wenv["GIT_INDEX_FILE"] = windex
+ # This one turned out to be needed in practice.
+ wenv["GIT_OBJECT_DIRECTORY"] = os.path.join(os.getcwd(), ".git", "objects")
+ wargs = {"destdir": wdir, "env": wenv}
+
+ for name in repos:
+ revision = revisions.get(name, None)
+ repo = conf.repos[name]
+ ldir = repo['local_repo_dir']
+ dest_dir = repo['dest_dir']
+ branch = repo.get('branch', "master")
+ hook = repo.get('hook', None)
+ largs = {"destdir": ldir, "env": None}
+ file_include = repo.get('file_filter', '').split()
+ file_include.sort() # make sure that short entries like '.' come first.
+ file_exclude = repo.get('file_exclude', '').split()
+
+ def include_file(file):
+ if not file_include:
+ # No explicit filter set, include file.
+ return True
+ for filter in file_include:
+ if filter == '.':
+ # Another special case: include current directory and thus all files.
+ return True
+ if os.path.commonprefix((filter, file)) == filter:
+ # Included in directory or direct file match.
+ return True
+ # Check for wildcard match *with* allowing * to match /, i.e.
+ # src/*.c does match src/foobar/*.c. That's not how it is done elsewhere
+ # when passing the filtering to "git archive", but it is unclear what
+ # the intended semantic is (the comment on file_exclude that "append a * wildcard
+ # at the end" to match the full content of a directories implies that
+ # slashes are indeed not special), so here we simply do what's easy to
+ # implement in Python.
+ logger.debug('fnmatch(%s, %s)' % (file, filter))
+ if fnmatch.fnmatchcase(file, filter):
+ return True
+ return False
+
+ def exclude_file(file):
+ for filter in file_exclude:
+ if fnmatch.fnmatchcase(file, filter):
+ return True
+ return False
+
+ def file_filter(files):
+ '''Clean up file list so that only included files remain.'''
+ index = 0
+ while index < len(files):
+ file = files[index]
+ if not include_file(file) or exclude_file(file):
+ del files[index]
+ else:
+ index += 1
+
+
+ # Generate the revision list.
+ logger.info("Analyzing commits from %s..." % name)
+ top_revision = revision or branch
+ if not check_rev_branch(name, ldir, top_revision, branch):
+ sys.exit(1)
+
+ last_revision = repo['last_revision']
+ rev_list_args = "--full-history --sparse --topo-order --reverse"
+ if not last_revision:
+ logger.info("Warning: last_revision of component %s is not set, starting from the first commit" % name)
+ rev_list_args = rev_list_args + ' ' + top_revision
+ else:
+ if not check_rev_branch(name, ldir, last_revision, branch):
+ sys.exit(1)
+ rev_list_args = "%s %s..%s" % (rev_list_args, last_revision, top_revision)
+
+ # By definition, the current HEAD contains the latest imported
+ # commit of each component. We use that as initial mapping even
+ # though the commits do not match exactly because
+ # a) it always works (in contrast to find_revs, which relies on special
+ # commit messages)
+ # b) it is faster than find_revs, which will only be called on demand
+ # and can be skipped entirely