Skip to contentSkip to conversation

git-merge: make it usable as the first class UI again.

PATCH In review Junio C Hamano 1 participant 2 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-merge: make it usable as the first class UI again.

reflog wants to have a short-and-sweet single line message, but its set_reflog_action was slurping the whole command line arguments. When given a multi-line commit message from the command line, reflog code refused to update the ref with the resulting commit.

---
* When pull threw huge merge message, it has already set the reflog action on its own so this breakage was hidden. Also because most of the time people did not specify -m message (or use the old-style <msg> HEAD <others>) from the command line, nobody seems to have noticed this.

 git-merge.sh |    2 +-
 git-pull.sh  |   27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletions(-)
git-merge.sh +1 -1
index 7b59026..91909b7 100755
@@ -7,7 +7,7 @@ USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commi
 
 SUBDIRECTORY_OK=Yes
 . git-sh-setup
-set_reflog_action "merge $*"
+set_reflog_action merge
 require_work_tree
 cd_to_toplevel
 
git-pull.sh +27 -0
index a3665d7..fc76aea 100755
@@ -51,6 +51,33 @@ do
 	shift
 done
 
+if test "X$1" = X.
+then
+	# We are merging from this repository.  We can avoid fetch
+	# and go right into merge if the user isn't doing something
+	# odd like asking us to also update tracking branches in
+	# this repository as part of the pull.  Yeah, they probably
+	# shouldn't do that - but we allowed it in the past...
+	#
+	direct_merge=1
+	for remote
+	do
+		case "$remote" in
+		*:*) direct_merge=0; break;;
+		esac
+	done
+	if test $direct_merge = 1
+	then
+		shift
+		exec git-merge \
+			$no_summary $no_commit $squash $strategy_args \
+			"$@"
+	else
+		echo >&2 "Clever... Updating tracking branch while pulling from yourself."
+		echo >&2
+	fi
+fi
+
 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
 git-fetch --update-head-ok "$@" || exit 1
 
Signature · 1 line
1.5.0.rc2.g8a816
#1 lore raw
Quoted 6 lines · Junio C Hamano

reflog wants to have a short-and-sweet single line message, but its set_reflog_action was slurping the whole command line arguments. When given a multi-line commit message from the command line, reflog code refused to update the ref with the resulting commit.

Actually, let's rescind that one -- it was very silly of me. We should do this instead.

-- >8 --
[PATCH] Make sure we do not write bogus reflog entries.

The file format dictates that entries are LF terminated so the message cannot have one in it. Chomp the message to make sure it only has a single line if necessary, while removing the leading whitespace.

---
 builtin-update-ref.c |    2 --
 refs.c               |   39 +++++++++++++++++++++++----------------
 2 files changed, 23 insertions(+), 18 deletions(-)
builtin-update-ref.c +0 -2
index b34e598..f2506fa 100644
@@ -23,8 +23,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 			msg = argv[++i];
 			if (!*msg)
 				die("Refusing to perform update with empty message.");
-			if (strchr(msg, '\n'))
-				die("Refusing to perform update with \\n in message.");
 			continue;
 		}
 		if (!strcmp("-d", argv[i])) {
refs.c +23 -16
index 4323e9a..0840b3b 100644
@@ -925,6 +925,7 @@ static int log_ref_write(struct ref_lock *lock,
 {
 	int logfd, written, oflags = O_APPEND | O_WRONLY;
 	unsigned maxlen, len;
+	int msglen;
 	char *logrec;
 	const char *committer;
 
@@ -958,24 +959,30 @@ static int log_ref_write(struct ref_lock *lock,
 				     lock->log_file, strerror(errno));
 	}
 
-	committer = git_committer_info(-1);
+	msglen = 0;
 	if (msg) {
-		maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
-		logrec = xmalloc(maxlen);
-		len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
-			sha1_to_hex(lock->old_sha1),
-			sha1_to_hex(sha1),
-			committer,
-			msg);
-	}
-	else {
-		maxlen = strlen(committer) + 2*40 + 4;
-		logrec = xmalloc(maxlen);
-		len = snprintf(logrec, maxlen, "%s %s %s\n",
-			sha1_to_hex(lock->old_sha1),
-			sha1_to_hex(sha1),
-			committer);
+		/* clean up the message and make sure it is a single line */
+		for ( ; *msg; msg++)
+			if (!isspace(*msg))
+				break;
+		if (*msg) {
+			const char *ep = strchr(msg, '\n');
+			if (ep)
+				msglen = ep - msg;
+			else
+				msglen = strlen(msg);
+		}
 	}
+
+	committer = git_committer_info(-1);
+	maxlen = strlen(committer) + msglen + 100;
+	logrec = xmalloc(maxlen);
+	len = sprintf(logrec, "%s %s %s\n",
+		      sha1_to_hex(lock->old_sha1),
+		      sha1_to_hex(sha1),
+		      committer);
+	if (msglen)
+		len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
 	written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
 	free(logrec);
 	close(logfd);