# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this, # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import subprocess import uuid from contextlib import contextmanager from datetime import datetime from pathlib import Path from typing import Dict, Iterator, List, Optional, Union
from mozpack.files import FileListFinder
from mozversioncontrol.errors import (
CannotDeleteFromRootOfRepositoryException,
MissingVCSExtension,
) from mozversioncontrol.repo.base import Repository
class GitRepository(Repository): """An implementation of `Repository` for Git repositories."""
def get_mozilla_upstream_remotes(self) -> Iterator[str]: """Return the Mozilla-official upstream remotes for this repo."""
out = self._run("remote", "-v") ifnot out: return
remotes = out.splitlines() ifnot remotes: return
for line in remotes:
name, url, action = line.split()
# Only consider fetch sources. if action != "(fetch)": continue
# Return any `hg.mozilla.org` remotes, ignoring `try`. if"hg.mozilla.org"in url andnot url.endswith("hg.mozilla.org/try"): yield name
def get_mozilla_remote_args(self) -> List[str]: """Return a list of `--remotes` arguments to limit commits to official remotes."""
official_remotes = [
f"--remotes={remote}"for remote in self.get_mozilla_upstream_remotes()
]
return official_remotes if official_remotes else ["--remotes"]
def get_outgoing_files(self, diff_filter="ADM", upstream=None): assert all(f.lower() in self._valid_diff_filter for f in diff_filter)
not_condition = upstream if upstream else"--remotes"
files = self._run( "log", "--name-only", "--diff-filter={}".format(diff_filter.upper()), "--oneline", "--pretty=format:", "HEAD", "--not",
not_condition,
).splitlines() return [f for f in files if f]
def get_tracked_files_finder(self, path=None):
files = [p for p in self._run("ls-files", "-z").split("\0") if p] return FileListFinder(files)
def get_ignored_files_finder(self):
files = [
p for p in self._run( "ls-files", "-i", "-o", "-z", "--exclude-standard"
).split("\0") if p
] return FileListFinder(files)
# Even in --porcelain mode, behavior is affected by the # ``status.showUntrackedFiles`` option, which means we need to be # explicit about how to treat untracked files. if untracked:
args.append("--untracked-files=all") else:
args.append("--untracked-files=no")
if ignored:
args.append("--ignored")
returnnot len(self._run(*args).strip())
def clean_directory(self, path: Union[str, Path]): if Path(self.path).samefile(path): raise CannotDeleteFromRootOfRepositoryException()
with self.try_commit(message, changed_files) as head:
cmd = (
str(self._tool), "-c", # Never store git-cinnabar metadata for pushes to try. # Normally git-cinnabar asks the server what the phase of what it pushed # is, and figures on its own, but that request takes a long time on try. "cinnabar.data=never", "push", "hg::ssh://hg.mozilla.org/try",
f"+{head}:refs/heads/branches/default/tip",
) if allow_log_capture:
self._push_to_try_with_log_capture(
cmd,
{ "stdout": subprocess.PIPE, "stderr": subprocess.STDOUT, "cwd": self.path, "universal_newlines": True, "bufsize": 1,
},
) else:
subprocess.check_call(cmd, cwd=self.path)
def get_branch_nodes(self, head: Optional[str] = None) -> List[str]: """Return a list of commit SHAs for nodes on the current branch."""
remote_args = self.get_mozilla_remote_args()
return self._run( "log",
head or"HEAD", "--reverse", "--not",
*remote_args, "--pretty=%H",
).splitlines()
def get_commit_patches(self, nodes: List[str]) -> List[bytes]: """Return the contents of the patch `node` in the VCS' standard format.""" return [
self._run("format-patch", node, "-1", "--always", "--stdout", encoding=None) for node in nodes
]
@contextmanager def try_commit(
self, commit_message: str, changed_files: Optional[Dict[str, str]] = None
): """Create a temporary try commit as a context manager.
Create a new commit using `commit_message` as the commit message. The commit
may be empty, for example when only including try syntax.
`changed_files` may contain a dict of file paths and their contents,
see `stage_changes`. """
current_head = self.head_ref
author = self._run("var", "GIT_AUTHOR_IDENT").strip()
committer = self._run("var", "GIT_COMMITTER_IDENT").strip() # A random enough temporary branch name that shouldn't conflict with # anything else, even in the machtry namespace.
branch = str(uuid.uuid4()) # The following fast-import script creates a new commit on a temporary # branch that it deletes at the end, based off the current HEAD, and # adding or modifying the files from `changed_files`. # fast-import will output the sha1 for that temporary commit on stdout # (via `get-mark`).
fast_import = "\n".join(
[
f"commit refs/machtry/{branch}", "mark :1",
f"author {author}",
f"committer {committer}",
data(commit_message),
f"from {current_head}", "\n".join(
f"M 100644 inline {path}\n{data(content)}" for path, content in (changed_files or {}).items()
),
f"reset refs/machtry/{branch}", "from 0000000000000000000000000000000000000000", "get-mark :1", "",
]
)
cmd = (str(self._tool), "fast-import", "--quiet")
stdout = subprocess.check_output(
cmd,
cwd=self.path,
env=self._env, # text=True changes line endings on Windows, and git fast-import # doesn't like \r\n.
input=fast_import.encode("utf-8"),
)
# Keep trace of the temporary push in the reflog, as if we did actually commit. # This does update HEAD for a small window of time. # If we raced with something else that changed the HEAD after we created our # commit, update-ref will fail and print an error message. Only the update in # the reflog would be lost in this case.
self._run("update-ref", "-m", "mach try: push", "HEAD", try_head, current_head) # Likewise, if we raced with something else that updated the HEAD between our # two update-ref, update-ref will fail and print an error message.
self._run( "update-ref", "-m", "mach try: restore", "HEAD",
current_head,
try_head,
)
def get_last_modified_time_for_file(self, path: Path): """Return last modified in VCS time for the specified file."""
out = self._run("log", "-1", "--format=%ad", "--date=iso", path)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.