diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -831,6 +831,22 @@ def service(opts, parentfn=None, initfn= if runfn: return runfn() +## facility to let extension process additional data into an import patch +# list of identifier to be executed in order +extrapreimport = [] # run before commit +# mapping from identifier to actual import function +# +# 'preimport' are run before the commit is made and are provided the following +# arguments: +# - repo: the localrepository instance, +# - patchdata: data extracted from patch header (cf m.patch.patchheadermap), +# - extra: the future extra dictionnary of the changeset, please mutate it, +# - opts: the import options. +# XXX ideally, we would just pass an ctx ready to be computed, that would allow +# mutation of in memory commit and more. Feel free to rework the code to get +# there. +extrapreimportmap = {} + def tryimportone(ui, repo, hunk, parents, opts, msgs, updatefunc): """Utility function used by commands.import to import a single patch @@ -949,12 +965,15 @@ def tryimportone(ui, repo, hunk, parents else: editor = getcommiteditor(editform=editform, **opts) allowemptyback = repo.ui.backupconfig('ui', 'allowemptycommit') + extra = {} + for idfunc in extrapreimport: + extrapreimportmap[idfunc](repo, extractdata, extra, opts) try: if partial: repo.ui.setconfig('ui', 'allowemptycommit', True) n = repo.commit(message, opts.get('user') or user, opts.get('date') or date, match=m, - editor=editor) + editor=editor, extra=extra) finally: repo.ui.restoreconfig(allowemptyback) dsguard.close() diff --git a/tests/test-import.t b/tests/test-import.t --- a/tests/test-import.t +++ b/tests/test-import.t @@ -1505,3 +1505,43 @@ Importing multiple failing patches: $ hg status -c . C a C b + +Importing some extra header +=========================== + + $ cat > $TESTTMP/parseextra.py < import mercurial.patch + > import mercurial.cmdutil + > + > def processfoo(repo, data, extra, opts): + > if 'foo' in data: + > extra['foo'] = data['foo'] + > + > mercurial.patch.patchheadermap.append(('Foo', 'foo')) + > mercurial.cmdutil.extrapreimport.append('foo') + > mercurial.cmdutil.extrapreimportmap['foo'] = processfoo + > EOF + $ printf "[extensions]\nparseextra=$TESTTMP/parseextra.py" >> $HGRCPATH + $ hg up -C tip + 0 files updated, 0 files merged, 0 files removed, 0 files unresolved + $ cat > $TESTTMP/foo.patch < # HG changeset patch + > # User Rataxes + > # Date 0 0 + > # Thu Jan 01 00:00:00 1970 +0000 + > # Foo bar + > height + > + > --- a/a Thu Jan 01 00:00:00 1970 +0000 + > +++ b/a Wed Oct 07 09:17:44 2015 +0000 + > @@ -5,3 +5,4 @@ + > five + > six + > seven + > +heigt + > EOF + $ hg import $TESTTMP/foo.patch + applying $TESTTMP/foo.patch + $ hg log --debug -r . | grep extra + extra: branch=default + extra: foo=bar