From 4acfd1b799acf43642a28a22cc794266c25129ef Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Mon, 10 Sep 2007 12:35:05 +0200 Subject: Change semantics of interpolate to work like snprintf. Also fix many off-by-ones and a useless memset. Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- interpolate.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'interpolate.c') diff --git a/interpolate.c b/interpolate.c index 00826778fc..3de583238d 100644 --- a/interpolate.c +++ b/interpolate.c @@ -44,9 +44,8 @@ void interp_clear_table(struct interp *table, int ninterps) * { "%%", "%"}, * } * - * Returns 0 on a successful substitution pass that fits in result, - * Returns a number of bytes needed to hold the full substituted - * string otherwise. + * Returns the length of the substituted string (not including the final \0). + * Like with snprintf, if the result is >= reslen, then it overflowed. */ unsigned long interpolate(char *result, unsigned long reslen, @@ -61,8 +60,6 @@ unsigned long interpolate(char *result, unsigned long reslen, int i; char c; - memset(result, 0, reslen); - while ((c = *src)) { if (c == '%') { /* Try to match an interpolation string. */ @@ -78,9 +75,9 @@ unsigned long interpolate(char *result, unsigned long reslen, value = interps[i].value; valuelen = strlen(value); - if (newlen + valuelen + 1 < reslen) { + if (newlen + valuelen < reslen) { /* Substitute. */ - strncpy(dest, value, valuelen); + memcpy(dest, value, valuelen); dest += valuelen; } newlen += valuelen; @@ -95,8 +92,9 @@ unsigned long interpolate(char *result, unsigned long reslen, newlen++; } - if (newlen + 1 < reslen) - return 0; - else - return newlen + 2; + /* XXX: the previous loop always keep room for the ending NUL, + we just need to check if there was room for a NUL in the first place */ + if (reslen > 0) + *dest = '\0'; + return newlen; } -- cgit v1.2.3 From 55246aac6717e86c14f31391ac903ed810d1a9a0 Mon Sep 17 00:00:00 2001 From: Michal Vitecek Date: Tue, 25 Sep 2007 16:38:46 +0200 Subject: Don't use "" for placeholders and suppress printing of empty user formats. This changes the interporate() to replace entries with NULL values by the empty string, and uses it to interpolate missing fields in custom format output used in git-log and friends. It is most useful to avoid output from %b format for a commit log message that lack any body text. Signed-off-by: Junio C Hamano --- builtin-rev-list.c | 3 ++- commit.c | 3 --- interpolate.c | 6 +++++- log-tree.c | 3 ++- t/t6006-rev-list-format.sh | 8 -------- t/t7500-commit.sh | 4 ++-- 6 files changed, 11 insertions(+), 16 deletions(-) (limited to 'interpolate.c') diff --git a/builtin-rev-list.c b/builtin-rev-list.c index 3894633999..0b74eb35bc 100644 --- a/builtin-rev-list.c +++ b/builtin-rev-list.c @@ -85,7 +85,8 @@ static void show_commit(struct commit *commit) pretty_print_commit(revs.commit_format, commit, ~0, &buf, &buflen, revs.abbrev, NULL, NULL, revs.date_mode); - printf("%s%c", buf, hdr_termination); + if (*buf) + printf("%s%c", buf, hdr_termination); free(buf); } maybe_flush_or_die(stdout, "stdout"); diff --git a/commit.c b/commit.c index 99f65cee0e..c9a18180be 100644 --- a/commit.c +++ b/commit.c @@ -917,9 +917,6 @@ long format_commit_message(const struct commit *commit, const void *format, } if (msg[i]) table[IBODY].value = xstrdup(msg + i); - for (i = 0; i < ARRAY_SIZE(table); i++) - if (!table[i].value) - interp_set_entry(table, i, ""); do { char *buf = *buf_p; diff --git a/interpolate.c b/interpolate.c index 00826778fc..2f727cd05b 100644 --- a/interpolate.c +++ b/interpolate.c @@ -76,8 +76,12 @@ unsigned long interpolate(char *result, unsigned long reslen, /* Check for valid interpolation. */ if (i < ninterps) { value = interps[i].value; - valuelen = strlen(value); + if (!value) { + src += namelen; + continue; + } + valuelen = strlen(value); if (newlen + valuelen + 1 < reslen) { /* Substitute. */ strncpy(dest, value, valuelen); diff --git a/log-tree.c b/log-tree.c index a6423718e7..79e3dee276 100644 --- a/log-tree.c +++ b/log-tree.c @@ -298,7 +298,8 @@ void show_log(struct rev_info *opt, const char *sep) if (opt->show_log_size) printf("log size %i\n", len); - printf("%s%s%s", msgbuf, extra, sep); + if (*msgbuf) + printf("%s%s%s", msgbuf, extra, sep); free(msgbuf); } diff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh index ad6d0b8c9d..1e4541afea 100755 --- a/t/t6006-rev-list-format.sh +++ b/t/t6006-rev-list-format.sh @@ -79,9 +79,7 @@ EOF test_format encoding %e <<'EOF' commit 131a310eb913d107dd3c09a65d1651175898735d - commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873 - EOF test_format subject %s <<'EOF' @@ -93,9 +91,7 @@ EOF test_format body %b <<'EOF' commit 131a310eb913d107dd3c09a65d1651175898735d - commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873 - EOF test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<'EOF' @@ -121,9 +117,7 @@ test_format complex-encoding %e <<'EOF' commit f58db70b055c5718631e5c61528b28b12090cdea iso8859-1 commit 131a310eb913d107dd3c09a65d1651175898735d - commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873 - EOF test_format complex-subject %s <<'EOF' @@ -142,9 +136,7 @@ and it will be encoded in iso8859-1. We should therefore include an iso8859 character: ¡bueno! commit 131a310eb913d107dd3c09a65d1651175898735d - commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873 - EOF test_done diff --git a/t/t7500-commit.sh b/t/t7500-commit.sh index f11ada8617..abbf54ba63 100755 --- a/t/t7500-commit.sh +++ b/t/t7500-commit.sh @@ -81,7 +81,7 @@ test_expect_success 'explicit commit message should override template' ' git add foo && GIT_EDITOR=../t7500/add-content git commit --template "$TEMPLATE" \ -m "command line msg" && - commit_msg_is "command line msg" + commit_msg_is "command line msg" ' test_expect_success 'commit message from file should override template' ' @@ -90,7 +90,7 @@ test_expect_success 'commit message from file should override template' ' echo "standard input msg" | GIT_EDITOR=../t7500/add-content git commit \ --template "$TEMPLATE" --file - && - commit_msg_is "standard input msg" + commit_msg_is "standard input msg" ' test_done -- cgit v1.2.3