Merge branch 'jk/warn-author-committer-after-commit'
authorJunio C Hamano <gitster@pobox.com>
Wed, 20 Jan 2010 22:40:12 +0000 (14:40 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 20 Jan 2010 22:40:12 +0000 (14:40 -0800)
* jk/warn-author-committer-after-commit:
user_ident_sufficiently_given(): refactor the logic to be usable from elsewhere
commit.c::print_summary: do not release the format string too early
commit: allow suppression of implicit identity advice
commit: show interesting ident information in summary
strbuf: add strbuf_addbuf_percentquote
strbuf_expand: convert "%%" to "%"

Conflicts:
builtin-commit.c
ident.c

Documentation/config.txt
Documentation/pretty-formats.txt
Documentation/technical/api-strbuf.txt
advice.c
advice.h
builtin-commit.c
daemon.c
strbuf.c
strbuf.h
t/t6006-rev-list-format.sh
t/t7501-commit.sh
index 2d6775c135b466ce8a95db3fd7c22c04151f8d42..d4332140a01635feab079e394f02d8c927cf37c5 100644 (file)
@@ -130,6 +130,10 @@ advice.*::
                Advice shown when linkgit:git-merge[1] refuses to
                merge to avoid overwritting local changes.
                Default: true.
+       implicitIdentity::
+               Advice on how to set your identity configuration when
+               your information is guessed from the system username and
+               domain name. Default: true.
 --
 
 core.fileMode::
index 53a9168ba7d8959e65c2442f6a078155d6f24c71..1686a54d22a746036b997d6eb8d5b85ca1d79c5d 100644 (file)
@@ -134,6 +134,7 @@ The placeholders are:
 - '%C(...)': color specification, as described in color.branch.* config option
 - '%m': left, right or boundary mark
 - '%n': newline
+- '%%': a raw '%'
 - '%x00': print a byte from a hex code
 - '%w([<w>[,<i1>[,<i2>]]])': switch line wrapping, like the -w option of
   linkgit:git-shortlog[1].
index a0e0f850f83fe164dd7c1ca87d001fe485ba2ec2..afe27599511c5f6bab6e4f799fd18e7d83bdd454 100644 (file)
@@ -199,6 +199,10 @@ character if the letter `n` appears after a `%`.  The function returns
 the length of the placeholder recognized and `strbuf_expand()` skips
 over it.
 +
+The format `%%` is automatically expanded to a single `%` as a quoting
+mechanism; callers do not need to handle the `%` placeholder themselves,
+and the callback function will not be invoked for this placeholder.
++
 All other characters (non-percent and not skipped ones) are copied
 verbatim to the strbuf.  If the callback returned zero, meaning that the
 placeholder is unknown, then the percent sign is copied, too.
@@ -214,6 +218,13 @@ which can be used by the programmer of the callback as she sees fit.
        placeholder and replacement string.  The array needs to be
        terminated by an entry with placeholder set to NULL.
 
+`strbuf_addbuf_percentquote`::
+
+       Append the contents of one strbuf to another, quoting any
+       percent signs ("%") into double-percents ("%%") in the
+       destination. This is useful for literal data to be fed to either
+       strbuf_expand or to the *printf family of functions.
+
 `strbuf_addf`::
 
        Add a formatted string to the buffer.
index cb666acc3cf8fdda777febbefac455f3667759f1..8f7de0e9ed70218f1f5355df670c537d1b818c76 100644 (file)
--- a/advice.c
+++ b/advice.c
@@ -3,6 +3,7 @@
 int advice_push_nonfastforward = 1;
 int advice_status_hints = 1;
 int advice_commit_before_merge = 1;
+int advice_implicit_identity = 1;
 
 static struct {
        const char *name;
@@ -11,6 +12,7 @@ static struct {
        { "pushnonfastforward", &advice_push_nonfastforward },
        { "statushints", &advice_status_hints },
        { "commitbeforemerge", &advice_commit_before_merge },
+       { "implicitidentity", &advice_implicit_identity },
 };
 
 int git_default_advice_config(const char *var, const char *value)
index 3de5000d8cb35c1d2c31867b942a54b487bed836..728ab90ef17b4ebccff4ccb662108f8c5fded690 100644 (file)
--- a/advice.h
+++ b/advice.h
@@ -4,6 +4,7 @@
 extern int advice_push_nonfastforward;
 extern int advice_status_hints;
 extern int advice_commit_before_merge;
+extern int advice_implicit_identity;
 
 int git_default_advice_config(const char *var, const char *value);
 
index 9b9dbaa5bf3a42c079d67902f1d8a125d0dacbba..42f11c30ca9e099cbed957107c25200efcc6ecd5 100644 (file)
@@ -36,7 +36,20 @@ static const char * const builtin_status_usage[] = {
        NULL
 };
 
+static const char implicit_ident_advice[] =
+"Your name and email address were configured automatically based\n"
+"on your username and hostname. Please check that they are accurate.\n"
+"You can suppress this message by setting them explicitly:\n"
+"\n"
+"    git config --global user.name Your Name\n"
+"    git config --global user.email you@example.com\n"
+"\n"
+"If the identity used for this commit is wrong, you can fix it with:\n"
+"\n"
+"    git commit --amend --author='Your Name <you@example.com>'\n";
+
 static unsigned char head_sha1[20];
+
 static char *use_message_buffer;
 static const char commit_editmsg[] = "COMMIT_EDITMSG";
 static struct lock_file index_lock; /* real index */
@@ -1055,9 +1068,12 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 {
        struct rev_info rev;
        struct commit *commit;
-       static const char *format = "format:%h] %s";
+       struct strbuf format = STRBUF_INIT;
        unsigned char junk_sha1[20];
        const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
+       struct pretty_print_context pctx = {0};
+       struct strbuf author_ident = STRBUF_INIT;
+       struct strbuf committer_ident = STRBUF_INIT;
 
        commit = lookup_commit(sha1);
        if (!commit)
@@ -1065,6 +1081,25 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
        if (!commit || parse_commit(commit))
                die("could not parse newly created commit");
 
+       strbuf_addstr(&format, "format:%h] %s");
+
+       format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
+       format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
+       if (strbuf_cmp(&author_ident, &committer_ident)) {
+               strbuf_addstr(&format, "\n Author: ");
+               strbuf_addbuf_percentquote(&format, &author_ident);
+       }
+       if (!user_ident_sufficiently_given()) {
+               strbuf_addstr(&format, "\n Committer: ");
+               strbuf_addbuf_percentquote(&format, &committer_ident);
+               if (advice_implicit_identity) {
+                       strbuf_addch(&format, '\n');
+                       strbuf_addstr(&format, implicit_ident_advice);
+               }
+       }
+       strbuf_release(&author_ident);
+       strbuf_release(&committer_ident);
+
        init_revisions(&rev, prefix);
        setup_revisions(0, NULL, &rev, NULL);
 
@@ -1075,7 +1110,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 
        rev.verbose_header = 1;
        rev.show_root_diff = 1;
-       get_commit_format(format, &rev);
+       get_commit_format(format.buf, &rev);
        rev.always_show_header = 0;
        rev.diffopt.detect_rename = 1;
        rev.diffopt.rename_limit = 100;
@@ -1094,10 +1129,11 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
                struct pretty_print_context ctx = {0};
                struct strbuf buf = STRBUF_INIT;
                ctx.date_mode = DATE_NORMAL;
-               format_commit_message(commit, format + 7, &buf, &ctx);
+               format_commit_message(commit, format.buf + 7, &buf, &ctx);
                printf("%s\n", buf.buf);
                strbuf_release(&buf);
        }
+       strbuf_release(&format);
 }
 
 static int git_commit_config(const char *k, const char *v, void *cb)
index 918e560b4e1d4b78bd2d3223db61e915c4a3ccbb..360635eb1c14608b44568ce39d969eb8822e688d 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -147,7 +147,6 @@ static char *path_ok(char *directory)
                        { "IP", ip_address },
                        { "P", tcp_port },
                        { "D", directory },
-                       { "%", "%" },
                        { NULL }
                };
 
index 3fa81b33656a5d2d4887b949fd103920173d37a7..67448b7b07aa9d07b0c8cc250e3e0fad64c8fb5e 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -220,6 +220,12 @@ void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
                        break;
                format = percent + 1;
 
+               if (*format == '%') {
+                       strbuf_addch(sb, '%');
+                       format++;
+                       continue;
+               }
+
                consumed = fn(sb, format, context);
                if (consumed)
                        format += consumed;
@@ -244,6 +250,17 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
        return 0;
 }
 
+void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
+{
+       int i, len = src->len;
+
+       for (i = 0; i < len; i++) {
+               if (src->buf[i] == '%')
+                       strbuf_addch(dst, '%');
+               strbuf_addch(dst, src->buf[i]);
+       }
+}
+
 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
 {
        size_t res;
index b37f06a34b75a14dccf99a034b683f702cb4724e..8ce2a2e54b0180ffb8dc573953872ff00dc419b5 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -115,6 +115,7 @@ struct strbuf_expand_dict_entry {
        const char *value;
 };
 extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
+extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
 
 __attribute__((format (printf,2,3)))
 extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
index 571931588eda5799efbf2b0789abd10b8770a0a7..b0047d3c6b593795561ce908ab8e10ff574d3dbc 100755 (executable)
@@ -19,6 +19,13 @@ test_cmp expect.$1 output.$1
 "
 }
 
+test_format percent %%h <<'EOF'
+commit 131a310eb913d107dd3c09a65d1651175898735d
+%h
+commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
+%h
+EOF
+
 test_format hash %H%n%h <<'EOF'
 commit 131a310eb913d107dd3c09a65d1651175898735d
 131a310eb913d107dd3c09a65d1651175898735d
index a5297012410271f64398124da6e4c7dd8eec3154..7940901d47fd457cda77ee333aa40145433be4d4 100755 (executable)
@@ -117,7 +117,11 @@ test_expect_success \
 test_expect_success \
        "overriding author from command line" \
        "echo 'gak' >file && \
-        git commit -m 'author' --author 'Rubber Duck <rduck@convoy.org>' -a"
+        git commit -m 'author' --author 'Rubber Duck <rduck@convoy.org>' -a >output 2>&1"
+
+test_expect_success \
+       "commit --author output mentions author" \
+       "grep Rubber.Duck output"
 
 test_expect_success PERL \
        "interactive add" \