1695
}
1696
1697
/*
1698
- * Return true if there is anything to report, otherwise false.
1698
+ * Compare a branch with its upstream, and save their differences (number
1699
+ * of commits) in *num_ours and *num_theirs.
1700
+ *
1701
+ * Return 0 if branch has no upstream (no base), -1 if upstream is missing
1702
+ * (with "gone" base), otherwise 1 (with base).
1703
*/
1704
int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1705
{
1710
const char *rev_argv[10], *base;
1711
int rev_argc;
1712
1709
- /*
1710
- * Nothing to report unless we are marked to build on top of
1711
- * somebody else.
1712
- */
1713
+ /* Cannot stat unless we are marked to build on top of somebody else. */
1714
if (!branch ||
1715
!branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1716
return 0;
1717
1717
- /*
1718
- * If what we used to build on no longer exists, there is
1719
- * nothing to report.
1720
- */
1718
+ /* Cannot stat if what we used to build on no longer exists */
1719
base = branch->merge[0]->dst;
1720
if (read_ref(base, sha1))
1723
- return 0;
1721
+ return -1;
1722
theirs = lookup_commit_reference(sha1);
1723
if (!theirs)
1726
- return 0;
1724
+ return -1;
1725
1726
if (read_ref(branch->refname, sha1))
1729
- return 0;
1727
+ return -1;
1728
ours = lookup_commit_reference(sha1);
1729
if (!ours)
1732
- return 0;
1730
+ return -1;
1731
1732
/* are we the same? */
1735
- if (theirs == ours)
1736
- return 0;
1733
+ if (theirs == ours) {
1734
+ *num_theirs = *num_ours = 0;
1735
+ return 1;
1736
+ }
1737
1738
/* Run "rev-list --left-right ours...theirs" internally... */
1739
rev_argc = 0;
1775
*/
1776
int format_tracking_info(struct branch *branch, struct strbuf *sb)
1777
{
1778
- int num_ours, num_theirs;
1778
+ int ours, theirs;
1779
const char *base;
1780
+ int upstream_is_gone = 0;
1781
1781
- if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1782
+ switch (stat_tracking_info(branch, &ours, &theirs)) {
1783
+ case 0:
1784
+ /* no base */
1785
return 0;
1786
+ case -1:
1787
+ /* with "gone" base */
1788
+ upstream_is_gone = 1;
1789
+ break;
1790
+ default:
1791
+ /* Nothing to report if neither side has changes. */
1792
+ if (!ours && !theirs)
1793
+ return 0;
1794
+ /* with base */
1795
+ break;
1796
+ }
1797
1798
base = branch->merge[0]->dst;
1799
base = shorten_unambiguous_ref(base, 0);
1786
- if (!num_theirs) {
1800
+ if (upstream_is_gone) {
1801
+ strbuf_addf(sb,
1802
+ _("Your branch is based on '%s', but the upstream is gone.\n"),
1803
+ base);
1804
+ if (advice_status_hints)
1805
+ strbuf_addf(sb,
1806
+ _(" (use \"git branch --unset-upstream\" to fixup)\n"));
1807
+ } else if (!theirs) {
1808
strbuf_addf(sb,
1809
Q_("Your branch is ahead of '%s' by %d commit.\n",
1810
"Your branch is ahead of '%s' by %d commits.\n",
1790
- num_ours),
1791
- base, num_ours);
1811
+ ours),
1812
+ base, ours);
1813
if (advice_status_hints)
1814
strbuf_addf(sb,
1815
_(" (use \"git push\" to publish your local commits)\n"));
1795
- } else if (!num_ours) {
1816
+ } else if (!ours) {
1817
strbuf_addf(sb,
1818
Q_("Your branch is behind '%s' by %d commit, "
1819
"and can be fast-forwarded.\n",
1820
"Your branch is behind '%s' by %d commits, "
1821
"and can be fast-forwarded.\n",
1801
- num_theirs),
1802
- base, num_theirs);
1822
+ theirs),
1823
+ base, theirs);
1824
if (advice_status_hints)
1825
strbuf_addf(sb,
1826
_(" (use \"git pull\" to update your local branch)\n"));
1832
"Your branch and '%s' have diverged,\n"
1833
"and have %d and %d different commits each, "
1834
"respectively.\n",
1814
- num_theirs),
1815
- base, num_ours, num_theirs);
1835
+ theirs),
1836
+ base, ours, theirs);
1837
if (advice_status_hints)
1838
strbuf_addf(sb,
1839
_(" (use \"git pull\" to merge the remote branch into yours)\n"));