Debian Codegraph @

hgext/closehead.py

raw
1 # closehead.py - Close arbitrary heads without checking them out first
2 #
3 # This software may be used and distributed according to the terms of the
4 # GNU General Public License version 2 or any later version.
5
6 '''close arbitrary heads without checking them out first'''
7
8 from __future__ import annotations
9
10 from mercurial.i18n import _
11 from mercurial.interfaces.types import (
12 RepoT,
13 )
14 from mercurial import (
15 bookmarks,
16 cmdutil,
17 context,
18 error,
19 logcmdutil,
20 pycompat,
21 registrar,
22 )
23
24 cmdtable = {}
25 command = registrar.command(cmdtable)
26 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
27 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
28 # be specifying the version(s) of Mercurial they are tested with, or
29 # leave the attribute unspecified.
30 testedwith = b'ships-with-hg-core'
31
32 commitopts = cmdutil.commitopts
33 commitopts2 = cmdutil.commitopts2
34 commitopts3 = [(b'r', b'rev', [], _(b'revision to check'), _(b'REV'))]
35
36
37 @command(
38 b'close-head|close-heads',
39 commitopts + commitopts2 + commitopts3,
40 _(b'[OPTION]... [REV]...'),
41 helpcategory=command.CATEGORY_CHANGE_MANAGEMENT,
42 inferrepo=True,
43 )
44 def close_branch(ui, repo: RepoT, *revs, **opts):
45 """close the given head revisions
46
47 This is equivalent to checking out each revision in a clean tree and running
48 ``hg commit --close-branch``, except that it doesn't change the working
49 directory.
50
51 The commit message must be specified with -l or -m.
52 """
53
54 def docommit(rev):
55 cctx = context.memctx(
56 repo,
57 parents=[rev, None],
58 text=message,
59 files=[],
60 filectxfn=None,
61 user=opts.get('user'),
62 date=opts.get('date'),
63 extra=extra,
64 )
65 with repo.transaction(b'commit'):
66 ret = repo.commitctx(cctx, True)
67 bookmarks.update(repo, [rev, None], ret)
68 cctx.markcommitted(ret)
69
70 revs += tuple(opts.get('rev', []))
71 revs = logcmdutil.revrange(repo, revs)
72
73 if not revs:
74 raise error.Abort(_(b'no revisions specified'))
75
76 heads = set()
77 bm = repo.branchmap()
78 for branch in bm:
79 heads.update(bm.branch_head_revs(branch))
80 for rev in revs:
81 if rev not in heads:
82 raise error.Abort(_(b'revision is not an open head: %d') % rev)
83
84 message = cmdutil.logmessage(ui, pycompat.byteskwargs(opts))
85 if not message:
86 raise error.Abort(_(b"no commit message specified with -l or -m"))
87 extra = {b'close': b'1'}
88
89 with repo.wlock(), repo.lock():
90 for rev in revs:
91 r = repo[rev]
92 branch = r.branch()
93 extra[b'branch'] = branch
94 docommit(r)
95 return 0