mutable-history / hgext / evolve.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# states.py - introduce the state concept for mercurial changeset
#
# Copyright 2011 Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
#                Logilab SA        <contact@logilab.fr>
#                Pierre-Yves David <pierre-yves.david@ens-lyon.org>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

'''A set of command to make changeset evolve.'''

from mercurial import cmdutil
from mercurial import scmutil
from mercurial import node
from mercurial import error
from mercurial import extensions
from mercurial import commands
from mercurial import bookmarks
from mercurial import phases
from mercurial import commands
from mercurial import context
from mercurial import copies
from mercurial import util
from mercurial.i18n import _
from mercurial.commands import walkopts, commitopts, commitopts2, logopts
from mercurial import hg

### util function
#############################
def noderange(repo, revsets):
    """The same as revrange but return node"""
    return map(repo.changelog.node,
               scmutil.revrange(repo, revsets))



def warnunstable(orig, ui, repo, *args, **kwargs):
    """display warning is the command resulted in more instable changeset"""
    priorunstables = len(repo.revs('unstable()'))
    #print orig, priorunstables
    #print len(repo.revs('secret() - obsolete()'))
    try:
        return orig(ui, repo, *args, **kwargs)
    finally:
        newunstables = len(repo.revs('unstable()')) - priorunstables
        #print orig, newunstables
        #print len(repo.revs('secret() - obsolete()'))
        if newunstables > 0:
            ui.warn(_('%i new unstables changesets\n') % newunstables)


### extension check
#############################


### changeset rewriting logic
#############################

def rewrite(repo, old, updates, head, newbases, commitopts):
    if len(old.parents()) > 1: #XXX remove this unecessary limitation.
        raise error.Abort(_('cannot amend merge changesets'))
    base = old.p1()
    bm = bookmarks.readcurrent(repo)

    wlock = repo.wlock()
    try:

        # commit a new version of the old changeset, including the update
        # collect all files which might be affected
        files = set(old.files())
        for u in updates:
            files.update(u.files())

        # Recompute copies (avoid recording a -> b -> a)
        copied = copies.pathcopies(base, head)


        # prune files which were reverted by the updates
        def samefile(f):
            if f in head.manifest():
                a = head.filectx(f)
                if f in base.manifest():
                    b = base.filectx(f)
                    return (a.data() == b.data()
                            and a.flags() == b.flags())
                else:
                    return False
            else:
                return f not in base.manifest()
        files = [f for f in files if not samefile(f)]
        # commit version of these files as defined by head
        headmf = head.manifest()
        def filectxfn(repo, ctx, path):
            if path in headmf:
                fctx = head[path]
                flags = fctx.flags()
                mctx = context.memfilectx(fctx.path(), fctx.data(),
                                          islink='l' in flags,
                                          isexec='x' in flags,
                                          copied=copied.get(path))
                return mctx
            raise IOError()
        if commitopts.get('message') and commitopts.get('logfile'):
            raise util.Abort(_('options --message and --logfile are mutually'
                               ' exclusive'))
        if commitopts.get('logfile'):
            message= open(commitopts['logfile']).read()
        elif commitopts.get('message'):
            message = commitopts['message']
        else:
            message = old.description()

        user = commitopts.get('user') or old.user()
        date = commitopts.get('date') or None # old.date()

        new = context.memctx(repo,
                             parents=newbases,
                             text=message,
                             files=files,
                             filectxfn=filectxfn,
                             user=user,
                             date=date,
                             extra=commitopts.get('extra') or None)

        if commitopts.get('edit'):
            new._text = cmdutil.commitforceeditor(repo, new, [])
        newid = repo.commitctx(new)
        new = repo[newid]

        # update the bookmark
        if bm:
            repo._bookmarks[bm] = newid
            bookmarks.write(repo)

        # add evolution metadata
        repo.addobsolete(new.node(), old.node())
        for u in updates:
            repo.addobsolete(u.node(), old.node())
            repo.addobsolete(new.node(), u.node())
        oldbookmarks = repo.nodebookmarks(old.node())
        for book in oldbookmarks:
            repo._bookmarks[book] = new.node()
        if oldbookmarks:
            bookmarks.write(repo)

    finally:
        wlock.release()

    return newid

def relocate(repo, orig, dest):
    """rewrite <rev> on dest"""
    try:
        rebase = extensions.find('rebase')
        # dummy state to trick rebase node
        assert orig.p2().rev() == node.nullrev, 'no support yet'
        destbookmarks = repo.nodebookmarks(dest.node())
        cmdutil.duplicatecopies(repo, orig.node(), dest.node())
        rebase.rebasenode(repo, orig.node(), dest.node(), {node.nullrev: node.nullrev})
        nodenew = rebase.concludenode(repo, orig.node(), dest.node(), node.nullid)
        nodesrc = orig.node()
        repo.addobsolete(nodenew, nodesrc)
        phases.retractboundary(repo, repo[nodesrc].phase(), [nodenew])
        oldbookmarks = repo.nodebookmarks(nodesrc)
        for book in oldbookmarks:
            repo._bookmarks[book] = nodenew
        for book in destbookmarks: # restore bookmark that rebase move
            repo._bookmarks[book] = dest.node()
        if oldbookmarks or destbookmarks:
            bookmarks.write(repo)
    except util.Abort:
        # Invalidate the previous setparents
        repo.dirstate.invalidate()
        raise



### new command
#############################
cmdtable = {}
command = cmdutil.command(cmdtable)

@command('^stabilize',
    [
     ('n', 'dry-run', False, 'Do nothing but printing what should be done'),
     ('-A', 'any', False, 'Stabilize unstable change on any topological branch'),
    ],
    '')
def stabilize(ui, repo, **opts):
    """move changeset out of they unstable state

    By default only works on changeset that will be rebase on ancestors of the
    current working directory parent (included)"""

    obsolete = extensions.find('obsolete')

    if opts['any']:
        rvstargets = 'unstable()'
    else:
        rvstargets = 'unstable() and ((suspended() and obsancestors(::.))::)'

    unstable = list(repo.set(rvstargets))
    if not unstable:
        unstable = opts['any'] and () or list(repo.set('unstable()'))
        if unstable:
            ui.write_err(_('nothing to stabilize here\n'))
            ui.status(_('(%i unstable changesets, do you want --any ?)\n')
                      % len(unstable))
            return 2
        else:
            ui.write_err(_('no unstable changeset\n'))
            return 1
    node = unstable[0]
    obs = node.parents()[0]
    if not obs.obsolete():
        obs = node.parents()[1]
    assert obs.obsolete()
    newer = obsolete.newerversion(repo, obs.node())
    if len(newer) > 1:
        ui.write_err(_("conflict rewriting. can't choose destination\n"))
        return 2
    targets = newer[0]
    if not targets:
        ui.write_err(_("does not handle kill parent yet\n"))
        return 2
    if len(targets) > 1:
        ui.write_err(_("does not handle splitted parent yet\n"))
        return 2
    target = targets[0]
    displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
    target = repo[target]
    repo.ui.status(_('move:'))
    if not ui.quiet:
        displayer.show(node)
    repo.ui.status(_('atop:'))
    if not ui.quiet:
        displayer.show(target)
    todo= 'hg rebase -Dr %s -d %s\n' % (node, target)
    if opts['dry_run']:
        repo.ui.write(todo)
    else:
        repo.ui.note(todo)
        lock = repo.lock()
        try:
            relocate(repo, node, target)
        finally:
            lock.release()

shorttemplate = '[{rev}] {desc|firstline}\n'

@command('^gdown',
    [],
    'update to working directory parent an display summary lines')
def cmdgdown(ui, repo):
    wkctx = repo[None]
    wparents = wkctx.parents()
    if len(wparents) != 1:
        raise util.Abort('merge in progress')

    parents = wparents[0].parents()
    displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
    if len(parents) == 1:
        p = parents[0]
        hg.update(repo, p.rev())
        displayer.show(p)
        return 0
    else:
        for p in parents:
            displayer.show(p)
        ui.warn(_('multiple parents, explicitly update to one\n'))
        return 1

@command('^gup',
    [],
    'update to working directory children an display summary lines')
def cmdup(ui, repo):
    wkctx = repo[None]
    wparents = wkctx.parents()
    if len(wparents) != 1:
        raise util.Abort('merge in progress')

    children = [ctx for ctx in wparents[0].children() if not ctx.obsolete()]
    displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
    if not children:
        ui.warn(_('No non-obsolete children\n'))
        return 1
    if len(children) == 1:
        c = children[0]
        hg.update(repo, c.rev())
        displayer.show(c)
        return 0
    else:
        for c in children:
            displayer.show(c)
        ui.warn(_('Multiple non-obsolete children, explicitly update to one\n'))
        return 1


@command('^kill',
    [
    ('n', 'new', [], _("New changeset that justify this one to be killed"))
    ],
    '<revs>')
def kill(ui, repo, *revs, **opts):
    """mark a changeset as obsolete

    This update the parent directory to a not-killed parent if the current
    working directory parent are killed.

    XXX bookmark support
    XXX handle merge
    XXX check immutable first
    """
    wlock = repo.wlock()
    try:
        new = set(noderange(repo, opts['new']))
        targetnodes = set(noderange(repo, revs))
        if not new:
            new = [node.nullid]
        for n in targetnodes:
            if not repo[n].mutable():
                ui.warn(_("Can't kill immutable changeset %s") % repo[n])
            else:
                for ne in new:
                    repo.addobsolete(ne, n)
        # update to an unkilled parent
        wdp = repo['.']
        newnode = wdp
        while newnode.obsolete():
            newnode = newnode.parents()[0]
        if newnode.node() != wdp.node():
            commands.update(ui, repo, newnode.rev())
            ui.status(_('working directory now at %s\n') % newnode)

    finally:
        wlock.release()

@command('^amend|refresh',
    [('A', 'addremove', None,
     _('mark new/missing files as added/removed before committing')),
    ('n', 'note', '',
     _('use text as commit message for this update')),
    ('c', 'change', '',
     _('specifies the changeset to amend'), _('REV')),
    ('b', 'branch', '',
     _('specifies a branch for the new.'), _('REV')),
    ('e', 'edit', False,
     _('edit commit message.'), _('')),
    ] + walkopts + commitopts + commitopts2,
    _('[OPTION]... [FILE]...'))

def amend(ui, repo, *pats, **opts):
    """combine a changeset with updates and replace it with a new one

    Commits a new changeset incorporating both the changes to the given files
    and all the changes from the current parent changeset into the repository.

    See :hg:`commit` for details about committing changes.

    If you don't specify -m, the parent's message will be reused.

    If you specify --change, amend additionally considers all changesets between
    the indicated changeset and the working copy parent as updates to be subsumed.
    This allows you to commit updates manually first. As a special shorthand you
    can say `--amend .` instead of '--amend p1(p1())', which subsumes your latest
    commit as an update of its parent.

    Behind the scenes, Mercurial first commits the update as a regular child
    of the current parent. Then it creates a new commit on the parent's parents
    with the updated contents. Then it changes the working copy parent to this
    new combined changeset. Finally, the old changeset and its update are hidden
    from :hg:`log` (unless you use --hidden with log).

    Returns 0 on success, 1 if nothing changed.
    """

    # determine updates to subsume
    change = opts.get('change')
    if change == '.':
        change = 'p1(p1())'
    old = scmutil.revsingle(repo, change)
    branch = opts.get('branch')
    if branch:
        opts.setdefault('extra', {})['branch'] = branch
    else:
        if old.branch() != 'default':
            opts.setdefault('extra', {})['branch'] = old.branch()

    lock = repo.lock()
    try:
        wlock = repo.wlock()
        try:
            if not old.phase():
                raise util.Abort(_("can not rewrite immutable changeset %s") % old)

            # commit current changes as update
            # code copied from commands.commit to avoid noisy messages
            ciopts = dict(opts)
            ciopts.pop('message', None)
            ciopts.pop('logfile', None)
            ciopts['message'] = opts.get('note') or ('amends %s' % old.hex())
            e = cmdutil.commiteditor
            def commitfunc(ui, repo, message, match, opts):
                return repo.commit(message, opts.get('user'), opts.get('date'), match,
                                   editor=e)
            cmdutil.commit(ui, repo, commitfunc, pats, ciopts)

            # find all changesets to be considered updates
            cl = repo.changelog
            head = repo['.']
            updatenodes = set(cl.nodesbetween(roots=[old.node()],
                                              heads=[head.node()])[0])
            updatenodes.remove(old.node())
            okoptions = ['message', 'logfile', 'edit', 'user', 'branch']
            if not updatenodes:
                for o in okoptions:
                    if opts.get(o):
                        break
                else:
                    raise error.Abort(_('no updates found'))
            updates = [repo[n] for n in updatenodes]



            # perform amend
            if opts.get('edit'):
                opts['force_editor'] = True
            newid = rewrite(repo, old, updates, head,
                            [old.p1().node(), old.p2().node()], opts)

            # reroute the working copy parent to the new changeset
            phases.retractboundary(repo, old.phase(), [newid])
            repo.dirstate.setparents(newid, node.nullid)

        finally:
            wlock.release()
    finally:
        lock.release()




def commitwrapper(orig, ui, repo, *arg, **kwargs):
    lock = repo.lock()
    try:
        obsoleted = kwargs.get('obsolete', [])
        if obsoleted:
            obsoleted = repo.set('%lr', obsoleted)
        result = orig(ui, repo, *arg, **kwargs)
        if not result: # commit successed
            new = repo['-1']
            oldbookmarks = []
            for old in obsoleted:
                oldbookmarks.extend(repo.nodebookmarks(old.node()))
                repo.addobsolete(new.node(), old.node())
            for book in oldbookmarks:
                repo._bookmarks[book] = new.node()
            if oldbookmarks:
                bookmarks.write(repo)
        return result
    finally:
        lock.release()

def graftwrapper(orig, ui, repo, *revs, **kwargs):
    lock = repo.lock()
    try:
        if kwargs.get('old_obsolete'):
            obsoleted = kwargs.setdefault('obsolete', [])
            if kwargs['continue']:
                obsoleted.extend(repo.opener.read('graftstate').splitlines())
            else:
                obsoleted.extend(revs)
        # convert obsolete target into revs to avoid alias joke
        obsoleted = kwargs.setdefault('obsolete', [])
        obsoleted[:] = [str(i) for i in repo.revs('%lr', obsoleted)]
        if obsoleted and len(revs) > 1:

            raise error.Abort(_('Can not graft multiple revision while '
                                'obsoleting (for now).'))

        return commitwrapper(orig, ui, repo,*revs, **kwargs)
    finally:
        lock.release()

def extsetup(ui):
    try:
        obsolete = extensions.find('obsolete')
    except KeyError:
        raise error.Abort(_('evolution extension require obsolete extension.'))
    try:
        rebase = extensions.find('rebase')
    except KeyError:
        rebase = None
        raise error.Abort(_('evolution extension require rebase extension.'))

    entry = extensions.wrapcommand(commands.table, 'commit', commitwrapper)
    entry[1].append(('o', 'obsolete', [], _("this commit obsolet this revision")))
    entry = extensions.wrapcommand(commands.table, 'graft', graftwrapper)
    entry[1].append(('o', 'obsolete', [], _("this graft obsolet this revision")))
    entry[1].append(('O', 'old-obsolete', False, _("graft result obsolete graft source")))

    # warning about more obsolete
    for cmd in ['commit', 'push', 'pull', 'graft']:
        entry = extensions.wrapcommand(commands.table, cmd, warnunstable)
    for cmd in ['kill', 'amend']:
        entry = extensions.wrapcommand(cmdtable, cmd, warnunstable)

    if rebase is not None:
        entry = extensions.wrapcommand(rebase.cmdtable, 'rebase', warnunstable)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.