@matheusbsbs / matheus-bagliano / commits / 146ea068a0
Fork 0

git commit --author=$name: look $name up in existing commits

This allows "git commit --author=$name" to accept a name that is not in the required "A U Thor <author@example.xz>" format, and use that to look up an author name that matches from existing commits. When using this feature, it is the user's responsibility to give a name that uniquely matches the name s/he wants, as the logic returns the name from the first matching commit. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 26, 2008 at 23:13 UTC 146ea068a0434a7423d8b0d77f27ccff0a584ac4
2 files changed +32 -2
Documentation/git-commit.txt
+4 -2
@@ -75,8 +75,10 @@ OPTIONS
75 read the message from the standard input.
76
77 --author=<author>::
78 - Override the author name used in the commit. Use
79 - `A U Thor <author@example.com>` format.
78 + Override the author name used in the commit. You can use the
79 + standard `A U Thor <author@example.com>` format. Otherwise,
80 + an existing commit that matches the given string and its author
81 + name is used.
82
83 -m <msg>::
84 --message=<msg>::
builtin-commit.c
+28
@@ -710,6 +710,31 @@ static int message_is_empty(struct strbuf *sb, int start)
710 return 1;
711 }
712
713 +static const char *find_author_by_nickname(const char *name)
714 +{
715 + struct rev_info revs;
716 + struct commit *commit;
717 + struct strbuf buf = STRBUF_INIT;
718 + const char *av[20];
719 + int ac = 0;
720 +
721 + init_revisions(&revs, NULL);
722 + strbuf_addf(&buf, "--author=%s", name);
723 + av[++ac] = "--all";
724 + av[++ac] = "-i";
725 + av[++ac] = buf.buf;
726 + av[++ac] = NULL;
727 + setup_revisions(ac, av, &revs, NULL);
728 + prepare_revision_walk(&revs);
729 + commit = get_revision(&revs);
730 + if (commit) {
731 + strbuf_release(&buf);
732 + format_commit_message(commit, "%an <%ae>", &buf, DATE_NORMAL);
733 + return strbuf_detach(&buf, NULL);
734 + }
735 + die("No existing author found with '%s'", name);
736 +}
737 +
738 static int parse_and_validate_options(int argc, const char *argv[],
739 const char * const usage[],
740 const char *prefix)
@@ -720,6 +745,9 @@ static int parse_and_validate_options(int argc, const char *argv[],
745 logfile = parse_options_fix_filename(prefix, logfile);
746 template_file = parse_options_fix_filename(prefix, template_file);
747
748 + if (force_author && !strchr(force_author, '>'))
749 + force_author = find_author_by_nickname(force_author);
750 +
751 if (logfile || message.len || use_message)
752 use_editor = 0;
753 if (edit_flag)