The dirstatemap code cover various aspects, it grow a bit messy over the years. So we shuffle the code around into some documented categories.
This will help use to clean up the code.
No code was changed in this changeset, only code move.
( )
Alphare |
hg-reviewers |
The dirstatemap code cover various aspects, it grow a bit messy over the years. So we shuffle the code around into some documented categories.
This will help use to clean up the code.
No code was changed in this changeset, only code move.
Automatic diff as part of commit; lint not applicable. |
Automatic diff as part of commit; unit tests not applicable. |
Path | Packages | |||
---|---|---|---|---|
M | mercurial/dirstatemap.py (953 lines) |
Status | Author | Revision | |
---|---|---|---|
Closed | SimonSapin | ||
Closed | SimonSapin | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Abandoned | pulkit | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute | ||
Closed | marmoute |
super(dirstatemap, self).__init__( | super(dirstatemap, self).__init__( | ||||
ui, opener, root, nodeconstants, use_dirstate_v2 | ui, opener, root, nodeconstants, use_dirstate_v2 | ||||
) | ) | ||||
if self._use_dirstate_v2: | if self._use_dirstate_v2: | ||||
msg = "Dirstate V2 not supportedi" | msg = "Dirstate V2 not supportedi" | ||||
msg += "(should have detected unsupported requirement)" | msg += "(should have detected unsupported requirement)" | ||||
raise error.ProgrammingError(msg) | raise error.ProgrammingError(msg) | ||||
### Core data storage and access | |||||
@propertycache | @propertycache | ||||
def _map(self): | def _map(self): | ||||
self._map = {} | self._map = {} | ||||
self.read() | self.read() | ||||
return self._map | return self._map | ||||
@propertycache | @propertycache | ||||
def copymap(self): | def copymap(self): | ||||
`all` is unused when Rust is not enabled | `all` is unused when Rust is not enabled | ||||
""" | """ | ||||
for (filename, item) in self.items(): | for (filename, item) in self.items(): | ||||
yield (filename, item.state, item.mode, item.size, item.mtime) | yield (filename, item.state, item.mode, item.size, item.mtime) | ||||
def keys(self): | def keys(self): | ||||
return self._map.keys() | return self._map.keys() | ||||
def _dirs_incr(self, filename, old_entry=None): | ### reading/setting parents | ||||
"""incremente the dirstate counter if applicable""" | |||||
if ( | |||||
old_entry is None or old_entry.removed | |||||
) and "_dirs" in self.__dict__: | |||||
self._dirs.addpath(filename) | |||||
if old_entry is None and "_alldirs" in self.__dict__: | |||||
self._alldirs.addpath(filename) | |||||
def _dirs_decr(self, filename, old_entry=None, remove_variant=False): | |||||
"""decremente the dirstate counter if applicable""" | |||||
if old_entry is not None: | |||||
if "_dirs" in self.__dict__ and not old_entry.removed: | |||||
self._dirs.delpath(filename) | |||||
if "_alldirs" in self.__dict__ and not remove_variant: | |||||
self._alldirs.delpath(filename) | |||||
elif remove_variant and "_alldirs" in self.__dict__: | |||||
self._alldirs.addpath(filename) | |||||
if "filefoldmap" in self.__dict__: | |||||
normed = util.normcase(filename) | |||||
self.filefoldmap.pop(normed, None) | |||||
def set_possibly_dirty(self, filename): | |||||
"""record that the current state of the file on disk is unknown""" | |||||
self[filename].set_possibly_dirty() | |||||
def set_clean(self, filename, mode, size, mtime): | |||||
"""mark a file as back to a clean state""" | |||||
entry = self[filename] | |||||
mtime = mtime & rangemask | |||||
size = size & rangemask | |||||
entry.set_clean(mode, size, mtime) | |||||
self.copymap.pop(filename, None) | |||||
def reset_state( | |||||
self, | |||||
filename, | |||||
wc_tracked=False, | |||||
p1_tracked=False, | |||||
p2_tracked=False, | |||||
merged=False, | |||||
clean_p1=False, | |||||
clean_p2=False, | |||||
possibly_dirty=False, | |||||
parentfiledata=None, | |||||
): | |||||
"""Set a entry to a given state, diregarding all previous state | |||||
This is to be used by the part of the dirstate API dedicated to | |||||
adjusting the dirstate after a update/merge. | |||||
note: calling this might result to no entry existing at all if the | |||||
dirstate map does not see any point at having one for this file | |||||
anymore. | |||||
""" | |||||
if merged and (clean_p1 or clean_p2): | |||||
msg = b'`merged` argument incompatible with `clean_p1`/`clean_p2`' | |||||
raise error.ProgrammingError(msg) | |||||
# copy information are now outdated | |||||
# (maybe new information should be in directly passed to this function) | |||||
self.copymap.pop(filename, None) | |||||
if not (p1_tracked or p2_tracked or wc_tracked): | |||||
old_entry = self._map.pop(filename, None) | |||||
self._dirs_decr(filename, old_entry=old_entry) | |||||
self.copymap.pop(filename, None) | |||||
return | |||||
elif merged: | |||||
pass | |||||
elif not (p1_tracked or p2_tracked) and wc_tracked: | |||||
pass # file is added, nothing special to adjust | |||||
elif (p1_tracked or p2_tracked) and not wc_tracked: | |||||
pass | |||||
elif clean_p2 and wc_tracked: | |||||
pass | |||||
elif not p1_tracked and p2_tracked and wc_tracked: | |||||
clean_p2 = True | |||||
elif possibly_dirty: | |||||
pass | |||||
elif wc_tracked: | |||||
# this is a "normal" file | |||||
if parentfiledata is None: | |||||
msg = b'failed to pass parentfiledata for a normal file: %s' | |||||
msg %= filename | |||||
raise error.ProgrammingError(msg) | |||||
else: | |||||
assert False, 'unreachable' | |||||
old_entry = self._map.get(filename) | |||||
self._dirs_incr(filename, old_entry) | |||||
entry = DirstateItem( | |||||
wc_tracked=wc_tracked, | |||||
p1_tracked=p1_tracked, | |||||
p2_tracked=p2_tracked, | |||||
merged=merged, | |||||
clean_p1=clean_p1, | |||||
clean_p2=clean_p2, | |||||
possibly_dirty=possibly_dirty, | |||||
parentfiledata=parentfiledata, | |||||
) | |||||
self._map[filename] = entry | |||||
def set_tracked(self, filename): | |||||
new = False | |||||
entry = self.get(filename) | |||||
if entry is None: | |||||
self._dirs_incr(filename) | |||||
entry = DirstateItem( | |||||
p1_tracked=False, | |||||
p2_tracked=False, | |||||
wc_tracked=True, | |||||
merged=False, | |||||
clean_p1=False, | |||||
clean_p2=False, | |||||
possibly_dirty=False, | |||||
parentfiledata=None, | |||||
) | |||||
self._map[filename] = entry | |||||
new = True | |||||
elif not entry.tracked: | |||||
self._dirs_incr(filename, entry) | |||||
entry.set_tracked() | |||||
new = True | |||||
else: | |||||
# XXX This is probably overkill for more case, but we need this to | |||||
# fully replace the `normallookup` call with `set_tracked` one. | |||||
# Consider smoothing this in the future. | |||||
self.set_possibly_dirty(filename) | |||||
return new | |||||
def set_untracked(self, f): | |||||
"""Mark a file as no longer tracked in the dirstate map""" | |||||
entry = self.get(f) | |||||
if entry is None: | |||||
return False | |||||
else: | |||||
self._dirs_decr(f, old_entry=entry, remove_variant=not entry.added) | |||||
if not entry.merged: | |||||
self.copymap.pop(f, None) | |||||
if entry.added: | |||||
self._map.pop(f, None) | |||||
else: | |||||
entry.set_untracked() | |||||
return True | |||||
@propertycache | |||||
def filefoldmap(self): | |||||
"""Returns a dictionary mapping normalized case paths to their | |||||
non-normalized versions. | |||||
""" | |||||
try: | |||||
makefilefoldmap = parsers.make_file_foldmap | |||||
except AttributeError: | |||||
pass | |||||
else: | |||||
return makefilefoldmap( | |||||
self._map, util.normcasespec, util.normcasefallback | |||||
) | |||||
f = {} | |||||
normcase = util.normcase | |||||
for name, s in pycompat.iteritems(self._map): | |||||
if not s.removed: | |||||
f[normcase(name)] = name | |||||
f[b'.'] = b'.' # prevents useless util.fspath() invocation | |||||
return f | |||||
def hastrackeddir(self, d): | |||||
""" | |||||
Returns True if the dirstate contains a tracked (not removed) file | |||||
in this directory. | |||||
""" | |||||
return d in self._dirs | |||||
def hasdir(self, d): | |||||
""" | |||||
Returns True if the dirstate contains a file (tracked or removed) | |||||
in this directory. | |||||
""" | |||||
return d in self._alldirs | |||||
@propertycache | |||||
def _dirs(self): | |||||
return pathutil.dirs(self._map, only_tracked=True) | |||||
@propertycache | |||||
def _alldirs(self): | |||||
return pathutil.dirs(self._map) | |||||
def _opendirstatefile(self): | |||||
fp, mode = txnutil.trypending(self._root, self._opener, self._filename) | |||||
if self._pendingmode is not None and self._pendingmode != mode: | |||||
fp.close() | |||||
raise error.Abort( | |||||
_(b'working directory state may be changed parallelly') | |||||
) | |||||
self._pendingmode = mode | |||||
return fp | |||||
def parents(self): | def parents(self): | ||||
if not self._parents: | if not self._parents: | ||||
try: | try: | ||||
fp = self._opendirstatefile() | fp = self._opendirstatefile() | ||||
st = fp.read(2 * self._nodelen) | st = fp.read(2 * self._nodelen) | ||||
fp.close() | fp.close() | ||||
except IOError as err: | except IOError as err: | ||||
# Discard "merged" markers when moving away from a merge state | # Discard "merged" markers when moving away from a merge state | ||||
if s.merged or s.from_p2: | if s.merged or s.from_p2: | ||||
source = self.copymap.pop(f, None) | source = self.copymap.pop(f, None) | ||||
if source: | if source: | ||||
copies[f] = source | copies[f] = source | ||||
s.drop_merge_data() | s.drop_merge_data() | ||||
return copies | return copies | ||||
### disk interaction | |||||
def read(self): | def read(self): | ||||
# ignore HG_PENDING because identity is used only for writing | # ignore HG_PENDING because identity is used only for writing | ||||
self.identity = util.filestat.frompath( | self.identity = util.filestat.frompath( | ||||
self._opener.join(self._filename) | self._opener.join(self._filename) | ||||
) | ) | ||||
try: | try: | ||||
fp = self._opendirstatefile() | fp = self._opendirstatefile() | ||||
self.setparents(*p) | self.setparents(*p) | ||||
# Avoid excess attribute lookups by fast pathing certain checks | # Avoid excess attribute lookups by fast pathing certain checks | ||||
self.__contains__ = self._map.__contains__ | self.__contains__ = self._map.__contains__ | ||||
self.__getitem__ = self._map.__getitem__ | self.__getitem__ = self._map.__getitem__ | ||||
self.get = self._map.get | self.get = self._map.get | ||||
def write(self, _tr, st, now): | def write(self, _tr, st, now): | ||||
st.write( | d = parsers.pack_dirstate(self._map, self.copymap, self.parents(), now) | ||||
parsers.pack_dirstate(self._map, self.copymap, self.parents(), now) | st.write(d) | ||||
) | |||||
st.close() | st.close() | ||||
self._dirtyparents = False | self._dirtyparents = False | ||||
def _opendirstatefile(self): | |||||
fp, mode = txnutil.trypending(self._root, self._opener, self._filename) | |||||
if self._pendingmode is not None and self._pendingmode != mode: | |||||
fp.close() | |||||
raise error.Abort( | |||||
_(b'working directory state may be changed parallelly') | |||||
) | |||||
self._pendingmode = mode | |||||
return fp | |||||
@propertycache | @propertycache | ||||
def identity(self): | def identity(self): | ||||
self._map | self._map | ||||
return self.identity | return self.identity | ||||
### code related to maintaining and accessing "extra" property | |||||
# (e.g. "has_dir") | |||||
def _dirs_incr(self, filename, old_entry=None): | |||||
"""incremente the dirstate counter if applicable""" | |||||
if ( | |||||
old_entry is None or old_entry.removed | |||||
) and "_dirs" in self.__dict__: | |||||
self._dirs.addpath(filename) | |||||
if old_entry is None and "_alldirs" in self.__dict__: | |||||
self._alldirs.addpath(filename) | |||||
def _dirs_decr(self, filename, old_entry=None, remove_variant=False): | |||||
"""decremente the dirstate counter if applicable""" | |||||
if old_entry is not None: | |||||
if "_dirs" in self.__dict__ and not old_entry.removed: | |||||
self._dirs.delpath(filename) | |||||
if "_alldirs" in self.__dict__ and not remove_variant: | |||||
self._alldirs.delpath(filename) | |||||
elif remove_variant and "_alldirs" in self.__dict__: | |||||
self._alldirs.addpath(filename) | |||||
if "filefoldmap" in self.__dict__: | |||||
normed = util.normcase(filename) | |||||
self.filefoldmap.pop(normed, None) | |||||
@propertycache | |||||
def filefoldmap(self): | |||||
"""Returns a dictionary mapping normalized case paths to their | |||||
non-normalized versions. | |||||
""" | |||||
try: | |||||
makefilefoldmap = parsers.make_file_foldmap | |||||
except AttributeError: | |||||
pass | |||||
else: | |||||
return makefilefoldmap( | |||||
self._map, util.normcasespec, util.normcasefallback | |||||
) | |||||
f = {} | |||||
normcase = util.normcase | |||||
for name, s in pycompat.iteritems(self._map): | |||||
if not s.removed: | |||||
f[normcase(name)] = name | |||||
f[b'.'] = b'.' # prevents useless util.fspath() invocation | |||||
return f | |||||
@propertycache | @propertycache | ||||
def dirfoldmap(self): | def dirfoldmap(self): | ||||
f = {} | f = {} | ||||
normcase = util.normcase | normcase = util.normcase | ||||
for name in self._dirs: | for name in self._dirs: | ||||
f[normcase(name)] = name | f[normcase(name)] = name | ||||
return f | return f | ||||
def hastrackeddir(self, d): | |||||
""" | |||||
Returns True if the dirstate contains a tracked (not removed) file | |||||
in this directory. | |||||
""" | |||||
return d in self._dirs | |||||
if rustmod is not None: | def hasdir(self, d): | ||||
""" | |||||
Returns True if the dirstate contains a file (tracked or removed) | |||||
in this directory. | |||||
""" | |||||
return d in self._alldirs | |||||
class dirstatemap(_dirstatemapcommon): | @propertycache | ||||
def __init__(self, ui, opener, root, nodeconstants, use_dirstate_v2): | def _dirs(self): | ||||
super(dirstatemap, self).__init__( | return pathutil.dirs(self._map, only_tracked=True) | ||||
ui, opener, root, nodeconstants, use_dirstate_v2 | |||||
) | |||||
self._docket = None | |||||
def addfile( | @propertycache | ||||
self, | def _alldirs(self): | ||||
f, | return pathutil.dirs(self._map) | ||||
mode=0, | |||||
size=None, | ### code related to manipulation of entries and copy-sources | ||||
mtime=None, | |||||
added=False, | def set_possibly_dirty(self, filename): | ||||
merged=False, | """record that the current state of the file on disk is unknown""" | ||||
from_p2=False, | self[filename].set_possibly_dirty() | ||||
possibly_dirty=False, | |||||
): | def set_clean(self, filename, mode, size, mtime): | ||||
if added: | """mark a file as back to a clean state""" | ||||
assert not possibly_dirty | entry = self[filename] | ||||
assert not from_p2 | |||||
item = DirstateItem.new_added() | |||||
elif merged: | |||||
assert not possibly_dirty | |||||
assert not from_p2 | |||||
item = DirstateItem.new_merged() | |||||
elif from_p2: | |||||
assert not possibly_dirty | |||||
item = DirstateItem.new_from_p2() | |||||
elif possibly_dirty: | |||||
item = DirstateItem.new_possibly_dirty() | |||||
else: | |||||
assert size is not None | |||||
assert mtime is not None | |||||
size = size & rangemask | |||||
mtime = mtime & rangemask | mtime = mtime & rangemask | ||||
item = DirstateItem.new_normal(mode, size, mtime) | size = size & rangemask | ||||
self._map.addfile(f, item) | entry.set_clean(mode, size, mtime) | ||||
if added: | self.copymap.pop(filename, None) | ||||
self.copymap.pop(f, None) | |||||
def reset_state( | def reset_state( | ||||
self, | self, | ||||
filename, | filename, | ||||
wc_tracked=False, | wc_tracked=False, | ||||
p1_tracked=False, | p1_tracked=False, | ||||
p2_tracked=False, | p2_tracked=False, | ||||
merged=False, | merged=False, | ||||
clean_p1=False, | clean_p1=False, | ||||
clean_p2=False, | clean_p2=False, | ||||
possibly_dirty=False, | possibly_dirty=False, | ||||
parentfiledata=None, | parentfiledata=None, | ||||
): | ): | ||||
"""Set a entry to a given state, disregarding all previous state | """Set a entry to a given state, diregarding all previous state | ||||
This is to be used by the part of the dirstate API dedicated to | This is to be used by the part of the dirstate API dedicated to | ||||
adjusting the dirstate after a update/merge. | adjusting the dirstate after a update/merge. | ||||
note: calling this might result to no entry existing at all if the | note: calling this might result to no entry existing at all if the | ||||
dirstate map does not see any point at having one for this file | dirstate map does not see any point at having one for this file | ||||
anymore. | anymore. | ||||
""" | """ | ||||
if merged and (clean_p1 or clean_p2): | if merged and (clean_p1 or clean_p2): | ||||
msg = ( | msg = b'`merged` argument incompatible with `clean_p1`/`clean_p2`' | ||||
b'`merged` argument incompatible with `clean_p1`/`clean_p2`' | |||||
) | |||||
raise error.ProgrammingError(msg) | raise error.ProgrammingError(msg) | ||||
# copy information are now outdated | # copy information are now outdated | ||||
# (maybe new information should be in directly passed to this function) | # (maybe new information should be in directly passed to this function) | ||||
self.copymap.pop(filename, None) | self.copymap.pop(filename, None) | ||||
if not (p1_tracked or p2_tracked or wc_tracked): | if not (p1_tracked or p2_tracked or wc_tracked): | ||||
self._map.drop_item_and_copy_source(filename) | old_entry = self._map.pop(filename, None) | ||||
self._dirs_decr(filename, old_entry=old_entry) | |||||
self.copymap.pop(filename, None) | |||||
return | |||||
elif merged: | elif merged: | ||||
# XXX might be merged and removed ? | pass | ||||
entry = self.get(filename) | |||||
if entry is not None and entry.tracked: | |||||
# XXX mostly replicate dirstate.other parent. We should get | |||||
# the higher layer to pass us more reliable data where `merged` | |||||
# actually mean merged. Dropping the else clause will show | |||||
# failure in `test-graft.t` | |||||
self.addfile(filename, merged=True) | |||||
else: | |||||
self.addfile(filename, from_p2=True) | |||||
elif not (p1_tracked or p2_tracked) and wc_tracked: | elif not (p1_tracked or p2_tracked) and wc_tracked: | ||||
self.addfile( | pass # file is added, nothing special to adjust | ||||
filename, added=True, possibly_dirty=possibly_dirty | |||||
) | |||||
elif (p1_tracked or p2_tracked) and not wc_tracked: | elif (p1_tracked or p2_tracked) and not wc_tracked: | ||||
# XXX might be merged and removed ? | pass | ||||
self[filename] = DirstateItem.from_v1_data(b'r', 0, 0, 0) | |||||
elif clean_p2 and wc_tracked: | elif clean_p2 and wc_tracked: | ||||
if p1_tracked or self.get(filename) is not None: | pass | ||||
# XXX the `self.get` call is catching some case in | |||||
# `test-merge-remove.t` where the file is tracked in p1, the | |||||
# p1_tracked argument is False. | |||||
# | |||||
# In addition, this seems to be a case where the file is marked | |||||
# as merged without actually being the result of a merge | |||||
# action. So thing are not ideal here. | |||||
self.addfile(filename, merged=True) | |||||
else: | |||||
self.addfile(filename, from_p2=True) | |||||
elif not p1_tracked and p2_tracked and wc_tracked: | elif not p1_tracked and p2_tracked and wc_tracked: | ||||
self.addfile( | clean_p2 = True | ||||
filename, from_p2=True, possibly_dirty=possibly_dirty | |||||
) | |||||
elif possibly_dirty: | elif possibly_dirty: | ||||
self.addfile(filename, possibly_dirty=possibly_dirty) | pass | ||||
elif wc_tracked: | elif wc_tracked: | ||||
# this is a "normal" file | # this is a "normal" file | ||||
if parentfiledata is None: | if parentfiledata is None: | ||||
msg = b'failed to pass parentfiledata for a normal file: %s' | msg = b'failed to pass parentfiledata for a normal file: %s' | ||||
msg %= filename | msg %= filename | ||||
raise error.ProgrammingError(msg) | raise error.ProgrammingError(msg) | ||||
mode, size, mtime = parentfiledata | |||||
self.addfile(filename, mode=mode, size=size, mtime=mtime) | |||||
else: | else: | ||||
assert False, 'unreachable' | assert False, 'unreachable' | ||||
old_entry = self._map.get(filename) | |||||
self._dirs_incr(filename, old_entry) | |||||
entry = DirstateItem( | |||||
wc_tracked=wc_tracked, | |||||
p1_tracked=p1_tracked, | |||||
p2_tracked=p2_tracked, | |||||
merged=merged, | |||||
clean_p1=clean_p1, | |||||
clean_p2=clean_p2, | |||||
possibly_dirty=possibly_dirty, | |||||
parentfiledata=parentfiledata, | |||||
) | |||||
self._map[filename] = entry | |||||
def set_tracked(self, filename): | def set_tracked(self, filename): | ||||
new = False | new = False | ||||
entry = self.get(filename) | entry = self.get(filename) | ||||
if entry is None: | if entry is None: | ||||
self.addfile(filename, added=True) | self._dirs_incr(filename) | ||||
entry = DirstateItem( | |||||
p1_tracked=False, | |||||
p2_tracked=False, | |||||
wc_tracked=True, | |||||
merged=False, | |||||
clean_p1=False, | |||||
clean_p2=False, | |||||
possibly_dirty=False, | |||||
parentfiledata=None, | |||||
) | |||||
self._map[filename] = entry | |||||
new = True | new = True | ||||
elif not entry.tracked: | elif not entry.tracked: | ||||
self._dirs_incr(filename, entry) | |||||
entry.set_tracked() | entry.set_tracked() | ||||
self._map.set_dirstate_item(filename, entry) | |||||
new = True | new = True | ||||
else: | else: | ||||
# XXX This is probably overkill for more case, but we need this to | # XXX This is probably overkill for more case, but we need this to | ||||
# fully replace the `normallookup` call with `set_tracked` one. | # fully replace the `normallookup` call with `set_tracked` one. | ||||
# Consider smoothing this in the future. | # Consider smoothing this in the future. | ||||
self.set_possibly_dirty(filename) | self.set_possibly_dirty(filename) | ||||
return new | return new | ||||
def set_untracked(self, f): | def set_untracked(self, f): | ||||
"""Mark a file as no longer tracked in the dirstate map""" | """Mark a file as no longer tracked in the dirstate map""" | ||||
# in merge is only trigger more logic, so it "fine" to pass it. | |||||
# | |||||
# the inner rust dirstate map code need to be adjusted once the API | |||||
# for dirstate/dirstatemap/DirstateItem is a bit more settled | |||||
entry = self.get(f) | entry = self.get(f) | ||||
if entry is None: | if entry is None: | ||||
return False | return False | ||||
else: | else: | ||||
self._dirs_decr(f, old_entry=entry, remove_variant=not entry.added) | |||||
if not entry.merged: | |||||
self.copymap.pop(f, None) | |||||
if entry.added: | if entry.added: | ||||
self._map.drop_item_and_copy_source(f) | self._map.pop(f, None) | ||||
else: | else: | ||||
self._map.removefile(f, in_merge=True) | entry.set_untracked() | ||||
return True | return True | ||||
def removefile(self, *args, **kwargs): | |||||
return self._map.removefile(*args, **kwargs) | if rustmod is not None: | ||||
class dirstatemap(_dirstatemapcommon): | |||||
def __init__(self, ui, opener, root, nodeconstants, use_dirstate_v2): | |||||
super(dirstatemap, self).__init__( | |||||
ui, opener, root, nodeconstants, use_dirstate_v2 | |||||
) | |||||
self._docket = None | |||||
### Core data storage and access | |||||
@property | |||||
def docket(self): | |||||
if not self._docket: | |||||
if not self._use_dirstate_v2: | |||||
raise error.ProgrammingError( | |||||
b'dirstate only has a docket in v2 format' | |||||
) | |||||
self._docket = docketmod.DirstateDocket.parse( | |||||
self._readdirstatefile(), self._nodeconstants | |||||
) | |||||
return self._docket | |||||
@propertycache | |||||
def _map(self): | |||||
""" | |||||
Fills the Dirstatemap when called. | |||||
""" | |||||
# ignore HG_PENDING because identity is used only for writing | |||||
self.identity = util.filestat.frompath( | |||||
self._opener.join(self._filename) | |||||
) | |||||
if self._use_dirstate_v2: | |||||
if self.docket.uuid: | |||||
# TODO: use mmap when possible | |||||
data = self._opener.read(self.docket.data_filename()) | |||||
else: | |||||
data = b'' | |||||
self._map = rustmod.DirstateMap.new_v2( | |||||
data, self.docket.data_size, self.docket.tree_metadata | |||||
) | |||||
parents = self.docket.parents | |||||
else: | |||||
self._map, parents = rustmod.DirstateMap.new_v1( | |||||
self._readdirstatefile() | |||||
) | |||||
if parents and not self._dirtyparents: | |||||
self.setparents(*parents) | |||||
self.__contains__ = self._map.__contains__ | |||||
self.__getitem__ = self._map.__getitem__ | |||||
self.get = self._map.get | |||||
return self._map | |||||
@property | @property | ||||
def copymap(self): | def copymap(self): | ||||
return self._map.copymap() | return self._map.copymap() | ||||
def debug_iter(self, all): | def debug_iter(self, all): | ||||
""" | """ | ||||
Return an iterator of (filename, state, mode, size, mtime) tuples | Return an iterator of (filename, state, mode, size, mtime) tuples | ||||
) | ) | ||||
util.clearcachedproperty(self, b"_dirs") | util.clearcachedproperty(self, b"_dirs") | ||||
util.clearcachedproperty(self, b"_alldirs") | util.clearcachedproperty(self, b"_alldirs") | ||||
util.clearcachedproperty(self, b"dirfoldmap") | util.clearcachedproperty(self, b"dirfoldmap") | ||||
def items(self): | def items(self): | ||||
return self._map.items() | return self._map.items() | ||||
def keys(self): | |||||
return iter(self._map) | |||||
# forward for python2,3 compat | # forward for python2,3 compat | ||||
iteritems = items | iteritems = items | ||||
def _opendirstatefile(self): | def keys(self): | ||||
fp, mode = txnutil.trypending( | return iter(self._map) | ||||
self._root, self._opener, self._filename | |||||
) | |||||
if self._pendingmode is not None and self._pendingmode != mode: | |||||
fp.close() | |||||
raise error.Abort( | |||||
_(b'working directory state may be changed parallelly') | |||||
) | |||||
self._pendingmode = mode | |||||
return fp | |||||
def _readdirstatefile(self, size=-1): | ### reading/setting parents | ||||
try: | |||||
with self._opendirstatefile() as fp: | |||||
return fp.read(size) | |||||
except IOError as err: | |||||
if err.errno != errno.ENOENT: | |||||
raise | |||||
# File doesn't exist, so the current state is empty | |||||
return b'' | |||||
def setparents(self, p1, p2, fold_p2=False): | def setparents(self, p1, p2, fold_p2=False): | ||||
self._parents = (p1, p2) | self._parents = (p1, p2) | ||||
self._dirtyparents = True | self._dirtyparents = True | ||||
copies = {} | copies = {} | ||||
if fold_p2: | if fold_p2: | ||||
# Collect into an intermediate list to avoid a `RuntimeError` | # Collect into an intermediate list to avoid a `RuntimeError` | ||||
# exception due to mutation during iteration. | # exception due to mutation during iteration. | ||||
) | ) | ||||
else: | else: | ||||
raise error.Abort( | raise error.Abort( | ||||
_(b'working directory state appears damaged!') | _(b'working directory state appears damaged!') | ||||
) | ) | ||||
return self._parents | return self._parents | ||||
@property | ### disk interaction | ||||
def docket(self): | |||||
if not self._docket: | |||||
if not self._use_dirstate_v2: | |||||
raise error.ProgrammingError( | |||||
b'dirstate only has a docket in v2 format' | |||||
) | |||||
self._docket = docketmod.DirstateDocket.parse( | |||||
self._readdirstatefile(), self._nodeconstants | |||||
) | |||||
return self._docket | |||||
@propertycache | @propertycache | ||||
def _map(self): | def identity(self): | ||||
""" | self._map | ||||
Fills the Dirstatemap when called. | return self.identity | ||||
""" | |||||
# ignore HG_PENDING because identity is used only for writing | |||||
self.identity = util.filestat.frompath( | |||||
self._opener.join(self._filename) | |||||
) | |||||
if self._use_dirstate_v2: | |||||
if self.docket.uuid: | |||||
# TODO: use mmap when possible | |||||
data = self._opener.read(self.docket.data_filename()) | |||||
else: | |||||
data = b'' | |||||
self._map = rustmod.DirstateMap.new_v2( | |||||
data, self.docket.data_size, self.docket.tree_metadata | |||||
) | |||||
parents = self.docket.parents | |||||
else: | |||||
self._map, parents = rustmod.DirstateMap.new_v1( | |||||
self._readdirstatefile() | |||||
) | |||||
if parents and not self._dirtyparents: | |||||
self.setparents(*parents) | |||||
self.__contains__ = self._map.__contains__ | |||||
self.__getitem__ = self._map.__getitem__ | |||||
self.get = self._map.get | |||||
return self._map | |||||
def write(self, tr, st, now): | def write(self, tr, st, now): | ||||
if not self._use_dirstate_v2: | if not self._use_dirstate_v2: | ||||
p1, p2 = self.parents() | p1, p2 = self.parents() | ||||
packed = self._map.write_v1(p1, p2, now) | packed = self._map.write_v1(p1, p2, now) | ||||
st.write(packed) | st.write(packed) | ||||
st.close() | st.close() | ||||
self._dirtyparents = False | self._dirtyparents = False | ||||
tr.addpostclose(category, unlink) | tr.addpostclose(category, unlink) | ||||
else: | else: | ||||
unlink() | unlink() | ||||
self._docket = new_docket | self._docket = new_docket | ||||
# Reload from the newly-written file | # Reload from the newly-written file | ||||
util.clearcachedproperty(self, b"_map") | util.clearcachedproperty(self, b"_map") | ||||
self._dirtyparents = False | self._dirtyparents = False | ||||
def _opendirstatefile(self): | |||||
fp, mode = txnutil.trypending( | |||||
self._root, self._opener, self._filename | |||||
) | |||||
if self._pendingmode is not None and self._pendingmode != mode: | |||||
fp.close() | |||||
raise error.Abort( | |||||
_(b'working directory state may be changed parallelly') | |||||
) | |||||
self._pendingmode = mode | |||||
return fp | |||||
def _readdirstatefile(self, size=-1): | |||||
try: | |||||
with self._opendirstatefile() as fp: | |||||
return fp.read(size) | |||||
except IOError as err: | |||||
if err.errno != errno.ENOENT: | |||||
raise | |||||
# File doesn't exist, so the current state is empty | |||||
return b'' | |||||
### code related to maintaining and accessing "extra" property | |||||
# (e.g. "has_dir") | |||||
@propertycache | @propertycache | ||||
def filefoldmap(self): | def filefoldmap(self): | ||||
"""Returns a dictionary mapping normalized case paths to their | """Returns a dictionary mapping normalized case paths to their | ||||
non-normalized versions. | non-normalized versions. | ||||
""" | """ | ||||
return self._map.filefoldmapasdict() | return self._map.filefoldmapasdict() | ||||
def hastrackeddir(self, d): | def hastrackeddir(self, d): | ||||
return self._map.hastrackeddir(d) | return self._map.hastrackeddir(d) | ||||
def hasdir(self, d): | def hasdir(self, d): | ||||
return self._map.hasdir(d) | return self._map.hasdir(d) | ||||
@propertycache | @propertycache | ||||
def identity(self): | |||||
self._map | |||||
return self.identity | |||||
@propertycache | |||||
def dirfoldmap(self): | def dirfoldmap(self): | ||||
f = {} | f = {} | ||||
normcase = util.normcase | normcase = util.normcase | ||||
for name in self._map.tracked_dirs(): | for name in self._map.tracked_dirs(): | ||||
f[normcase(name)] = name | f[normcase(name)] = name | ||||
return f | return f | ||||
### code related to manipulation of entries and copy-sources | |||||
def set_possibly_dirty(self, filename): | def set_possibly_dirty(self, filename): | ||||
"""record that the current state of the file on disk is unknown""" | """record that the current state of the file on disk is unknown""" | ||||
entry = self[filename] | entry = self[filename] | ||||
entry.set_possibly_dirty() | entry.set_possibly_dirty() | ||||
self._map.set_dirstate_item(filename, entry) | self._map.set_dirstate_item(filename, entry) | ||||
def set_clean(self, filename, mode, size, mtime): | def set_clean(self, filename, mode, size, mtime): | ||||
"""mark a file as back to a clean state""" | """mark a file as back to a clean state""" | ||||
entry = self[filename] | entry = self[filename] | ||||
mtime = mtime & rangemask | mtime = mtime & rangemask | ||||
size = size & rangemask | size = size & rangemask | ||||
entry.set_clean(mode, size, mtime) | entry.set_clean(mode, size, mtime) | ||||
self._map.set_dirstate_item(filename, entry) | self._map.set_dirstate_item(filename, entry) | ||||
self._map.copymap().pop(filename, None) | self._map.copymap().pop(filename, None) | ||||
def __setitem__(self, key, value): | def __setitem__(self, key, value): | ||||
assert isinstance(value, DirstateItem) | assert isinstance(value, DirstateItem) | ||||
self._map.set_dirstate_item(key, value) | self._map.set_dirstate_item(key, value) | ||||
def reset_state( | |||||
self, | |||||
filename, | |||||
wc_tracked=False, | |||||
p1_tracked=False, | |||||
p2_tracked=False, | |||||
merged=False, | |||||
clean_p1=False, | |||||
clean_p2=False, | |||||
possibly_dirty=False, | |||||
parentfiledata=None, | |||||
): | |||||
"""Set a entry to a given state, disregarding all previous state | |||||
This is to be used by the part of the dirstate API dedicated to | |||||
adjusting the dirstate after a update/merge. | |||||
note: calling this might result to no entry existing at all if the | |||||
dirstate map does not see any point at having one for this file | |||||
anymore. | |||||
""" | |||||
if merged and (clean_p1 or clean_p2): | |||||
msg = ( | |||||
b'`merged` argument incompatible with `clean_p1`/`clean_p2`' | |||||
) | |||||
raise error.ProgrammingError(msg) | |||||
# copy information are now outdated | |||||
# (maybe new information should be in directly passed to this function) | |||||
self.copymap.pop(filename, None) | |||||
if not (p1_tracked or p2_tracked or wc_tracked): | |||||
self._map.drop_item_and_copy_source(filename) | |||||
elif merged: | |||||
# XXX might be merged and removed ? | |||||
entry = self.get(filename) | |||||
if entry is not None and entry.tracked: | |||||
# XXX mostly replicate dirstate.other parent. We should get | |||||
# the higher layer to pass us more reliable data where `merged` | |||||
# actually mean merged. Dropping the else clause will show | |||||
# failure in `test-graft.t` | |||||
self.addfile(filename, merged=True) | |||||
else: | |||||
self.addfile(filename, from_p2=True) | |||||
elif not (p1_tracked or p2_tracked) and wc_tracked: | |||||
self.addfile( | |||||
filename, added=True, possibly_dirty=possibly_dirty | |||||
) | |||||
elif (p1_tracked or p2_tracked) and not wc_tracked: | |||||
# XXX might be merged and removed ? | |||||
self[filename] = DirstateItem.from_v1_data(b'r', 0, 0, 0) | |||||
elif clean_p2 and wc_tracked: | |||||
if p1_tracked or self.get(filename) is not None: | |||||
# XXX the `self.get` call is catching some case in | |||||
# `test-merge-remove.t` where the file is tracked in p1, the | |||||
# p1_tracked argument is False. | |||||
# | |||||
# In addition, this seems to be a case where the file is marked | |||||
# as merged without actually being the result of a merge | |||||
# action. So thing are not ideal here. | |||||
self.addfile(filename, merged=True) | |||||
else: | |||||
self.addfile(filename, from_p2=True) | |||||
elif not p1_tracked and p2_tracked and wc_tracked: | |||||
self.addfile( | |||||
filename, from_p2=True, possibly_dirty=possibly_dirty | |||||
) | |||||
elif possibly_dirty: | |||||
self.addfile(filename, possibly_dirty=possibly_dirty) | |||||
elif wc_tracked: | |||||
# this is a "normal" file | |||||
if parentfiledata is None: | |||||
msg = b'failed to pass parentfiledata for a normal file: %s' | |||||
msg %= filename | |||||
raise error.ProgrammingError(msg) | |||||
mode, size, mtime = parentfiledata | |||||
self.addfile(filename, mode=mode, size=size, mtime=mtime) | |||||
else: | |||||
assert False, 'unreachable' | |||||
def set_tracked(self, filename): | |||||
new = False | |||||
entry = self.get(filename) | |||||
if entry is None: | |||||
self.addfile(filename, added=True) | |||||
new = True | |||||
elif not entry.tracked: | |||||
entry.set_tracked() | |||||
self._map.set_dirstate_item(filename, entry) | |||||
new = True | |||||
else: | |||||
# XXX This is probably overkill for more case, but we need this to | |||||
# fully replace the `normallookup` call with `set_tracked` one. | |||||
# Consider smoothing this in the future. | |||||
self.set_possibly_dirty(filename) | |||||
return new | |||||
def set_untracked(self, f): | |||||
"""Mark a file as no longer tracked in the dirstate map""" | |||||
# in merge is only trigger more logic, so it "fine" to pass it. | |||||
# | |||||
# the inner rust dirstate map code need to be adjusted once the API | |||||
# for dirstate/dirstatemap/DirstateItem is a bit more settled | |||||
entry = self.get(f) | |||||
if entry is None: | |||||
return False | |||||
else: | |||||
if entry.added: | |||||
self._map.drop_item_and_copy_source(f) | |||||
else: | |||||
self._map.removefile(f, in_merge=True) | |||||
return True | |||||
### Legacy method we need to get rid of | |||||
def addfile( | |||||
self, | |||||
f, | |||||
mode=0, | |||||
size=None, | |||||
mtime=None, | |||||
added=False, | |||||
merged=False, | |||||
from_p2=False, | |||||
possibly_dirty=False, | |||||
): | |||||
if added: | |||||
assert not possibly_dirty | |||||
assert not from_p2 | |||||
item = DirstateItem.new_added() | |||||
elif merged: | |||||
assert not possibly_dirty | |||||
assert not from_p2 | |||||
item = DirstateItem.new_merged() | |||||
elif from_p2: | |||||
assert not possibly_dirty | |||||
item = DirstateItem.new_from_p2() | |||||
elif possibly_dirty: | |||||
item = DirstateItem.new_possibly_dirty() | |||||
else: | |||||
assert size is not None | |||||
assert mtime is not None | |||||
size = size & rangemask | |||||
mtime = mtime & rangemask | |||||
item = DirstateItem.new_normal(mode, size, mtime) | |||||
self._map.addfile(f, item) | |||||
if added: | |||||
self.copymap.pop(f, None) | |||||
def removefile(self, *args, **kwargs): | |||||
return self._map.removefile(*args, **kwargs) |