hgext/mq.py
changeset 3141 e21337e06952
parent 3128 15fde1970003
child 3176 0e6b58c7beea
equal deleted inserted replaced
3140:1eb50c9d2649 3141:e21337e06952
  1274             return 1
  1274             return 1
  1275         else:
  1275         else:
  1276             self.ui.write("No patches applied\n")
  1276             self.ui.write("No patches applied\n")
  1277             return 1
  1277             return 1
  1278 
  1278 
  1279     def qimport(self, repo, files, patchname=None, existing=None, force=None):
  1279     def qimport(self, repo, files, patchname=None, rev=None, existing=None,
  1280         if len(files) > 1 and patchname:
  1280                 force=None):
       
  1281         def checkseries(patchname):
       
  1282             if patchname in self.series:
       
  1283                 raise util.Abort(_('patch %s is already in the series file')
       
  1284                                  % patchname)
       
  1285         def checkfile(patchname):
       
  1286             if not force and os.path.exists(self.join(patchname)):
       
  1287                 raise util.Abort(_('patch "%s" already exists')
       
  1288                                  % patchname)
       
  1289 
       
  1290         if rev:
       
  1291             if files:
       
  1292                 raise util.Abort(_('option "-r" not valid when importing '
       
  1293                                    'files'))
       
  1294             rev = [int(r) for r in cmdutil.revrange(self.ui, repo, rev)]
       
  1295             rev.sort(lambda x, y: cmp(y, x))
       
  1296         if (len(files) > 1 or len(rev) > 1) and patchname:
  1281             raise util.Abort(_('option "-n" not valid when importing multiple '
  1297             raise util.Abort(_('option "-n" not valid when importing multiple '
  1282                                'files'))
  1298                                'patches'))
  1283         i = 0
  1299         i = 0
  1284         added = []
  1300         added = []
       
  1301         if rev:
       
  1302             # If mq patches are applied, we can only import revisions
       
  1303             # that form a linear path to qbase.
       
  1304             # Otherwise, they should form a linear path to a head.
       
  1305             heads = repo.changelog.heads(repo.changelog.node(rev[-1]))
       
  1306             if len(heads) > 1:
       
  1307                 raise util.Abort(_('revision %d is the root of more than one '
       
  1308                                    'branch') % rev[-1])
       
  1309             if self.applied:
       
  1310                 base = revlog.hex(repo.changelog.node(rev[0]))
       
  1311                 if base in [n.rev for n in self.applied]:
       
  1312                     raise util.Abort(_('revision %d is already managed')
       
  1313                                      % rev[0])
       
  1314                 if heads != [revlog.bin(self.applied[-1].rev)]:
       
  1315                     raise util.Abort(_('revision %d is not the parent of '
       
  1316                                        'the queue') % rev[0])
       
  1317                 base = repo.changelog.rev(revlog.bin(self.applied[0].rev))
       
  1318                 lastparent = repo.changelog.parentrevs(base)[0]
       
  1319             else:
       
  1320                 if heads != [repo.changelog.node(rev[0])]:
       
  1321                     raise util.Abort(_('revision %d has unmanaged children')
       
  1322                                      % rev[0])
       
  1323                 lastparent = None
       
  1324 
       
  1325             for r in rev:
       
  1326                 p1, p2 = repo.changelog.parentrevs(r)
       
  1327                 n = repo.changelog.node(r)
       
  1328                 if p2 != -1:
       
  1329                     raise util.Abort(_('cannot import merge revision %d') % r)
       
  1330                 if lastparent and lastparent != r:
       
  1331                     raise util.Abort(_('revision %d is not the parent of %d')
       
  1332                                      % (r, lastparent))
       
  1333                 lastparent = p1
       
  1334 
       
  1335                 if not patchname:
       
  1336                     patchname = '%d.diff' % r
       
  1337                 checkseries(patchname)
       
  1338                 checkfile(patchname)
       
  1339                 self.full_series.insert(0, patchname)
       
  1340 
       
  1341                 patchf = self.opener(patchname, "w")
       
  1342                 patch.export(repo, [n], fp=patchf, opts=self.diffopts())
       
  1343                 patchf.close()
       
  1344 
       
  1345                 se = statusentry(revlog.hex(n), patchname)
       
  1346                 self.applied.insert(0, se)
       
  1347 
       
  1348                 added.append(patchname)
       
  1349                 patchname = None
       
  1350             self.parse_series()
       
  1351             self.applied_dirty = 1
       
  1352 
  1285         for filename in files:
  1353         for filename in files:
  1286             if existing:
  1354             if existing:
  1287                 if not patchname:
  1355                 if not patchname:
  1288                     patchname = filename
  1356                     patchname = filename
  1289                 if not os.path.isfile(self.join(patchname)):
  1357                 if not os.path.isfile(self.join(patchname)):
  1293                     text = file(filename).read()
  1361                     text = file(filename).read()
  1294                 except IOError:
  1362                 except IOError:
  1295                     raise util.Abort(_("unable to read %s") % patchname)
  1363                     raise util.Abort(_("unable to read %s") % patchname)
  1296                 if not patchname:
  1364                 if not patchname:
  1297                     patchname = os.path.basename(filename)
  1365                     patchname = os.path.basename(filename)
  1298                 if not force and os.path.exists(self.join(patchname)):
  1366                 checkfile(patchname)
  1299                     raise util.Abort(_('patch "%s" already exists')
       
  1300                                      % patchname)
       
  1301                 patchf = self.opener(patchname, "w")
  1367                 patchf = self.opener(patchname, "w")
  1302                 patchf.write(text)
  1368                 patchf.write(text)
  1303             if patchname in self.series:
  1369             checkseries(patchname)
  1304                 raise util.Abort(_('patch %s is already in the series file')
       
  1305                                  % patchname)
       
  1306             index = self.full_series_end() + i
  1370             index = self.full_series_end() + i
  1307             self.full_series[index:index] = [patchname]
  1371             self.full_series[index:index] = [patchname]
  1308             self.parse_series()
  1372             self.parse_series()
  1309             self.ui.warn("adding %s to series file\n" % patchname)
  1373             self.ui.warn("adding %s to series file\n" % patchname)
  1310             i += 1
  1374             i += 1
  1341         if ui.verbose:
  1405         if ui.verbose:
  1342             ui.write("%d " % i)
  1406             ui.write("%d " % i)
  1343         ui.write("%s\n" % p)
  1407         ui.write("%s\n" % p)
  1344 
  1408 
  1345 def qimport(ui, repo, *filename, **opts):
  1409 def qimport(ui, repo, *filename, **opts):
  1346     """import a patch"""
  1410     """import a patch
       
  1411 
       
  1412     The patch will have the same name as its source file unless you
       
  1413     give it a new one with --name.
       
  1414 
       
  1415     You can register an existing patch inside the patch directory
       
  1416     with the --existing flag.
       
  1417 
       
  1418     With --force, an existing patch of the same name will be overwritten.
       
  1419 
       
  1420     An existing changeset may be placed under mq control with --rev
       
  1421     (e.g. qimport --rev tip -n patch will place tip under mq control).
       
  1422     """
  1347     q = repo.mq
  1423     q = repo.mq
  1348     q.qimport(repo, filename, patchname=opts['name'],
  1424     q.qimport(repo, filename, patchname=opts['name'],
  1349               existing=opts['existing'], force=opts['force'])
  1425               existing=opts['existing'], force=opts['force'], rev=opts['rev'])
  1350     q.save_dirty()
  1426     q.save_dirty()
  1351     return 0
  1427     return 0
  1352 
  1428 
  1353 def init(ui, repo, **opts):
  1429 def init(ui, repo, **opts):
  1354     """init a new queue repository
  1430     """init a new queue repository
  1951                 _('hg qheader [PATCH]')),
  2027                 _('hg qheader [PATCH]')),
  1952     "^qimport":
  2028     "^qimport":
  1953         (qimport,
  2029         (qimport,
  1954          [('e', 'existing', None, 'import file in patch dir'),
  2030          [('e', 'existing', None, 'import file in patch dir'),
  1955           ('n', 'name', '', 'patch file name'),
  2031           ('n', 'name', '', 'patch file name'),
  1956           ('f', 'force', None, 'overwrite existing files')],
  2032           ('f', 'force', None, 'overwrite existing files'),
  1957          'hg qimport [-e] [-n NAME] [-f] FILE...'),
  2033           ('r', 'rev', [], 'place existing revisions under mq control')],
       
  2034          'hg qimport [-e] [-n NAME] [-f] [-r REV]... FILE...'),
  1958     "^qinit":
  2035     "^qinit":
  1959         (init,
  2036         (init,
  1960          [('c', 'create-repo', None, 'create queue repository')],
  2037          [('c', 'create-repo', None, 'create queue repository')],
  1961          'hg qinit [-c]'),
  2038          'hg qinit [-c]'),
  1962     "qnew":
  2039     "qnew":