[upstream_utils] Refactor upstream_utils scripts (#4367)

* Root folder variable names are now more descriptive
* clone_repo() now restores the current working directory
* Removed setup_upstream_repo() since it's now identical to clone_repo()
* Moved am_patches()'s for loop into user scripts so the filename prefix
  doesn't need to be included in every patch filename
* Renamed am_patches() to git_am() since its only job now is to run "git am"
* Removed unused apply_patches() function
* Fixed typo in git_am()'s ignore_whitespace arg name
This commit is contained in:
Tyler Veness
2022-08-20 07:26:34 -07:00
committed by GitHub
parent d80e8039d7
commit 5adf50d93c
7 changed files with 140 additions and 178 deletions

View File

@@ -6,16 +6,18 @@ import tempfile
def clone_repo(url, treeish, shallow=True):
"""Clones a git repo at the given URL into a temp folder and checks out the
given tree-ish (either branch or tag).
The current working directory will be set to the repository folder.
"""Clones a Git repo at the given URL into a temp folder, checks out the
given tree-ish (either branch or tag), then returns the repo root.
Keyword argument:
url -- The URL of the git repo
url -- The URL of the Git repo
treeish -- The tree-ish to check out (branch or tag)
shallow -- Whether to do a shallow clone
Returns:
root -- root directory of the cloned Git repository
"""
cwd = os.getcwd()
os.chdir(tempfile.gettempdir())
repo = os.path.basename(url)
@@ -49,6 +51,9 @@ def clone_repo(url, treeish, shallow=True):
else:
subprocess.run(["git", "checkout", treeish])
os.chdir(cwd)
return dest
def get_repo_root():
"""Returns the Git repository root as an absolute path.
@@ -63,27 +68,6 @@ def get_repo_root():
return ""
def setup_upstream_repo(url, treeish, shallow=True):
"""Clones the given upstream repository, then returns the root of the
destination Git repository as well as the cloned upstream Git repository.
The current working directory will be set to the cloned upstream repository
folder.
Keyword arguments:
url -- The URL of the git repo
treeish -- The tree-ish to check out (branch or tag)
shallow -- Whether to do a shallow clone
Returns:
root -- root directory of destination Git repository
repo -- root directory of cloned upstream Git repository
"""
root = get_repo_root()
clone_repo(url, treeish, shallow=shallow)
return root, os.getcwd()
def walk_if(top, pred):
"""Walks the current directory, then returns a list of files for which the
given predicate is true.
@@ -196,32 +180,18 @@ def comment_out_invalid_includes(filename, include_roots):
f.write(new_contents)
def apply_patches(root, patches):
"""Apply list of patches to the destination Git repository using "git
apply".
def git_am(patch, use_threeway=False, ignore_whitespace=False):
"""Apply patch to a Git repository in the current directory using "git am".
Keyword arguments:
root -- the root directory of the destination Git repository
patches -- list of patch files relative to the root
patch -- patch file relative to the root
use_threeway -- use a three-way merge when applying the patch
ignore_whitespace -- ignore whitespace in the patch file
"""
os.chdir(root)
for patch in patches:
subprocess.check_output(["git", "apply", patch])
def am_patches(root, patches, use_threeway=False, ignore_whitespce=False):
"""Apply list of patches to the destination Git repository using "git am".
Keyword arguments:
root -- the root directory of the destination Git repository
patches -- list of patch files relative to the root
"""
os.chdir(root)
args = ["git", "am"]
if use_threeway:
args.append("-3")
if ignore_whitespce:
if ignore_whitespace:
args.append("--ignore-whitespace")
for patch in patches:
subprocess.check_output(args + [patch])
subprocess.check_output(args + [patch])