##// END OF EJS Templates
release: added 2 ee-only features to release notes
release: added 2 ee-only features to release notes

File last commit:

r5651:bad147da default
r5661:2ba2d4b2 default
Show More
commit.py
397 lines | 12.8 KiB | text/x-python | PythonLexer
core: updated copyright to 2024
r5608 # Copyright (C) 2014-2024 RhodeCode GmbH
project: added all source files and assets
r1 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
"""
HG commit module
"""
fix(hg-items): fixed missing submodules code and add forgotten stats item
r5649 import os
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 import logging
project: added all source files and assets
r1
from zope.cachedescriptors.property import Lazy as LazyProperty
dan
vcs: make commit datetimes utc
r154 from rhodecode.lib.datelib import utcdate_fromtimestamp
vcs-lib: bulk of changes for python3 support
r5074 from rhodecode.lib.str_utils import safe_bytes, safe_str
project: added all source files and assets
r1 from rhodecode.lib.vcs.backends import base
from rhodecode.lib.vcs.exceptions import CommitError
from rhodecode.lib.vcs.nodes import (
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 DirNode,
FileNode,
NodeKind,
RootNode,
SubModuleNode,
LargeFileNode,
)
from rhodecode.lib.vcs_common import FILEMODE_LINK
log = logging.getLogger(__name__)
project: added all source files and assets
r1
class MercurialCommit(base.BaseCommit):
"""
Represents state of the repository at the single commit.
"""
_filter_pre_load = [
# git specific property not supported here
"_commit",
]
def __init__(self, repository, raw_id, idx, pre_load=None):
raw_id = safe_str(raw_id)
self.repository = repository
self._remote = repository._remote
self.raw_id = raw_id
mercurial: compatability changes for mercurial 4.6.0 release
r2735 self.idx = idx
project: added all source files and assets
r1
self._set_bulk_properties(pre_load)
# caches
self.nodes = {}
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 self._path_mode_cache = {} # path stats cache, e.g filemode etc
self._path_type_cache = {} # path type dir/file/link etc cache
project: added all source files and assets
r1
def _set_bulk_properties(self, pre_load):
if not pre_load:
return
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 pre_load = [entry for entry in pre_load if entry not in self._filter_pre_load]
project: added all source files and assets
r1 if not pre_load:
return
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 result = self._remote.bulk_request(self.raw_id, pre_load)
python3: fixed various code issues...
r4973
project: added all source files and assets
r1 for attr, value in result.items():
if attr in ["author", "branch", "message"]:
vcs-lib: bulk of changes for python3 support
r5074 value = safe_str(value)
project: added all source files and assets
r1 elif attr == "affected_files":
vcs-lib: bulk of changes for python3 support
r5074 value = list(map(safe_str, value))
project: added all source files and assets
r1 elif attr == "date":
dan
vcs: make commit datetimes utc
r154 value = utcdate_fromtimestamp(*value)
project: added all source files and assets
r1 elif attr in ["children", "parents"]:
value = self._make_commits(value)
changelog: fix and optimize loading of chunks for file history....
r2130 elif attr in ["phase"]:
value = self._get_phase_text(value)
project: added all source files and assets
r1 self.__dict__[attr] = value
@LazyProperty
def tags(self):
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 tags = [name for name, commit_id in self.repository.tags.items() if commit_id == self.raw_id]
project: added all source files and assets
r1 return tags
@LazyProperty
def branch(self):
vcs-lib: bulk of changes for python3 support
r5074 return safe_str(self._remote.ctx_branch(self.raw_id))
project: added all source files and assets
r1
@LazyProperty
def bookmarks(self):
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 bookmarks = [name for name, commit_id in self.repository.bookmarks.items() if commit_id == self.raw_id]
project: added all source files and assets
r1 return bookmarks
@LazyProperty
def message(self):
vcs-lib: bulk of changes for python3 support
r5074 return safe_str(self._remote.ctx_description(self.raw_id))
project: added all source files and assets
r1
@LazyProperty
def committer(self):
vcs-lib: bulk of changes for python3 support
r5074 return safe_str(self.author)
project: added all source files and assets
r1
@LazyProperty
def author(self):
vcs-lib: bulk of changes for python3 support
r5074 return safe_str(self._remote.ctx_user(self.raw_id))
project: added all source files and assets
r1
@LazyProperty
def date(self):
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 return utcdate_fromtimestamp(*self._remote.ctx_date(self.raw_id))
project: added all source files and assets
r1
@LazyProperty
def status(self):
"""
Returns modified, added, removed, deleted files for current commit
"""
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 modified, added, deleted, *_ = self._remote.ctx_status(self.raw_id)
return modified, added, deleted
project: added all source files and assets
r1
@LazyProperty
def id(self):
if self.last:
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 return "tip"
project: added all source files and assets
r1 return self.short_id
@LazyProperty
def short_id(self):
return self.raw_id[:12]
mercurial: use commit_ids for parents/children instead of revisions which could change when evolve is used.
r3935 def _make_commits(self, commit_ids, pre_load=None):
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 return [self.repository.get_commit(commit_id=commit_id, pre_load=pre_load) for commit_id in commit_ids]
project: added all source files and assets
r1
@LazyProperty
def parents(self):
"""
Returns list of parent commits.
"""
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 parents = self._remote.ctx_parents(self.raw_id)
project: added all source files and assets
r1 return self._make_commits(parents)
changelog: fix and optimize loading of chunks for file history....
r2130 def _get_phase_text(self, phase_id):
return {
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 0: "public",
1: "draft",
2: "secret",
}.get(phase_id) or ""
changelog: fix and optimize loading of chunks for file history....
r2130
project: added all source files and assets
r1 @LazyProperty
mercurial: show phases status in changelog....
r1674 def phase(self):
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 phase_id = self._remote.ctx_phase(self.raw_id)
changelog: fix and optimize loading of chunks for file history....
r2130 phase_text = self._get_phase_text(phase_id)
mercurial: show phases status in changelog....
r1674
vcs-lib: bulk of changes for python3 support
r5074 return safe_str(phase_text)
mercurial: show phases status in changelog....
r1674
@LazyProperty
mercurial-evolve: enable evolve setting on repositories.
r1738 def obsolete(self):
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 obsolete = self._remote.ctx_obsolete(self.raw_id)
mercurial-evolve: enable evolve setting on repositories.
r1738 return obsolete
@LazyProperty
def hidden(self):
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 hidden = self._remote.ctx_hidden(self.raw_id)
mercurial-evolve: enable evolve setting on repositories.
r1738 return hidden
@LazyProperty
project: added all source files and assets
r1 def children(self):
"""
Returns list of child commits.
"""
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 children = self._remote.ctx_children(self.raw_id)
project: added all source files and assets
r1 return self._make_commits(children)
def _get_kind(self, path):
path = self._fix_path(path)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 path_type = self._get_path_type(path)
return path_type
project: added all source files and assets
r1
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 def _assert_is_path(self, path) -> str | bytes:
project: added all source files and assets
r1 path = self._fix_path(path)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647
project: added all source files and assets
r1 if self._get_kind(path) != NodeKind.FILE:
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 raise CommitError(f"File at path={path} does not exist for commit {self.raw_id}")
vcs-lib: bulk of changes for python3 support
r5074
project: added all source files and assets
r1 return path
vcs-lib: bulk of changes for python3 support
r5074 def get_file_mode(self, path: bytes):
project: added all source files and assets
r1 """
Returns stat mode of the file at the given ``path``.
"""
vcs-lib: bulk of changes for python3 support
r5074 path = self._assert_is_path(path)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 if path not in self._path_mode_cache:
self._path_mode_cache[path] = self._remote.fctx_flags(self.raw_id, path)
vcs-lib: bulk of changes for python3 support
r5074
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 return self._path_mode_cache[path]
vcs-lib: bulk of changes for python3 support
r5074
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 def is_link(self, path: bytes):
path = self._assert_is_path(path)
if path not in self._path_mode_cache:
self._path_mode_cache[path] = self._remote.fctx_flags(self.raw_id, path)
project: added all source files and assets
r1
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 return self._path_mode_cache[path] == FILEMODE_LINK
project: added all source files and assets
r1
dan
vcsserver: made binary content check be calculated on vcsserver...
r3896 def is_node_binary(self, path):
vcs-lib: bulk of changes for python3 support
r5074 path = self._assert_is_path(path)
dan
vcsserver: made binary content check be calculated on vcsserver...
r3896 return self._remote.is_binary(self.raw_id, path)
vcs-lib: bulk of changes for python3 support
r5074 def node_md5_hash(self, path):
path = self._assert_is_path(path)
return self._remote.md5_hash(self.raw_id, path)
project: added all source files and assets
r1 def get_file_content(self, path):
"""
Returns content of the file at given ``path``.
"""
vcs-lib: bulk of changes for python3 support
r5074 path = self._assert_is_path(path)
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 return self._remote.fctx_node_data(self.raw_id, path)
project: added all source files and assets
r1
dan
file-nodes: added streaming remote attributes for vcsserver....
r3895 def get_file_content_streamed(self, path):
vcs-lib: bulk of changes for python3 support
r5074 path = self._assert_is_path(path)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 stream_method = getattr(self._remote, "stream:fctx_node_data")
dan
file-nodes: added streaming remote attributes for vcsserver....
r3895 return stream_method(self.raw_id, path)
project: added all source files and assets
r1 def get_file_size(self, path):
"""
Returns size of the file at given ``path``.
"""
vcs-lib: bulk of changes for python3 support
r5074 path = self._assert_is_path(path)
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 return self._remote.fctx_size(self.raw_id, path)
project: added all source files and assets
r1
vcs: rename get_file_history to get_path_history as it better reflects what it does.
r3275 def get_path_history(self, path, limit=None, pre_load=None):
project: added all source files and assets
r1 """
Returns history of file as reversed list of `MercurialCommit` objects
for which file at given ``path`` has been modified.
"""
vcs-lib: bulk of changes for python3 support
r5074 path = self._assert_is_path(path)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 history = self._remote.node_history(self.raw_id, path, limit)
return [self.repository.get_commit(commit_id=commit_id, pre_load=pre_load) for commit_id in history]
project: added all source files and assets
r1
def get_file_annotate(self, path, pre_load=None):
"""
Returns a generator of four element tuples with
lineno, commit_id, commit lazy loader and line
"""
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 result = self._remote.fctx_annotate(self.raw_id, path)
project: added all source files and assets
r1
for ln_no, commit_id, content in result:
yield (
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 ln_no,
commit_id,
git: replaced some raw subprocess commands with dedicated GIT vcsserver commands.
r3862 lambda: self.repository.get_commit(commit_id=commit_id, pre_load=pre_load),
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 content,
)
project: added all source files and assets
r1
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 def get_nodes(self, path: bytes, pre_load=None):
project: added all source files and assets
r1 """
Returns combined ``DirNode`` and ``FileNode`` objects list representing
state of commit at the given ``path``. If node at the given ``path``
is not instance of ``DirNode``, CommitError would be raised.
"""
if self._get_kind(path) != NodeKind.DIR:
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 raise CommitError(f"Directory does not exist for idx {self.raw_id} at '{path}'")
project: added all source files and assets
r1 path = self._fix_path(path)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 path_nodes = []
fix(file-caching): fixed cases when old cache was used before changes to operate on bytestrings
r5651 for obj_path, node_kind, flags, pre_load_data in self._remote.get_nodes(self.raw_id, path, pre_load):
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647
if node_kind is None:
raise CommitError(f"Requested object type={node_kind} cannot be mapped to a proper type")
fix(hg-items): fixed missing submodules code and add forgotten stats item
r5649 stat_ = flags
# cache file mode
if obj_path not in self._path_mode_cache:
self._path_mode_cache[obj_path] = stat_
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647
# cache type
if node_kind not in self._path_type_cache:
self._path_type_cache[obj_path] = node_kind
project: added all source files and assets
r1
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 entry = None
if obj_path in self.nodes:
entry = self.nodes[obj_path]
else:
if node_kind == NodeKind.DIR:
entry = DirNode(safe_bytes(obj_path), commit=self)
elif node_kind == NodeKind.FILE:
fix(file-caching): fixed cases when old cache was used before changes to operate on bytestrings
r5651 entry = FileNode(safe_bytes(obj_path), commit=self, mode=stat_, pre_load=pre_load, pre_load_data=pre_load_data)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 if entry:
self.nodes[obj_path] = entry
path_nodes.append(entry)
git: adjusted code for new libgit2 backend...
r3842
fix(hg-items): fixed missing submodules code and add forgotten stats item
r5649 for obj_path, (location, commit, scm_type) in self._submodules.items():
if os.path.dirname(obj_path) == path:
entry = SubModuleNode(obj_path, url=location, commit=commit, alias=scm_type)
self.nodes[obj_path] = entry
path_nodes.append(entry)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 path_nodes.sort()
return path_nodes
project: added all source files and assets
r1
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 def get_node(self, path: bytes, pre_load=None):
project: added all source files and assets
r1 """
Returns `Node` object from the given `path`. If there is no node at
the given `path`, `NodeDoesNotExistError` would be raised.
"""
path = self._fix_path(path)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 # use cached, if we have one
if path in self.nodes:
return self.nodes[path]
path_type = self._get_path_type(path)
if path == b"":
node = RootNode(commit=self)
else:
if path_type == NodeKind.DIR:
node = DirNode(safe_bytes(path), commit=self)
elif path_type == NodeKind.FILE:
vcs-lib: bulk of changes for python3 support
r5074 node = FileNode(safe_bytes(path), commit=self, pre_load=pre_load)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 self._path_mode_cache[path] = node.mode
project: added all source files and assets
r1 else:
raise self.no_node_at_path(path)
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 # cache node
self.nodes[path] = node
project: added all source files and assets
r1 return self.nodes[path]
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 def _get_path_type(self, path: bytes):
if path in self._path_type_cache:
return self._path_type_cache[path]
if path == b"":
self._path_type_cache[b""] = NodeKind.DIR
return NodeKind.DIR
path_type, flags = self._remote.get_path_type(self.raw_id, path)
if not path_type:
raise self.no_node_at_path(path)
self._path_type_cache[path] = path_type
self._path_mode_cache[path] = flags
return self._path_type_cache[path]
def get_largefile_node(self, path: bytes):
dan
files: ensure we ask hg vcsserver check for largefiles for path and commit, else we cache path that might become a LF node later on
r3899 pointer_spec = self._remote.is_large_file(self.raw_id, path)
files: only check for git_lfs/hg_largefiles if they are enabled....
r3894 if pointer_spec:
project: added all source files and assets
r1 # content of that file regular FileNode is the hash of largefile
file_id = self.get_file_content(path).strip()
largefiles: enabled download of largefiles for git and mercurial from web interface....
r1577
if self._remote.in_largefiles_store(file_id):
lf_path = self._remote.store_path(file_id)
vcs-lib: bulk of changes for python3 support
r5074 return LargeFileNode(safe_bytes(lf_path), commit=self, org_path=path)
project: added all source files and assets
r1 elif self._remote.in_user_cache(file_id):
largefiles: enabled download of largefiles for git and mercurial from web interface....
r1577 lf_path = self._remote.store_path(file_id)
project: added all source files and assets
r1 self._remote.link(file_id, path)
vcs-lib: bulk of changes for python3 support
r5074 return LargeFileNode(safe_bytes(lf_path), commit=self, org_path=path)
project: added all source files and assets
r1
@LazyProperty
def _submodules(self):
"""
Returns a dictionary with submodule information from substate file
of hg repository.
"""
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 return self._remote.ctx_substate(self.raw_id)
project: added all source files and assets
r1
@LazyProperty
fix(encoding for file): fixed support of non utf-8 files in all backends
r5647 def affected_files(self) -> list[bytes]:
project: added all source files and assets
r1 """
Gets a fast accessible file changes for given commit
"""
mercurial: use hash as vcsserver parameters instead of ints which can change even on a server side when a strip is performed.
r3859 return self._remote.ctx_files(self.raw_id)
project: added all source files and assets
r1
api: exposed modified added/modified/deleted functions of commit to return only paths....
r4242 @LazyProperty
def added_paths(self):
return [n for n in self.status[1]]
project: added all source files and assets
r1
api: exposed modified added/modified/deleted functions of commit to return only paths....
r4242 @LazyProperty
def changed_paths(self):
return [n for n in self.status[0]]
project: added all source files and assets
r1
api: exposed modified added/modified/deleted functions of commit to return only paths....
r4242
@LazyProperty
def removed_paths(self):
return [n for n in self.status[2]]