Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/ui.py @ 34046:6e6452bc441d
editor: use an unambiguous path suffix for editor files
Changes the API of `ui.edit()` to take an optional `action` argument,
which is used when constructing the suffix of the temp file.
Previously, it was possible to set the suffix by specifying a `suffix` to the
optional `extra` dict that was passed to `ui.edit()`, but the goal is to
drop support for `extra.suffix` and make `action` a required argument.
To this end, `ui.edit()` now yields a `develwarn()` if `action` is not set
or if `extra.suffix` is set.
I updated all calls to `ui.edit()` I could find in `hg-crew` to specify the
appropriate `action`. This means that when creating a commit, instead
of the path to the editor file being something like:
`/tmp/hg-editor-XXXXXX.txt`
it is now something like:
`/tmp/hg-editor-XXXXXX.commit.hg.txt`
Some editors (such as Atom) make it possible to statically define a [TextMate]
grammar for files with a particular suffix. For example, because Git reliably
uses `.git/COMMIT_EDITMSG` and `.git/MERGE_MSG` as the paths for commit-type
messages, it is trivial to define a grammar that is applied when files of
either name are opened in Atom:
https://github.com/atom/language-git/blob/v0.19.1/grammars/git%20commit%20message.cson#L4-L5
Because Hg historically used a generic `.txt` suffix, it was much harder to
disambiguate whether a file was an arbitrary text file as opposed to one
created for the specific purpose of authoring an Hg commit message.
This also makes it easier to add special support for `histedit`, as it has its own
suffix that is distinct from a commit:
`/tmp/hg-histedit-XXXXXX.histedit.hg.txt`
Test Plan:
Added an integration test: `test-editor-filename.t`.
Manually tested: ran `hg ci --amend` for this change and saw that it
used `/tmp/hg-editor-ZZjcz0.commit.hg.txt` as the path instead of
`/tmp/hg-editor-ZZjcz0.txt` as the path.
Verified `make tests` passes.
Differential Revision: https://phab.mercurial-scm.org/D464
author | Michael Bolin <mbolin@fb.com> |
---|---|
date | Wed, 30 Aug 2017 20:25:56 +0000 |
parents | 31a2eb0f74e5 |
children | 3c82b14d2838 |
comparison
equal
deleted
inserted
replaced
34045:bfb38c5cebf4 | 34046:6e6452bc441d |
---|---|
1344 if self.debugflag: | 1344 if self.debugflag: |
1345 opts[r'label'] = opts.get(r'label', '') + ' ui.debug' | 1345 opts[r'label'] = opts.get(r'label', '') + ' ui.debug' |
1346 self.write(*msg, **opts) | 1346 self.write(*msg, **opts) |
1347 | 1347 |
1348 def edit(self, text, user, extra=None, editform=None, pending=None, | 1348 def edit(self, text, user, extra=None, editform=None, pending=None, |
1349 repopath=None): | 1349 repopath=None, action=None): |
1350 if action is None: | |
1351 self.develwarn('action is None but will soon be a required ' | |
1352 'parameter to ui.edit()') | |
1350 extra_defaults = { | 1353 extra_defaults = { |
1351 'prefix': 'editor', | 1354 'prefix': 'editor', |
1352 'suffix': '.txt', | 1355 'suffix': '.txt', |
1353 } | 1356 } |
1354 if extra is not None: | 1357 if extra is not None: |
1358 if extra.get('suffix') is not None: | |
1359 self.develwarn('extra.suffix is not None but will soon be ' | |
1360 'ignored by ui.edit()') | |
1355 extra_defaults.update(extra) | 1361 extra_defaults.update(extra) |
1356 extra = extra_defaults | 1362 extra = extra_defaults |
1363 | |
1364 if action: | |
1365 suffix = '.%s.hg.txt' % action | |
1366 else: | |
1367 suffix = extra['suffix'] | |
1357 | 1368 |
1358 rdir = None | 1369 rdir = None |
1359 if self.configbool('experimental', 'editortmpinhg'): | 1370 if self.configbool('experimental', 'editortmpinhg'): |
1360 rdir = repopath | 1371 rdir = repopath |
1361 (fd, name) = tempfile.mkstemp(prefix='hg-' + extra['prefix'] + '-', | 1372 (fd, name) = tempfile.mkstemp(prefix='hg-' + extra['prefix'] + '-', |
1362 suffix=extra['suffix'], | 1373 suffix=suffix, |
1363 dir=rdir) | 1374 dir=rdir) |
1364 try: | 1375 try: |
1365 f = os.fdopen(fd, r'wb') | 1376 f = os.fdopen(fd, r'wb') |
1366 f.write(util.tonativeeol(text)) | 1377 f.write(util.tonativeeol(text)) |
1367 f.close() | 1378 f.close() |