static void fill_tracking_info(struct strbuf *stat, const char *branch_name, int show_upstream_ref) { int ours, theirs; struct branch *branch = branch_get(branch_name); if (!stat_tracking_info(branch, &ours, &theirs)) { if (branch && branch->merge && branch->merge[0]->dst && show_upstream_ref) strbuf_addf(stat, "[%s] ", shorten_unambiguous_ref(branch->merge[0]->dst, 0)); return; } strbuf_addch(stat, '['); if (show_upstream_ref) strbuf_addf(stat, "%s: ", shorten_unambiguous_ref(branch->merge[0]->dst, 0)); if (!ours) strbuf_addf(stat, "behind %d] ", theirs); else if (!theirs) strbuf_addf(stat, "ahead %d] ", ours); else strbuf_addf(stat, "ahead %d, behind %d] ", ours, theirs); }
void get_reflog_selector(struct strbuf *sb, struct reflog_walk_info *reflog_info, const struct date_mode *dmode, int force_date, int shorten) { struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; struct reflog_info *info; const char *printed_ref; if (!commit_reflog) return; if (shorten) { if (!commit_reflog->reflogs->short_ref) commit_reflog->reflogs->short_ref = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0); printed_ref = commit_reflog->reflogs->short_ref; } else { printed_ref = commit_reflog->reflogs->ref; } strbuf_addf(sb, "%s@{", printed_ref); if (commit_reflog->selector == SELECTOR_DATE || (commit_reflog->selector == SELECTOR_NONE && force_date)) { info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode)); } else { strbuf_addf(sb, "%d", commit_reflog->reflogs->nr - 2 - commit_reflog->recno); } strbuf_addch(sb, '}'); }
static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len) { struct strbuf sb = STRBUF_INIT; int cur_path_len = strlen(wt->path); int path_adj = cur_path_len - utf8_strwidth(wt->path); strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path); if (wt->is_bare) strbuf_addstr(&sb, "(bare)"); else { strbuf_addf(&sb, "%-*s ", abbrev_len, find_unique_abbrev(wt->head_oid.hash, DEFAULT_ABBREV)); if (wt->is_detached) strbuf_addstr(&sb, "(detached HEAD)"); else if (wt->head_ref) { char *ref = shorten_unambiguous_ref(wt->head_ref, 0); strbuf_addf(&sb, "[%s]", ref); free(ref); } else strbuf_addstr(&sb, "(error)"); } printf("%s\n", sb.buf); strbuf_release(&sb); }
static void wt_shortstatus_print_tracking(struct wt_status *s) { struct branch *branch; const char *header_color = color(WT_STATUS_HEADER, s); const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s); const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s); const char *base; const char *branch_name; int num_ours, num_theirs; color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## "); if (!s->branch) return; branch_name = s->branch; if (!prefixcmp(branch_name, "refs/heads/")) branch_name += 11; else if (!strcmp(branch_name, "HEAD")) { branch_name = _("HEAD (no branch)"); branch_color_local = color(WT_STATUS_NOBRANCH, s); } branch = branch_get(s->branch + 11); if (s->is_initial) color_fprintf(s->fp, header_color, _("Initial commit on ")); if (!stat_tracking_info(branch, &num_ours, &num_theirs)) { color_fprintf(s->fp, branch_color_local, "%s", branch_name); fputc(s->null_termination ? '\0' : '\n', s->fp); return; } base = branch->merge[0]->dst; base = shorten_unambiguous_ref(base, 0); color_fprintf(s->fp, branch_color_local, "%s", branch_name); color_fprintf(s->fp, header_color, "..."); color_fprintf(s->fp, branch_color_remote, "%s", base); color_fprintf(s->fp, header_color, " ["); if (!num_ours) { color_fprintf(s->fp, header_color, _("behind ")); color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); } else if (!num_theirs) { color_fprintf(s->fp, header_color, _("ahead ")); color_fprintf(s->fp, branch_color_local, "%d", num_ours); } else { color_fprintf(s->fp, header_color, _("ahead ")); color_fprintf(s->fp, branch_color_local, "%d", num_ours); color_fprintf(s->fp, header_color, _(", behind ")); color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); } color_fprintf(s->fp, header_color, "]"); fputc(s->null_termination ? '\0' : '\n', s->fp); }
static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous) { if (shorten_unambiguous) refname = shorten_unambiguous_ref(refname, 0); else if (starts_with(refname, "refs/heads/")) refname = refname + 11; else if (starts_with(refname, "refs/")) refname = refname + 5; return refname; }
static void fill_tracking_info(struct strbuf *stat, const char *branch_name, int show_upstream_ref) { int ours, theirs; char *ref = NULL; struct branch *branch = branch_get(branch_name); if (!stat_tracking_info(branch, &ours, &theirs)) { if (branch && branch->merge && branch->merge[0]->dst && show_upstream_ref) strbuf_addf(stat, "[%s] ", shorten_unambiguous_ref(branch->merge[0]->dst, 0)); return; } if (show_upstream_ref) ref = shorten_unambiguous_ref(branch->merge[0]->dst, 0); if (!ours) { if (ref) strbuf_addf(stat, _("[%s: behind %d]"), ref, theirs); else strbuf_addf(stat, _("[behind %d]"), theirs); } else if (!theirs) { if (ref) strbuf_addf(stat, _("[%s: ahead %d]"), ref, ours); else strbuf_addf(stat, _("[ahead %d]"), ours); } else { if (ref) strbuf_addf(stat, _("[%s: ahead %d, behind %d]"), ref, ours, theirs); else strbuf_addf(stat, _("[ahead %d, behind %d]"), ours, theirs); } strbuf_addch(stat, ' '); free(ref); }
static void check_symref(const char *HEAD, int quiet) { unsigned char sha1[20]; int flag; const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag); if (!refname) die("No such ref: %s", HEAD); else if (!(flag & REF_ISSYMREF)) { if (!quiet) die("ref %s is not a symbolic ref", HEAD); else exit(1); } if (shorten) refname = shorten_unambiguous_ref(refname, 0); puts(refname); }
/* Output a revision, only if filter allows it */ static void show_rev(int type, const unsigned char *sha1, const char *name) { if (!(filter & DO_REVS)) return; def = NULL; if ((symbolic || abbrev_ref) && name) { if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) { unsigned char discard[20]; char *full; switch (dwim_ref(name, strlen(name), discard, &full)) { case 0: /* * Not found -- not a ref. We could * emit "name" here, but symbolic-full * users are interested in finding the * refs spelled in full, and they would * need to filter non-refs if we did so. */ break; case 1: /* happy */ if (abbrev_ref) full = shorten_unambiguous_ref(full, abbrev_ref_strict); show_with_type(type, full); break; default: /* ambiguous */ error("refname '%s' is ambiguous", name); break; } free(full); } else { show_with_type(type, name); } } else if (abbrev) show_with_type(type, find_unique_abbrev(sha1, abbrev)); else show_with_type(type, sha1_to_hex(sha1)); }
static void fill_tracking_info(struct strbuf *stat, const char *branch_name, int show_upstream_ref) { int ours, theirs; char *ref = NULL; struct branch *branch = branch_get(branch_name); struct strbuf fancy = STRBUF_INIT; int upstream_is_gone = 0; int added_decoration = 1; switch (stat_tracking_info(branch, &ours, &theirs)) { case 0: /* no base */ return; case -1: /* with "gone" base */ upstream_is_gone = 1; break; default: /* with base */ break; } if (show_upstream_ref) { ref = shorten_unambiguous_ref(branch->merge[0]->dst, 0); if (want_color(branch_use_color)) strbuf_addf(&fancy, "%s%s%s", branch_get_color(BRANCH_COLOR_UPSTREAM), ref, branch_get_color(BRANCH_COLOR_RESET)); else strbuf_addstr(&fancy, ref); } if (upstream_is_gone) { if (show_upstream_ref) strbuf_addf(stat, _("[%s: gone]"), fancy.buf); else added_decoration = 0; } else if (!ours && !theirs) { if (show_upstream_ref) strbuf_addf(stat, _("[%s]"), fancy.buf); else added_decoration = 0; } else if (!ours) { if (show_upstream_ref) strbuf_addf(stat, _("[%s: behind %d]"), fancy.buf, theirs); else strbuf_addf(stat, _("[behind %d]"), theirs); } else if (!theirs) { if (show_upstream_ref) strbuf_addf(stat, _("[%s: ahead %d]"), fancy.buf, ours); else strbuf_addf(stat, _("[ahead %d]"), ours); } else { if (show_upstream_ref) strbuf_addf(stat, _("[%s: ahead %d, behind %d]"), fancy.buf, ours, theirs); else strbuf_addf(stat, _("[ahead %d, behind %d]"), ours, theirs); } strbuf_release(&fancy); if (added_decoration) strbuf_addch(stat, ' '); free(ref); }
static void wt_shortstatus_print_tracking(struct wt_status *s) { struct branch *branch; const char *header_color = color(WT_STATUS_HEADER, s); const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s); const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s); const char *base; const char *branch_name; int num_ours, num_theirs; int upstream_is_gone = 0; color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## "); if (!s->branch) return; branch_name = s->branch; if (starts_with(branch_name, "refs/heads/")) branch_name += 11; else if (!strcmp(branch_name, "HEAD")) { branch_name = _("HEAD (no branch)"); branch_color_local = color(WT_STATUS_NOBRANCH, s); } branch = branch_get(s->branch + 11); if (s->is_initial) color_fprintf(s->fp, header_color, _("Initial commit on ")); color_fprintf(s->fp, branch_color_local, "%s", branch_name); if (stat_tracking_info(branch, &num_ours, &num_theirs, &base) < 0) { if (!base) { fputc(s->null_termination ? '\0' : '\n', s->fp); return; } upstream_is_gone = 1; } base = shorten_unambiguous_ref(base, 0); color_fprintf(s->fp, header_color, "..."); color_fprintf(s->fp, branch_color_remote, "%s", base); free((char *)base); if (!upstream_is_gone && !num_ours && !num_theirs) { fputc(s->null_termination ? '\0' : '\n', s->fp); return; } #define LABEL(string) (s->no_gettext ? (string) : _(string)) color_fprintf(s->fp, header_color, " ["); if (upstream_is_gone) { color_fprintf(s->fp, header_color, LABEL(N_("gone"))); } else if (!num_ours) { color_fprintf(s->fp, header_color, LABEL(N_("behind "))); color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); } else if (!num_theirs) { color_fprintf(s->fp, header_color, LABEL(N_(("ahead ")))); color_fprintf(s->fp, branch_color_local, "%d", num_ours); } else { color_fprintf(s->fp, header_color, LABEL(N_(("ahead ")))); color_fprintf(s->fp, branch_color_local, "%d", num_ours); color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind "))); color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); } color_fprintf(s->fp, header_color, "]"); fputc(s->null_termination ? '\0' : '\n', s->fp); }