Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 34554:6f11a74d489f
util: add safename function for generating safe names to rename to
This function finds a name which does not clash with any other name in the
manifest, and so can be used to safely rename a file.
Differential Revision: https://phab.mercurial-scm.org/D783
author | Mark Thomas <mbthomas@fb.com> |
---|---|
date | Mon, 02 Oct 2017 14:05:30 -0700 |
parents | 192f7b126ed2 |
children | 75979c8d4572 |
comparison
equal
deleted
inserted
replaced
34553:0217d66846f7 | 34554:6f11a74d489f |
---|---|
24 import datetime | 24 import datetime |
25 import errno | 25 import errno |
26 import gc | 26 import gc |
27 import hashlib | 27 import hashlib |
28 import imp | 28 import imp |
29 import itertools | |
29 import mmap | 30 import mmap |
30 import os | 31 import os |
31 import platform as pyplatform | 32 import platform as pyplatform |
32 import re as remod | 33 import re as remod |
33 import shutil | 34 import shutil |
3833 | 3834 |
3834 i18nfunctions = bundlecompressiontopics().values() | 3835 i18nfunctions = bundlecompressiontopics().values() |
3835 | 3836 |
3836 # convenient shortcut | 3837 # convenient shortcut |
3837 dst = debugstacktrace | 3838 dst = debugstacktrace |
3839 | |
3840 def safename(f, tag, ctx, others=None): | |
3841 """ | |
3842 Generate a name that it is safe to rename f to in the given context. | |
3843 | |
3844 f: filename to rename | |
3845 tag: a string tag that will be included in the new name | |
3846 ctx: a context, in which the new name must not exist | |
3847 others: a set of other filenames that the new name must not be in | |
3848 | |
3849 Returns a file name of the form oldname~tag[~number] which does not exist | |
3850 in the provided context and is not in the set of other names. | |
3851 """ | |
3852 if others is None: | |
3853 others = set() | |
3854 | |
3855 fn = '%s~%s' % (f, tag) | |
3856 if fn not in ctx and fn not in others: | |
3857 return fn | |
3858 for n in itertools.count(1): | |
3859 fn = '%s~%s~%s' % (f, tag, n) | |
3860 if fn not in ctx and fn not in others: | |
3861 return fn |