def GitRevInfo(directory): """Get the git revision information of a git checkout. Args: directory: Existing git working directory. """ url = log_tools.CheckOutput(GitCmd() + ['ls-remote', '--get-url', 'origin'], cwd=directory) rev = log_tools.CheckOutput(GitCmd() + ['rev-parse', 'HEAD'], cwd=directory) return url.strip(), rev.strip()
def GitRevInfo(directory): """Get the git revision information of a git checkout. Args: directory: Existing git working directory. """ get_url_command = GitCmd() + ['ls-remote', '--get-url', 'origin'] url = log_tools.CheckOutput(get_url_command, cwd=directory).strip() # If the URL is actually a directory, it might be a git-cache directory. # Re-run from that directory to get the actual remote URL. if os.path.isdir(url): url = log_tools.CheckOutput(get_url_command, cwd=url).strip() rev = log_tools.CheckOutput(GitCmd() + ['rev-parse', 'HEAD'], cwd=directory).strip() return url, rev
def GetGitCacheURL(cache_dir, url, logger=None): """Converts a regular git URL to a git cache URL within a cache directory. Args: url: original Git URL that is already populated within the cache directory. cache_dir: Git cache directory that has already populated the URL. Returns: Git Cache URL where a git repository can clone/fetch from. """ # Make sure we are using absolute paths or else cache exists return relative. cache_dir = os.path.abspath(cache_dir) # For CygWin, we must first convert the cache_dir name to a non-cygwin path. cygwin_path = False if platform.IsCygWin() and cache_dir.startswith('/cygdrive/'): cygwin_path = True drive, file_path = cache_dir[len('/cygdrive/'):].split('/', 1) cache_dir = drive + ':\\' + file_path.replace('/', '\\') git_url = log_tools.CheckOutput(GitCmd() + ['cache', 'exists', '-c', cache_dir, url], logger=logger).strip() # For windows, make sure the git cache URL is a posix path. if platform.IsWindows(): git_url = git_url.replace('\\', '/') return git_url
def SvnRevInfo(directory): """Get the SVN revision information of an existing svn/gclient checkout. Args: directory: Directory where the svn repo is currently checked out """ info = log_tools.CheckOutput(SvnCmd() + ['info'], cwd=directory) url = '' rev = '' for line in info.splitlines(): pieces = line.split(':', 1) if len(pieces) != 2: continue if pieces[0] == 'URL': url = pieces[1].strip() elif pieces[0] == 'Revision': rev = pieces[1].strip() if not url or not rev: raise RuntimeError('Missing svn info url: %s and rev: %s' % (url, rev)) return url, rev
def GitRemoteRepoList(directory, include_fetch=True, include_push=True, logger=None): """Returns a list of remote git repos associated with a directory. Args: directory: Existing git working directory. Returns: List of (repo_name, repo_url) for tracked remote repos. """ remote_repos = log_tools.CheckOutput(GitCmd() + ['remote', '-v'], logger=logger, cwd=directory) repo_set = set() for remote_repo_line in remote_repos.splitlines(): repo_name, repo_url, repo_type = remote_repo_line.split() if include_fetch and repo_type == '(fetch)': repo_set.add((repo_name, repo_url)) elif include_push and repo_type == '(push)': repo_set.add((repo_name, repo_url)) return sorted(repo_set)
def CheckGitOutput(args): """Run a git subcommand and capture its stdout a la subprocess.check_output. Args: args: list of arguments to 'git' """ return log_tools.CheckOutput(GitCmd() + args)