diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-07-15 02:04:13 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-07-15 02:04:13 +0000 |
commit | c72a0e5f0d0e5ac38313e63ff50cd18ab0a9c77c (patch) | |
tree | 73936aba47fe1dc71e9cc05af9747036e935608c /llvm_tools/git.py | |
parent | b75f321fc8978b92ce3db6886ccb966768f0c7a8 (diff) | |
parent | 4e4201457e5f51a132101c611c79ccff9f713c8b (diff) | |
download | toolchain-utils-c72a0e5f0d0e5ac38313e63ff50cd18ab0a9c77c.tar.gz |
Snap for 7550930 from 4e4201457e5f51a132101c611c79ccff9f713c8b to mainline-captiveportallogin-releaseandroid-mainline-12.0.0_r6android-mainline-12.0.0_r23android12-mainline-captiveportallogin-release
Change-Id: Iedfae00fc40c41956a3a0b9a7b94674329a6130a
Diffstat (limited to 'llvm_tools/git.py')
-rwxr-xr-x | llvm_tools/git.py | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/llvm_tools/git.py b/llvm_tools/git.py new file mode 100755 index 00000000..f38d5e72 --- /dev/null +++ b/llvm_tools/git.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Git helper functions.""" + +from __future__ import print_function + +import collections +import os +import re +import subprocess +import tempfile + +CommitContents = collections.namedtuple('CommitContents', ['url', 'cl_number']) + + +def InChroot(): + """Returns True if currently in the chroot.""" + return 'CROS_WORKON_SRCROOT' in os.environ + + +def VerifyOutsideChroot(): + """Checks whether the script invoked was executed in the chroot. + + Raises: + AssertionError: The script was run inside the chroot. + """ + + assert not InChroot(), 'Script should be run outside the chroot.' + + +def CreateBranch(repo, branch): + """Creates a branch in the given repo. + + Args: + repo: The absolute path to the repo. + branch: The name of the branch to create. + + Raises: + ValueError: Failed to create a repo in that directory. + """ + + if not os.path.isdir(repo): + raise ValueError('Invalid directory path provided: %s' % repo) + + subprocess.check_output(['git', '-C', repo, 'reset', 'HEAD', '--hard']) + + subprocess.check_output(['repo', 'start', branch], cwd=repo) + + +def DeleteBranch(repo, branch): + """Deletes a branch in the given repo. + + Args: + repo: The absolute path of the repo. + branch: The name of the branch to delete. + + Raises: + ValueError: Failed to delete the repo in that directory. + """ + + if not os.path.isdir(repo): + raise ValueError('Invalid directory path provided: %s' % repo) + + subprocess.check_output(['git', '-C', repo, 'checkout', 'cros/master']) + + subprocess.check_output(['git', '-C', repo, 'reset', 'HEAD', '--hard']) + + subprocess.check_output(['git', '-C', repo, 'branch', '-D', branch]) + + +def UploadChanges(repo, branch, commit_messages): + """Uploads the changes in the specifed branch of the given repo for review. + + Args: + repo: The absolute path to the repo where changes were made. + branch: The name of the branch to upload. + commit_messages: A string of commit message(s) (i.e. '[message]' + of the changes made. + + Returns: + A nametuple that has two (key, value) pairs, where the first pair is the + Gerrit commit URL and the second pair is the change list number. + + Raises: + ValueError: Failed to create a commit or failed to upload the + changes for review. + """ + + if not os.path.isdir(repo): + raise ValueError('Invalid path provided: %s' % repo) + + # Create a git commit. + with tempfile.NamedTemporaryFile(mode='w+t') as f: + f.write('\n'.join(commit_messages)) + f.flush() + + subprocess.check_output(['git', 'commit', '-F', f.name], cwd=repo) + + # Upload the changes for review. + out = subprocess.check_output( + ['repo', 'upload', '--yes', '--ne', '--no-verify', + '--br=%s' % branch], + stderr=subprocess.STDOUT, + cwd=repo, + encoding='utf-8') + + print(out) + + found_url = re.search( + r'https://chromium-review.googlesource.com/c/' + r'chromiumos/overlays/chromiumos-overlay/\+/([0-9]+)', out.rstrip()) + + if not found_url: + raise ValueError('Failed to find change list URL.') + + return CommitContents( + url=found_url.group(0), cl_number=int(found_url.group(1))) |