def github_commit_web_link(self, commit_hash, repo=None): """Generates a link for a Github commit, if the repository belongs to Github. Args: commit_hash: SHA1 commit hash. repo: Git repository address. Returns: A Github link to the specified commit, if relevant, or None. """ assert len(commit_hash) == 40 # ID of a Git repository: "username/project": git_id = None ssh_prefix = "[email protected]:" git_prefix = "git://github.com/" if repo is None: repos = map(lambda pair: pair[1], self.ListRemoteAddresses()) else: repos = (repo,) for repo in repos: if repo.startswith(ssh_prefix): git_id = base.strip_prefix(repo, ssh_prefix) elif repo.startswith(git_prefix): git_id = base.strip_prefix(repo, git_prefix) else: pass if git_id is None: return None git_id = base.strip_optional_suffix(git_id, ".git") return "https://github.com/%s/commit/%s" % (git_id, commit_hash)
def list_submodule_status(self, recursive=True): """Lists the submodules in this git repository. Args: recursive: Whether to lists the submodules recursively. Yields: Tuples (git commit hash, submodule name, ref diff or None). """ args = ["submodule", "status"] if recursive: args.append("--recursive") cmd = self.command(*args) for line in cmd.output_lines: line = line.strip() if len(line) == 0: continue split = line.split() (commit_hash, module_name) = split[:2] if len(split) > 2: ref_diff = split[2] # ref_diff is formatted as "(<tag>-<commit-count>-g<short-hash>)" ref_diff = base.strip_prefix(ref_diff, "(") ref_diff = base.strip_suffix(ref_diff, ")") else: ref_diff = None yield (commit_hash, module_name, ref_diff)
def artifact_for_path(self, path): """Reverse engineer the Artifact coordinate from a repository file path. Args: path: Path of a Maven artifact in this repository. Returns: The parsed Artifact coordinate. None if the file path does not belong to this repository. """ path = "file://" + os.path.abspath(path) if not path.startswith(self.path): return None path = os.path.relpath(path, self.path) [*group_id, artifact_id, version, name] = path.split("/") group_id = ".".join(group_id) name = base.strip_prefix(name, artifact_id + "-" + version) if name.startswith("-"): (classifier, extension) = name[1:].split(".", 1) elif name.startswith("."): classifier = None extension = name[1:] else: raise Error("Invalid file name does not match expected Maven artifact format: {!r}".format(path)) if (classifier == "tests") and (extension == "jar"): packaging = "test-jar" else: packaging = extension return artifact.Artifact( group_id=group_id, artifact_id=artifact_id, version=version, classifier=classifier, packaging=packaging )
def artifact_for_path(self, path): """Reverse engineer the Artifact coordinate from a repository file path. Args: path: Path of a Maven artifact in this repository. Returns: The parsed Artifact coordinate. None if the file path does not belong to this repository. """ path = "file://" + os.path.abspath(path) if not path.startswith(self.path): return None path = os.path.relpath(path, self.path) [*group_id, artifact_id, version, name] = path.split("/") group_id = ".".join(group_id) name = base.strip_prefix(name, artifact_id + "-" + version) if name.startswith("-"): (classifier, extension) = name[1:].split(".", 1) elif name.startswith("."): classifier = None extension = name[1:] else: raise Error( "Invalid file name does not match expected Maven artifact format: {!r}" .format(path)) if (classifier == "tests") and (extension == "jar"): packaging = "test-jar" else: packaging = extension return artifact.Artifact( group_id=group_id, artifact_id=artifact_id, version=version, classifier=classifier, packaging=packaging, )