combo-layer 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. #!/usr/bin/env python3
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. #
  5. # Copyright 2011 Intel Corporation
  6. # Authored-by: Yu Ke <ke.yu@intel.com>
  7. # Paul Eggleton <paul.eggleton@intel.com>
  8. # Richard Purdie <richard.purdie@intel.com>
  9. #
  10. # SPDX-License-Identifier: GPL-2.0-only
  11. #
  12. import fnmatch
  13. import os, sys
  14. import optparse
  15. import logging
  16. import subprocess
  17. import tempfile
  18. import configparser
  19. import re
  20. import copy
  21. import shlex
  22. import shutil
  23. from string import Template
  24. from functools import reduce
  25. __version__ = "0.2.1"
  26. def logger_create():
  27. logger = logging.getLogger("")
  28. loggerhandler = logging.StreamHandler()
  29. loggerhandler.setFormatter(logging.Formatter("[%(asctime)s] %(message)s","%H:%M:%S"))
  30. logger.addHandler(loggerhandler)
  31. logger.setLevel(logging.INFO)
  32. return logger
  33. logger = logger_create()
  34. def get_current_branch(repodir=None):
  35. try:
  36. if not os.path.exists(os.path.join(repodir if repodir else '', ".git")):
  37. # Repo not created yet (i.e. during init) so just assume master
  38. return "master"
  39. branchname = runcmd("git symbolic-ref HEAD 2>/dev/null", repodir).strip()
  40. if branchname.startswith("refs/heads/"):
  41. branchname = branchname[11:]
  42. return branchname
  43. except subprocess.CalledProcessError:
  44. return ""
  45. class Configuration(object):
  46. """
  47. Manages the configuration
  48. For an example config file, see combo-layer.conf.example
  49. """
  50. def __init__(self, options):
  51. for key, val in options.__dict__.items():
  52. setattr(self, key, val)
  53. def readsection(parser, section, repo):
  54. for (name, value) in parser.items(section):
  55. if value.startswith("@"):
  56. self.repos[repo][name] = eval(value.strip("@"))
  57. else:
  58. # Apply special type transformations for some properties.
  59. # Type matches the RawConfigParser.get*() methods.
  60. types = {'signoff': 'boolean', 'update': 'boolean', 'history': 'boolean'}
  61. if name in types:
  62. value = getattr(parser, 'get' + types[name])(section, name)
  63. self.repos[repo][name] = value
  64. def readglobalsection(parser, section):
  65. for (name, value) in parser.items(section):
  66. if name == "commit_msg":
  67. self.commit_msg_template = value
  68. logger.debug("Loading config file %s" % self.conffile)
  69. self.parser = configparser.ConfigParser()
  70. with open(self.conffile) as f:
  71. self.parser.read_file(f)
  72. # initialize default values
  73. self.commit_msg_template = "Automatic commit to update last_revision"
  74. self.repos = {}
  75. for repo in self.parser.sections():
  76. if repo == "combo-layer-settings":
  77. # special handling for global settings
  78. readglobalsection(self.parser, repo)
  79. else:
  80. self.repos[repo] = {}
  81. readsection(self.parser, repo, repo)
  82. # Load local configuration, if available
  83. self.localconffile = None
  84. self.localparser = None
  85. self.combobranch = None
  86. if self.conffile.endswith('.conf'):
  87. lcfile = self.conffile.replace('.conf', '-local.conf')
  88. if os.path.exists(lcfile):
  89. # Read combo layer branch
  90. self.combobranch = get_current_branch()
  91. logger.debug("Combo layer branch is %s" % self.combobranch)
  92. self.localconffile = lcfile
  93. logger.debug("Loading local config file %s" % self.localconffile)
  94. self.localparser = configparser.ConfigParser()
  95. with open(self.localconffile) as f:
  96. self.localparser.readfp(f)
  97. for section in self.localparser.sections():
  98. if '|' in section:
  99. sectionvals = section.split('|')
  100. repo = sectionvals[0]
  101. if sectionvals[1] != self.combobranch:
  102. continue
  103. else:
  104. repo = section
  105. if repo in self.repos:
  106. readsection(self.localparser, section, repo)
  107. def update(self, repo, option, value, initmode=False):
  108. # If the main config has the option already, that is what we
  109. # are expected to modify.
  110. if self.localparser and not self.parser.has_option(repo, option):
  111. parser = self.localparser
  112. section = "%s|%s" % (repo, self.combobranch)
  113. conffile = self.localconffile
  114. if initmode and not parser.has_section(section):
  115. parser.add_section(section)
  116. else:
  117. parser = self.parser
  118. section = repo
  119. conffile = self.conffile
  120. parser.set(section, option, value)
  121. with open(conffile, "w") as f:
  122. parser.write(f)
  123. self.repos[repo][option] = value
  124. def sanity_check(self, initmode=False):
  125. required_options=["src_uri", "local_repo_dir", "dest_dir", "last_revision"]
  126. if initmode:
  127. required_options.remove("last_revision")
  128. msg = ""
  129. missing_options = []
  130. for name in self.repos:
  131. for option in required_options:
  132. if option not in self.repos[name]:
  133. msg = "%s\nOption %s is not defined for component %s" %(msg, option, name)
  134. missing_options.append(option)
  135. # Sanitize dest_dir so that we do not have to deal with edge cases
  136. # (unset, empty string, double slashes) in the rest of the code.
  137. # It not being set will still be flagged as error because it is
  138. # listed as required option above; that could be changed now.
  139. dest_dir = os.path.normpath(self.repos[name].get("dest_dir", "."))
  140. self.repos[name]["dest_dir"] = "." if not dest_dir else dest_dir
  141. if msg != "":
  142. logger.error("configuration file %s has the following error: %s" % (self.conffile,msg))
  143. if self.localconffile and 'last_revision' in missing_options:
  144. logger.error("local configuration file %s may be missing configuration for combo branch %s" % (self.localconffile, self.combobranch))
  145. sys.exit(1)
  146. # filterdiff is required by action_splitpatch, so check its availability
  147. if subprocess.call("which filterdiff > /dev/null 2>&1", shell=True) != 0:
  148. logger.error("ERROR: patchutils package is missing, please install it (e.g. # apt-get install patchutils)")
  149. sys.exit(1)
  150. def runcmd(cmd,destdir=None,printerr=True,out=None,env=None):
  151. """
  152. execute command, raise CalledProcessError if fail
  153. return output if succeed
  154. """
  155. logger.debug("run cmd '%s' in %s" % (cmd, os.getcwd() if destdir is None else destdir))
  156. if not out:
  157. out = tempfile.TemporaryFile()
  158. err = out
  159. else:
  160. err = tempfile.TemporaryFile()
  161. try:
  162. subprocess.check_call(cmd, stdout=out, stderr=err, cwd=destdir, shell=isinstance(cmd, str), env=env or os.environ)
  163. except subprocess.CalledProcessError as e:
  164. err.seek(0)
  165. if printerr:
  166. logger.error("%s" % err.read())
  167. raise e
  168. err.seek(0)
  169. output = err.read().decode('utf-8')
  170. logger.debug("output: %s" % output.replace(chr(0), '\\0'))
  171. return output
  172. def action_sync_revs(conf, args):
  173. """
  174. Update the last_revision config option for each repo with the latest
  175. revision in the remote's branch. Useful if multiple people are using
  176. combo-layer.
  177. """
  178. repos = get_repos(conf, args[1:])
  179. for name in repos:
  180. repo = conf.repos[name]
  181. ldir = repo['local_repo_dir']
  182. branch = repo.get('branch', "master")
  183. runcmd("git fetch", ldir)
  184. lastrev = runcmd('git rev-parse origin/%s' % branch, ldir).strip()
  185. print("Updating %s to %s" % (name, lastrev))
  186. conf.update(name, "last_revision", lastrev)
  187. def action_init(conf, args):
  188. """
  189. Clone component repositories
  190. Check git is initialised; if not, copy initial data from component repos
  191. """
  192. for name in conf.repos:
  193. ldir = conf.repos[name]['local_repo_dir']
  194. if not os.path.exists(ldir):
  195. logger.info("cloning %s to %s" %(conf.repos[name]['src_uri'], ldir))
  196. subprocess.check_call("git clone %s %s" % (conf.repos[name]['src_uri'], ldir), shell=True)
  197. if not os.path.exists(".git"):
  198. runcmd("git init")
  199. if conf.history:
  200. # Need a common ref for all trees.
  201. runcmd('git commit -m "initial empty commit" --allow-empty')
  202. startrev = runcmd('git rev-parse master').strip()
  203. for name in conf.repos:
  204. repo = conf.repos[name]
  205. ldir = repo['local_repo_dir']
  206. branch = repo.get('branch', "master")
  207. lastrev = repo.get('last_revision', None)
  208. if lastrev and lastrev != "HEAD":
  209. initialrev = lastrev
  210. if branch:
  211. if not check_rev_branch(name, ldir, lastrev, branch):
  212. sys.exit(1)
  213. logger.info("Copying data from %s at specified revision %s..." % (name, lastrev))
  214. else:
  215. lastrev = None
  216. initialrev = branch
  217. logger.info("Copying data from %s..." % name)
  218. # Sanity check initialrev and turn it into hash (required for copying history,
  219. # because resolving a name ref only works in the component repo).
  220. rev = runcmd('git rev-parse %s' % initialrev, ldir).strip()
  221. if rev != initialrev:
  222. try:
  223. refs = runcmd('git show-ref -s %s' % initialrev, ldir).split('\n')
  224. if len(set(refs)) > 1:
  225. # Happens for example when configured to track
  226. # "master" and there is a refs/heads/master. The
  227. # traditional behavior from "git archive" (preserved
  228. # here) it to choose the first one. This might not be
  229. # intended, so at least warn about it.
  230. logger.warning("%s: initial revision '%s' not unique, picking result of rev-parse = %s" %
  231. (name, initialrev, refs[0]))
  232. initialrev = rev
  233. except:
  234. # show-ref fails for hashes. Skip the sanity warning in that case.
  235. pass
  236. initialrev = rev
  237. dest_dir = repo['dest_dir']
  238. if dest_dir != ".":
  239. extract_dir = os.path.join(os.getcwd(), dest_dir)
  240. if not os.path.exists(extract_dir):
  241. os.makedirs(extract_dir)
  242. else:
  243. extract_dir = os.getcwd()
  244. file_filter = repo.get('file_filter', "")
  245. exclude_patterns = repo.get('file_exclude', '').split()
  246. def copy_selected_files(initialrev, extract_dir, file_filter, exclude_patterns, ldir,
  247. subdir=""):
  248. # When working inside a filtered branch which had the
  249. # files already moved, we need to prepend the
  250. # subdirectory to all filters, otherwise they would
  251. # not match.
  252. if subdir == '.':
  253. subdir = ''
  254. elif subdir:
  255. subdir = os.path.normpath(subdir)
  256. file_filter = ' '.join([subdir + '/' + x for x in file_filter.split()])
  257. exclude_patterns = [subdir + '/' + x for x in exclude_patterns]
  258. # To handle both cases, we cd into the target
  259. # directory and optionally tell tar to strip the path
  260. # prefix when the files were already moved.
  261. subdir_components = len(subdir.split(os.path.sep)) if subdir else 0
  262. strip=('--strip-components=%d' % subdir_components) if subdir else ''
  263. # TODO: file_filter wild cards do not work (and haven't worked before either), because
  264. # a) GNU tar requires a --wildcards parameter before turning on wild card matching.
  265. # b) The semantic is not as intendend (src/*.c also matches src/foo/bar.c,
  266. # in contrast to the other use of file_filter as parameter of "git archive"
  267. # where it only matches .c files directly in src).
  268. files = runcmd("git archive %s %s | tar -x -v %s -C %s %s" %
  269. (initialrev, subdir,
  270. strip, extract_dir, file_filter),
  271. ldir)
  272. if exclude_patterns:
  273. # Implement file removal by letting tar create the
  274. # file and then deleting it in the file system
  275. # again. Uses the list of files created by tar (easier
  276. # than walking the tree).
  277. for file in files.split('\n'):
  278. if file.endswith(os.path.sep):
  279. continue
  280. for pattern in exclude_patterns:
  281. if fnmatch.fnmatch(file, pattern):
  282. os.unlink(os.path.join(*([extract_dir] + ['..'] * subdir_components + [file])))
  283. break
  284. if not conf.history:
  285. copy_selected_files(initialrev, extract_dir, file_filter, exclude_patterns, ldir)
  286. else:
  287. # First fetch remote history into local repository.
  288. # We need a ref for that, so ensure that there is one.
  289. refname = "combo-layer-init-%s" % name
  290. runcmd("git branch -f %s %s" % (refname, initialrev), ldir)
  291. runcmd("git fetch %s %s" % (ldir, refname))
  292. runcmd("git branch -D %s" % refname, ldir)
  293. # Make that the head revision.
  294. runcmd("git checkout -b %s %s" % (name, initialrev))
  295. # Optional: cut the history by replacing the given
  296. # start point(s) with commits providing the same
  297. # content (aka tree), but with commit information that
  298. # makes it clear that this is an artifically created
  299. # commit and nothing the original authors had anything
  300. # to do with.
  301. since_rev = repo.get('since_revision', '')
  302. if since_rev:
  303. committer = runcmd('git var GIT_AUTHOR_IDENT').strip()
  304. # Same time stamp, no name.
  305. author = re.sub('.* (\d+ [+-]\d+)', r'unknown <unknown> \1', committer)
  306. logger.info('author %s' % author)
  307. for rev in since_rev.split():
  308. # Resolve in component repo...
  309. rev = runcmd('git log --oneline --no-abbrev-commit -n1 %s' % rev, ldir).split()[0]
  310. # ... and then get the tree in current
  311. # one. The commit should be in both repos with
  312. # the same tree, but better check here.
  313. tree = runcmd('git show -s --pretty=format:%%T %s' % rev).strip()
  314. with tempfile.NamedTemporaryFile(mode='wt') as editor:
  315. editor.write('''cat >$1 <<EOF
  316. tree %s
  317. author %s
  318. committer %s
  319. %s: squashed import of component
  320. This commit copies the entire set of files as found in
  321. %s %s
  322. For more information about previous commits, see the
  323. upstream repository.
  324. Commit created by combo-layer.
  325. EOF
  326. ''' % (tree, author, committer, name, name, since_rev))
  327. editor.flush()
  328. os.environ['GIT_EDITOR'] = 'sh %s' % editor.name
  329. runcmd('git replace --edit %s' % rev)
  330. # Optional: rewrite history to change commit messages or to move files.
  331. if 'hook' in repo or dest_dir != ".":
  332. filter_branch = ['git', 'filter-branch', '--force']
  333. with tempfile.NamedTemporaryFile(mode='wt') as hookwrapper:
  334. if 'hook' in repo:
  335. # Create a shell script wrapper around the original hook that
  336. # can be used by git filter-branch. Hook may or may not have
  337. # an absolute path.
  338. hook = repo['hook']
  339. hook = os.path.join(os.path.dirname(conf.conffile), '..', hook)
  340. # The wrappers turns the commit message
  341. # from stdin into a fake patch header.
  342. # This is good enough for changing Subject
  343. # and commit msg body with normal
  344. # combo-layer hooks.
  345. hookwrapper.write('''set -e
  346. tmpname=$(mktemp)
  347. trap "rm $tmpname" EXIT
  348. echo -n 'Subject: [PATCH] ' >>$tmpname
  349. cat >>$tmpname
  350. if ! [ $(tail -c 1 $tmpname | od -A n -t x1) == '0a' ]; then
  351. echo >>$tmpname
  352. fi
  353. echo '---' >>$tmpname
  354. %s $tmpname $GIT_COMMIT %s
  355. tail -c +18 $tmpname | head -c -4
  356. ''' % (hook, name))
  357. hookwrapper.flush()
  358. filter_branch.extend(['--msg-filter', 'bash %s' % hookwrapper.name])
  359. if dest_dir != ".":
  360. parent = os.path.dirname(dest_dir)
  361. if not parent:
  362. parent = '.'
  363. # May run outside of the current directory, so do not assume that .git exists.
  364. 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)])
  365. filter_branch.append('HEAD')
  366. runcmd(filter_branch)
  367. runcmd('git update-ref -d refs/original/refs/heads/%s' % name)
  368. repo['rewritten_revision'] = runcmd('git rev-parse HEAD').strip()
  369. repo['stripped_revision'] = repo['rewritten_revision']
  370. # Optional filter files: remove everything and re-populate using the normal filtering code.
  371. # Override any potential .gitignore.
  372. if file_filter or exclude_patterns:
  373. runcmd('git rm -rf .')
  374. if not os.path.exists(extract_dir):
  375. os.makedirs(extract_dir)
  376. copy_selected_files('HEAD', extract_dir, file_filter, exclude_patterns, '.',
  377. subdir=dest_dir)
  378. runcmd('git add --all --force .')
  379. if runcmd('git status --porcelain'):
  380. # Something to commit.
  381. runcmd(['git', 'commit', '-m',
  382. '''%s: select file subset
  383. Files from the component repository were chosen based on
  384. the following filters:
  385. file_filter = %s
  386. file_exclude = %s''' % (name, file_filter or '<empty>', repo.get('file_exclude', '<empty>'))])
  387. repo['stripped_revision'] = runcmd('git rev-parse HEAD').strip()
  388. if not lastrev:
  389. lastrev = runcmd('git rev-parse %s' % initialrev, ldir).strip()
  390. conf.update(name, "last_revision", lastrev, initmode=True)
  391. if not conf.history:
  392. runcmd("git add .")
  393. else:
  394. # Create Octopus merge commit according to http://stackoverflow.com/questions/10874149/git-octopus-merge-with-unrelated-repositoies
  395. runcmd('git checkout master')
  396. merge = ['git', 'merge', '--no-commit']
  397. for name in conf.repos:
  398. repo = conf.repos[name]
  399. # Use branch created earlier.
  400. merge.append(name)
  401. # Root all commits which have no parent in the common
  402. # ancestor in the new repository.
  403. for start in runcmd('git log --pretty=format:%%H --max-parents=0 %s --' % name).split('\n'):
  404. runcmd('git replace --graft %s %s' % (start, startrev))
  405. try:
  406. runcmd(merge)
  407. except Exception as error:
  408. logger.info('''Merging component repository history failed, perhaps because of merge conflicts.
  409. It may be possible to commit anyway after resolving these conflicts.
  410. %s''' % error)
  411. # Create MERGE_HEAD and MERGE_MSG. "git merge" itself
  412. # does not create MERGE_HEAD in case of a (harmless) failure,
  413. # and we want certain auto-generated information in the
  414. # commit message for future reference and/or automation.
  415. with open('.git/MERGE_HEAD', 'w') as head:
  416. with open('.git/MERGE_MSG', 'w') as msg:
  417. msg.write('repo: initial import of components\n\n')
  418. # head.write('%s\n' % startrev)
  419. for name in conf.repos:
  420. repo = conf.repos[name]
  421. # <upstream ref> <rewritten ref> <rewritten + files removed>
  422. msg.write('combo-layer-%s: %s %s %s\n' % (name,
  423. repo['last_revision'],
  424. repo['rewritten_revision'],
  425. repo['stripped_revision']))
  426. rev = runcmd('git rev-parse %s' % name).strip()
  427. head.write('%s\n' % rev)
  428. if conf.localconffile:
  429. localadded = True
  430. try:
  431. runcmd("git rm --cached %s" % conf.localconffile, printerr=False)
  432. except subprocess.CalledProcessError:
  433. localadded = False
  434. if localadded:
  435. localrelpath = os.path.relpath(conf.localconffile)
  436. runcmd("grep -q %s .gitignore || echo %s >> .gitignore" % (localrelpath, localrelpath))
  437. runcmd("git add .gitignore")
  438. logger.info("Added local configuration file %s to .gitignore", localrelpath)
  439. logger.info("Initial combo layer repository data has been created; please make any changes if desired and then use 'git commit' to make the initial commit.")
  440. else:
  441. logger.info("Repository already initialised, nothing to do.")
  442. def check_repo_clean(repodir):
  443. """
  444. check if the repo is clean
  445. exit if repo is dirty
  446. """
  447. output=runcmd("git status --porcelain", repodir)
  448. r = re.compile(r'\?\? patch-.*/')
  449. dirtyout = [item for item in output.splitlines() if not r.match(item)]
  450. if dirtyout:
  451. logger.error("git repo %s is dirty, please fix it first", repodir)
  452. sys.exit(1)
  453. def check_patch(patchfile):
  454. f = open(patchfile, 'rb')
  455. ln = f.readline()
  456. of = None
  457. in_patch = False
  458. beyond_msg = False
  459. pre_buf = b''
  460. while ln:
  461. if not beyond_msg:
  462. if ln == b'---\n':
  463. if not of:
  464. break
  465. in_patch = False
  466. beyond_msg = True
  467. elif ln.startswith(b'--- '):
  468. # We have a diff in the commit message
  469. in_patch = True
  470. if not of:
  471. print('WARNING: %s contains a diff in its commit message, indenting to avoid failure during apply' % patchfile)
  472. of = open(patchfile + '.tmp', 'wb')
  473. of.write(pre_buf)
  474. pre_buf = b''
  475. elif in_patch and not ln[0] in b'+-@ \n\r':
  476. in_patch = False
  477. if of:
  478. if in_patch:
  479. of.write(b' ' + ln)
  480. else:
  481. of.write(ln)
  482. else:
  483. pre_buf += ln
  484. ln = f.readline()
  485. f.close()
  486. if of:
  487. of.close()
  488. os.rename(of.name, patchfile)
  489. def drop_to_shell(workdir=None):
  490. if not sys.stdin.isatty():
  491. print("Not a TTY so can't drop to shell for resolution, exiting.")
  492. return False
  493. shell = os.environ.get('SHELL', 'bash')
  494. print('Dropping to shell "%s"\n' \
  495. 'When you are finished, run the following to continue:\n' \
  496. ' exit -- continue to apply the patches\n' \
  497. ' exit 1 -- abort\n' % shell);
  498. ret = subprocess.call([shell], cwd=workdir)
  499. if ret != 0:
  500. print("Aborting")
  501. return False
  502. else:
  503. return True
  504. def check_rev_branch(component, repodir, rev, branch):
  505. try:
  506. actualbranch = runcmd("git branch --contains %s" % rev, repodir, printerr=False)
  507. except subprocess.CalledProcessError as e:
  508. if e.returncode == 129:
  509. actualbranch = ""
  510. else:
  511. raise
  512. if not actualbranch:
  513. logger.error("%s: specified revision %s is invalid!" % (component, rev))
  514. return False
  515. branches = []
  516. branchlist = actualbranch.split("\n")
  517. for b in branchlist:
  518. branches.append(b.strip().split(' ')[-1])
  519. if branch not in branches:
  520. logger.error("%s: specified revision %s is not on specified branch %s!" % (component, rev, branch))
  521. return False
  522. return True
  523. def get_repos(conf, repo_names):
  524. repos = []
  525. for name in repo_names:
  526. if name.startswith('-'):
  527. break
  528. else:
  529. repos.append(name)
  530. for repo in repos:
  531. if not repo in conf.repos:
  532. logger.error("Specified component '%s' not found in configuration" % repo)
  533. sys.exit(1)
  534. if not repos:
  535. repos = [ repo for repo in conf.repos if conf.repos[repo].get("update", True) ]
  536. return repos
  537. def action_pull(conf, args):
  538. """
  539. update the component repos only
  540. """
  541. repos = get_repos(conf, args[1:])
  542. # make sure all repos are clean
  543. for name in repos:
  544. check_repo_clean(conf.repos[name]['local_repo_dir'])
  545. for name in repos:
  546. repo = conf.repos[name]
  547. ldir = repo['local_repo_dir']
  548. branch = repo.get('branch', "master")
  549. logger.info("update branch %s of component repo %s in %s ..." % (branch, name, ldir))
  550. if not conf.hard_reset:
  551. # Try to pull only the configured branch. Beware that this may fail
  552. # when the branch is currently unknown (for example, after reconfiguring
  553. # combo-layer). In that case we need to fetch everything and try the check out
  554. # and pull again.
  555. try:
  556. runcmd("git checkout %s" % branch, ldir, printerr=False)
  557. except subprocess.CalledProcessError:
  558. output=runcmd("git fetch", ldir)
  559. logger.info(output)
  560. runcmd("git checkout %s" % branch, ldir)
  561. runcmd("git pull --ff-only", ldir)
  562. else:
  563. output=runcmd("git pull --ff-only", ldir)
  564. logger.info(output)
  565. else:
  566. output=runcmd("git fetch", ldir)
  567. logger.info(output)
  568. runcmd("git checkout %s" % branch, ldir)
  569. runcmd("git reset --hard FETCH_HEAD", ldir)
  570. def action_update(conf, args):
  571. """
  572. update the component repos
  573. either:
  574. generate the patch list
  575. apply the generated patches
  576. or:
  577. re-creates the entire component history and merges them
  578. into the current branch with a merge commit
  579. """
  580. components = [arg.split(':')[0] for arg in args[1:]]
  581. revisions = {}
  582. for arg in args[1:]:
  583. if ':' in arg:
  584. a = arg.split(':', 1)
  585. revisions[a[0]] = a[1]
  586. repos = get_repos(conf, components)
  587. # make sure combo repo is clean
  588. check_repo_clean(os.getcwd())
  589. # Check whether we keep the component histories. Must be
  590. # set either via --history command line parameter or consistently
  591. # in combo-layer.conf. Mixing modes is (currently, and probably
  592. # permanently because it would be complicated) not supported.
  593. if conf.history:
  594. history = True
  595. else:
  596. history = None
  597. for name in repos:
  598. repo = conf.repos[name]
  599. repo_history = repo.get('history', False)
  600. if history is None:
  601. history = repo_history
  602. elif history != repo_history:
  603. logger.error("'history' property is set inconsistently")
  604. sys.exit(1)
  605. # Step 1: update the component repos
  606. if conf.nopull:
  607. logger.info("Skipping pull (-n)")
  608. else:
  609. action_pull(conf, ['arg0'] + components)
  610. if history:
  611. update_with_history(conf, components, revisions, repos)
  612. else:
  613. update_with_patches(conf, components, revisions, repos)
  614. def update_with_patches(conf, components, revisions, repos):
  615. import uuid
  616. patch_dir = "patch-%s" % uuid.uuid4()
  617. if not os.path.exists(patch_dir):
  618. os.mkdir(patch_dir)
  619. for name in repos:
  620. revision = revisions.get(name, None)
  621. repo = conf.repos[name]
  622. ldir = repo['local_repo_dir']
  623. dest_dir = repo['dest_dir']
  624. branch = repo.get('branch', "master")
  625. repo_patch_dir = os.path.join(os.getcwd(), patch_dir, name)
  626. # Step 2: generate the patch list and store to patch dir
  627. logger.info("Generating patches from %s..." % name)
  628. top_revision = revision or branch
  629. if not check_rev_branch(name, ldir, top_revision, branch):
  630. sys.exit(1)
  631. if dest_dir != ".":
  632. prefix = "--src-prefix=a/%s/ --dst-prefix=b/%s/" % (dest_dir, dest_dir)
  633. else:
  634. prefix = ""
  635. if repo['last_revision'] == "":
  636. logger.info("Warning: last_revision of component %s is not set, starting from the first commit" % name)
  637. patch_cmd_range = "--root %s" % top_revision
  638. rev_cmd_range = top_revision
  639. else:
  640. if not check_rev_branch(name, ldir, repo['last_revision'], branch):
  641. sys.exit(1)
  642. patch_cmd_range = "%s..%s" % (repo['last_revision'], top_revision)
  643. rev_cmd_range = patch_cmd_range
  644. file_filter = repo.get('file_filter',".")
  645. # Filter out unwanted files
  646. exclude = repo.get('file_exclude', '')
  647. if exclude:
  648. for path in exclude.split():
  649. p = "%s/%s" % (dest_dir, path) if dest_dir != '.' else path
  650. file_filter += " ':!%s'" % p
  651. patch_cmd = "git format-patch -N %s --output-directory %s %s -- %s" % \
  652. (prefix,repo_patch_dir, patch_cmd_range, file_filter)
  653. output = runcmd(patch_cmd, ldir)
  654. logger.debug("generated patch set:\n%s" % output)
  655. patchlist = output.splitlines()
  656. rev_cmd = "git rev-list --no-merges %s -- %s" % (rev_cmd_range, file_filter)
  657. revlist = runcmd(rev_cmd, ldir).splitlines()
  658. # Step 3: Call repo specific hook to adjust patch
  659. if 'hook' in repo:
  660. # hook parameter is: ./hook patchpath revision reponame
  661. count=len(revlist)-1
  662. for patch in patchlist:
  663. runcmd("%s %s %s %s" % (repo['hook'], patch, revlist[count], name))
  664. count=count-1
  665. # Step 4: write patch list and revision list to file, for user to edit later
  666. patchlist_file = os.path.join(os.getcwd(), patch_dir, "patchlist-%s" % name)
  667. repo['patchlist'] = patchlist_file
  668. f = open(patchlist_file, 'w')
  669. count=len(revlist)-1
  670. for patch in patchlist:
  671. f.write("%s %s\n" % (patch, revlist[count]))
  672. check_patch(os.path.join(patch_dir, patch))
  673. count=count-1
  674. f.close()
  675. # Step 5: invoke bash for user to edit patch and patch list
  676. if conf.interactive:
  677. print('You may now edit the patch and patch list in %s\n' \
  678. 'For example, you can remove unwanted patch entries from patchlist-*, so that they will be not applied later' % patch_dir);
  679. if not drop_to_shell(patch_dir):
  680. sys.exit(1)
  681. # Step 6: apply the generated and revised patch
  682. apply_patchlist(conf, repos)
  683. runcmd("rm -rf %s" % patch_dir)
  684. # Step 7: commit the updated config file if it's being tracked
  685. commit_conf_file(conf, components)
  686. def conf_commit_msg(conf, components):
  687. # create the "components" string
  688. component_str = "all components"
  689. if len(components) > 0:
  690. # otherwise tell which components were actually changed
  691. component_str = ", ".join(components)
  692. # expand the template with known values
  693. template = Template(conf.commit_msg_template)
  694. msg = template.substitute(components = component_str)
  695. return msg
  696. def commit_conf_file(conf, components, commit=True):
  697. relpath = os.path.relpath(conf.conffile)
  698. try:
  699. output = runcmd("git status --porcelain %s" % relpath, printerr=False)
  700. except:
  701. # Outside the repository
  702. output = None
  703. if output:
  704. if output.lstrip().startswith("M"):
  705. logger.info("Committing updated configuration file")
  706. if commit:
  707. msg = conf_commit_msg(conf, components)
  708. runcmd('git commit -m'.split() + [msg, relpath])
  709. else:
  710. runcmd('git add %s' % relpath)
  711. return True
  712. return False
  713. def apply_patchlist(conf, repos):
  714. """
  715. apply the generated patch list to combo repo
  716. """
  717. for name in repos:
  718. repo = conf.repos[name]
  719. lastrev = repo["last_revision"]
  720. prevrev = lastrev
  721. # Get non-blank lines from patch list file
  722. patchlist = []
  723. if os.path.exists(repo['patchlist']) or not conf.interactive:
  724. # Note: we want this to fail here if the file doesn't exist and we're not in
  725. # interactive mode since the file should exist in this case
  726. with open(repo['patchlist']) as f:
  727. for line in f:
  728. line = line.rstrip()
  729. if line:
  730. patchlist.append(line)
  731. ldir = conf.repos[name]['local_repo_dir']
  732. branch = conf.repos[name].get('branch', "master")
  733. branchrev = runcmd("git rev-parse %s" % branch, ldir).strip()
  734. if patchlist:
  735. logger.info("Applying patches from %s..." % name)
  736. linecount = len(patchlist)
  737. i = 1
  738. for line in patchlist:
  739. patchfile = line.split()[0]
  740. lastrev = line.split()[1]
  741. patchdisp = os.path.relpath(patchfile)
  742. if os.path.getsize(patchfile) == 0:
  743. logger.info("(skipping %d/%d %s - no changes)" % (i, linecount, patchdisp))
  744. else:
  745. cmd = "git am --keep-cr %s-p1 %s" % ('-s ' if repo.get('signoff', True) else '', patchfile)
  746. logger.info("Applying %d/%d: %s" % (i, linecount, patchdisp))
  747. try:
  748. runcmd(cmd)
  749. except subprocess.CalledProcessError:
  750. logger.info('Running "git am --abort" to cleanup repo')
  751. runcmd("git am --abort")
  752. logger.error('"%s" failed' % cmd)
  753. logger.info("Please manually apply patch %s" % patchdisp)
  754. logger.info("Note: if you exit and continue applying without manually applying the patch, it will be skipped")
  755. if not drop_to_shell():
  756. if prevrev != repo['last_revision']:
  757. conf.update(name, "last_revision", prevrev)
  758. sys.exit(1)
  759. prevrev = lastrev
  760. i += 1
  761. # Once all patches are applied, we should update
  762. # last_revision to the branch head instead of the last
  763. # applied patch. The two are not necessarily the same when
  764. # the last commit is a merge commit or when the patches at
  765. # the branch head were intentionally excluded.
  766. #
  767. # If we do not do that for a merge commit, the next
  768. # combo-layer run will only exclude patches reachable from
  769. # one of the merged branches and try to re-apply patches
  770. # from other branches even though they were already
  771. # copied.
  772. #
  773. # If patches were intentionally excluded, the next run will
  774. # present them again instead of skipping over them. This
  775. # may or may not be intended, so the code here is conservative
  776. # and only addresses the "head is merge commit" case.
  777. if lastrev != branchrev and \
  778. len(runcmd("git show --pretty=format:%%P --no-patch %s" % branch, ldir).split()) > 1:
  779. lastrev = branchrev
  780. else:
  781. logger.info("No patches to apply from %s" % name)
  782. lastrev = branchrev
  783. if lastrev != repo['last_revision']:
  784. conf.update(name, "last_revision", lastrev)
  785. def action_splitpatch(conf, args):
  786. """
  787. generate the commit patch and
  788. split the patch per repo
  789. """
  790. logger.debug("action_splitpatch")
  791. if len(args) > 1:
  792. commit = args[1]
  793. else:
  794. commit = "HEAD"
  795. patchdir = "splitpatch-%s" % commit
  796. if not os.path.exists(patchdir):
  797. os.mkdir(patchdir)
  798. # filerange_root is for the repo whose dest_dir is root "."
  799. # and it should be specified by excluding all other repo dest dir
  800. # like "-x repo1 -x repo2 -x repo3 ..."
  801. filerange_root = ""
  802. for name in conf.repos:
  803. dest_dir = conf.repos[name]['dest_dir']
  804. if dest_dir != ".":
  805. filerange_root = '%s -x "%s/*"' % (filerange_root, dest_dir)
  806. for name in conf.repos:
  807. dest_dir = conf.repos[name]['dest_dir']
  808. patch_filename = "%s/%s.patch" % (patchdir, name)
  809. if dest_dir == ".":
  810. cmd = "git format-patch -n1 --stdout %s^..%s | filterdiff -p1 %s > %s" % (commit, commit, filerange_root, patch_filename)
  811. else:
  812. cmd = "git format-patch --no-prefix -n1 --stdout %s^..%s -- %s > %s" % (commit, commit, dest_dir, patch_filename)
  813. runcmd(cmd)
  814. # Detect empty patches (including those produced by filterdiff above
  815. # that contain only preamble text)
  816. if os.path.getsize(patch_filename) == 0 or runcmd("filterdiff %s" % patch_filename) == "":
  817. os.remove(patch_filename)
  818. logger.info("(skipping %s - no changes)", name)
  819. else:
  820. logger.info(patch_filename)
  821. def update_with_history(conf, components, revisions, repos):
  822. '''Update all components with full history.
  823. Works by importing all commits reachable from a component's
  824. current head revision. If those commits are rooted in an already
  825. imported commit, their content gets mixed with the content of the
  826. combined repo of that commit (new or modified files overwritten,
  827. removed files removed).
  828. The last commit is an artificial merge commit that merges all the
  829. updated components into the combined repository.
  830. The HEAD ref only gets updated at the very end. All intermediate work
  831. happens in a worktree which will get garbage collected by git eventually
  832. after a failure.
  833. '''
  834. # Remember current HEAD and what we need to add to it.
  835. head = runcmd("git rev-parse HEAD").strip()
  836. additional_heads = {}
  837. # Track the mapping between original commit and commit in the
  838. # combined repo. We do not have to distinguish between components,
  839. # because commit hashes are different anyway. Often we can
  840. # skip find_revs() entirely (for example, when all new commits
  841. # are derived from the last imported revision).
  842. #
  843. # Using "head" (typically the merge commit) instead of the actual
  844. # commit for the component leads to a nicer history in the combined
  845. # repo.
  846. old2new_revs = {}
  847. for name in repos:
  848. repo = conf.repos[name]
  849. revision = repo['last_revision']
  850. if revision:
  851. old2new_revs[revision] = head
  852. def add_p(parents):
  853. '''Insert -p before each entry.'''
  854. parameters = []
  855. for p in parents:
  856. parameters.append('-p')
  857. parameters.append(p)
  858. return parameters
  859. # Do all intermediate work with a separate work dir and index,
  860. # chosen via env variables (can't use "git worktree", it is too
  861. # new). This is useful (no changes to current work tree unless the
  862. # update succeeds) and required (otherwise we end up temporarily
  863. # removing the combo-layer hooks that we currently use when
  864. # importing a new component).
  865. #
  866. # Not cleaned up after a failure at the moment.
  867. wdir = os.path.join(os.getcwd(), ".git", "combo-layer")
  868. windex = wdir + ".index"
  869. if os.path.isdir(wdir):
  870. shutil.rmtree(wdir)
  871. os.mkdir(wdir)
  872. wenv = copy.deepcopy(os.environ)
  873. wenv["GIT_WORK_TREE"] = wdir
  874. wenv["GIT_INDEX_FILE"] = windex
  875. # This one turned out to be needed in practice.
  876. wenv["GIT_OBJECT_DIRECTORY"] = os.path.join(os.getcwd(), ".git", "objects")
  877. wargs = {"destdir": wdir, "env": wenv}
  878. for name in repos:
  879. revision = revisions.get(name, None)
  880. repo = conf.repos[name]
  881. ldir = repo['local_repo_dir']
  882. dest_dir = repo['dest_dir']
  883. branch = repo.get('branch', "master")
  884. hook = repo.get('hook', None)
  885. largs = {"destdir": ldir, "env": None}
  886. file_include = repo.get('file_filter', '').split()
  887. file_include.sort() # make sure that short entries like '.' come first.
  888. file_exclude = repo.get('file_exclude', '').split()
  889. def include_file(file):
  890. if not file_include:
  891. # No explicit filter set, include file.
  892. return True
  893. for filter in file_include:
  894. if filter == '.':
  895. # Another special case: include current directory and thus all files.
  896. return True
  897. if os.path.commonprefix((filter, file)) == filter:
  898. # Included in directory or direct file match.
  899. return True
  900. # Check for wildcard match *with* allowing * to match /, i.e.
  901. # src/*.c does match src/foobar/*.c. That's not how it is done elsewhere
  902. # when passing the filtering to "git archive", but it is unclear what
  903. # the intended semantic is (the comment on file_exclude that "append a * wildcard
  904. # at the end" to match the full content of a directories implies that
  905. # slashes are indeed not special), so here we simply do what's easy to
  906. # implement in Python.
  907. logger.debug('fnmatch(%s, %s)' % (file, filter))
  908. if fnmatch.fnmatchcase(file, filter):
  909. return True
  910. return False
  911. def exclude_file(file):
  912. for filter in file_exclude:
  913. if fnmatch.fnmatchcase(file, filter):
  914. return True
  915. return False
  916. def file_filter(files):
  917. '''Clean up file list so that only included files remain.'''
  918. index = 0
  919. while index < len(files):
  920. file = files[index]
  921. if not include_file(file) or exclude_file(file):
  922. del files[index]
  923. else:
  924. index += 1
  925. # Generate the revision list.
  926. logger.info("Analyzing commits from %s..." % name)
  927. top_revision = revision or branch
  928. if not check_rev_branch(name, ldir, top_revision, branch):
  929. sys.exit(1)
  930. last_revision = repo['last_revision']
  931. rev_list_args = "--full-history --sparse --topo-order --reverse"
  932. if not last_revision:
  933. logger.info("Warning: last_revision of component %s is not set, starting from the first commit" % name)
  934. rev_list_args = rev_list_args + ' ' + top_revision
  935. else:
  936. if not check_rev_branch(name, ldir, last_revision, branch):
  937. sys.exit(1)
  938. rev_list_args = "%s %s..%s" % (rev_list_args, last_revision, top_revision)
  939. # By definition, the current HEAD contains the latest imported
  940. # commit of each component. We use that as initial mapping even
  941. # though the commits do not match exactly because
  942. # a) it always works (in contrast to find_revs, which relies on special
  943. # commit messages)
  944. # b) it is faster than find_revs, which will only be called on demand
  945. # and can be skipped entirely in most cases
  946. # c) last but not least, the combined history looks nicer when all
  947. # new commits are rooted in the same merge commit
  948. old2new_revs[last_revision] = head
  949. # We care about all commits (--full-history and --sparse) and
  950. # we want reconstruct the topology and thus do not care
  951. # about ordering by time (--topo-order). We ask for the ones
  952. # we need to import first to be listed first (--reverse).
  953. revs = runcmd("git rev-list %s" % rev_list_args, **largs).split()
  954. logger.debug("To be imported: %s" % revs)
  955. # Now 'revs' contains all revisions reachable from the top revision.
  956. # All revisions derived from the 'last_revision' definitely are new,
  957. # whereas the others may or may not have been imported before. For
  958. # a linear history in the component, that second set will be empty.
  959. # To distinguish between them, we also get the shorter list
  960. # of revisions starting at the ancestor.
  961. if last_revision:
  962. ancestor_revs = runcmd("git rev-list --ancestry-path %s" % rev_list_args, **largs).split()
  963. else:
  964. ancestor_revs = []
  965. logger.debug("Ancestors: %s" % ancestor_revs)
  966. # Now import each revision.
  967. logger.info("Importing commits from %s..." % name)
  968. def import_rev(rev):
  969. global scanned_revs
  970. # If it is part of the new commits, we definitely need
  971. # to import it. Otherwise we need to check, we might have
  972. # imported it before. If it was imported and we merely
  973. # fail to find it because commit messages did not track
  974. # the mapping, then we end up importing it again. So
  975. # combined repos using "updating with history" really should
  976. # enable the "From ... rev:" commit header modifications.
  977. if rev not in ancestor_revs and rev not in old2new_revs and not scanned_revs:
  978. logger.debug("Revision %s triggers log analysis." % rev)
  979. find_revs(old2new_revs, head)
  980. scanned_revs = True
  981. new_rev = old2new_revs.get(rev, None)
  982. if new_rev:
  983. return new_rev
  984. # If the commit is not in the original list of revisions
  985. # to be imported, then it must be a parent of one of those
  986. # commits and it was skipped during earlier imports or not
  987. # found. Importing such merge commits leads to very ugly
  988. # history (long cascade of merge commits which all point
  989. # to to older commits) when switching from "update via
  990. # patches" to "update with history".
  991. #
  992. # We can avoid importing merge commits if all non-merge commits
  993. # reachable from it were already imported. In that case we
  994. # can root the new commits in the current head revision.
  995. def is_imported(prev):
  996. parents = runcmd("git show --no-patch --pretty=format:%P " + prev, **largs).split()
  997. if len(parents) > 1:
  998. for p in parents:
  999. if not is_imported(p):
  1000. logger.debug("Must import %s because %s is not imported." % (rev, p))
  1001. return False
  1002. return True
  1003. elif prev in old2new_revs:
  1004. return True
  1005. else:
  1006. logger.debug("Must import %s because %s is not imported." % (rev, prev))
  1007. return False
  1008. if rev not in revs and is_imported(rev):
  1009. old2new_revs[rev] = head
  1010. return head
  1011. # Need to import rev. Collect some information about it.
  1012. logger.debug("Importing %s" % rev)
  1013. (parents, author_name, author_email, author_timestamp, body) = \
  1014. runcmd("git show --no-patch --pretty=format:%P%x00%an%x00%ae%x00%at%x00%B " + rev, **largs).split(chr(0))
  1015. parents = parents.split()
  1016. if parents:
  1017. # Arbitrarily pick the first parent as base. It may or may not have
  1018. # been imported before. For example, if the parent is a merge commit
  1019. # and previously the combined repository used patching as update
  1020. # method, then the actual merge commit parent never was imported.
  1021. # To cover this, We recursively import parents.
  1022. parent = parents[0]
  1023. new_parent = import_rev(parent)
  1024. # Clean index and working tree. TODO: can we combine this and the
  1025. # next into one command with less file IO?
  1026. # "git reset --hard" does not work, it changes HEAD of the parent
  1027. # repo, which we wanted to avoid. Probably need to keep
  1028. # track of the rev that corresponds to the index and use apply_commit().
  1029. runcmd("git rm -q --ignore-unmatch -rf .", **wargs)
  1030. # Update index and working tree to match the parent.
  1031. runcmd("git checkout -q -f %s ." % new_parent, **wargs)
  1032. else:
  1033. parent = None
  1034. # Clean index and working tree.
  1035. runcmd("git rm -q --ignore-unmatch -rf .", **wargs)
  1036. # Modify index and working tree such that it mirrors the commit.
  1037. apply_commit(parent, rev, largs, wargs, dest_dir, file_filter=file_filter)
  1038. # Now commit.
  1039. new_tree = runcmd("git write-tree", **wargs).strip()
  1040. env = copy.deepcopy(wenv)
  1041. env['GIT_AUTHOR_NAME'] = author_name
  1042. env['GIT_AUTHOR_EMAIL'] = author_email
  1043. env['GIT_AUTHOR_DATE'] = author_timestamp
  1044. if hook:
  1045. # Need to turn the verbatim commit message into something resembling a patch header
  1046. # for the hook.
  1047. with tempfile.NamedTemporaryFile(mode='wt', delete=False) as patch:
  1048. patch.write('Subject: [PATCH] ')
  1049. patch.write(body)
  1050. patch.write('\n---\n')
  1051. patch.close()
  1052. runcmd([hook, patch.name, rev, name])
  1053. with open(patch.name) as f:
  1054. body = f.read()[len('Subject: [PATCH] '):][:-len('\n---\n')]
  1055. # We can skip non-merge commits that did not change any files. Those are typically
  1056. # the result of file filtering, although they could also have been introduced
  1057. # intentionally upstream, in which case we drop some information here.
  1058. if len(parents) == 1:
  1059. parent_rev = import_rev(parents[0])
  1060. old_tree = runcmd("git show -s --pretty=format:%T " + parent_rev, **wargs).strip()
  1061. commit = old_tree != new_tree
  1062. if not commit:
  1063. new_rev = parent_rev
  1064. else:
  1065. commit = True
  1066. if commit:
  1067. new_rev = runcmd("git commit-tree".split() + add_p([import_rev(p) for p in parents]) +
  1068. ["-m", body, new_tree],
  1069. env=env).strip()
  1070. old2new_revs[rev] = new_rev
  1071. return new_rev
  1072. if revs:
  1073. for rev in revs:
  1074. import_rev(rev)
  1075. # Remember how to update our current head. New components get added,
  1076. # updated components get the delta between current head and the updated component
  1077. # applied.
  1078. additional_heads[old2new_revs[revs[-1]]] = head if repo['last_revision'] else None
  1079. repo['last_revision'] = revs[-1]
  1080. # Now construct the final merge commit. We create the tree by
  1081. # starting with the head and applying the changes from each
  1082. # components imported head revision.
  1083. if additional_heads:
  1084. runcmd("git reset --hard", **wargs)
  1085. for rev, base in additional_heads.items():
  1086. apply_commit(base, rev, wargs, wargs, None)
  1087. # Commit with all component branches as parents as well as the previous head.
  1088. logger.info("Writing final merge commit...")
  1089. msg = conf_commit_msg(conf, components)
  1090. new_tree = runcmd("git write-tree", **wargs).strip()
  1091. new_rev = runcmd("git commit-tree".split() +
  1092. add_p([head] + list(additional_heads.keys())) +
  1093. ["-m", msg, new_tree],
  1094. **wargs).strip()
  1095. # And done! This is the first time we change the HEAD in the actual work tree.
  1096. runcmd("git reset --hard %s" % new_rev)
  1097. # Update and stage the (potentially modified)
  1098. # combo-layer.conf, but do not commit separately.
  1099. for name in repos:
  1100. repo = conf.repos[name]
  1101. rev = repo['last_revision']
  1102. conf.update(name, "last_revision", rev)
  1103. if commit_conf_file(conf, components, False):
  1104. # Must augment the previous commit.
  1105. runcmd("git commit --amend -C HEAD")
  1106. scanned_revs = False
  1107. def find_revs(old2new, head):
  1108. '''Construct mapping from original commit hash to commit hash in
  1109. combined repo by looking at the commit messages. Depends on the
  1110. "From ... rev: ..." convention.'''
  1111. logger.info("Analyzing log messages to find previously imported commits...")
  1112. num_known = len(old2new)
  1113. log = runcmd("git log --grep='From .* rev: [a-fA-F0-9][a-fA-F0-9]*' --pretty=format:%H%x00%B%x00 " + head).split(chr(0))
  1114. regex = re.compile(r'From .* rev: ([a-fA-F0-9]+)')
  1115. for new_rev, body in zip(*[iter(log)]* 2):
  1116. # Use the last one, in the unlikely case there are more than one.
  1117. rev = regex.findall(body)[-1]
  1118. if rev not in old2new:
  1119. old2new[rev] = new_rev.strip()
  1120. logger.info("Found %d additional commits, leading to: %s" % (len(old2new) - num_known, old2new))
  1121. def apply_commit(parent, rev, largs, wargs, dest_dir, file_filter=None):
  1122. '''Compare revision against parent, remove files deleted in the
  1123. commit, re-write new or modified ones. Moves them into dest_dir.
  1124. Optionally filters files.
  1125. '''
  1126. if not dest_dir:
  1127. dest_dir = "."
  1128. # -r recurses into sub-directories, given is the full overview of
  1129. # what changed. We do not care about copy/edits or renames, so we
  1130. # can disable those with --no-renames (but we still parse them,
  1131. # because it was not clear from git documentation whether C and M
  1132. # lines can still occur).
  1133. logger.debug("Applying changes between %s and %s in %s" % (parent, rev, largs["destdir"]))
  1134. delete = []
  1135. update = []
  1136. if parent:
  1137. # Apply delta.
  1138. changes = runcmd("git diff-tree --no-commit-id --no-renames --name-status -r --raw -z %s %s" % (parent, rev), **largs).split(chr(0))
  1139. for status, name in zip(*[iter(changes)]*2):
  1140. if status[0] in "ACMRT":
  1141. update.append(name)
  1142. elif status[0] in "D":
  1143. delete.append(name)
  1144. else:
  1145. logger.error("Unknown status %s of file %s in revision %s" % (status, name, rev))
  1146. sys.exit(1)
  1147. else:
  1148. # Copy all files.
  1149. update.extend(runcmd("git ls-tree -r --name-only -z %s" % rev, **largs).split(chr(0)))
  1150. # Include/exclude files as define in the component config.
  1151. # Both updated and deleted file lists get filtered, because it might happen
  1152. # that a file gets excluded, pulled from a different component, and then the
  1153. # excluded file gets deleted. In that case we must keep the copy.
  1154. if file_filter:
  1155. file_filter(update)
  1156. file_filter(delete)
  1157. # We export into a tar archive here and extract with tar because it is simple (no
  1158. # need to implement file and symlink writing ourselves) and gives us some degree
  1159. # of parallel IO. The downside is that we have to pass the list of files via
  1160. # command line parameters - hopefully there will never be too many at once.
  1161. if update:
  1162. target = os.path.join(wargs["destdir"], dest_dir)
  1163. if not os.path.isdir(target):
  1164. os.makedirs(target)
  1165. quoted_target = shlex.quote(target)
  1166. # os.sysconf('SC_ARG_MAX') is lying: running a command with
  1167. # string length 629343 already failed with "Argument list too
  1168. # long" although SC_ARG_MAX = 2097152. "man execve" explains
  1169. # the limitations, but those are pretty complicated. So here
  1170. # we just hard-code a fixed value which is more likely to work.
  1171. max_cmdsize = 64 * 1024
  1172. while update:
  1173. quoted_args = []
  1174. unquoted_args = []
  1175. cmdsize = 100 + len(quoted_target)
  1176. while update:
  1177. quoted_next = shlex.quote(update[0])
  1178. size_next = len(quoted_next) + len(dest_dir) + 1
  1179. logger.debug('cmdline length %d + %d < %d?' % (cmdsize, size_next, os.sysconf('SC_ARG_MAX')))
  1180. if cmdsize + size_next < max_cmdsize:
  1181. quoted_args.append(quoted_next)
  1182. unquoted_args.append(update.pop(0))
  1183. cmdsize += size_next
  1184. else:
  1185. logger.debug('Breaking the cmdline at length %d' % cmdsize)
  1186. break
  1187. logger.debug('Final cmdline length %d / %d' % (cmdsize, os.sysconf('SC_ARG_MAX')))
  1188. cmd = "git archive %s %s | tar -C %s -xf -" % (rev, ' '.join(quoted_args), quoted_target)
  1189. logger.debug('First cmdline length %d' % len(cmd))
  1190. runcmd(cmd, **largs)
  1191. cmd = "git add -f".split() + [os.path.join(dest_dir, x) for x in unquoted_args]
  1192. logger.debug('Second cmdline length %d' % reduce(lambda x, y: x + len(y), cmd, 0))
  1193. runcmd(cmd, **wargs)
  1194. if delete:
  1195. for path in delete:
  1196. if dest_dir:
  1197. path = os.path.join(dest_dir, path)
  1198. runcmd("git rm -f --ignore-unmatch".split() + [os.path.join(dest_dir, x) for x in delete], **wargs)
  1199. def action_error(conf, args):
  1200. logger.info("invalid action %s" % args[0])
  1201. actions = {
  1202. "init": action_init,
  1203. "update": action_update,
  1204. "pull": action_pull,
  1205. "splitpatch": action_splitpatch,
  1206. "sync-revs": action_sync_revs,
  1207. }
  1208. def main():
  1209. parser = optparse.OptionParser(
  1210. version = "Combo Layer Repo Tool version %s" % __version__,
  1211. usage = """%prog [options] action
  1212. Create and update a combination layer repository from multiple component repositories.
  1213. Action:
  1214. init initialise the combo layer repo
  1215. update [components] get patches from component repos and apply them to the combo repo
  1216. pull [components] just pull component repos only
  1217. sync-revs [components] update the config file's last_revision for each repository
  1218. splitpatch [commit] generate commit patch and split per component, default commit is HEAD""")
  1219. parser.add_option("-c", "--conf", help = "specify the config file (conf/combo-layer.conf is the default).",
  1220. action = "store", dest = "conffile", default = "conf/combo-layer.conf")
  1221. parser.add_option("-i", "--interactive", help = "interactive mode, user can edit the patch list and patches",
  1222. action = "store_true", dest = "interactive", default = False)
  1223. parser.add_option("-D", "--debug", help = "output debug information",
  1224. action = "store_true", dest = "debug", default = False)
  1225. parser.add_option("-n", "--no-pull", help = "skip pulling component repos during update",
  1226. action = "store_true", dest = "nopull", default = False)
  1227. parser.add_option("--hard-reset",
  1228. help = "instead of pull do fetch and hard-reset in component repos",
  1229. action = "store_true", dest = "hard_reset", default = False)
  1230. parser.add_option("-H", "--history", help = "import full history of components during init",
  1231. action = "store_true", default = False)
  1232. options, args = parser.parse_args(sys.argv)
  1233. # Dispatch to action handler
  1234. if len(args) == 1:
  1235. logger.error("No action specified, exiting")
  1236. parser.print_help()
  1237. elif args[1] not in actions:
  1238. logger.error("Unsupported action %s, exiting\n" % (args[1]))
  1239. parser.print_help()
  1240. elif not os.path.exists(options.conffile):
  1241. logger.error("No valid config file, exiting\n")
  1242. parser.print_help()
  1243. else:
  1244. if options.debug:
  1245. logger.setLevel(logging.DEBUG)
  1246. confdata = Configuration(options)
  1247. initmode = (args[1] == 'init')
  1248. confdata.sanity_check(initmode)
  1249. actions.get(args[1], action_error)(confdata, args[1:])
  1250. if __name__ == "__main__":
  1251. try:
  1252. ret = main()
  1253. except Exception:
  1254. ret = 1
  1255. import traceback
  1256. traceback.print_exc()
  1257. sys.exit(ret)