All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH/RFCv5 1/3] git-rebase -i: add command "drop" to remove a commit
@ 2015-06-10 10:10 Galan Rémi
  2015-06-10 10:10 ` [PATCH/RFCv5 2/3] git rebase -i: warn about removed commits Galan Rémi
  2015-06-10 10:10 ` [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1 Galan Rémi
  0 siblings, 2 replies; 12+ messages in thread
From: Galan Rémi @ 2015-06-10 10:10 UTC (permalink / raw)
  To: Git List
  Cc: Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Matthieu Moy, Junio C Hamano, Eric Sunshine,
	Galan Rémi

Instead of removing a line to remove the commit, you can use the
command "drop" (just like "pick" or "edit"). It has the same effect as
deleting the line (removing the commit) except that you keep a visual
trace of your actions, allowing a better control and reducing the
possibility of removing a commit by mistake.

Signed-off-by: Galan Rémi <remi.galan-alfonso@ensimag.grenoble-inp.fr>
---
 In t3404, test_rebase_end is introduced, mainly because it will be
 reused in future tests (in 2/3 and 3/3).

 Documentation/git-rebase.txt  |  3 +++
 git-rebase--interactive.sh    |  3 ++-
 t/lib-rebase.sh               |  4 ++--
 t/t3404-rebase-interactive.sh | 16 ++++++++++++++++
 4 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-rebase.txt b/Documentation/git-rebase.txt
index 1d01baa..34bd070 100644
--- a/Documentation/git-rebase.txt
+++ b/Documentation/git-rebase.txt
@@ -514,6 +514,9 @@ rebasing.
 If you just want to edit the commit message for a commit, replace the
 command "pick" with the command "reword".
 
+To drop a commit, replace the command "pick" with "drop", or just
+delete the matching line.
+
 If you want to fold two or more commits into one, replace the command
 "pick" for the second and subsequent commits with "squash" or "fixup".
 If the commits had different authors, the folded commit will be
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index dc3133f..72abf90 100644
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -152,6 +152,7 @@ Commands:
  s, squash = use commit, but meld into previous commit
  f, fixup = like "squash", but discard this commit's log message
  x, exec = run command (the rest of the line) using shell
+ d, drop = remove commit
 
 These lines can be re-ordered; they are executed from top to bottom.
 
@@ -505,7 +506,7 @@ do_next () {
 	rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
 	read -r command sha1 rest < "$todo"
 	case "$command" in
-	"$comment_char"*|''|noop)
+	"$comment_char"*|''|noop|drop|d)
 		mark_action_done
 		;;
 	pick|p)
diff --git a/t/lib-rebase.sh b/t/lib-rebase.sh
index 6bd2522..fdbc900 100644
--- a/t/lib-rebase.sh
+++ b/t/lib-rebase.sh
@@ -14,7 +14,7 @@
 #       specified line.
 #
 #   "<cmd> <lineno>" -- add a line with the specified command
-#       ("squash", "fixup", "edit", or "reword") and the SHA1 taken
+#       ("squash", "fixup", "edit", "reword" or "drop") and the SHA1 taken
 #       from the specified line.
 #
 #   "exec_cmd_with_args" -- add an "exec cmd with args" line.
@@ -46,7 +46,7 @@ set_fake_editor () {
 	action=pick
 	for line in $FAKE_LINES; do
 		case $line in
-		squash|fixup|edit|reword)
+		squash|fixup|edit|reword|drop)
 			action="$line";;
 		exec*)
 			echo "$line" | sed 's/_/ /g' >> "$1";;
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index ac429a0..ecd277c 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -1102,4 +1102,20 @@ test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
 	test $(git cat-file commit HEAD | sed -ne \$p) = I
 '
 
+test_rebase_end () {
+	test_when_finished "git checkout master &&
+	git branch -D $1 &&
+	test_might_fail git rebase --abort" &&
+	git checkout -b $1 master
+}
+
+test_expect_success 'drop' '
+	test_rebase_end dropTest &&
+	set_fake_editor &&
+	FAKE_LINES="1 drop 2 3 drop 4 5" git rebase -i --root &&
+	test E = $(git cat-file commit HEAD | sed -ne \$p) &&
+	test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
+	test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
+'
+
 test_done
-- 
2.4.2.496.gdc9319a

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH/RFCv5 2/3] git rebase -i: warn about removed commits
  2015-06-10 10:10 [PATCH/RFCv5 1/3] git-rebase -i: add command "drop" to remove a commit Galan Rémi
@ 2015-06-10 10:10 ` Galan Rémi
  2015-06-10 14:53   ` Matthieu Moy
  2015-06-10 10:10 ` [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1 Galan Rémi
  1 sibling, 1 reply; 12+ messages in thread
From: Galan Rémi @ 2015-06-10 10:10 UTC (permalink / raw)
  To: Git List
  Cc: Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Matthieu Moy, Junio C Hamano, Eric Sunshine,
	Galan Rémi

Check if commits were removed (i.e. a line was deleted) and print
warnings or stop git rebase depending on the value of the
configuration variable rebase.missingCommitsCheck.

This patch gives the user the possibility to avoid silent loss of
information (losing a commit through deleting the line in this case)
if he wants.

Add the configuration variable rebase.missingCommitsCheck.
    - When unset or set to "ignore", no checking is done.
    - When set to "warn", the commits are checked, warnings are
      displayed but git rebase still proceeds.
    - When set to "error", the commits are checked, warnings are
      displayed and the rebase is stopped.
      (The user can then use 'git rebase --edit-todo' and
      'git rebase --continue', or 'git rebase --abort')

rebase.missingCommitsCheck defaults to "ignore".

Signed-off-by: Galan Rémi <remi.galan-alfonso@ensimag.grenoble-inp.fr>
---
 In git-rebase--interactive, in the error case of check_todo_list, I
 added 'git checkout $onto' so that using 'die' for the error allows
 to use 'git rebase --edit-todo' (otherwise HEAD would not have been
 changed and it still would be placed after the commits of the
 rebase).
 Since now it doesn't abort the rebase, the documentation and the
 messages in the case error have changed.
 I moved the error case away from the initial test case for missing
 commits as to prepare for 3/3 part of the patch. It is something that
 was advised by Eric Sunshine when I checked both missing and
 duplicated commits, but that I removed it when removing the checking
 for duplicated commits since there was only one test. However I add
 it again since 3/3 will add more checking.
 I use the variable raiseError that I affect if the error must be
 raised instead of testing directly because I think it makes more
 sense with 3/3 and if we add other check in the future since it adds
 more possible errors (the test for the error case if not something
 like 'if (test checkLevel = error && test -s todo.miss) || test cond2
 || test cond3 || ...').
 I am wondering if a check_todo_list call should be added in the
 '--continue' part of the code: with this patch, the checking is only
 done once, if the user doesn't edit correctly with 'git rebase
 --edit-todo', it won't be picked by this.
 In the tests in t3404 I now also test that the warning/error messages
 are correct.
 I tried to not change this patch too much since it was already
 heavily reviewed, but there are some changes (mostly the ones
 mentionned above).

 Documentation/config.txt      | 11 +++++
 Documentation/git-rebase.txt  |  6 +++
 git-rebase--interactive.sh    | 96 +++++++++++++++++++++++++++++++++++++++++++
 t/t3404-rebase-interactive.sh | 66 +++++++++++++++++++++++++++++
 4 files changed, 179 insertions(+)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 4d21ce1..25b2a04 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -2160,6 +2160,17 @@ rebase.autoStash::
 	successful rebase might result in non-trivial conflicts.
 	Defaults to false.
 
+rebase.missingCommitsCheck::
+	If set to "warn", git rebase -i will print a warning if some
+	commits are removed (e.g. a line was deleted), however the
+	rebase will still proceed. If set to "error", it will print
+	the previous warning and stop the rebase, 'git rebase
+	--edit-todo' can then be used to correct the error. If set to
+	"ignore", no checking is done.
+	To drop a commit without warning or error, use the `drop`
+	command in the todo-list.
+	Defaults to "ignore".
+
 receive.advertiseAtomic::
 	By default, git-receive-pack will advertise the atomic push
 	capability to its clients. If you don't want to this capability
diff --git a/Documentation/git-rebase.txt b/Documentation/git-rebase.txt
index 34bd070..2ca3b8d 100644
--- a/Documentation/git-rebase.txt
+++ b/Documentation/git-rebase.txt
@@ -213,6 +213,12 @@ rebase.autoSquash::
 rebase.autoStash::
 	If set to true enable '--autostash' option by default.
 
+rebase.missingCommitsCheck::
+	If set to "warn", print warnings about removed commits in
+	interactive mode. If set to "error", print the warnings and
+	stop the rebase. If set to "ignore", no checking is
+	done. "ignore" by default.
+
 OPTIONS
 -------
 --onto <newbase>::
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 72abf90..68a71d0 100644
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -834,6 +834,100 @@ add_exec_commands () {
 	mv "$1.new" "$1"
 }
 
+# Print the list of the SHA-1 of the commits
+# from stdin to stdout
+todo_list_to_sha_list () {
+	git stripspace --strip-comments |
+	while read -r command sha1 rest
+	do
+		case $command in
+		"$comment_char"*|''|noop|x|"exec")
+			;;
+		*)
+			printf "%s\n" "$sha1"
+			;;
+		esac
+	done
+}
+
+# Use warn for each line of a file
+# $1: file
+warn_file () {
+	while read -r line
+	do
+		warn " - $line"
+	done <"$1"
+}
+
+# Check if the user dropped some commits by mistake
+# Behaviour determined by rebase.missingCommitsCheck.
+check_todo_list () {
+	raiseError=f
+
+	checkLevel=$(git config --get rebase.missingCommitsCheck)
+	checkLevel=${checkLevel:-ignore}
+	# Don't be case sensitive
+	checkLevel=$(echo "$checkLevel" | tr 'A-Z' 'a-z')
+
+	case "$checkLevel" in
+	warn|error)
+		# Get the SHA-1 of the commits
+		todo_list_to_sha_list <"$todo".backup >"$todo".oldsha1
+		todo_list_to_sha_list <"$todo" >"$todo".newsha1
+
+		# Sort the SHA-1 and compare them
+		sort -u "$todo".oldsha1 >"$todo".oldsha1+
+		mv "$todo".oldsha1+ "$todo".oldsha1
+		sort -u "$todo".newsha1 >"$todo".newsha1+
+		mv "$todo".newsha1+ "$todo".newsha1
+		comm -2 -3 "$todo".oldsha1 "$todo".newsha1 >"$todo".miss
+
+		# Warn about missing commits
+		if test -s "$todo".miss
+		then
+			test "$checkLevel" = error && raiseError=t
+
+			# Make the list user-friendly
+			opt="--no-walk=sorted --format=oneline --abbrev-commit --stdin"
+			git rev-list $opt <"$todo".miss >"$todo".miss+
+			mv "$todo".miss+ "$todo".miss
+
+			warn "Warning: some commits may have been dropped" \
+				"accidentally."
+			warn "Dropped commits (newer to older):"
+			warn_file "$todo".miss
+			warn "To avoid this message, use \"drop\" to" \
+				"explicitly remove a commit."
+			warn
+			warn "Use 'git --config rebase.missingCommitsCheck' to change" \
+				"the level of warnings."
+			warn "The possible behaviours are: ignore, warn, error."
+			warn
+		fi
+		;;
+	ignore)
+		;;
+	*)
+		warn "Unrecognized setting $checkLevel for option" \
+			"rebase.missingCommitsCheck. Ignoring."
+		;;
+	esac
+
+	if test $raiseError = t
+	then
+		# Checkout before the first commit of the
+		# rebase: this way git rebase --continue
+		# will work correctly as it expects HEAD to be
+		# placed before the commit of the next action
+		GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
+		output git checkout $onto || die_abort "could not detach HEAD"
+		git update-ref ORIG_HEAD $orig_head
+
+		warn "You can fix this with 'git rebase --edit-todo'."
+		die "Or you can abort the rebase with 'git rebase --abort'."
+	fi
+}
+
 # The whole contents of this file is run by dot-sourcing it from
 # inside a shell function.  It used to be that "return"s we see
 # below were not inside any function, and expected to return
@@ -1079,6 +1173,8 @@ has_action "$todo" ||
 
 expand_todo_ids
 
+check_todo_list
+
 test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
 
 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index ecd277c..a92ae19 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -1118,4 +1118,70 @@ test_expect_success 'drop' '
 	test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
 '
 
+cat >expect <<EOF
+Successfully rebased and updated refs/heads/tmp2.
+EOF
+
+test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
+	test_config rebase.missingCommitsCheck ignore &&
+	test_rebase_end tmp2 &&
+	set_fake_editor &&
+	FAKE_LINES="1 2 3 4" \
+		git rebase -i --root 2>actual &&
+	test D = $(git cat-file commit HEAD | sed -ne \$p) &&
+	test_cmp expect actual
+'
+
+cat >expect <<EOF
+Warning: some commits may have been dropped accidentally.
+Dropped commits (newer to older):
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
+To avoid this message, use "drop" to explicitly remove a commit.
+
+Use 'git --config rebase.missingCommitsCheck' to change the level of warnings.
+The possible behaviours are: ignore, warn, error.
+
+Successfully rebased and updated refs/heads/tmp2.
+EOF
+
+test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
+	test_config rebase.missingCommitsCheck warn &&
+	test_rebase_end tmp2 &&
+	set_fake_editor &&
+	FAKE_LINES="1 2 3 4" \
+		git rebase -i --root 2>actual &&
+	test_cmp expect actual &&
+	test D = $(git cat-file commit HEAD | sed -ne \$p)
+'
+
+cat >expect <<EOF
+Warning: some commits may have been dropped accidentally.
+Dropped commits (newer to older):
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
+To avoid this message, use "drop" to explicitly remove a commit.
+
+Use 'git --config rebase.missingCommitsCheck' to change the level of warnings.
+The possible behaviours are: ignore, warn, error.
+
+You can fix this with 'git rebase --edit-todo'.
+Or you can abort the rebase with 'git rebase --abort'.
+EOF
+
+test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
+	test_config rebase.missingCommitsCheck error &&
+	test_rebase_end tmp2 &&
+	set_fake_editor &&
+	test_must_fail env FAKE_LINES="1 2 4" \
+		git rebase -i --root 2>actual &&
+	test_cmp expect actual &&
+	cp .git/rebase-merge/git-rebase-todo.backup \
+		.git/rebase-merge/git-rebase-todo &&
+	FAKE_LINES="1 2 drop 3 4 drop 5" \
+		git rebase --edit-todo &&
+	git rebase --continue &&
+	test D = $(git cat-file commit HEAD | sed -ne \$p) &&
+	test B = $(git cat-file commit HEAD^ | sed -ne \$p)
+'
+
 test_done
-- 
2.4.2.496.gdc9319a

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1
  2015-06-10 10:10 [PATCH/RFCv5 1/3] git-rebase -i: add command "drop" to remove a commit Galan Rémi
  2015-06-10 10:10 ` [PATCH/RFCv5 2/3] git rebase -i: warn about removed commits Galan Rémi
@ 2015-06-10 10:10 ` Galan Rémi
  2015-06-10 15:20   ` Matthieu Moy
  1 sibling, 1 reply; 12+ messages in thread
From: Galan Rémi @ 2015-06-10 10:10 UTC (permalink / raw)
  To: Git List
  Cc: Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Matthieu Moy, Junio C Hamano, Eric Sunshine,
	Galan Rémi

Check before the start of the rebasing if the commands exists, and for
the commands expecting a SHA-1, check if the SHA-1 is present and
corresponds to a commit. In case of error, print the error, stop git
rebase and prompt the user to fix with 'git rebase --edit-todo' or to
abort.

This allows to avoid doing half of a rebase before finding an error
and giving back what's left of the todo list to the user and prompt
him to fix when it might be too late for him to do so (he might have
to abort and restart the rebase).

Signed-off-by: Galan Rémi <remi.galan-alfonso@ensimag.grenoble-inp.fr>
---
 git-rebase--interactive.sh    | 63 +++++++++++++++++++++++++++++++++++++++++++
 t/lib-rebase.sh               |  5 ++++
 t/t3404-rebase-interactive.sh | 40 +++++++++++++++++++++++++++
 3 files changed, 108 insertions(+)

diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 68a71d0..226a8a8 100644
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -834,6 +834,47 @@ add_exec_commands () {
 	mv "$1.new" "$1"
 }
 
+# prints the bad commits and bad commands
+# from the todolist in stdin
+check_bad_cmd_and_sha () {
+	git stripspace --strip-comments |
+	while read -r command sha1 rest
+	do
+		case $command in
+		''|noop|x|"exec")
+			;;
+		pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)
+			if test -z $sha1
+			then
+				echo "$command $rest" >>"$todo".badsha
+			else
+				sha1_verif="$(git rev-parse --verify --quiet $sha1^{commit})"
+				if test -z $sha1_verif
+				then
+					echo "$command $sha1 $rest" \
+						>>"$todo".badsha
+				fi
+			fi
+			;;
+		*)
+			if test -z $sha1
+			then
+				echo "$command" >>"$todo".badcmd
+			else
+				commit="$(git rev-list --oneline -1 --ignore-missing $sha1 2>/dev/null)"
+				if test -z "$commit"
+				then
+					echo "$command $sha1 $rest" \
+						>>"$todo".badcmd
+				else
+					echo "$command $commit" >>"$todo".badcmd
+				fi
+			fi
+			;;
+		esac
+	done
+}
+
 # Print the list of the SHA-1 of the commits
 # from stdin to stdout
 todo_list_to_sha_list () {
@@ -913,6 +954,28 @@ check_todo_list () {
 		;;
 	esac
 
+	check_bad_cmd_and_sha <"$todo"
+
+	if test -s "$todo".badsha
+	then
+		raiseError=t
+
+		warn "Warning: the SHA-1 is missing or isn't" \
+			"a commit in the following line(s):"
+		warn_file "$todo".badsha
+		warn
+	fi
+
+	if test -s "$todo".badcmd
+	then
+		raiseError=t
+
+		warn "Warning: the command isn't recognized" \
+			"in the following line(s):"
+		warn_file "$todo".badcmd
+		warn
+	fi
+
 	if test $raiseError = t
 	then
 		# Checkout before the first commit of the
diff --git a/t/lib-rebase.sh b/t/lib-rebase.sh
index fdbc900..9a96e15 100644
--- a/t/lib-rebase.sh
+++ b/t/lib-rebase.sh
@@ -54,6 +54,11 @@ set_fake_editor () {
 			echo '# comment' >> "$1";;
 		">")
 			echo >> "$1";;
+		bad)
+			action="badcmd";;
+		fakesha)
+			echo "$action XXXXXXX False commit" >> "$1"
+			action=pick;;
 		*)
 			sed -n "${line}s/^pick/$action/p" < "$1".tmp >> "$1"
 			action=pick;;
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index a92ae19..d691b1c 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -1184,4 +1184,44 @@ test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
 	test B = $(git cat-file commit HEAD^ | sed -ne \$p)
 '
 
+cat >expect <<EOF
+Warning: the command isn't recognized in the following line(s):
+ - badcmd $(git rev-list --oneline -1 master~1)
+
+You can fix this with 'git rebase --edit-todo'.
+Or you can abort the rebase with 'git rebase --abort'.
+EOF
+
+test_expect_success 'static check of bad command' '
+	test_rebase_end tmp2 &&
+	set_fake_editor &&
+	test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
+		git rebase -i --root 2>actual &&
+	test_cmp expect actual &&
+	FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
+	git rebase --continue &&
+	test E = $(git cat-file commit HEAD | sed -ne \$p) &&
+	test C = $(git cat-file commit HEAD^ | sed -ne \$p)
+'
+
+cat >expect <<EOF
+Warning: the SHA-1 is missing or isn't a commit in the following line(s):
+ - edit XXXXXXX False commit
+
+You can fix this with 'git rebase --edit-todo'.
+Or you can abort the rebase with 'git rebase --abort'.
+EOF
+
+test_expect_success 'static check of bad SHA-1' '
+	test_config rebase.missingCommitsCheck error &&
+	test_rebase_end tmp2 &&
+	set_fake_editor &&
+	test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
+		git rebase -i --root 2>actual &&
+	test_cmp expect actual &&
+	FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
+	git rebase --continue &&
+	test E = $(git cat-file commit HEAD | sed -ne \$p)
+'
+
 test_done
-- 
2.4.2.496.gdc9319a

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH/RFCv5 2/3] git rebase -i: warn about removed commits
  2015-06-10 10:10 ` [PATCH/RFCv5 2/3] git rebase -i: warn about removed commits Galan Rémi
@ 2015-06-10 14:53   ` Matthieu Moy
  2015-06-10 15:47     ` Remi Galan Alfonso
  0 siblings, 1 reply; 12+ messages in thread
From: Matthieu Moy @ 2015-06-10 14:53 UTC (permalink / raw)
  To: Galan Rémi
  Cc: Git List, Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Junio C Hamano, Eric Sunshine

Galan Rémi <remi.galan-alfonso@ensimag.grenoble-inp.fr> writes:

> Check if commits were removed (i.e. a line was deleted) and print
> warnings or stop git rebase depending on the value of the
> configuration variable rebase.missingCommitsCheck.
>
> This patch gives the user the possibility to avoid silent loss of
> information (losing a commit through deleting the line in this case)
> if he wants.
>
> Add the configuration variable rebase.missingCommitsCheck.
>     - When unset or set to "ignore", no checking is done.
>     - When set to "warn", the commits are checked, warnings are
>       displayed but git rebase still proceeds.
>     - When set to "error", the commits are checked, warnings are
>       displayed and the rebase is stopped.
>       (The user can then use 'git rebase --edit-todo' and
>       'git rebase --continue', or 'git rebase --abort')
>
> rebase.missingCommitsCheck defaults to "ignore".
>
> Signed-off-by: Galan Rémi <remi.galan-alfonso@ensimag.grenoble-inp.fr>
> ---
>  In git-rebase--interactive, in the error case of check_todo_list, I
>  added 'git checkout $onto' so that using 'die' for the error allows
>  to use 'git rebase --edit-todo' (otherwise HEAD would not have been
>  changed and it still would be placed after the commits of the
>  rebase).
>  Since now it doesn't abort the rebase, the documentation and the
>  messages in the case error have changed.
>  I moved the error case away from the initial test case for missing
>  commits as to prepare for 3/3 part of the patch. It is something that
>  was advised by Eric Sunshine when I checked both missing and
>  duplicated commits, but that I removed it when removing the checking
>  for duplicated commits since there was only one test. However I add
>  it again since 3/3 will add more checking.
>  I use the variable raiseError that I affect if the error must be
>  raised instead of testing directly because I think it makes more
>  sense with 3/3 and if we add other check in the future since it adds
>  more possible errors (the test for the error case if not something
>  like 'if (test checkLevel = error && test -s todo.miss) || test cond2
>  || test cond3 || ...').
>  I am wondering if a check_todo_list call should be added in the
>  '--continue' part of the code: with this patch, the checking is only
>  done once, if the user doesn't edit correctly with 'git rebase
>  --edit-todo', it won't be picked by this.
>  In the tests in t3404 I now also test that the warning/error messages
>  are correct.
>  I tried to not change this patch too much since it was already
>  heavily reviewed, but there are some changes (mostly the ones
>  mentionned above).
>
>  Documentation/config.txt      | 11 +++++
>  Documentation/git-rebase.txt  |  6 +++
>  git-rebase--interactive.sh    | 96 +++++++++++++++++++++++++++++++++++++++++++
>  t/t3404-rebase-interactive.sh | 66 +++++++++++++++++++++++++++++
>  4 files changed, 179 insertions(+)
>
> diff --git a/Documentation/config.txt b/Documentation/config.txt
> index 4d21ce1..25b2a04 100644
> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -2160,6 +2160,17 @@ rebase.autoStash::
>  	successful rebase might result in non-trivial conflicts.
>  	Defaults to false.
>  
> +rebase.missingCommitsCheck::
> +	If set to "warn", git rebase -i will print a warning if some
> +	commits are removed (e.g. a line was deleted), however the
> +	rebase will still proceed. If set to "error", it will print
> +	the previous warning and stop the rebase, 'git rebase
> +	--edit-todo' can then be used to correct the error. If set to
> +	"ignore", no checking is done.
> +	To drop a commit without warning or error, use the `drop`
> +	command in the todo-list.
> +	Defaults to "ignore".
> +
>  receive.advertiseAtomic::
>  	By default, git-receive-pack will advertise the atomic push
>  	capability to its clients. If you don't want to this capability
> diff --git a/Documentation/git-rebase.txt b/Documentation/git-rebase.txt
> index 34bd070..2ca3b8d 100644
> --- a/Documentation/git-rebase.txt
> +++ b/Documentation/git-rebase.txt
> @@ -213,6 +213,12 @@ rebase.autoSquash::
>  rebase.autoStash::
>  	If set to true enable '--autostash' option by default.
>  
> +rebase.missingCommitsCheck::
> +	If set to "warn", print warnings about removed commits in
> +	interactive mode. If set to "error", print the warnings and
> +	stop the rebase. If set to "ignore", no checking is
> +	done. "ignore" by default.
> +
>  OPTIONS
>  -------
>  --onto <newbase>::
> diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
> index 72abf90..68a71d0 100644
> --- a/git-rebase--interactive.sh
> +++ b/git-rebase--interactive.sh
> @@ -834,6 +834,100 @@ add_exec_commands () {
>  	mv "$1.new" "$1"
>  }
>  
> +# Print the list of the SHA-1 of the commits
> +# from stdin to stdout
> +todo_list_to_sha_list () {
> +	git stripspace --strip-comments |
> +	while read -r command sha1 rest
> +	do
> +		case $command in
> +		"$comment_char"*|''|noop|x|"exec")
> +			;;
> +		*)
> +			printf "%s\n" "$sha1"
> +			;;
> +		esac
> +	done
> +}
> +
> +# Use warn for each line of a file
> +# $1: file
> +warn_file () {
> +	while read -r line
> +	do
> +		warn " - $line"
> +	done <"$1"
> +}
> +
> +# Check if the user dropped some commits by mistake
> +# Behaviour determined by rebase.missingCommitsCheck.
> +check_todo_list () {
> +	raiseError=f
> +
> +	checkLevel=$(git config --get rebase.missingCommitsCheck)
> +	checkLevel=${checkLevel:-ignore}
> +	# Don't be case sensitive
> +	checkLevel=$(echo "$checkLevel" | tr 'A-Z' 'a-z')

Avoid echo on user-supplied data. If $checkLevel starts with a "-", the
behavior is platform-dependant. Should not happen if the user is
sensible, but using

  printf '%s' "$checkLevel"

is safer.

> +			opt="--no-walk=sorted --format=oneline --abbrev-commit --stdin"
> +			git rev-list $opt <"$todo".miss >"$todo".miss+
> +			mv "$todo".miss+ "$todo".miss
> +
> +			warn "Warning: some commits may have been dropped" \
> +				"accidentally."
> +			warn "Dropped commits (newer to older):"
> +			warn_file "$todo".miss

I would find it more elegant with less intermediate files, like

git rev-list $opt <"$todo".miss | while read -r line
do
	warn " - $line"
done

> +	if test $raiseError = t
> +	then
> +		# Checkout before the first commit of the
> +		# rebase: this way git rebase --continue
> +		# will work correctly as it expects HEAD to be
> +		# placed before the commit of the next action
> +		GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
> +		output git checkout $onto || die_abort "could not detach HEAD"
> +		git update-ref ORIG_HEAD $orig_head

This is cut-and-pasted from below in the same file. It would deserve a
helper function I think.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1
  2015-06-10 10:10 ` [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1 Galan Rémi
@ 2015-06-10 15:20   ` Matthieu Moy
  2015-06-10 15:56     ` Remi Galan Alfonso
  0 siblings, 1 reply; 12+ messages in thread
From: Matthieu Moy @ 2015-06-10 15:20 UTC (permalink / raw)
  To: Galan Rémi
  Cc: Git List, Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Junio C Hamano, Eric Sunshine

Galan Rémi <remi.galan-alfonso@ensimag.grenoble-inp.fr> writes:

> +# from the todolist in stdin
> +check_bad_cmd_and_sha () {
> +	git stripspace --strip-comments |
> +	while read -r command sha1 rest
> +	do
> +		case $command in
> +		''|noop|x|"exec")
> +			;;
> +		pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)
> +			if test -z $sha1
> +			then
> +				echo "$command $rest" >>"$todo".badsha
> +			else
> +				sha1_verif="$(git rev-parse --verify --quiet $sha1^{commit})"
> +				if test -z $sha1_verif
> +				then
> +					echo "$command $sha1 $rest" \
> +						>>"$todo".badsha

When you reach the right end of your screen because of indentation,
cutting lines with \ is rarely the best option. Having 5 levels of
indentation is a sign that you should make more functions.

How about:

check_bad_cmd_and_sha () {
	git stripspace --strip-comments |
	while read -r command sha1 rest
	do
		case $command in
		''|noop|x|"exec")
			;;
		pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)
			check_commit_id $sha1
			;;
		*)
			record_bad_command $sha1
			;;
	esac
}

?

> +		*)
> +			if test -z $sha1
> +			then
> +				echo "$command" >>"$todo".badcmd

Avoid echo on user-supplied data.

> +			else
> +				commit="$(git rev-list --oneline -1 --ignore-missing $sha1 2>/dev/null)"
> +				if test -z "$commit"
> +				then
> +					echo "$command $sha1 $rest" \
> +						>>"$todo".badcmd
> +				else
> +					echo "$command $commit" >>"$todo".badcmd
> +				fi
> +			fi

What are you trying to do here? It seems that you are trying to recover
the line, but the line is your input, you shouldn't have to recompute
it.

Why isn't printf '%s %s %s' "$command" "$sha1" "$rest" sufficient in all
cases?

Maybe it would be better to read line by line (to avoid loosing
whitespace information for example), like

	while read -r line
	do
		printf '%s' "$line" | read -r cmd sha1 rest
		case $sha1 in
			...

or maybe it's overkill.

> +	check_bad_cmd_and_sha <"$todo"
> +
> +	if test -s "$todo".badsha
> +	then
> +		raiseError=t

We usually don't use camelCase in shell-scripts. raise_error would be
the usual way to spell in in Git's codebase.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH/RFCv5 2/3] git rebase -i: warn about removed commits
  2015-06-10 14:53   ` Matthieu Moy
@ 2015-06-10 15:47     ` Remi Galan Alfonso
  2015-06-10 15:55       ` Matthieu Moy
  0 siblings, 1 reply; 12+ messages in thread
From: Remi Galan Alfonso @ 2015-06-10 15:47 UTC (permalink / raw)
  To: Matthieu Moy
  Cc: Git List, Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Junio C Hamano, Eric Sunshine

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
> > +                        warn_file "$todo".miss
> 
> I would find it more elegant with less intermediate files, like
> 
> git rev-list $opt <"$todo".miss | while read -r line
> do
>         warn " - $line"
> done

I am not really sure since I also use warn_file to display the bad
commands and SHA-1 in 3/3.

Noted for the other points.

Thanks,
Rémi

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH/RFCv5 2/3] git rebase -i: warn about removed commits
  2015-06-10 15:47     ` Remi Galan Alfonso
@ 2015-06-10 15:55       ` Matthieu Moy
  2015-06-10 15:59         ` Remi Galan Alfonso
  0 siblings, 1 reply; 12+ messages in thread
From: Matthieu Moy @ 2015-06-10 15:55 UTC (permalink / raw)
  To: Remi Galan Alfonso
  Cc: Git List, Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Junio C Hamano, Eric Sunshine

Remi Galan Alfonso <remi.galan-alfonso@ensimag.grenoble-inp.fr> writes:

> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
>> > +                        warn_file "$todo".miss
>> 
>> I would find it more elegant with less intermediate files, like
>> 
>> git rev-list $opt <"$todo".miss | while read -r line
>> do
>>         warn " - $line"
>> done
>
> I am not really sure since I also use warn_file to display the bad
> commands and SHA-1 in 3/3.

I noticed this later indeed. But had the function been eg. warn_pipe,
you could have written

git rev-list $opt <"$todo".miss | warn_pipe

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1
  2015-06-10 15:20   ` Matthieu Moy
@ 2015-06-10 15:56     ` Remi Galan Alfonso
  2015-06-10 16:08       ` Matthieu Moy
  0 siblings, 1 reply; 12+ messages in thread
From: Remi Galan Alfonso @ 2015-06-10 15:56 UTC (permalink / raw)
  To: Matthieu Moy
  Cc: Git List, Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Junio C Hamano, Eric Sunshine

> > +        git stripspace --strip-comments |
> > +        while read -r command sha1 rest
> > +        do
> > +                case $command in
> > +                ''|noop|x|"exec")
> > +                        ;;
> > +                pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)
> > +                        if test -z $sha1
> > +                        then
> > +                                echo "$command $rest" >>"$todo".badsha
> > +                        else
> > +                                sha1_verif="$(git rev-parse --verify --quiet $sha1^{commit})"
> > +                                if test -z $sha1_verif
> > +                                then
> > +                                        echo "$command $sha1 $rest" \
> > +                                                >>"$todo".badsha
> 
> When you reach the right end of your screen because of indentation,
> cutting lines with \ is rarely the best option. Having 5 levels of
> indentation is a sign that you should make more functions.

Yeah, I wasn't overly happy with that either, I will try to add some
functions (your example seems like a good way of refactoring).

> > +                                commit="$(git rev-list --oneline -1 --ignore-missing $sha1 2>/dev/null)"
> > +                                if test -z "$commit"
> > +                                then
> > +                                        echo "$command $sha1 $rest" \
> > +                                                >>"$todo".badcmd
> > +                                else
> > +                                        echo "$command $commit" >>"$todo".badcmd
> > +                                fi
> > +                        fi
> 
> What are you trying to do here? It seems that you are trying to recover
> the line, but the line is your input, you shouldn't have to recompute
> it.
> 
> Why isn't printf '%s %s %s' "$command" "$sha1" "$rest" sufficient in all
> cases?

It is mainly because here the SHA-1 is a long one (40 chars), however
I agree that computing short_sha1 and then printf '%s %s %s'
"$command" "$short_sha1" "$rest" should be good in this case.

> Maybe it would be better to read line by line (to avoid loosing
> whitespace information for example), like
> 
>         while read -r line
>         do
>                 printf '%s' "$line" | read -r cmd sha1 rest
>                 case $sha1 in
>                         ...
> 
> or maybe it's overkill.

Could be a good idea, though I am not completely convinced about it
yet.

Noted for the other points.

Thanks,
Rémi

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH/RFCv5 2/3] git rebase -i: warn about removed commits
  2015-06-10 15:55       ` Matthieu Moy
@ 2015-06-10 15:59         ` Remi Galan Alfonso
  0 siblings, 0 replies; 12+ messages in thread
From: Remi Galan Alfonso @ 2015-06-10 15:59 UTC (permalink / raw)
  To: Matthieu Moy
  Cc: Git List, Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Junio C Hamano, Eric Sunshine

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
> Remi Galan Alfonso <remi.galan-alfonso@ensimag.grenoble-inp.fr> writes:
> 
> > Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
> >> > +                        warn_file "$todo".miss
> >>
> >> I would find it more elegant with less intermediate files, like
> >>
> >> git rev-list $opt <"$todo".miss | while read -r line
> >> do
> >>         warn " - $line"
> >> done
> >
> > I am not really sure since I also use warn_file to display the bad
> > commands and SHA-1 in 3/3.
> 
> I noticed this later indeed. But had the function been eg. warn_pipe,
> you could have written
> 
> git rev-list $opt <"$todo".miss | warn_pipe

Interesting, I'll try to do something similar to this.

Rémi

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1
  2015-06-10 15:56     ` Remi Galan Alfonso
@ 2015-06-10 16:08       ` Matthieu Moy
  2015-06-13 23:17         ` Remi Galan Alfonso
  0 siblings, 1 reply; 12+ messages in thread
From: Matthieu Moy @ 2015-06-10 16:08 UTC (permalink / raw)
  To: Remi Galan Alfonso
  Cc: Git List, Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Junio C Hamano, Eric Sunshine

Remi Galan Alfonso <remi.galan-alfonso@ensimag.grenoble-inp.fr> writes:

> It is mainly because here the SHA-1 is a long one (40 chars)

OK, but then the minimum would be to add a comment saying that.

Now, this makes me wonder why you are doing the check after the sha1
expansion and not before. Also, when running `git bisect --edit-todo`, I
do get the short sha1. So, there's a piece of code doing what you want
somewhere already. You may want to use it.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1
  2015-06-10 16:08       ` Matthieu Moy
@ 2015-06-13 23:17         ` Remi Galan Alfonso
  2015-06-15  8:25           ` Matthieu Moy
  0 siblings, 1 reply; 12+ messages in thread
From: Remi Galan Alfonso @ 2015-06-13 23:17 UTC (permalink / raw)
  To: Matthieu Moy
  Cc: Git List, Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Junio C Hamano, Eric Sunshine

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
> Remi Galan Alfonso <remi.galan-alfonso@ensimag.grenoble-inp.fr> writes:
> 
> > It is mainly because here the SHA-1 is a long one (40 chars)
> 
> OK, but then the minimum would be to add a comment saying that.
> 
> Now, this makes me wonder why you are doing the check after the sha1
> expansion and not before. Also, when running `git bisect --edit-todo`, I
> do get the short sha1. So, there's a piece of code doing what you want
> somewhere already. You may want to use it.

Originally I did the whole checking after the expansion because I
though that it was a better idea to avoid doing it myself (Comparing
the whole SHA-1 instead of partial ones to find missing ones made more
sense for me since otherwise I would have to check if one is the
prefix of the other or expand to the same size before comparing).

However I agree that adding a comment would make things clearer. Will
probably do that.

Thank you,
Rémi

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1
  2015-06-13 23:17         ` Remi Galan Alfonso
@ 2015-06-15  8:25           ` Matthieu Moy
  0 siblings, 0 replies; 12+ messages in thread
From: Matthieu Moy @ 2015-06-15  8:25 UTC (permalink / raw)
  To: Remi Galan Alfonso
  Cc: Git List, Remi Lespinet, Guillaume Pages, Louis-Alexandre Stuber,
	Antoine Delaite, Junio C Hamano, Eric Sunshine

Remi Galan Alfonso <remi.galan-alfonso@ensimag.grenoble-inp.fr> writes:

> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
>> Remi Galan Alfonso <remi.galan-alfonso@ensimag.grenoble-inp.fr> writes:
>> 
>> > It is mainly because here the SHA-1 is a long one (40 chars)
>> 
>> OK, but then the minimum would be to add a comment saying that.
>> 
>> Now, this makes me wonder why you are doing the check after the sha1
>> expansion and not before. Also, when running `git bisect --edit-todo`, I
>> do get the short sha1. So, there's a piece of code doing what you want
>> somewhere already. You may want to use it.
>
> Originally I did the whole checking after the expansion because I
> though that it was a better idea to avoid doing it myself (Comparing
> the whole SHA-1 instead of partial ones to find missing ones made more
> sense for me since otherwise I would have to check if one is the
> prefix of the other or expand to the same size before comparing).

Checking the missing commits after expansion makes sense (but it is only
a matter of adding "| git rev-list --no-walk --stdin" somewhere in the
pipeline).

But IMHO, checking the syntax errors is better done as early as possible
if you want accurate error messages. This way you still have what the
user typed available.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2015-06-15  8:26 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-10 10:10 [PATCH/RFCv5 1/3] git-rebase -i: add command "drop" to remove a commit Galan Rémi
2015-06-10 10:10 ` [PATCH/RFCv5 2/3] git rebase -i: warn about removed commits Galan Rémi
2015-06-10 14:53   ` Matthieu Moy
2015-06-10 15:47     ` Remi Galan Alfonso
2015-06-10 15:55       ` Matthieu Moy
2015-06-10 15:59         ` Remi Galan Alfonso
2015-06-10 10:10 ` [PATCH/RFCv5 3/3] git rebase -i: add static check for commands and SHA-1 Galan Rémi
2015-06-10 15:20   ` Matthieu Moy
2015-06-10 15:56     ` Remi Galan Alfonso
2015-06-10 16:08       ` Matthieu Moy
2015-06-13 23:17         ` Remi Galan Alfonso
2015-06-15  8:25           ` Matthieu Moy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.