Skip to content

Commit

Permalink
werk: improve "werk pick"
Browse files Browse the repository at this point in the history
 * if pre-commit hooks fail, the "werk pick" can now be continued
   with "git cherry-pick --continue"
 * if the sha given to "werk pick" isn't a werk, it will work like
   "git cherry-pick", but it doesn't support the same command line
   parameters.

Change-Id: I32ae2032b0ba8298706d537e33573f35065426d7
  • Loading branch information
Christoph Rauch committed May 10, 2021
1 parent 0ca1b69 commit 42ed2c1
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions werk
Expand Up @@ -874,30 +874,51 @@ def main_pick(args: argparse.Namespace) -> None:


def werk_cherry_pick(commit_id: str, no_commit: bool) -> None:
# First get the werk_id
result = subprocess.run(
["git", "diff-tree", "--no-commit-id", "--name-only", "-r", commit_id],
capture_output=True,
check=True,
)
werk_id = None
for line in result.stdout.splitlines():
filename = line.decode('utf-8')
if filename.startswith(".werks/") and filename[7:].isdigit():
werk_id = int(filename[7:])

if werk_id is not None:
if os.path.exists(str(werk_id)):
bail_out(f"Trying to pick werk {werk_id}, but werk already present. Aborted.")

# Cherry-pick the commit in question from the other branch
os.system("git cherry-pick --no-commit '%s'" % commit_id) # nosec
cmd = ["git", "cherry-pick"]
if no_commit:
cmd.append("--no-commit")
cmd.append(commit_id)
pick = subprocess.run(cmd, check=False)
if pick.returncode:
# Exit with the result of the pick. This may be a merge conflict, so
# other tools may need to know about this.
sys.exit(pick.returncode)

# Find werks that have been cherry-picked and change their version
# to our current version
load_werks() # might have changed
for line in os.popen("git status --porcelain"): # nosec
# M .werks/103
# M werk
_status, filename = line.strip().split(None, 1)
if filename.startswith(".werks/") and filename[7].isdigit():
werk_id = int(filename[7:])
change_werk_version(werk_id, g_current_version)
sys.stdout.write("Changed version of werk %s to %s.\n" %
(format_werk_id(werk_id), g_current_version))

# Commit
if not no_commit:
os.system("git commit -C '%s'" % commit_id) # nosec

else:
sys.stdout.write("We don't commit yet. Here is the status:\n")
sys.stdout.write("Please commit with git commit -C '%s'\n\n" % commit_id)
os.system("git status")
if werk_id is not None:
change_werk_version(werk_id, g_current_version)
sys.stdout.write("Changed version of werk %s to %s.\n" %
(format_werk_id(werk_id), g_current_version))

# This allows for picking regular commits as well
if not no_commit:
subprocess.run(["git", "add", str(werk_id)], check=True)
subprocess.run(["git", "commit", "--no-edit", "--amend"], check=True)
else:
sys.stdout.write("We don't commit yet. Here is the status:\n")
sys.stdout.write("Please commit with git commit -C '%s'\n\n" % commit_id)
subprocess.run(["git", "status"], check=True)


def get_werk_ids() -> List[int]:
Expand Down

0 comments on commit 42ed2c1

Please sign in to comment.