Skip to content

Commit f9db0c0

Browse files
committed
Merge branch 'jc/abbrev-auto'
"git push" and "git fetch" reports from what old object to what new object each ref was updated, using abbreviated refnames, and they attempt to align the columns for this and other pieces of information. The way these codepaths compute how many display columns to allocate for the object names portion of this output has been updated to match the recent "auto scale the default abbreviation length" change. * jc/abbrev-auto: transport: compute summary-width dynamically transport: allow summary-width to be computed dynamically fetch: pass summary_width down the callchain transport: pass summary_width down the callchain
2 parents d7ae013 + db98d9b commit f9db0c0

File tree

3 files changed

+81
-42
lines changed

3 files changed

+81
-42
lines changed

builtin/fetch.c

+21-16
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
#include "argv-array.h"
1818
#include "utf8.h"
1919

20-
#define TRANSPORT_SUMMARY(x) \
21-
(int)(TRANSPORT_SUMMARY_WIDTH + strlen(x) - gettext_width(x)), (x)
22-
2320
static const char * const builtin_fetch_usage[] = {
2421
N_("git fetch [<options>] [<repository> [<refspec>...]]"),
2522
N_("git fetch [<options>] <group>"),
@@ -580,9 +577,12 @@ static void print_compact(struct strbuf *display,
580577

581578
static void format_display(struct strbuf *display, char code,
582579
const char *summary, const char *error,
583-
const char *remote, const char *local)
580+
const char *remote, const char *local,
581+
int summary_width)
584582
{
585-
strbuf_addf(display, "%c %-*s ", code, TRANSPORT_SUMMARY(summary));
583+
int width = (summary_width + strlen(summary) - gettext_width(summary));
584+
585+
strbuf_addf(display, "%c %-*s ", code, width, summary);
586586
if (!compact_format)
587587
print_remote_to_local(display, remote, local);
588588
else
@@ -594,7 +594,8 @@ static void format_display(struct strbuf *display, char code,
594594
static int update_local_ref(struct ref *ref,
595595
const char *remote,
596596
const struct ref *remote_ref,
597-
struct strbuf *display)
597+
struct strbuf *display,
598+
int summary_width)
598599
{
599600
struct commit *current = NULL, *updated;
600601
enum object_type type;
@@ -608,7 +609,7 @@ static int update_local_ref(struct ref *ref,
608609
if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
609610
if (verbosity > 0)
610611
format_display(display, '=', _("[up to date]"), NULL,
611-
remote, pretty_ref);
612+
remote, pretty_ref, summary_width);
612613
return 0;
613614
}
614615

@@ -622,7 +623,7 @@ static int update_local_ref(struct ref *ref,
622623
*/
623624
format_display(display, '!', _("[rejected]"),
624625
_("can't fetch in current branch"),
625-
remote, pretty_ref);
626+
remote, pretty_ref, summary_width);
626627
return 1;
627628
}
628629

@@ -632,7 +633,7 @@ static int update_local_ref(struct ref *ref,
632633
r = s_update_ref("updating tag", ref, 0);
633634
format_display(display, r ? '!' : 't', _("[tag update]"),
634635
r ? _("unable to update local ref") : NULL,
635-
remote, pretty_ref);
636+
remote, pretty_ref, summary_width);
636637
return r;
637638
}
638639

@@ -665,7 +666,7 @@ static int update_local_ref(struct ref *ref,
665666
r = s_update_ref(msg, ref, 0);
666667
format_display(display, r ? '!' : '*', what,
667668
r ? _("unable to update local ref") : NULL,
668-
remote, pretty_ref);
669+
remote, pretty_ref, summary_width);
669670
return r;
670671
}
671672

@@ -681,7 +682,7 @@ static int update_local_ref(struct ref *ref,
681682
r = s_update_ref("fast-forward", ref, 1);
682683
format_display(display, r ? '!' : ' ', quickref.buf,
683684
r ? _("unable to update local ref") : NULL,
684-
remote, pretty_ref);
685+
remote, pretty_ref, summary_width);
685686
strbuf_release(&quickref);
686687
return r;
687688
} else if (force || ref->force) {
@@ -696,12 +697,12 @@ static int update_local_ref(struct ref *ref,
696697
r = s_update_ref("forced-update", ref, 1);
697698
format_display(display, r ? '!' : '+', quickref.buf,
698699
r ? _("unable to update local ref") : _("forced update"),
699-
remote, pretty_ref);
700+
remote, pretty_ref, summary_width);
700701
strbuf_release(&quickref);
701702
return r;
702703
} else {
703704
format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
704-
remote, pretty_ref);
705+
remote, pretty_ref, summary_width);
705706
return 1;
706707
}
707708
}
@@ -732,6 +733,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
732733
char *url;
733734
const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
734735
int want_status;
736+
int summary_width = transport_summary_width(ref_map);
735737

736738
fp = fopen(filename, "a");
737739
if (!fp)
@@ -841,13 +843,14 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
841843

842844
strbuf_reset(&note);
843845
if (ref) {
844-
rc |= update_local_ref(ref, what, rm, &note);
846+
rc |= update_local_ref(ref, what, rm, &note,
847+
summary_width);
845848
free(ref);
846849
} else
847850
format_display(&note, '*',
848851
*kind ? kind : "branch", NULL,
849852
*what ? what : "HEAD",
850-
"FETCH_HEAD");
853+
"FETCH_HEAD", summary_width);
851854
if (note.len) {
852855
if (verbosity >= 0 && !shown_url) {
853856
fprintf(stderr, _("From %.*s\n"),
@@ -914,6 +917,7 @@ static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
914917
int url_len, i, result = 0;
915918
struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
916919
char *url;
920+
int summary_width = transport_summary_width(stale_refs);
917921
const char *dangling_msg = dry_run
918922
? _(" (%s will become dangling)")
919923
: _(" (%s has become dangling)");
@@ -949,7 +953,8 @@ static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
949953
shown_url = 1;
950954
}
951955
format_display(&sb, '-', _("[deleted]"), NULL,
952-
_("(none)"), prettify_refname(ref->name));
956+
_("(none)"), prettify_refname(ref->name),
957+
summary_width);
953958
fprintf(stderr, " %s\n",sb.buf);
954959
strbuf_release(&sb);
955960
warn_dangling_symref(stderr, dangling_msg, ref->name);

transport.c

+59-25
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int v
307307
}
308308
}
309309

310-
static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
310+
static void print_ref_status(char flag, const char *summary,
311+
struct ref *to, struct ref *from, const char *msg,
312+
int porcelain, int summary_width)
311313
{
312314
if (porcelain) {
313315
if (from)
@@ -319,7 +321,7 @@ static void print_ref_status(char flag, const char *summary, struct ref *to, str
319321
else
320322
fprintf(stdout, "%s\n", summary);
321323
} else {
322-
fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
324+
fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
323325
if (from)
324326
fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
325327
else
@@ -333,15 +335,16 @@ static void print_ref_status(char flag, const char *summary, struct ref *to, str
333335
}
334336
}
335337

336-
static void print_ok_ref_status(struct ref *ref, int porcelain)
338+
static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
337339
{
338340
if (ref->deletion)
339-
print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
341+
print_ref_status('-', "[deleted]", ref, NULL, NULL,
342+
porcelain, summary_width);
340343
else if (is_null_oid(&ref->old_oid))
341344
print_ref_status('*',
342345
(starts_with(ref->name, "refs/tags/") ? "[new tag]" :
343346
"[new branch]"),
344-
ref, ref->peer_ref, NULL, porcelain);
347+
ref, ref->peer_ref, NULL, porcelain, summary_width);
345348
else {
346349
struct strbuf quickref = STRBUF_INIT;
347350
char type;
@@ -361,12 +364,14 @@ static void print_ok_ref_status(struct ref *ref, int porcelain)
361364
strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
362365
DEFAULT_ABBREV);
363366

364-
print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
367+
print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
368+
porcelain, summary_width);
365369
strbuf_release(&quickref);
366370
}
367371
}
368372

369-
static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
373+
static int print_one_push_status(struct ref *ref, const char *dest, int count,
374+
int porcelain, int summary_width)
370375
{
371376
if (!count) {
372377
char *url = transport_anonymize_url(dest);
@@ -376,88 +381,117 @@ static int print_one_push_status(struct ref *ref, const char *dest, int count, i
376381

377382
switch(ref->status) {
378383
case REF_STATUS_NONE:
379-
print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
384+
print_ref_status('X', "[no match]", ref, NULL, NULL,
385+
porcelain, summary_width);
380386
break;
381387
case REF_STATUS_REJECT_NODELETE:
382388
print_ref_status('!', "[rejected]", ref, NULL,
383-
"remote does not support deleting refs", porcelain);
389+
"remote does not support deleting refs",
390+
porcelain, summary_width);
384391
break;
385392
case REF_STATUS_UPTODATE:
386393
print_ref_status('=', "[up to date]", ref,
387-
ref->peer_ref, NULL, porcelain);
394+
ref->peer_ref, NULL, porcelain, summary_width);
388395
break;
389396
case REF_STATUS_REJECT_NONFASTFORWARD:
390397
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
391-
"non-fast-forward", porcelain);
398+
"non-fast-forward", porcelain, summary_width);
392399
break;
393400
case REF_STATUS_REJECT_ALREADY_EXISTS:
394401
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
395-
"already exists", porcelain);
402+
"already exists", porcelain, summary_width);
396403
break;
397404
case REF_STATUS_REJECT_FETCH_FIRST:
398405
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
399-
"fetch first", porcelain);
406+
"fetch first", porcelain, summary_width);
400407
break;
401408
case REF_STATUS_REJECT_NEEDS_FORCE:
402409
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
403-
"needs force", porcelain);
410+
"needs force", porcelain, summary_width);
404411
break;
405412
case REF_STATUS_REJECT_STALE:
406413
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
407-
"stale info", porcelain);
414+
"stale info", porcelain, summary_width);
408415
break;
409416
case REF_STATUS_REJECT_SHALLOW:
410417
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
411-
"new shallow roots not allowed", porcelain);
418+
"new shallow roots not allowed",
419+
porcelain, summary_width);
412420
break;
413421
case REF_STATUS_REMOTE_REJECT:
414422
print_ref_status('!', "[remote rejected]", ref,
415-
ref->deletion ? NULL : ref->peer_ref,
416-
ref->remote_status, porcelain);
423+
ref->deletion ? NULL : ref->peer_ref,
424+
ref->remote_status, porcelain, summary_width);
417425
break;
418426
case REF_STATUS_EXPECTING_REPORT:
419427
print_ref_status('!', "[remote failure]", ref,
420-
ref->deletion ? NULL : ref->peer_ref,
421-
"remote failed to report status", porcelain);
428+
ref->deletion ? NULL : ref->peer_ref,
429+
"remote failed to report status",
430+
porcelain, summary_width);
422431
break;
423432
case REF_STATUS_ATOMIC_PUSH_FAILED:
424433
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
425-
"atomic push failed", porcelain);
434+
"atomic push failed", porcelain, summary_width);
426435
break;
427436
case REF_STATUS_OK:
428-
print_ok_ref_status(ref, porcelain);
437+
print_ok_ref_status(ref, porcelain, summary_width);
429438
break;
430439
}
431440

432441
return 1;
433442
}
434443

444+
static int measure_abbrev(const struct object_id *oid, int sofar)
445+
{
446+
char hex[GIT_SHA1_HEXSZ + 1];
447+
int w = find_unique_abbrev_r(hex, oid->hash, DEFAULT_ABBREV);
448+
449+
return (w < sofar) ? sofar : w;
450+
}
451+
452+
int transport_summary_width(const struct ref *refs)
453+
{
454+
int maxw = -1;
455+
456+
for (; refs; refs = refs->next) {
457+
maxw = measure_abbrev(&refs->old_oid, maxw);
458+
maxw = measure_abbrev(&refs->new_oid, maxw);
459+
}
460+
if (maxw < 0)
461+
maxw = FALLBACK_DEFAULT_ABBREV;
462+
return (2 * maxw + 3);
463+
}
464+
435465
void transport_print_push_status(const char *dest, struct ref *refs,
436466
int verbose, int porcelain, unsigned int *reject_reasons)
437467
{
438468
struct ref *ref;
439469
int n = 0;
440470
unsigned char head_sha1[20];
441471
char *head;
472+
int summary_width = transport_summary_width(refs);
442473

443474
head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
444475

445476
if (verbose) {
446477
for (ref = refs; ref; ref = ref->next)
447478
if (ref->status == REF_STATUS_UPTODATE)
448-
n += print_one_push_status(ref, dest, n, porcelain);
479+
n += print_one_push_status(ref, dest, n,
480+
porcelain, summary_width);
449481
}
450482

451483
for (ref = refs; ref; ref = ref->next)
452484
if (ref->status == REF_STATUS_OK)
453-
n += print_one_push_status(ref, dest, n, porcelain);
485+
n += print_one_push_status(ref, dest, n,
486+
porcelain, summary_width);
454487

455488
*reject_reasons = 0;
456489
for (ref = refs; ref; ref = ref->next) {
457490
if (ref->status != REF_STATUS_NONE &&
458491
ref->status != REF_STATUS_UPTODATE &&
459492
ref->status != REF_STATUS_OK)
460-
n += print_one_push_status(ref, dest, n, porcelain);
493+
n += print_one_push_status(ref, dest, n,
494+
porcelain, summary_width);
461495
if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
462496
if (head != NULL && !strcmp(head, ref->name))
463497
*reject_reasons |= REJECT_NON_FF_HEAD;

transport.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct transport {
147147
#define TRANSPORT_PUSH_ATOMIC 8192
148148
#define TRANSPORT_PUSH_OPTIONS 16384
149149

150-
#define TRANSPORT_SUMMARY_WIDTH (2 * FALLBACK_DEFAULT_ABBREV + 3)
150+
extern int transport_summary_width(const struct ref *refs);
151151

152152
/* Returns a transport suitable for the url */
153153
struct transport *transport_get(struct remote *, const char *);

0 commit comments

Comments
 (0)