@matheusbsbs / matheus-bagliano / commits / 0ec29a4760
Fork 0

log_ref_write() -- do not chomp reflog message at the first LF

A reflog file is organized as one-line-per-entry records, and we enforced the file format integrity by chomping the given message at the first LF. This changes it to convert them to SP, which is more in line with the --pretty=oneline format. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 28, 2007 at 17:17 UTC 0ec29a4760e7c1dd1e168f6dbdd22b87559a26e9
1 file changed +28 -16
refs.c
+28 -16
@@ -1036,6 +1036,32 @@ void unlock_ref(struct ref_lock *lock)
1036 free(lock);
1037 }
1038
1039 +/*
1040 + * copy the reflog message msg to buf, which has been allocated sufficiently
1041 + * large, while cleaning up the whitespaces. Especially, convert LF to space,
1042 + * because reflog file is one line per entry.
1043 + */
1044 +static int copy_msg(char *buf, const char *msg)
1045 +{
1046 + char *cp = buf;
1047 + char c;
1048 + int wasspace = 1;
1049 +
1050 + *cp++ = '\t';
1051 + while ((c = *msg++)) {
1052 + if (wasspace && isspace(c))
1053 + continue;
1054 + wasspace = isspace(c);
1055 + if (wasspace)
1056 + c = ' ';
1057 + *cp++ = c;
1058 + }
1059 + while (buf < cp && isspace(cp[-1]))
1060 + cp--;
1061 + *cp++ = '\n';
1062 + return cp - buf;
1063 +}
1064 +
1065 static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
1066 const unsigned char *new_sha1, const char *msg)
1067 {
@@ -1080,21 +1106,7 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
1106
1107 adjust_shared_perm(log_file);
1108
1083 - msglen = 0;
1084 - if (msg) {
1085 - /* clean up the message and make sure it is a single line */
1086 - for ( ; *msg; msg++)
1087 - if (!isspace(*msg))
1088 - break;
1089 - if (*msg) {
1090 - const char *ep = strchr(msg, '\n');
1091 - if (ep)
1092 - msglen = ep - msg;
1093 - else
1094 - msglen = strlen(msg);
1095 - }
1096 - }
1097 -
1109 + msglen = msg ? strlen(msg) : 0;
1110 committer = git_committer_info(-1);
1111 maxlen = strlen(committer) + msglen + 100;
1112 logrec = xmalloc(maxlen);
@@ -1103,7 +1115,7 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
1115 sha1_to_hex(new_sha1),
1116 committer);
1117 if (msglen)
1106 - len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
1118 + len += copy_msg(logrec + len - 1, msg) - 1;
1119 written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
1120 free(logrec);
1121 if (close(logfd) != 0 || written != len)