305
return 0;
306
}
307
308
-static void report_tracking(struct branch_info *new, struct checkout_opts *opts)
308
+static void report_tracking(struct branch_info *new)
309
{
310
- /*
311
- * We have switched to a new branch; is it building on
312
- * top of another branch, and if so does that other branch
313
- * have changes we do not have yet?
314
- */
315
- char *base;
316
- unsigned char sha1[20];
317
- struct commit *ours, *theirs;
318
- char symmetric[84];
319
- struct rev_info revs;
320
- const char *rev_argv[10];
321
- int rev_argc;
322
- int num_ours, num_theirs;
323
- const char *remote_msg;
310
+ struct strbuf sb = STRBUF_INIT;
311
struct branch *branch = branch_get(new->name);
312
326
- /*
327
- * Nothing to report unless we are marked to build on top of
328
- * somebody else.
329
- */
330
- if (!branch || !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
331
- return;
332
-
333
- /*
334
- * If what we used to build on no longer exists, there is
335
- * nothing to report.
336
- */
337
- base = branch->merge[0]->dst;
338
- if (!resolve_ref(base, sha1, 1, NULL))
313
+ if (!format_tracking_info(branch, &sb))
314
return;
340
-
341
- theirs = lookup_commit(sha1);
342
- ours = new->commit;
343
- if (!hashcmp(sha1, ours->object.sha1))
344
- return; /* we are the same */
345
-
346
- /* Run "rev-list --left-right ours...theirs" internally... */
347
- rev_argc = 0;
348
- rev_argv[rev_argc++] = NULL;
349
- rev_argv[rev_argc++] = "--left-right";
350
- rev_argv[rev_argc++] = symmetric;
351
- rev_argv[rev_argc++] = "--";
352
- rev_argv[rev_argc] = NULL;
353
-
354
- strcpy(symmetric, sha1_to_hex(ours->object.sha1));
355
- strcpy(symmetric + 40, "...");
356
- strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
357
-
358
- init_revisions(&revs, NULL);
359
- setup_revisions(rev_argc, rev_argv, &revs, NULL);
360
- prepare_revision_walk(&revs);
361
-
362
- /* ... and count the commits on each side. */
363
- num_ours = 0;
364
- num_theirs = 0;
365
- while (1) {
366
- struct commit *c = get_revision(&revs);
367
- if (!c)
368
- break;
369
- if (c->object.flags & SYMMETRIC_LEFT)
370
- num_ours++;
371
- else
372
- num_theirs++;
373
- }
374
-
375
- if (!prefixcmp(base, "refs/remotes/")) {
376
- remote_msg = " remote";
377
- base += strlen("refs/remotes/");
378
- } else {
379
- remote_msg = "";
380
- }
381
-
382
- if (!num_theirs)
383
- printf("Your branch is ahead of the tracked%s branch '%s' "
384
- "by %d commit%s.\n",
385
- remote_msg, base,
386
- num_ours, (num_ours == 1) ? "" : "s");
387
- else if (!num_ours)
388
- printf("Your branch is behind the tracked%s branch '%s' "
389
- "by %d commit%s,\n"
390
- "and can be fast-forwarded.\n",
391
- remote_msg, base,
392
- num_theirs, (num_theirs == 1) ? "" : "s");
393
- else
394
- printf("Your branch and the tracked%s branch '%s' "
395
- "have diverged,\nand respectively "
396
- "have %d and %d different commit(s) each.\n",
397
- remote_msg, base,
398
- num_ours, num_theirs);
315
+ fputs(sb.buf, stdout);
316
+ strbuf_release(&sb);
317
}
318
319
static void update_refs_for_switch(struct checkout_opts *opts,
359
remove_branch_state();
360
strbuf_release(&msg);
361
if (!opts->quiet && (new->path || !strcmp(new->name, "HEAD")))
444
- report_tracking(new, opts);
362
+ report_tracking(new);
363
}
364
365
static int switch_branches(struct checkout_opts *opts, struct branch_info *new)