checkout: tone down the "forked status" diagnostic messages
When checking out a branch that is behind or forked from a branch you are building on top of, we used to show full left-right log but if you already _know_ you have long history since you forked, it is a bit too much. This tones down the message quite a bit, by only showing the number of commits each side has since they diverged. Also the message is not shown at all under --quiet. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Feb 20, 2008 at 15:05 UTC
b0030db331141bedfaf02f34a83f18712c0ae011
1 file changed
+64
-40
builtin-checkout.c
+64
-40
@@ -301,64 +301,88 @@ static void adjust_to_tracking(struct branch_info *new, struct checkout_opts *op
301
char *base;
302
unsigned char sha1[20];
303
struct commit *ours, *theirs;
304
- const char *msgfmt;
304
char symmetric[84];
306
- int show_log;
305
+ struct rev_info revs;
306
+ const char *rev_argv[10];
307
+ int rev_argc;
308
+ int num_ours, num_theirs;
309
+ const char *remote_msg;
310
struct branch *branch = branch_get(NULL);
311
312
+ /*
313
+ * Nothing to report unless we are marked to build on top of
314
+ * somebody else.
315
+ */
316
if (!branch || !branch->merge)
317
return;
318
312
- base = branch->merge[0]->dst;
313
-
314
- ours = new->commit;
315
-
316
- sprintf(symmetric, "%s", sha1_to_hex(ours->object.sha1));
317
-
319
/*
319
- * Ok, it is tracking base; is it ahead of us?
320
+ * If what we used to build on no longer exists, there is
321
+ * nothing to report.
322
*/
323
+ base = branch->merge[0]->dst;
324
if (!resolve_ref(base, sha1, 1, NULL))
325
return;
323
- theirs = lookup_commit(sha1);
324
-
325
- sprintf(symmetric + 40, "...%s", sha1_to_hex(sha1));
326
327
+ theirs = lookup_commit(sha1);
328
+ ours = new->commit;
329
if (!hashcmp(sha1, ours->object.sha1))
330
return; /* we are the same */
331
330
- show_log = 1;
331
- if (in_merge_bases(theirs, &ours, 1)) {
332
- msgfmt = "You are ahead of the tracked branch '%s'\n";
333
- show_log = 0;
332
+ /* Run "rev-list --left-right ours...theirs" internally... */
333
+ rev_argc = 0;
334
+ rev_argv[rev_argc++] = NULL;
335
+ rev_argv[rev_argc++] = "--left-right";
336
+ rev_argv[rev_argc++] = symmetric;
337
+ rev_argv[rev_argc++] = "--";
338
+ rev_argv[rev_argc] = NULL;
339
+
340
+ strcpy(symmetric, sha1_to_hex(ours->object.sha1));
341
+ strcpy(symmetric + 40, "...");
342
+ strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
343
+
344
+ init_revisions(&revs, NULL);
345
+ setup_revisions(rev_argc, rev_argv, &revs, NULL);
346
+ prepare_revision_walk(&revs);
347
+
348
+ /* ... and count the commits on each side. */
349
+ num_ours = 0;
350
+ num_theirs = 0;
351
+ while (1) {
352
+ struct commit *c = get_revision(&revs);
353
+ if (!c)
354
+ break;
355
+ if (c->object.flags & SYMMETRIC_LEFT)
356
+ num_ours++;
357
+ else
358
+ num_theirs++;
359
}
335
- else if (in_merge_bases(ours, &theirs, 1))
336
- msgfmt = "Your branch can be fast-forwarded to the tracked branch '%s'\n";
337
- else
338
- msgfmt = "Both your branch and the tracked branch '%s' have own changes, you would eventually need to merge\n";
360
340
- if (!prefixcmp(base, "refs/remotes/"))
361
+ if (!prefixcmp(base, "refs/remotes/")) {
362
+ remote_msg = " remote";
363
base += strlen("refs/remotes/");
342
- fprintf(stderr, msgfmt, base);
343
-
344
- if (show_log) {
345
- const char *args[32];
346
- int ac;
347
-
348
- ac = 0;
349
- args[ac++] = "log";
350
- args[ac++] = "--pretty=oneline";
351
- args[ac++] = "--abbrev-commit";
352
- args[ac++] = "--left-right";
353
- args[ac++] = "--boundary";
354
- args[ac++] = symmetric;
355
- args[ac++] = "--";
356
- args[ac] = NULL;
357
-
358
- run_command_v_opt(args, RUN_GIT_CMD);
364
+ } else {
365
+ remote_msg = "";
366
}
360
-}
367
368
+ if (!num_theirs)
369
+ printf("Your branch is ahead of the tracked%s branch '%s' "
370
+ "by %d commit%s.\n",
371
+ remote_msg, base,
372
+ num_ours, (num_ours == 1) ? "" : "s");
373
+ else if (!num_ours)
374
+ printf("Your branch is behind of the tracked%s branch '%s' "
375
+ "by %d commit%s,\n"
376
+ "and can be fast-forwarded.\n",
377
+ remote_msg, base,
378
+ num_theirs, (num_theirs == 1) ? "" : "s");
379
+ else
380
+ printf("Your branch and the tracked%s branch '%s' "
381
+ "have diverged,\nand respectively "
382
+ "have %d and %d different commit(s) each.\n",
383
+ remote_msg, base,
384
+ num_ours, num_theirs);
385
+}
386
387
static void update_refs_for_switch(struct checkout_opts *opts,
388
struct branch_info *old,
@@ -402,7 +426,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
426
}
427
remove_branch_state();
428
strbuf_release(&msg);
405
- if (new->path || !strcmp(new->name, "HEAD"))
429
+ if (!opts->quiet && (new->path || !strcmp(new->name, "HEAD")))
430
adjust_to_tracking(new, opts);
431
}
432