From: Will Palmer <wmpalmer@gmail.com>
To: git@vger.kernel.org
Cc: wmpalmer@gmail.com, gitster@pobox.com, peff@peff.net,
raa.lkml@gmail.com, jrnieder@gmail.com
Subject: [PATCH v3 2/5] pretty: make %H/%h/etc respect --abbrev[-commit]
Date: Fri, 30 Apr 2010 20:35:25 +0100 [thread overview]
Message-ID: <1272656128-2002-3-git-send-email-wmpalmer@gmail.com> (raw)
In-Reply-To: <1272656128-2002-1-git-send-email-wmpalmer@gmail.com>
Prior to this, the output of %H was always 40 characters long, and the
output of %h always DEFAULT_ABBREV characters long, without regard to
whether --abbrev-commit or --abbrev had been passed.
Here we make "git log --pretty=%H --abbrev-commit" synonymous with
"git log --pretty=%h", and make %h/abbreviated-%H respect the length
specified for --abbrev.
The same is applied to other commit-placeholders %P and %p, and
--abbrev is respected for %t, though %T is not changed.
Signed-off-by: Will Palmer <wmpalmer@gmail.com>
---
builtin/rev-list.c | 1 +
builtin/shortlog.c | 2 ++
commit.h | 3 ++-
log-tree.c | 6 ++++--
pretty.c | 30 +++++++++++++++++++-----------
t/t4205-log-pretty-formats.sh | 17 +++++++++++++++++
6 files changed, 45 insertions(+), 14 deletions(-)
diff --git a/builtin/rev-list.c b/builtin/rev-list.c
index 5a53862..1d1e59c 100644
--- a/builtin/rev-list.c
+++ b/builtin/rev-list.c
@@ -98,6 +98,7 @@ static void show_commit(struct commit *commit, void *data)
struct strbuf buf = STRBUF_INIT;
struct pretty_print_context ctx = {0};
ctx.abbrev = revs->abbrev;
+ ctx.abbrev_commit = revs->abbrev_commit;
ctx.date_mode = revs->date_mode;
ctx.use_color = DIFF_OPT_TST(&revs->diffopt, COLOR_DIFF);
pretty_print_commit(revs->commit_format, commit, &buf, &ctx);
diff --git a/builtin/shortlog.c b/builtin/shortlog.c
index 7aee491..5c0721c 100644
--- a/builtin/shortlog.c
+++ b/builtin/shortlog.c
@@ -143,6 +143,8 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit, struct rev
struct strbuf ufbuf = STRBUF_INIT;
struct pretty_print_context ctx = {0};
+ ctx.abbrev = rev->abbrev;
+ ctx.abbrev_commit = rev->abbrev_commit;
ctx.use_color = DIFF_OPT_TST(&rev->diffopt, COLOR_DIFF);
pretty_print_commit(CMIT_FMT_RAW, commit, &buf, &ctx);
buffer = buf.buf;
diff --git a/commit.h b/commit.h
index b6caf91..4c52069 100644
--- a/commit.h
+++ b/commit.h
@@ -65,7 +65,8 @@ enum cmit_fmt {
struct pretty_print_context
{
- int abbrev;
+ int abbrev; /* length of abbreviated hashes */
+ int abbrev_commit; /* whether or not to abbreviate commit hashes */
const char *subject;
const char *after_subject;
enum date_mode date_mode;
diff --git a/log-tree.c b/log-tree.c
index 6bb4748..02233ed 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -279,10 +279,13 @@ void show_log(struct rev_info *opt)
struct strbuf msgbuf = STRBUF_INIT;
struct log_info *log = opt->loginfo;
struct commit *commit = log->commit, *parent = log->parent;
- int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
+ int abbrev_commit;
const char *extra_headers = opt->extra_headers;
struct pretty_print_context ctx = {0};
+ ctx.abbrev = opt->diffopt.abbrev;
+ ctx.abbrev_commit = opt->abbrev_commit && opt->commit_format != CMIT_FMT_RAW;
ctx.use_color = DIFF_OPT_TST(&opt->diffopt, COLOR_DIFF);
+ abbrev_commit = ctx.abbrev_commit ? ctx.abbrev : 40;
opt->loginfo = NULL;
ctx.show_notes = opt->show_notes;
@@ -411,7 +414,6 @@ void show_log(struct rev_info *opt)
if (ctx.need_8bit_cte >= 0)
ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
ctx.date_mode = opt->date_mode;
- ctx.abbrev = opt->diffopt.abbrev;
ctx.after_subject = extra_headers;
ctx.reflog_info = opt->reflog_info;
pretty_print_commit(opt->commit_format, commit, &msgbuf, &ctx);
diff --git a/pretty.c b/pretty.c
index fdb5e16..60ed9f6 100644
--- a/pretty.c
+++ b/pretty.c
@@ -725,13 +725,16 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
switch (placeholder[0]) {
case 'H': /* commit hash */
- strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
- return 1;
case 'h': /* abbreviated commit hash */
+ if (placeholder[0] != 'h' && !c->pretty_ctx->abbrev_commit) {
+ strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
+ return 1;
+ }
+
if (add_again(sb, &c->abbrev_commit_hash))
return 1;
strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
- DEFAULT_ABBREV));
+ c->pretty_ctx->abbrev));
c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
return 1;
case 'T': /* tree hash */
@@ -741,24 +744,29 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
if (add_again(sb, &c->abbrev_tree_hash))
return 1;
strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
- DEFAULT_ABBREV));
+ c->pretty_ctx->abbrev));
c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
return 1;
case 'P': /* parent hashes */
- for (p = commit->parents; p; p = p->next) {
- if (p != commit->parents)
- strbuf_addch(sb, ' ');
- strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
- }
- return 1;
case 'p': /* abbreviated parent hashes */
+ if (placeholder[0] != 'p' && !c->pretty_ctx->abbrev_commit) {
+ for (p = commit->parents; p; p = p->next) {
+ if (p != commit->parents)
+ strbuf_addch(sb, ' ');
+ strbuf_addstr(sb,
+ sha1_to_hex(p->item->object.sha1));
+ }
+ return 1;
+ }
+
if (add_again(sb, &c->abbrev_parent_hashes))
return 1;
for (p = commit->parents; p; p = p->next) {
if (p != commit->parents)
strbuf_addch(sb, ' ');
strbuf_addstr(sb, find_unique_abbrev(
- p->item->object.sha1, DEFAULT_ABBREV));
+ p->item->object.sha1,
+ c->pretty_ctx->abbrev));
}
c->abbrev_parent_hashes.len = sb->len -
c->abbrev_parent_hashes.off;
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index b7ec943..a33f157 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -48,5 +48,22 @@ for color in red green blue reset; do
done
done
+test_expect_success "reset color flags" "git config --unset color.ui"
+
+test_expect_success "%H with --abbrev-commit should be synonym for %h" \
+ "git log -1 --pretty='format:%H' --abbrev-commit > expected &&
+ git log -1 --pretty='format:%h' > actual &&
+ test_cmp expected actual"
+
+test_expect_success "%H with --abbrev-commit should respect --abbrev" \
+ 'test 20 = $(git log -1 --pretty="format:%H" --abbrev-commit --abbrev=20 | wc -c)'
+
+test_expect_success "%h should respect --abbrev" \
+ 'test 20 = $(git log -1 --pretty="format:%h" --abbrev-commit --abbrev=20 | wc -c)'
+
+test_expect_success "log --pretty=raw should NOT respect --abbrev-commit" \
+ 'git log -1 --pretty=raw > expected &&
+ git log -1 --pretty=raw --abbrev-commit > actual &&
+ test_cmp expected actual'
test_done
--
1.7.1.rc1.13.gbb0a0a.dirty
next prev parent reply other threads:[~2010-04-30 19:38 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-04-30 19:35 [PATCH v3 0/5] pretty: format aliases Will Palmer
2010-04-30 19:35 ` [PATCH v3 1/5] pretty: add conditional %C?colorname placeholders Will Palmer
2010-05-02 3:12 ` Junio C Hamano
2010-05-02 9:31 ` Will Palmer
2010-04-30 19:35 ` Will Palmer [this message]
2010-05-02 3:13 ` [PATCH v3 2/5] pretty: make %H/%h/etc respect --abbrev[-commit] Junio C Hamano
2010-05-02 4:45 ` Jeff King
2010-05-02 5:33 ` Junio C Hamano
2010-05-02 5:40 ` Jeff King
2010-05-02 7:52 ` Will Palmer
2010-05-02 8:50 ` Will Palmer
2010-05-02 9:11 ` Junio C Hamano
2010-05-02 9:17 ` Jonathan Nieder
2010-04-30 19:35 ` [PATCH v3 3/5] pretty: make it easier to add new formats Will Palmer
2010-04-30 19:35 ` [PATCH v3 4/5] pretty: add infrastructure to allow format aliases Will Palmer
2010-05-02 3:13 ` Junio C Hamano
2010-05-02 9:01 ` Will Palmer
2010-04-30 19:35 ` [PATCH v3 5/5] pretty: add aliases for pretty formats Will Palmer
2010-05-02 3:13 ` Junio C Hamano
2010-05-02 4:47 ` Jeff King
2010-05-02 3:12 ` [PATCH v3 0/5] pretty: format aliases Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1272656128-2002-3-git-send-email-wmpalmer@gmail.com \
--to=wmpalmer@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jrnieder@gmail.com \
--cc=peff@peff.net \
--cc=raa.lkml@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).