Skip to content

Commit

Permalink
Merge branch 'jj-am.sh'
Browse files Browse the repository at this point in the history
* Branch commit log:
  contrib/jj-am.sh: use `jj status` at the end to also snapshot the last patch
  contrib/jj-am.sh: fix order of commit creation and patching
  contrib/jj-am.sh: apply several patches in email format

Signed-off-by: Tim Janik <[email protected]>
  • Loading branch information
tim-janik committed Dec 15, 2024
2 parents 182df5d + 8c59614 commit fedbc83
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions contrib/jj-am.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bash
# This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
set -Eeuo pipefail #-x
SCRIPTNAME=`basename $0` && function die { [ -n "$*" ] && echo "$SCRIPTNAME: **ERROR**: ${*:-aborting}" >&2; exit 127 ; }
VERSION=0.0.1-alpha

# == Parse Options ==
MBOXES=()
while test $# -ne 0 ; do
case "$1" in \
--version) echo "$SCRIPTNAME $VERSION"; exit ;;
-*) die "unknown option: $1" ;;
*) MBOXES+=("$1") ;;
esac
shift
done

# == Functions ==
# Create temporary dir, assigns $TEMPD
temp_dir()
{
test -n "${TEMPD:-}" || {
TEMPD="`mktemp --tmpdir -d $SCRIPTNAME-XXXXXX`" || die "mktemp failed"
trap "rm -rf '$TEMPD'" 0 HUP INT QUIT TRAP USR1 PIPE TERM
echo "$$" > $TEMPD/$SCRIPTNAME.pid
}
}
# Create new commit
jj_commit()
(
# collect commit infor from header
HEADER="$1" BODY="$(<"$2")" PATCH="$3"
AUTHOR="$(sed -nr '/^Author: /{ s/^[^:]*: //; p; q; }' < $HEADER)"
EMAIL="$(sed -nr '/^Email: /{ s/^[^:]*: //; p; q; }' < $HEADER)"
DATE="$(sed -nr '/^Date: /{ s/^[^:]*: //; p; q; }' < $HEADER)"
DATE="$(date --rfc-3339=ns -d "$DATE")"
SUBJECT="$(sed -nr '/^Subject: /{ s/^[^:]*: //; p; q; }' < $HEADER)"
export JJ_TIMESTAMP="$DATE"
test -z "$BODY" && NL='' || NL=$'\n\n'
ARGS=(
--config-toml "user.name=\"$AUTHOR\""
--config-toml "user.email=\"$EMAIL\""
--message="$SUBJECT$NL$BODY"
)
# create commit
jj new "${ARGS[@]}"
# try patch
patch -p1 < "$PATCH"
)

# == Process ==
temp_dir # for $TEMPD
for mbox in "${MBOXES[@]}" ; do
echo "Apply: ${mbox##*/}"
rm -f "$TEMPD/header" "$TEMPD/body" "$TEMPD/patch"
git mailinfo -b -u --encoding=POSIX.UTF-8 "$TEMPD/body" "$TEMPD/patch" > "$TEMPD/header" < "$mbox"
jj_commit "$TEMPD/header" "$TEMPD/body" "$TEMPD/patch"
done
# snapshot last patch
jj status

0 comments on commit fedbc83

Please sign in to comment.