/* * Show dialog with two buttons; returns the botton that was selected. */ int dia_binary(char *txt, char *button0_txt, char *button1_txt, int def) { window_t win; button_t button0, button1; button_t *buttons[2] = { &button0, &button1 }; int width, answer; int len0, len1, len_max; if (config.linemode) { int current_ii; for (;;) { putchar('\n'); dia_printformatted(txt, 0, max_x_ig - 1, 1); putchar('\n'); printf("0) <-- Back <--\n"); printf("1) %s\n", button0_txt); printf("2) %s\n", button1_txt); printf("\n> ");fflush(stdout); current_ii = dia_readnum(); if(current_ii == -'x') dia_handle_ctrlc(); if (current_ii < 0 || current_ii > 2) continue; return current_ii - 1; } } disp_toggle_output(DISP_OFF); memset(&win, 0, sizeof win); win.bg_color = colors_prg->choice_win; win.fg_color = colors_prg->choice_fg; width = dia_win_open(&win, txt, 0); len0 = utf8_strwidth(button0_txt); len1 = utf8_strwidth(button1_txt); len_max = len0 > len1 ? len0 : len1; util_generate_button(buttons[0], button0_txt, len_max); util_generate_button(buttons[1], button1_txt, len_max); len0 = utf8_strwidth(button0.text); len1 = utf8_strwidth(button1.text); win_add_button(&win, buttons[0], width / 3 - len0 / 2 - 3, len0); win_add_button(&win, buttons[1], width - width / 3 - len1 / 2 + 2, len1); disp_flush_area(&win); answer = win_choose_button(buttons, 2, def + 1) - 1; win_close(&win); return answer; }
/* * How many columns do we need to show line numbers, authors, * and filenames? */ static void find_alignment(struct blame_scoreboard *sb, int *option) { int longest_src_lines = 0; int longest_dst_lines = 0; unsigned largest_score = 0; struct blame_entry *e; int compute_auto_abbrev = (abbrev < 0); int auto_abbrev = DEFAULT_ABBREV; for (e = sb->ent; e; e = e->next) { struct blame_origin *suspect = e->suspect; int num; if (compute_auto_abbrev) auto_abbrev = update_auto_abbrev(auto_abbrev, suspect); if (strcmp(suspect->path, sb->path)) *option |= OUTPUT_SHOW_NAME; num = strlen(suspect->path); if (longest_file < num) longest_file = num; if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { struct commit_info ci; suspect->commit->object.flags |= METAINFO_SHOWN; get_commit_info(suspect->commit, &ci, 1); if (*option & OUTPUT_SHOW_EMAIL) num = utf8_strwidth(ci.author_mail.buf); else num = utf8_strwidth(ci.author.buf); if (longest_author < num) longest_author = num; commit_info_destroy(&ci); } num = e->s_lno + e->num_lines; if (longest_src_lines < num) longest_src_lines = num; num = e->lno + e->num_lines; if (longest_dst_lines < num) longest_dst_lines = num; if (largest_score < blame_entry_score(sb, e)) largest_score = blame_entry_score(sb, e); } max_orig_digits = decimal_width(longest_src_lines); max_digits = decimal_width(longest_dst_lines); max_score_digits = decimal_width(largest_score); if (compute_auto_abbrev) /* one more abbrev length is needed for the boundary commit */ abbrev = auto_abbrev + 1; }
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_status_print_unmerged_data(struct wt_status *s, struct string_list_item *it) { const char *c = color(WT_STATUS_UNMERGED, s); struct wt_status_change_data *d = it->util; struct strbuf onebuf = STRBUF_INIT; static char *padding; static int label_width; const char *one, *how; int len; if (!padding) { label_width = maxwidth(wt_status_unmerged_status_string, 1, 7); label_width += strlen(" "); padding = xmallocz(label_width); memset(padding, ' ', label_width); } one = quote_path(it->string, s->prefix, &onebuf); status_printf(s, color(WT_STATUS_HEADER, s), "\t"); how = wt_status_unmerged_status_string(d->stagemask); len = label_width - utf8_strwidth(how); status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one); strbuf_release(&onebuf); }
static const char *format_time(timestamp_t time, const char *tz_str, int show_raw_time) { static struct strbuf time_buf = STRBUF_INIT; strbuf_reset(&time_buf); if (show_raw_time) { strbuf_addf(&time_buf, "%"PRItime" %s", time, tz_str); } else { const char *time_str; size_t time_width; int tz; tz = atoi(tz_str); time_str = show_date(time, tz, &blame_date_mode); strbuf_addstr(&time_buf, time_str); /* * Add space paddings to time_buf to display a fixed width * string, and use time_width for display width calibration. */ for (time_width = utf8_strwidth(time_str); time_width < blame_date_width; time_width++) strbuf_addch(&time_buf, ' '); } return time_buf.buf; }
/* return the number of columns of string 's' in current locale */ int gettext_width(const char *s) { static int is_utf8 = -1; if (is_utf8 == -1) is_utf8 = !strcmp(charset, "UTF-8"); return is_utf8 ? utf8_strwidth(s) : strlen(s); }
/* * lComputeStringWidth - compute the string width * * Note: the fontsize is specified in half-points! * the stringlength is specified in bytes, not characters! * * Returns the string width in millipoints */ long lComputeStringWidth(const char *szString, size_t tStringLength, drawfile_fontref tFontRef, USHORT usFontSize) { USHORT *ausCharWidths; UCHAR *pucChar; long lRelWidth; size_t tIndex; int iFontRef; fail(szString == NULL); fail(usFontSize < MIN_FONT_SIZE || usFontSize > MAX_FONT_SIZE); if (szString[0] == '\0' || tStringLength == 0) { /* Empty string */ return 0; } if (eEncoding == encoding_utf_8) { fail(!bUsePlainText); return lChar2MilliPoints( utf8_strwidth(szString, tStringLength)); } if (bUsePlainText) { /* No current font, use "systemfont" */ return lChar2MilliPoints(tStringLength); } if (eEncoding == encoding_cyrillic) { /* FIXME: until the character tables are available */ return (long)((tStringLength * 600L * (long)usFontSize + 1) / 2); } DBG_DEC_C(eEncoding != encoding_latin_1 && eEncoding != encoding_latin_2, eEncoding); fail(eEncoding != encoding_latin_1 && eEncoding != encoding_latin_2); /* Compute the relative string width */ iFontRef = (int)(UCHAR)tFontRef; if (eEncoding == encoding_latin_2) { ausCharWidths = ausCharacterWidths2[iFontRef]; } else { ausCharWidths = ausCharacterWidths1[iFontRef]; } lRelWidth = 0; for (tIndex = 0, pucChar = (UCHAR *)szString; tIndex < tStringLength; tIndex++, pucChar++) { lRelWidth += (long)ausCharWidths[(int)*pucChar]; } /* Compute the absolute string width */ return (lRelWidth * (long)usFontSize + 1) / 2; } /* end of lComputeStringWidth */
/* * tCountColumns - count the number of columns in a string * * Note: the length is specified in bytes! * A UTF-8 a character can be 0, 1 or 2 columns wide. * * Returns the number of columns */ size_t tCountColumns(const char *szString, size_t tLength) { fail(szString == NULL); if (eEncoding != encoding_utf_8) { /* One byte, one character, one column */ return tLength; } return (size_t)utf8_strwidth(szString, tLength); } /* end of tCountColumns */
static void print_ref_item(struct ref_item *item, int maxwidth, int verbose, int abbrev, int current, char *prefix) { char c; int color; struct commit *commit = item->commit; struct strbuf out = STRBUF_INIT, name = STRBUF_INIT; if (!matches_merge_filter(commit)) return; switch (item->kind) { case REF_LOCAL_BRANCH: color = BRANCH_COLOR_LOCAL; break; case REF_REMOTE_BRANCH: color = BRANCH_COLOR_REMOTE; break; default: color = BRANCH_COLOR_PLAIN; break; } c = ' '; if (current) { c = '*'; color = BRANCH_COLOR_CURRENT; } strbuf_addf(&name, "%s%s", prefix, item->name); if (verbose) { int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf); strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color), maxwidth + utf8_compensation, name.buf, branch_get_color(BRANCH_COLOR_RESET)); } else strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color), name.buf, branch_get_color(BRANCH_COLOR_RESET)); if (item->dest) strbuf_addf(&out, " -> %s", item->dest); else if (verbose) /* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */ add_verbose_info(&out, item, verbose, abbrev); if (column_active(colopts)) { assert(!verbose && "--column and --verbose are incompatible"); string_list_append(&output, out.buf); } else { printf("%s\n", out.buf); } strbuf_release(&name); strbuf_release(&out); }
static int maxwidth(const char *(*label)(int), int minval, int maxval) { int result = 0, i; for (i = minval; i <= maxval; i++) { const char *s = label(i); int len = s ? utf8_strwidth(s) : 0; if (len > result) result = len; } return result; }
/* return length of 's' in letters, ANSI escapes stripped */ static int item_length(unsigned int colopts, const char *s) { int len, i = 0; struct strbuf str = STRBUF_INIT; strbuf_addstr(&str, s); while ((s = strstr(str.buf + i, "\033[")) != NULL) { int len = strspn(s + 2, "0123456789;"); i = s - str.buf; strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */ } len = utf8_strwidth(str.buf); strbuf_release(&str); return len; }
static int calc_maxwidth(struct ref_array *refs, int remote_bonus) { int i, max = 0; for (i = 0; i < refs->nr; i++) { struct ref_array_item *it = refs->items[i]; const char *desc = it->refname; int w; skip_prefix(it->refname, "refs/heads/", &desc); skip_prefix(it->refname, "refs/remotes/", &desc); if (it->kind == FILTER_REFS_DETACHED_HEAD) { char *head_desc = get_head_description(); w = utf8_strwidth(head_desc); free(head_desc); } else w = utf8_strwidth(desc); if (it->kind == FILTER_REFS_REMOTES) w += remote_bonus; if (w > max) max = w; } return max; }
/* * Wrapper for fprintf and returns the total number of columns required * for the printed string, assuming that the string is utf8. */ int utf8_fprintf(FILE *stream, const char *format, ...) { struct strbuf buf = STRBUF_INIT; va_list arg; int columns; va_start(arg, format); strbuf_vaddf(&buf, format, arg); va_end(arg); columns = fputs(buf.buf, stream); if (0 <= columns) /* keep the error from the I/O */ columns = utf8_strwidth(buf.buf); strbuf_release(&buf); return columns; }
static void show_detached(struct ref_list *ref_list) { struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1); if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) { struct ref_item item; item.name = get_head_description(); item.width = utf8_strwidth(item.name); item.kind = REF_LOCAL_BRANCH; item.dest = NULL; item.commit = head_commit; if (item.width > ref_list->maxwidth) ref_list->maxwidth = item.width; print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, ""); free(item.name); } }
static void refreshLine(const char *prompt, struct current *current) { int plen; int pchars; int backup = 0; int i; const char *buf = current->buf; int chars = current->chars; int pos = current->pos; int b; int ch; int n; int width; int bufwidth; /* Should intercept SIGWINCH. For now, just get the size every time */ getWindowSize(current); plen = strlen(prompt); pchars = utf8_strwidth(prompt, utf8_strlen(prompt, plen)); /* Scan the prompt for embedded ansi color control sequences and * discount them as characters/columns. */ pchars -= countColorControlChars(prompt); /* Account for a line which is too long to fit in the window. * Note that control chars require an extra column */ /* How many cols are required to the left of 'pos'? * The prompt, plus one extra for each control char */ n = pchars + utf8_strwidth(buf, utf8_strlen(buf, current->len)); b = 0; for (i = 0; i < pos; i++) { b += utf8_tounicode(buf + b, &ch); if (ch < ' ') { n++; } } /* Pluse one if the current char is a control character */ if (current->pos < current->chars && get_char(current, current->pos) < ' ') { n++; } /* If too many are needed, strip chars off the front of 'buf' * until it fits. Note that if the current char is a control character, * we need one extra col. */ while (n >= current->cols && pos > 0) { b = utf8_tounicode(buf, &ch); if (ch < ' ') { n--; } n -= utf8_width(ch); buf += b; pos--; chars--; } /* Cursor to left edge, then the prompt */ cursorToLeft(current); outputChars(current, prompt, plen); /* Now the current buffer content */ /* Need special handling for control characters. * If we hit 'cols', stop. */ b = 0; /* unwritted bytes */ n = 0; /* How many control chars were written */ width = 0; /* current display width */ bufwidth = utf8_strwidth(buf, pos); for (i = 0; i < chars; i++) { int ch; int w = utf8_tounicode(buf + b, &ch); if (ch < ' ') { n++; } width += utf8_width(ch); if (pchars + width + n >= current->cols) { break; } if (ch < ' ') { /* A control character, so write the buffer so far */ outputChars(current, buf, b); buf += b + w; b = 0; outputControlChar(current, ch + '@'); if (i < pos) { backup++; } } else { b += w; } } outputChars(current, buf, b); /* Erase to right, move cursor to original position */ eraseEol(current); setCursorPos(current, bufwidth + pchars + backup); }
static void wt_status_print_change_data(struct wt_status *s, int change_type, struct string_list_item *it) { struct wt_status_change_data *d = it->util; const char *c = color(change_type, s); int status; char *one_name; char *two_name; const char *one, *two; struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT; struct strbuf extra = STRBUF_INIT; static char *padding; static int label_width; const char *what; int len; if (!padding) { /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */ label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z'); label_width += strlen(" "); padding = xmallocz(label_width); memset(padding, ' ', label_width); } one_name = two_name = it->string; switch (change_type) { case WT_STATUS_UPDATED: status = d->index_status; if (d->head_path) one_name = d->head_path; break; case WT_STATUS_CHANGED: if (d->new_submodule_commits || d->dirty_submodule) { strbuf_addstr(&extra, " ("); if (d->new_submodule_commits) strbuf_addf(&extra, _("new commits, ")); if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) strbuf_addf(&extra, _("modified content, ")); if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) strbuf_addf(&extra, _("untracked content, ")); strbuf_setlen(&extra, extra.len - 2); strbuf_addch(&extra, ')'); } status = d->worktree_status; break; default: die("BUG: unhandled change_type %d in wt_status_print_change_data", change_type); } one = quote_path(one_name, s->prefix, &onebuf); two = quote_path(two_name, s->prefix, &twobuf); status_printf(s, color(WT_STATUS_HEADER, s), "\t"); what = wt_status_diff_status_string(status); if (!what) die(_("bug: unhandled diff status %c"), status); len = label_width - utf8_strwidth(what); assert(len >= 0); if (status == DIFF_STATUS_COPIED || status == DIFF_STATUS_RENAMED) status_printf_more(s, c, "%s%.*s%s -> %s", what, len, padding, one, two); else status_printf_more(s, c, "%s%.*s%s", what, len, padding, one); if (extra.len) { status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf); strbuf_release(&extra); } status_printf_more(s, GIT_COLOR_NORMAL, "\n"); strbuf_release(&onebuf); strbuf_release(&twobuf); }
static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data) { struct append_ref_cb *cb = (struct append_ref_cb *)(cb_data); struct ref_list *ref_list = cb->ref_list; struct ref_item *newitem; struct commit *commit; int kind, i; const char *prefix, *orig_refname = refname; static struct { int kind; const char *prefix; int pfxlen; } ref_kind[] = { { REF_LOCAL_BRANCH, "refs/heads/", 11 }, { REF_REMOTE_BRANCH, "refs/remotes/", 13 }, }; /* Detect kind */ for (i = 0; i < ARRAY_SIZE(ref_kind); i++) { prefix = ref_kind[i].prefix; if (strncmp(refname, prefix, ref_kind[i].pfxlen)) continue; kind = ref_kind[i].kind; refname += ref_kind[i].pfxlen; break; } if (ARRAY_SIZE(ref_kind) <= i) return 0; /* Don't add types the caller doesn't want */ if ((kind & ref_list->kinds) == 0) return 0; if (!match_patterns(cb->pattern, refname)) return 0; commit = NULL; if (ref_list->verbose || ref_list->with_commit || merge_filter != NO_FILTER) { commit = lookup_commit_reference_gently(sha1, 1); if (!commit) { cb->ret = error(_("branch '%s' does not point at a commit"), refname); return 0; } /* Filter with with_commit if specified */ if (!is_descendant_of(commit, ref_list->with_commit)) return 0; if (merge_filter != NO_FILTER) add_pending_object(&ref_list->revs, (struct object *)commit, refname); } ALLOC_GROW(ref_list->list, ref_list->index + 1, ref_list->alloc); /* Record the new item */ newitem = &(ref_list->list[ref_list->index++]); newitem->name = xstrdup(refname); newitem->kind = kind; newitem->commit = commit; newitem->width = utf8_strwidth(refname); newitem->dest = resolve_symref(orig_refname, prefix); /* adjust for "remotes/" */ if (newitem->kind == REF_REMOTE_BRANCH && ref_list->kinds != REF_REMOTE_BRANCH) newitem->width += 8; if (newitem->width > ref_list->maxwidth) ref_list->maxwidth = newitem->width; return 0; }
static void format_and_print_ref_item(struct ref_array_item *item, int maxwidth, struct ref_filter *filter, const char *remote_prefix) { char c; int current = 0; int color; struct strbuf out = STRBUF_INIT, name = STRBUF_INIT; const char *prefix_to_show = ""; const char *prefix_to_skip = NULL; const char *desc = item->refname; char *to_free = NULL; switch (item->kind) { case FILTER_REFS_BRANCHES: prefix_to_skip = "refs/heads/"; skip_prefix(desc, prefix_to_skip, &desc); if (!filter->detached && !strcmp(desc, head)) current = 1; else color = BRANCH_COLOR_LOCAL; break; case FILTER_REFS_REMOTES: prefix_to_skip = "refs/remotes/"; skip_prefix(desc, prefix_to_skip, &desc); color = BRANCH_COLOR_REMOTE; prefix_to_show = remote_prefix; break; case FILTER_REFS_DETACHED_HEAD: desc = to_free = get_head_description(); current = 1; break; default: color = BRANCH_COLOR_PLAIN; break; } c = ' '; if (current) { c = '*'; color = BRANCH_COLOR_CURRENT; } strbuf_addf(&name, "%s%s", prefix_to_show, desc); if (filter->verbose) { int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf); strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color), maxwidth + utf8_compensation, name.buf, branch_get_color(BRANCH_COLOR_RESET)); } else strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color), name.buf, branch_get_color(BRANCH_COLOR_RESET)); if (item->symref) { const char *symref = item->symref; if (prefix_to_skip) skip_prefix(symref, prefix_to_skip, &symref); strbuf_addf(&out, " -> %s", symref); } else if (filter->verbose) /* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */ add_verbose_info(&out, item, filter, desc); if (column_active(colopts)) { assert(!filter->verbose && "--column and --verbose are incompatible"); string_list_append(&output, out.buf); } else { printf("%s\n", out.buf); } strbuf_release(&name); strbuf_release(&out); free(to_free); }
int dia_message (char *txt_tv, int msgtype_iv) { window_t win_ri; int width_ii; button_t button_ri; int key_ii; char *s; if(!config.win) util_disp_init(); if (config.linemode) { putchar('\n'); if (msgtype_iv == MSGTYPE_ERROR || msgtype_iv == MSGTYPE_REBOOT) printf("*** "); dia_printformatted(txt_tv, 0, max_x_ig - 5, 1); if (msgtype_iv == MSGTYPE_INFOENTER) { int c; do c = lgetchar(); while (c != '\n' && c != '\r' && c != EOF); } return 0; } disp_toggle_output (DISP_OFF); kbd_clear_buffer (); memset (&win_ri, 0, sizeof (window_t)); if (msgtype_iv == MSGTYPE_ERROR || msgtype_iv == MSGTYPE_REBOOT) { win_ri.bg_color = colors_prg->error_win; win_ri.fg_color = colors_prg->error_fg; } else { win_ri.bg_color = colors_prg->msg_win; win_ri.fg_color = colors_prg->msg_fg; } width_ii = dia_win_open (&win_ri, txt_tv, 0); s = msgtype_iv == MSGTYPE_REBOOT ? "Reboot" : "OK"; util_generate_button (&button_ri, s, utf8_strwidth(s)); win_add_button (&win_ri, &button_ri, width_ii / 2 - utf8_strwidth (button_ri.text) / 2 - 1, utf8_strwidth (button_ri.text)); /* win_button_select (&button_ri); */ disp_flush_area (&win_ri); do { key_ii = kbd_getch (TRUE); if (key_ii == KEY_CTRL_C) dia_handle_ctrlc (); if (key_ii == KEY_ENTER) win_button_pressed (&button_ri, FALSE); } while ( key_ii != KEY_ENTER && key_ii != KEY_ESC && key_ii != 'q' && key_ii != 'r' && key_ii != 's' && key_ii != 'i' && key_ii != 'f' && key_ii != 'c' ); win_close (&win_ri); if (key_ii == KEY_ENTER) return (0); else if (key_ii == KEY_ESC) return (-1); else if (key_ii == 'r') return (-69); else if (key_ii == 'f') return (-70); else if (key_ii == 'i') return (-71); else if (key_ii == 'c') return (-73); else if (key_ii == 's') return (-74); else return (-42); }
int cmd_blame(int argc, const char **argv, const char *prefix) { struct rev_info revs; const char *path; struct blame_scoreboard sb; struct blame_origin *o; struct blame_entry *ent = NULL; long dashdash_pos, lno; struct progress_info pi = { NULL, 0 }; struct string_list range_list = STRING_LIST_INIT_NODUP; int output_option = 0, opt = 0; int show_stats = 0; const char *revs_file = NULL; const char *contents_from = NULL; const struct option options[] = { OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")), OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")), OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")), OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")), OPT_BOOL(0, "progress", &show_progress, N_("Force progress reporting")), OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE), OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME), OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER), OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN), OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN), OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT), OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP), OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME), OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR), OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL), OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE), /* * The following two options are parsed by parse_revision_opt() * and are only included here to get included in the "-h" * output: */ { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb }, OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL), OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")), OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")), { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback }, { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback }, OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")), OPT__ABBREV(&abbrev), OPT_END() }; struct parse_opt_ctx_t ctx; int cmd_is_annotate = !strcmp(argv[0], "annotate"); struct range_set ranges; unsigned int range_i; long anchor; git_config(git_blame_config, &output_option); init_revisions(&revs, NULL); revs.date_mode = blame_date_mode; revs.diffopt.flags.allow_textconv = 1; revs.diffopt.flags.follow_renames = 1; save_commit_buffer = 0; dashdash_pos = 0; show_progress = -1; parse_options_start(&ctx, argc, argv, prefix, options, PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0); for (;;) { switch (parse_options_step(&ctx, options, blame_opt_usage)) { case PARSE_OPT_HELP: exit(129); case PARSE_OPT_DONE: if (ctx.argv[0]) dashdash_pos = ctx.cpidx; goto parse_done; } if (!strcmp(ctx.argv[0], "--reverse")) { ctx.argv[0] = "--children"; reverse = 1; } parse_revision_opt(&revs, &ctx, options, blame_opt_usage); } parse_done: no_whole_file_rename = !revs.diffopt.flags.follow_renames; xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC; revs.diffopt.flags.follow_renames = 0; argc = parse_options_end(&ctx); if (incremental || (output_option & OUTPUT_PORCELAIN)) { if (show_progress > 0) die(_("--progress can't be used with --incremental or porcelain formats")); show_progress = 0; } else if (show_progress < 0) show_progress = isatty(2); if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ) /* one more abbrev length is needed for the boundary commit */ abbrev++; else if (!abbrev) abbrev = GIT_SHA1_HEXSZ; if (revs_file && read_ancestry(revs_file)) die_errno("reading graft file '%s' failed", revs_file); if (cmd_is_annotate) { output_option |= OUTPUT_ANNOTATE_COMPAT; blame_date_mode.type = DATE_ISO8601; } else { blame_date_mode = revs.date_mode; } /* The maximum width used to show the dates */ switch (blame_date_mode.type) { case DATE_RFC2822: blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700"); break; case DATE_ISO8601_STRICT: blame_date_width = sizeof("2006-10-19T16:00:04-07:00"); break; case DATE_ISO8601: blame_date_width = sizeof("2006-10-19 16:00:04 -0700"); break; case DATE_RAW: blame_date_width = sizeof("1161298804 -0700"); break; case DATE_UNIX: blame_date_width = sizeof("1161298804"); break; case DATE_SHORT: blame_date_width = sizeof("2006-10-19"); break; case DATE_RELATIVE: /* * TRANSLATORS: This string is used to tell us the * maximum display width for a relative timestamp in * "git blame" output. For C locale, "4 years, 11 * months ago", which takes 22 places, is the longest * among various forms of relative timestamps, but * your language may need more or fewer display * columns. */ blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */ break; case DATE_NORMAL: blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700"); break; case DATE_STRFTIME: blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */ break; } blame_date_width -= 1; /* strip the null */ if (revs.diffopt.flags.find_copies_harder) opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE | PICKAXE_BLAME_COPY_HARDER); /* * We have collected options unknown to us in argv[1..unk] * which are to be passed to revision machinery if we are * going to do the "bottom" processing. * * The remaining are: * * (1) if dashdash_pos != 0, it is either * "blame [revisions] -- <path>" or * "blame -- <path> <rev>" * * (2) otherwise, it is one of the two: * "blame [revisions] <path>" * "blame <path> <rev>" * * Note that we must strip out <path> from the arguments: we do not * want the path pruning but we may want "bottom" processing. */ if (dashdash_pos) { switch (argc - dashdash_pos - 1) { case 2: /* (1b) */ if (argc != 4) usage_with_options(blame_opt_usage, options); /* reorder for the new way: <rev> -- <path> */ argv[1] = argv[3]; argv[3] = argv[2]; argv[2] = "--"; /* FALLTHROUGH */ case 1: /* (1a) */ path = add_prefix(prefix, argv[--argc]); argv[argc] = NULL; break; default: usage_with_options(blame_opt_usage, options); } } else { if (argc < 2) usage_with_options(blame_opt_usage, options); if (argc == 3 && is_a_rev(argv[argc - 1])) { /* (2b) */ path = add_prefix(prefix, argv[1]); argv[1] = argv[2]; } else { /* (2a) */ if (argc == 2 && is_a_rev(argv[1]) && !get_git_work_tree()) die("missing <path> to blame"); path = add_prefix(prefix, argv[argc - 1]); } argv[argc - 1] = "--"; } revs.disable_stdin = 1; setup_revisions(argc, argv, &revs, NULL); init_scoreboard(&sb); sb.revs = &revs; sb.contents_from = contents_from; sb.reverse = reverse; setup_scoreboard(&sb, path, &o); lno = sb.num_lines; if (lno && !range_list.nr) string_list_append(&range_list, "1"); anchor = 1; range_set_init(&ranges, range_list.nr); for (range_i = 0; range_i < range_list.nr; ++range_i) { long bottom, top; if (parse_range_arg(range_list.items[range_i].string, nth_line_cb, &sb, lno, anchor, &bottom, &top, sb.path)) usage(blame_usage); if (lno < top || ((lno || bottom) && lno < bottom)) die(Q_("file %s has only %lu line", "file %s has only %lu lines", lno), path, lno); if (bottom < 1) bottom = 1; if (top < 1) top = lno; bottom--; range_set_append_unsafe(&ranges, bottom, top); anchor = top + 1; } sort_and_merge_range_set(&ranges); for (range_i = ranges.nr; range_i > 0; --range_i) { const struct range *r = &ranges.ranges[range_i - 1]; ent = blame_entry_prepend(ent, r->start, r->end, o); } o->suspects = ent; prio_queue_put(&sb.commits, o->commit); blame_origin_decref(o); range_set_release(&ranges); string_list_clear(&range_list, 0); sb.ent = NULL; sb.path = path; if (blame_move_score) sb.move_score = blame_move_score; if (blame_copy_score) sb.copy_score = blame_copy_score; sb.debug = DEBUG; sb.on_sanity_fail = &sanity_check_on_fail; sb.show_root = show_root; sb.xdl_opts = xdl_opts; sb.no_whole_file_rename = no_whole_file_rename; read_mailmap(&mailmap, NULL); sb.found_guilty_entry = &found_guilty_entry; sb.found_guilty_entry_data = π if (show_progress) pi.progress = start_delayed_progress(_("Blaming lines"), sb.num_lines); assign_blame(&sb, opt); stop_progress(&pi.progress); if (!incremental) setup_pager(); else return 0; blame_sort_final(&sb); blame_coalesce(&sb); if (!(output_option & OUTPUT_PORCELAIN)) find_alignment(&sb, &output_option); output(&sb, output_option); free((void *)sb.final_buf); for (ent = sb.ent; ent; ) { struct blame_entry *e = ent->next; free(ent); ent = e; } if (show_stats) { printf("num read blob: %d\n", sb.num_read_blob); printf("num get patch: %d\n", sb.num_get_patch); printf("num commits: %d\n", sb.num_commits); } return 0; }
static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int opt) { int cnt; const char *cp; struct blame_origin *suspect = ent->suspect; struct commit_info ci; char hex[GIT_MAX_HEXSZ + 1]; int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP); get_commit_info(suspect->commit, &ci, 1); oid_to_hex_r(hex, &suspect->commit->object.oid); cp = blame_nth_line(sb, ent->lno); for (cnt = 0; cnt < ent->num_lines; cnt++) { char ch; int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev; if (suspect->commit->object.flags & UNINTERESTING) { if (blank_boundary) memset(hex, ' ', length); else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) { length--; putchar('^'); } } printf("%.*s", length, hex); if (opt & OUTPUT_ANNOTATE_COMPAT) { const char *name; if (opt & OUTPUT_SHOW_EMAIL) name = ci.author_mail.buf; else name = ci.author.buf; printf("\t(%10s\t%10s\t%d)", name, format_time(ci.author_time, ci.author_tz.buf, show_raw_time), ent->lno + 1 + cnt); } else { if (opt & OUTPUT_SHOW_SCORE) printf(" %*d %02d", max_score_digits, ent->score, ent->suspect->refcnt); if (opt & OUTPUT_SHOW_NAME) printf(" %-*.*s", longest_file, longest_file, suspect->path); if (opt & OUTPUT_SHOW_NUMBER) printf(" %*d", max_orig_digits, ent->s_lno + 1 + cnt); if (!(opt & OUTPUT_NO_AUTHOR)) { const char *name; int pad; if (opt & OUTPUT_SHOW_EMAIL) name = ci.author_mail.buf; else name = ci.author.buf; pad = longest_author - utf8_strwidth(name); printf(" (%s%*s %10s", name, pad, "", format_time(ci.author_time, ci.author_tz.buf, show_raw_time)); } printf(" %*d) ", max_digits, ent->lno + 1 + cnt); } do { ch = *cp++; putchar(ch); } while (ch != '\n' && cp < sb->final_buf + sb->final_buf_size); } if (sb->final_buf_size && cp[-1] != '\n') putchar('\n'); commit_info_destroy(&ci); }