Lines Matching +full:rev +full:- +full:parse
2 # -*- coding: utf-8 -*-
4 # Use of this source code is governed by a BSD-style license that can be
31 # llvm-svn: 375505
39 # - |dir| is the directory of the LLVM checkout
40 # - |remote| is the name of the LLVM remote. Generally it's "origin".
44 class Rev(t.NamedTuple("Rev", (("branch", str), ("number", int)))): class
48 def parse(rev: str) -> "Rev": member in Rev
49 """Parses a Rev from the given string.
51 Raises a ValueError on a failed parse.
58 if rev.startswith("r"):
60 rev_string = rev[1:]
62 match = re.match(r"\((.+), r(\d+)\)", rev)
64 raise ValueError("%r isn't a valid revision" % rev)
68 return Rev(branch=branch_name, number=int(rev_string))
70 def __str__(self) -> str:
77 def is_git_sha(xs: str) -> bool:
86 def check_output(command: t.List[str], cwd: str) -> str:
87 """Shorthand for subprocess.check_output. Auto-decodes any stdout."""
94 encoding="utf-8",
101 ) -> int:
102 """Translates a sha to a revision number (e.g., "llvm-svn: 1234").
107 ["git", "log", "-n1", "--format=%B", sha],
110 last_line = commit_message.strip().splitlines()[-1]
111 svn_match = re.match(r"^llvm-svn: (\d+)$", last_line)
115 f"No llvm-svn line found for {sha}, which... shouldn't happen?"
121 def translate_sha_to_rev(llvm_config: LLVMConfig, sha_or_ref: str) -> Rev:
122 """Translates a sha or git ref to a Rev."""
128 ["git", "rev-parse", sha_or_ref],
134 ["git", "merge-base", base_llvm_sha, sha],
143 "rev-list",
144 "--count",
145 "--first-parent",
151 return Rev(branch=MAIN_BRANCH, number=count + base_llvm_revision)
154 # - |merge_base| is |sha| (we have a guaranteed llvm-svn number on |sha|)
155 # - |merge_base| is neither (we have a guaranteed llvm-svn number on
161 return Rev(branch=MAIN_BRANCH, number=merge_base_number)
166 "rev-list",
167 "--count",
168 "--first-parent",
176 ["git", "branch", "-r", "--contains", sha],
204 return Rev(branch=candidates[0], number=revision_number)
209 ) -> t.Iterable[t.Tuple[str, str]]:
229 # pylint: disable=stop-iteration-return
246 def translate_prebase_rev_to_sha(llvm_config: LLVMConfig, rev: Rev) -> str: argument
247 """Translates a Rev to a SHA.
249 This function assumes that the given rev refers to a commit that's an
252 # Because reverts may include reverted commit messages, we can't just |-n1|
255 looking_for = f"llvm-svn: {rev.number}"
260 "--grep",
262 f"--format=%H%n%B{separator}",
271 encoding="utf-8",
276 last_line = message.splitlines()[-1]
283 raise ValueError(f"No commit with revision {rev} found")
286 def translate_rev_to_sha(llvm_config: LLVMConfig, rev: Rev) -> str: argument
287 """Translates a Rev to a SHA.
289 Raises a ValueError if the given Rev doesn't exist in the given config.
291 branch, number = rev
295 return translate_prebase_rev_to_sha(llvm_config, rev)
302 "merge-base",
316 # Alternatively, we could |git log --format=%H|, but git is *super* fast
317 # about rev walking/counting locally compared to long |log|s, so we walk back
320 ["git", "rev-parse", f"{llvm_config.remote}/{branch}"],
325 commit_number = number - base_revision_number
329 "rev-list",
330 "--count",
331 "--first-parent",
338 commits_behind_head = revs_between - commit_number
341 f"Revision {rev} is past {llvm_config.remote}/{branch}. Try updating "
346 ["git", "rev-parse", f"{branch_head_sha}~{commits_behind_head}"],
353 def find_root_llvm_dir(root_dir: str = ".") -> str:
359 ["git", "rev-parse", "--show-toplevel"],
365 def main(argv: t.List[str]) -> None:
368 "--llvm_dir",
373 "--upstream",
379 "--sha", help="A git SHA (or ref) to convert to a rev"
381 sha_or_rev.add_argument("--rev", help="A rev to convert into a sha")
390 "Couldn't autodetect an LLVM tree; please use --llvm_dir"
399 rev = translate_sha_to_rev(config, opts.sha)
400 print(rev)
402 sha = translate_rev_to_sha(config, Rev.parse(opts.rev))