Skip to content

Commit

Permalink
Add root tag
Browse files Browse the repository at this point in the history
  • Loading branch information
KangarooKoala committed Jul 16, 2024
1 parent 4e15117 commit 2c23924
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions upstream_utils/upstream_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,41 @@ def open_repo(self, *, err_msg_if_absent):
exit(1)
os.chdir(dest)

def get_root_tags(self):
root_tag_output = subprocess.run(
["git", "tag", "--list", "upstream_utils_root-*"],
capture_output=True,
text=True,
).stdout
return root_tag_output.splitlines()

def get_root_tag(self):
root_tags = self.get_root_tags()
if len(root_tags) == 0:
print(
"ERROR: Could not determine root tag: No tags match 'upstream_utils_root-*'",
file=sys.stderr,
)
exit(1)
if len(root_tags) > 1:
print(
f"ERROR: Could not determine root tag: Multiple candidates: {root_tags}",
file=sys.stderr,
)
exit(1)
return root_tags[0]

def set_root_tag(self, tag):
root_tags = self.get_root_tags()

if len(root_tags) > 1:
print(f"WARNING: Deleting multiple root tags {root_tags}", file=sys.stderr)

for root_tag in root_tags:
subprocess.run(["git", "tag", "-d", root_tag])

subprocess.run(["git", "tag", f"upstream_utils_root-{tag}", tag])

def apply_patches(self):
if self.pre_patch_hook is not None:
self.pre_patch_hook()
Expand Down Expand Up @@ -328,6 +363,8 @@ def clone(self):

subprocess.run(["git", "switch", "--detach", self.old_tag])

self.set_root_tag(self.old_tag)

def reset(self):
self.open_repo(
err_msg_if_absent='There\'s nothing to reset. Run the "clone" command first.'
Expand All @@ -348,6 +385,8 @@ def rebase(self, new_tag):

self.apply_patches()

self.set_root_tag(new_tag)

subprocess.run(["git", "rebase", "--onto", new_tag, self.old_tag])

# Detect merge conflict by detecting if we stopped in the middle of a rebase
Expand All @@ -358,13 +397,16 @@ def rebase(self, new_tag):
)

def format_patch(self, tag=None):
if tag is None:
tag = self.old_tag

self.open_repo(
err_msg_if_absent='There\'s nothing to run format-patch on. Run the "clone" and "rebase" commands first.'
)

if tag is None:
tag = self.get_root_tag()
script_tag = tag.removeprefix("upstream_utils_root-")
else:
script_tag = tag

start_commit = tag
if self.pre_patch_commits > 0:
commits_since_tag_output = subprocess.run(
Expand Down Expand Up @@ -404,7 +446,7 @@ def format_patch(self, tag=None):
is_first = False
shutil.move(f, patch_dest)

self.replace_tag(tag)
self.replace_tag(script_tag)

def copy_upstream_to_thirdparty(self):
self.open_repo(
Expand Down Expand Up @@ -448,7 +490,7 @@ def main(self, argv=sys.argv[1:]):
parser_format_patch.add_argument(
"new_tag",
nargs="?",
default=self.old_tag,
default=None,
help="The tag for the commit before the patches",
)

Expand Down

0 comments on commit 2c23924

Please sign in to comment.