Skip to content

Commit

Permalink
commit: support .committemplate based on changed files
Browse files Browse the repository at this point in the history
Summary:
See the test for an example. This can be useful to set templates for
sub-projects in a larger project.

Reviewed By: zzl0

Differential Revision: D49781544

fbshipit-source-id: ce9336c0b394377d3e90f135fcccf1fae01e81cb
  • Loading branch information
quark-zju authored and facebook-github-bot committed Oct 6, 2023
1 parent 4bd1726 commit ff7e964
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
6 changes: 3 additions & 3 deletions eden/scm/lib/config/loader/src/builtin_static/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ exportstack-max-bytes=1M
log-implicit-follow-threshold=10000
titles-namespace=true
local-committemplate=true
[zsh]
completion-age=7
completion-description=false
Expand All @@ -65,7 +68,4 @@ selectivepulldiscovery=true
autopullhoistpattern=
autopullpattern=re:^(?:default|remote)/[A-Za-z0-9._/-]+$
hoist=default
[experimental]
titles-namespace=true
"#);
42 changes: 42 additions & 0 deletions eden/scm/sapling/cmdutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4012,6 +4012,11 @@ def buildcommittemplate(repo, ctx, extramsg, ref):
for k, v in repo.ui.configitems("committemplate")
)

# load extra aliases based on changed files
if repo.ui.configbool("experimental", "local-committemplate"):
localtemplate = localcommittemplate(repo, ctx)
t.t.cache.update((k, templater.unquotestring(v)) for k, v in localtemplate)

if not extramsg:
extramsg = "" # ensure that extramsg is string

Expand All @@ -4020,6 +4025,43 @@ def buildcommittemplate(repo, ctx, extramsg, ref):
return pycompat.decodeutf8(ui.popbufferbytes(), errors="replace")


def localcommittemplate(repo, ctx):
"""return [(k, v)], local [committemplate] config based on changed files.
The local commit template only works for working copy ctx.
The local commit template is decided in these steps:
- First, calculate the common prefix of the changed files.
For example, the common prefix of a/x/1, a/x/2/3 is a/x.
- Then, find the config file, starting with prefix/.committemplate,
recursively look at parent directories until repo root.
For the above example, check a/x/.committemplate, then a/.committemplate,
then .committemplate from repo root.
- Load the config file. The format of the config file is ``key = value``,
similar to config files but without the [committemplate] header.
"""

if ctx.node() is None:
files = ctx.files()
prefix = os.path.commonpath(files) if files else ""
wvfs = repo.wvfs
while True:
config_path = prefix + (prefix and "/" or "") + ".committemplate"
if wvfs.isdir(prefix) and wvfs.exists(config_path):
cfg = bindings.configloader.config()
content = wvfs.tryreadutf8(config_path)
cfg.parse(content, source=config_path)
section = ""
names = cfg.names(section)
return [(name, cfg.get(section, name)) for name in names]
next_prefix = os.path.dirname(prefix)
if next_prefix == prefix:
break
prefix = next_prefix

return []


def hgprefix(msg):
return "\n".join([f"{identity.tmplprefix()}: {a}" for a in msg.split("\n") if a])

Expand Down
43 changes: 43 additions & 0 deletions eden/scm/tests/test-commit-localtemplate.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#debugruntest-compatible

$ newrepo
$ setconfig 'committemplate.changeset={foo}\n'
$ setconfig 'committemplate.foo=foo'

Default commit template

$ HGEDITOR=cat hg commit --config ui.allowemptycommit=true
foo
abort: commit message unchanged
[255]

$ mkdir -p x/y/z/k z/y
$ touch x/y/z/k/1 x/y/z/1 x/y/1 x/1 z/y/1
$ echo 'foo = z' > x/y/z/.committemplate
$ echo 'foo = y' > x/y/.committemplate
$ echo 'foo = x' > x/.committemplate
$ echo 'foo = root' > .committemplate

When x/y/z/k/.committemplate does not exist, check parents x/y/z:

$ hg add -q x/y/z/k/1
$ HGEDITOR=cat hg commit --config ui.allowemptycommit=true
z
abort: commit message unchanged
[255]

Common prefix is now y:

$ hg add -q x/y/1
$ HGEDITOR=cat hg commit --config ui.allowemptycommit=true
y
abort: commit message unchanged
[255]

Common prefix is now repo root:

$ hg add z/y/1
$ HGEDITOR=cat hg commit --config ui.allowemptycommit=true
root
abort: commit message unchanged
[255]

0 comments on commit ff7e964

Please sign in to comment.