Skip to content

Commit 52b195f

Browse files
committed
Merge branch 'jk/maint-fetch-status-table' into maint-1.7.6
* jk/maint-fetch-status-table: fetch: create status table using strbuf
2 parents 43176d1 + 5914f2d commit 52b195f

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

builtin/fetch.c

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -239,23 +239,23 @@ static int s_update_ref(const char *action,
239239

240240
static int update_local_ref(struct ref *ref,
241241
const char *remote,
242-
char *display)
242+
struct strbuf *display)
243243
{
244244
struct commit *current = NULL, *updated;
245245
enum object_type type;
246246
struct branch *current_branch = branch_get(NULL);
247247
const char *pretty_ref = prettify_refname(ref->name);
248248

249-
*display = 0;
250249
type = sha1_object_info(ref->new_sha1, NULL);
251250
if (type < 0)
252251
die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
253252

254253
if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
255254
if (verbosity > 0)
256-
sprintf(display, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH,
257-
_("[up to date]"), REFCOL_WIDTH, remote,
258-
pretty_ref);
255+
strbuf_addf(display, "= %-*s %-*s -> %s",
256+
TRANSPORT_SUMMARY_WIDTH,
257+
_("[up to date]"), REFCOL_WIDTH,
258+
remote, pretty_ref);
259259
return 0;
260260
}
261261

@@ -267,19 +267,22 @@ static int update_local_ref(struct ref *ref,
267267
* If this is the head, and it's not okay to update
268268
* the head, and the old value of the head isn't empty...
269269
*/
270-
sprintf(display, _("! %-*s %-*s -> %s (can't fetch in current branch)"),
271-
TRANSPORT_SUMMARY_WIDTH, _("[rejected]"), REFCOL_WIDTH, remote,
272-
pretty_ref);
270+
strbuf_addf(display,
271+
_("! %-*s %-*s -> %s (can't fetch in current branch)"),
272+
TRANSPORT_SUMMARY_WIDTH, _("[rejected]"),
273+
REFCOL_WIDTH, remote, pretty_ref);
273274
return 1;
274275
}
275276

276277
if (!is_null_sha1(ref->old_sha1) &&
277278
!prefixcmp(ref->name, "refs/tags/")) {
278279
int r;
279280
r = s_update_ref("updating tag", ref, 0);
280-
sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
281-
TRANSPORT_SUMMARY_WIDTH, _("[tag update]"), REFCOL_WIDTH, remote,
282-
pretty_ref, r ? _(" (unable to update local ref)") : "");
281+
strbuf_addf(display, "%c %-*s %-*s -> %s%s",
282+
r ? '!' : '-',
283+
TRANSPORT_SUMMARY_WIDTH, _("[tag update]"),
284+
REFCOL_WIDTH, remote, pretty_ref,
285+
r ? _(" (unable to update local ref)") : "");
283286
return r;
284287
}
285288

@@ -302,9 +305,11 @@ static int update_local_ref(struct ref *ref,
302305
}
303306

304307
r = s_update_ref(msg, ref, 0);
305-
sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
306-
TRANSPORT_SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
307-
r ? _(" (unable to update local ref)") : "");
308+
strbuf_addf(display, "%c %-*s %-*s -> %s%s",
309+
r ? '!' : '*',
310+
TRANSPORT_SUMMARY_WIDTH, what,
311+
REFCOL_WIDTH, remote, pretty_ref,
312+
r ? _(" (unable to update local ref)") : "");
308313
return r;
309314
}
310315

@@ -318,9 +323,11 @@ static int update_local_ref(struct ref *ref,
318323
(recurse_submodules != RECURSE_SUBMODULES_ON))
319324
check_for_new_submodule_commits(ref->new_sha1);
320325
r = s_update_ref("fast-forward", ref, 1);
321-
sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
322-
TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
323-
pretty_ref, r ? _(" (unable to update local ref)") : "");
326+
strbuf_addf(display, "%c %-*s %-*s -> %s%s",
327+
r ? '!' : ' ',
328+
TRANSPORT_SUMMARY_WIDTH, quickref,
329+
REFCOL_WIDTH, remote, pretty_ref,
330+
r ? _(" (unable to update local ref)") : "");
324331
return r;
325332
} else if (force || ref->force) {
326333
char quickref[84];
@@ -332,15 +339,17 @@ static int update_local_ref(struct ref *ref,
332339
(recurse_submodules != RECURSE_SUBMODULES_ON))
333340
check_for_new_submodule_commits(ref->new_sha1);
334341
r = s_update_ref("forced-update", ref, 1);
335-
sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+',
336-
TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
337-
pretty_ref,
338-
r ? _("unable to update local ref") : _("forced update"));
342+
strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
343+
r ? '!' : '+',
344+
TRANSPORT_SUMMARY_WIDTH, quickref,
345+
REFCOL_WIDTH, remote, pretty_ref,
346+
r ? _("unable to update local ref") : _("forced update"));
339347
return r;
340348
} else {
341-
sprintf(display, "! %-*s %-*s -> %s %s",
342-
TRANSPORT_SUMMARY_WIDTH, _("[rejected]"), REFCOL_WIDTH, remote,
343-
pretty_ref, _("(non-fast-forward)"));
349+
strbuf_addf(display, "! %-*s %-*s -> %s %s",
350+
TRANSPORT_SUMMARY_WIDTH, _("[rejected]"),
351+
REFCOL_WIDTH, remote, pretty_ref,
352+
_("(non-fast-forward)"));
344353
return 1;
345354
}
346355
}
@@ -350,8 +359,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
350359
{
351360
FILE *fp;
352361
struct commit *commit;
353-
int url_len, i, note_len, shown_url = 0, rc = 0;
354-
char note[1024];
362+
int url_len, i, shown_url = 0, rc = 0;
363+
struct strbuf note = STRBUF_INIT;
355364
const char *what, *kind;
356365
struct ref *rm;
357366
char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
@@ -407,41 +416,42 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
407416
if (4 < i && !strncmp(".git", url + i - 3, 4))
408417
url_len = i - 3;
409418

410-
note_len = 0;
419+
strbuf_reset(&note);
411420
if (*what) {
412421
if (*kind)
413-
note_len += sprintf(note + note_len, "%s ",
414-
kind);
415-
note_len += sprintf(note + note_len, "'%s' of ", what);
422+
strbuf_addf(&note, "%s ", kind);
423+
strbuf_addf(&note, "'%s' of ", what);
416424
}
417-
note[note_len] = '\0';
418425
fprintf(fp, "%s\t%s\t%s",
419426
sha1_to_hex(commit ? commit->object.sha1 :
420427
rm->old_sha1),
421428
rm->merge ? "" : "not-for-merge",
422-
note);
429+
note.buf);
423430
for (i = 0; i < url_len; ++i)
424431
if ('\n' == url[i])
425432
fputs("\\n", fp);
426433
else
427434
fputc(url[i], fp);
428435
fputc('\n', fp);
429436

437+
strbuf_reset(&note);
430438
if (ref) {
431-
rc |= update_local_ref(ref, what, note);
439+
rc |= update_local_ref(ref, what, &note);
432440
free(ref);
433441
} else
434-
sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
435-
TRANSPORT_SUMMARY_WIDTH, *kind ? kind : "branch",
436-
REFCOL_WIDTH, *what ? what : "HEAD");
437-
if (*note) {
442+
strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
443+
TRANSPORT_SUMMARY_WIDTH,
444+
*kind ? kind : "branch",
445+
REFCOL_WIDTH,
446+
*what ? what : "HEAD");
447+
if (note.len) {
438448
if (verbosity >= 0 && !shown_url) {
439449
fprintf(stderr, _("From %.*s\n"),
440450
url_len, url);
441451
shown_url = 1;
442452
}
443453
if (verbosity >= 0)
444-
fprintf(stderr, " %s\n", note);
454+
fprintf(stderr, " %s\n", note.buf);
445455
}
446456
}
447457
free(url);
@@ -450,6 +460,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
450460
error(_("some local refs could not be updated; try running\n"
451461
" 'git remote prune %s' to remove any old, conflicting "
452462
"branches"), remote_name);
463+
strbuf_release(&note);
453464
return rc;
454465
}
455466

0 commit comments

Comments
 (0)