From 5e0628acee7d39f83f39659723b7f9513eab6870 Mon Sep 17 00:00:00 2001 From: Michael Lohmann Date: Sun, 7 Jan 2024 11:27:39 +0100 Subject: [PATCH] WIP interactive rebase --- create_challenge.sh | 24 ++++++++++++++++--- .../interactive-rebase-continued.md | 15 ++++++++++++ .../interactive-rebase-sequence-editor.sh | 8 +++++++ src/hooks/pre-rebase | 3 +++ src/nuggits | 1 + test.sh | 10 ++++++-- todo.md | 2 -- 7 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 src/07_rebase_merge/interactive-rebase-continued.md create mode 100755 src/07_rebase_merge/interactive-rebase-sequence-editor.sh create mode 100755 src/hooks/pre-rebase diff --git a/create_challenge.sh b/create_challenge.sh index eb80066..53c0f46 100755 --- a/create_challenge.sh +++ b/create_challenge.sh @@ -54,9 +54,27 @@ replace_placeholders "$DOCDIR/03_commit/commit.md" > commit.md git add commit.md commit -m "Add description on commit" -git switch --detach @ -# TODO: create interactive rebase commit -INTERACTIVE_REBASE_COMMIT="INTERACTIVE_REBASE_COMMIT" +git switch --detach main +printf "%s" '# Interactive rebase + +An interactive rebase lets you (as the name suggests) interact with it instead of just step by step applying each patch. This can be incredibly useful after you created a lot of `git commit -m "WIP: Coffee break"`' > interactive-rebase.md +git add interactive-rebase.md +commit -m "WIP: Coffee break" +echo " commits, but after you are done with your feature, you don't want keep them forever and you don't want to bother the reviewer (and you yourself when you will eventually try to find a bug in your code and look at the log) with them." >> interactive-rebase.md +git add interactive-rebase.md +commit -m "WIP: finish sentence on interactive rebases" +INTERACTIVE_REBASE_EXAMPLE_PICKS="$(git log --oneline main..@ | sed 's/^/pick /' | sed 's/$/\\/g') +[...]" +# FIXME +INTERACTIVE_REBASE_BASE_COMMIT=INTERACTIVE_REBASE_BASE_COMMIT +replace_placeholders "$DOCDIR/07_rebase_merge/interactive-rebase-continued.md" >> interactive-rebase.md +git add interactive-rebase.md +commit -m "Finish describing interactive rebases + +TODO: squash commits..." +INTERACTIVE_REBASE_COMMIT="$(git rev-parse --short @)" + +git switch --detach main replace_placeholders "$DOCDIR/07_rebase_merge/combine_history.md" > combine_history.md git add combine_history.md commit -m "Add description on how to combine branches" diff --git a/src/07_rebase_merge/interactive-rebase-continued.md b/src/07_rebase_merge/interactive-rebase-continued.md new file mode 100644 index 0000000..e7c7106 --- /dev/null +++ b/src/07_rebase_merge/interactive-rebase-continued.md @@ -0,0 +1,15 @@ +When you run +```sh +git rebase -i INTERACTIVE_REBASE_BASE_COMMIT +``` +(or `--interactive`), an editor will open, showing a list of all the commits that would be applied. + +CAREFUL: in contrast to the output of `git log` where the newest commit is at the top, in this list the last commit is at the bottom! + +The output starts with a block of lines that look like this: +``` +INTERACTIVE_REBASE_EXAMPLE_PICKS +``` +But what does this mean? Fortunately at the bottom of the file it shows a short summary on all the possible commands. + + diff --git a/src/07_rebase_merge/interactive-rebase-sequence-editor.sh b/src/07_rebase_merge/interactive-rebase-sequence-editor.sh new file mode 100755 index 0000000..b419c16 --- /dev/null +++ b/src/07_rebase_merge/interactive-rebase-sequence-editor.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +# Mac sed has a different format for -i, so manually replace that file +# hack: just replace "pick" with "fixup" until line 1000 (which does not exist), but I can't be bothered to find a nicer way not to change the first line... +sed '2,1000s/^pick/fixup/' < "$1" > "$1.bak" +# output the file for grabbing the flag +cat "$1.bak" +mv "$1.bak" "$1" diff --git a/src/hooks/pre-rebase b/src/hooks/pre-rebase new file mode 100755 index 0000000..3aabfae --- /dev/null +++ b/src/hooks/pre-rebase @@ -0,0 +1,3 @@ +#!/bin/sh + +echo 'nuggit: ItsAllAboutTheRebase' diff --git a/src/nuggits b/src/nuggits index 9bf25d1..c9e9f1b 100644 --- a/src/nuggits +++ b/src/nuggits @@ -8,3 +8,4 @@ ShowMeMore MyFirstBranch LogCat AnnotateMeIfYouCan +ItsAllAboutTheRebase diff --git a/test.sh b/test.sh index 96e71ee..d9180aa 100755 --- a/test.sh +++ b/test.sh @@ -81,8 +81,14 @@ redeem_nuggit AnnotateMeIfYouCan git switch --detach -q the-first-tag ' -xit 'TODO: find title for combine_history testcase' <&1' to contain "nuggit: ItsAllAboutTheRebase" +redeem_nuggit ItsAllAboutTheRebase +EOF + +xit 'interactive rebase succeeds' </dev/null EOF it 'An invalid nuggit should show an error' ' diff --git a/todo.md b/todo.md index b5dfd73..3234ecc 100644 --- a/todo.md +++ b/todo.md @@ -22,7 +22,6 @@ - hooks (e.g. automatically add flag to commit message) - post-merge - post-rewrite e.g. for commit --amend - - pre-rebase - `--word-diff --word-diff-regex=.` (flag hidden "inbetween" two commits) - submodule - `blame` first column @@ -68,7 +67,6 @@ See documentation in the git repository under Documentation/githooks.txt - pre-merge-commit - commit-msg - post-commit -- pre-rebase - post-merge - pre-push - pre-receive