Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
git-mirror
/
git
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security
Insights
Files
9a8c2b6
Documentation
block-sha1
builtin
compat
contrib
ewah
git-gui
gitk-git
gitweb
mergetools
perl
po
ppc
t
templates
vcs-svn
xdiff
.gitattributes
.gitignore
.mailmap
COPYING
GIT-VERSION-GEN
INSTALL
LGPL-2.1
Makefile
README
RelNotes
abspath.c
aclocal.m4
advice.c
advice.h
alias.c
alloc.c
archive-tar.c
archive-zip.c
archive.c
archive.h
argv-array.c
argv-array.h
attr.c
attr.h
base85.c
bisect.c
bisect.h
blob.c
blob.h
branch.c
branch.h
builtin.h
bulk-checkin.c
bulk-checkin.h
bundle.c
bundle.h
cache-tree.c
cache-tree.h
cache.h
check-builtins.sh
check-racy.c
check_bindir
color.c
color.h
column.c
column.h
combine-diff.c
command-list.txt
commit-slab.h
commit.c
commit.h
config.c
config.mak.in
config.mak.uname
configure.ac
connect.c
connect.h
connected.c
connected.h
convert.c
convert.h
copy.c
credential-cache--daemon.c
credential-cache.c
credential-store.c
credential.c
credential.h
csum-file.c
csum-file.h
ctype.c
daemon.c
date.c
decorate.c
decorate.h
delta.h
diff-delta.c
diff-lib.c
diff-no-index.c
diff.c
diff.h
diffcore-break.c
diffcore-delta.c
diffcore-order.c
diffcore-pickaxe.c
diffcore-rename.c
diffcore.h
dir.c
dir.h
editor.c
entry.c
environment.c
exec_cmd.c
exec_cmd.h
fast-import.c
fetch-pack.c
fetch-pack.h
fmt-merge-msg.h
fsck.c
fsck.h
generate-cmdlist.sh
gettext.c
gettext.h
git-add--interactive.perl
git-am.sh
git-archimport.perl
git-bisect.sh
git-compat-util.h
git-cvsexportcommit.perl
git-cvsimport.perl
git-cvsserver.perl
git-difftool--helper.sh
git-difftool.perl
git-filter-branch.sh
git-instaweb.sh
git-merge-octopus.sh
git-merge-one-file.sh
git-merge-resolve.sh
git-mergetool--lib.sh
git-mergetool.sh
git-p4.py
git-parse-remote.sh
git-pull.sh
git-quiltimport.sh
git-rebase--am.sh
git-rebase--interactive.sh
git-rebase--merge.sh
git-rebase.sh
git-relink.perl
git-remote-testgit.sh
git-request-pull.sh
git-send-email.perl
git-sh-i18n.sh
git-sh-setup.sh
git-stash.sh
git-submodule.sh
git-svn.perl
git-web--browse.sh
git.c
git.rc
git.spec.in
gpg-interface.c
gpg-interface.h
graph.c
graph.h
grep.c
grep.h
hashmap.c
hashmap.h
help.c
help.h
hex.c
http-backend.c
http-fetch.c
http-push.c
http-walker.c
http.c
http.h
ident.c
imap-send.c
khash.h
kwset.c
kwset.h
levenshtein.c
levenshtein.h
line-log.c
line-log.h
line-range.c
line-range.h
list-objects.c
list-objects.h
ll-merge.c
ll-merge.h
lockfile.c
log-tree.c
log-tree.h
mailmap.c
mailmap.h
match-trees.c
merge-blobs.c
merge-blobs.h
merge-recursive.c
merge-recursive.h
merge.c
mergesort.c
mergesort.h
name-hash.c
notes-cache.c
notes-cache.h
notes-merge.c
notes-merge.h
notes-utils.c
notes-utils.h
notes.c
notes.h
object.c
object.h
pack-bitmap-write.c
pack-bitmap.c
pack-bitmap.h
pack-check.c
pack-objects.c
pack-objects.h
pack-revindex.c
pack-revindex.h
pack-write.c
pack.h
pager.c
parse-options-cb.c
parse-options.c
parse-options.h
patch-delta.c
patch-ids.c
patch-ids.h
path.c
pathspec.c
pathspec.h
pkt-line.c
pkt-line.h
preload-index.c
pretty.c
prio-queue.c
prio-queue.h
progress.c
progress.h
prompt.c
prompt.h
quote.c
quote.h
reachable.c
reachable.h
read-cache.c
reflog-walk.c
reflog-walk.h
refs.c
refs.h
remote-curl.c
remote-testsvn.c
remote.c
remote.h
replace_object.c
rerere.c
rerere.h
resolve-undo.c
resolve-undo.h
revision.c
revision.h
run-command.c
run-command.h
send-pack.c
send-pack.h
sequencer.c
sequencer.h
server-info.c
setup.c
sh-i18n--envsubst.c
sha1-array.c
sha1-array.h
sha1-lookup.c
sha1-lookup.h
sha1_file.c
sha1_name.c
shallow.c
shell.c
shortlog.h
show-index.c
sideband.c
sideband.h
sigchain.c
sigchain.h
strbuf.c
strbuf.h
streaming.c
streaming.h
string-list.c
string-list.h
submodule.c
submodule.h
symlinks.c
tag.c
tag.h
tar.h
test-chmtime.c
test-ctype.c
test-date.c
test-delta.c
test-dump-cache-tree.c
test-genrandom.c
test-hashmap.c
test-index-version.c
test-line-buffer.c
test-match-trees.c
test-mergesort.c
test-mktemp.c
test-parse-options.c
test-path-utils.c
test-prio-queue.c
test-read-cache.c
test-regex.c
test-revision-walking.c
test-run-command.c
test-scrap-cache-tree.c
test-sha1.c
test-sha1.sh
test-sigchain.c
test-string-list.c
test-subprocess.c
test-svn-fe.c
test-urlmatch-normalization.c
test-wildmatch.c
thread-utils.c
thread-utils.h
trace.c
transport-helper.c
transport.c
transport.h
tree-diff.c
tree-walk.c
tree-walk.h
tree.c
tree.h
unimplemented.sh
unix-socket.c
unix-socket.h
unpack-trees.c
unpack-trees.h
upload-pack.c
url.c
url.h
urlmatch.c
urlmatch.h
usage.c
userdiff.c
userdiff.h
utf8.c
utf8.h
varint.c
varint.h
version.c
version.h
versioncmp.c
walker.c
walker.h
wildmatch.c
wildmatch.h
wrap-for-bin.sh
wrapper.c
write_or_die.c
ws.c
wt-status.c
wt-status.h
xdiff-interface.c
xdiff-interface.h
zlib.c
Breadcrumbs
git
/
column.c
Blame
Blame
Latest commit
History
History
434 lines (372 loc) · 10.2 KB
Breadcrumbs
git
/
column.c
Top
File metadata and controls
Code
Blame
434 lines (372 loc) · 10.2 KB
Raw
#include "cache.h" #include "column.h" #include "string-list.h" #include "parse-options.h" #include "run-command.h" #include "utf8.h" #define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \ (x) * (d)->rows + (y) : \ (y) * (d)->cols + (x)) struct column_data { const struct string_list *list; unsigned int colopts; struct column_options opts; int rows, cols; int *len; /* cell length */ int *width; /* index to the longest row in column */ }; /* 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; } /* * Calculate cell width, rows and cols for a table of equal cells, given * table width and how many spaces between cells. */ static void layout(struct column_data *data, int *width) { int i; *width = 0; for (i = 0; i < data->list->nr; i++) if (*width < data->len[i]) *width = data->len[i]; *width += data->opts.padding; data->cols = (data->opts.width - strlen(data->opts.indent)) / *width; if (data->cols == 0) data->cols = 1; data->rows = DIV_ROUND_UP(data->list->nr, data->cols); } static void compute_column_width(struct column_data *data) { int i, x, y; for (x = 0; x < data->cols; x++) { data->width[x] = XY2LINEAR(data, x, 0); for (y = 0; y < data->rows; y++) { i = XY2LINEAR(data, x, y); if (i < data->list->nr && data->len[data->width[x]] < data->len[i]) data->width[x] = i; } } } /* * Shrink all columns by shortening them one row each time (and adding * more columns along the way). Hopefully the longest cell will be * moved to the next column, column is shrunk so we have more space * for new columns. The process ends when the whole thing no longer * fits in data->total_width. */ static void shrink_columns(struct column_data *data) { data->width = xrealloc(data->width, sizeof(*data->width) * data->cols); while (data->rows > 1) { int x, total_width, cols, rows; rows = data->rows; cols = data->cols; data->rows--; data->cols = DIV_ROUND_UP(data->list->nr, data->rows); if (data->cols != cols) data->width = xrealloc(data->width, sizeof(*data->width) * data->cols); compute_column_width(data); total_width = strlen(data->opts.indent); for (x = 0; x < data->cols; x++) { total_width += data->len[data->width[x]]; total_width += data->opts.padding; } if (total_width > data->opts.width) { data->rows = rows; data->cols = cols; break; } } compute_column_width(data); } /* Display without layout when not enabled */ static void display_plain(const struct string_list *list, const char *indent, const char *nl) { int i; for (i = 0; i < list->nr; i++) printf("%s%s%s", indent, list->items[i].string, nl); } /* Print a cell to stdout with all necessary leading/traling space */ static int display_cell(struct column_data *data, int initial_width, const char *empty_cell, int x, int y) { int i, len, newline; i = XY2LINEAR(data, x, y); if (i >= data->list->nr) return -1; len = data->len[i]; if (data->width && data->len[data->width[x]] < initial_width) { /* * empty_cell has initial_width chars, if real column * is narrower, increase len a bit so we fill less * space. */ len += initial_width - data->len[data->width[x]]; len -= data->opts.padding; } if (COL_LAYOUT(data->colopts) == COL_COLUMN) newline = i + data->rows >= data->list->nr; else newline = x == data->cols - 1 || i == data->list->nr - 1; printf("%s%s%s", x == 0 ? data->opts.indent : "", data->list->items[i].string, newline ? data->opts.nl : empty_cell + len); return 0; } /* Display COL_COLUMN or COL_ROW */ static void display_table(const struct string_list *list, unsigned int colopts, const struct column_options *opts) { struct column_data data; int x, y, i, initial_width; char *empty_cell; memset(&data, 0, sizeof(data)); data.list = list; data.colopts = colopts; data.opts = *opts; data.len = xmalloc(sizeof(*data.len) * list->nr); for (i = 0; i < list->nr; i++) data.len[i] = item_length(colopts, list->items[i].string); layout(&data, &initial_width); if (colopts & COL_DENSE) shrink_columns(&data); empty_cell = xmalloc(initial_width + 1); memset(empty_cell, ' ', initial_width); empty_cell[initial_width] = '\0'; for (y = 0; y < data.rows; y++) { for (x = 0; x < data.cols; x++) if (display_cell(&data, initial_width, empty_cell, x, y)) break; } free(data.len); free(data.width); free(empty_cell); } void print_columns(const struct string_list *list, unsigned int colopts, const struct column_options *opts) { struct column_options nopts; if (!list->nr) return; assert((colopts & COL_ENABLE_MASK) != COL_AUTO); memset(&nopts, 0, sizeof(nopts)); nopts.indent = opts && opts->indent ? opts->indent : ""; nopts.nl = opts && opts->nl ? opts->nl : "\n"; nopts.padding = opts ? opts->padding : 1; nopts.width = opts && opts->width ? opts->width : term_columns() - 1; if (!column_active(colopts)) { display_plain(list, "", "\n"); return; } switch (COL_LAYOUT(colopts)) { case COL_PLAIN: display_plain(list, nopts.indent, nopts.nl); break; case COL_ROW: case COL_COLUMN: display_table(list, colopts, &nopts); break; default: die("BUG: invalid layout mode %d", COL_LAYOUT(colopts)); } } int finalize_colopts(unsigned int *colopts, int stdout_is_tty) { if ((*colopts & COL_ENABLE_MASK) == COL_AUTO) { if (stdout_is_tty < 0) stdout_is_tty = isatty(1); *colopts &= ~COL_ENABLE_MASK; if (stdout_is_tty) *colopts |= COL_ENABLED; } return 0; } struct colopt { const char *name; unsigned int value; unsigned int mask; }; #define LAYOUT_SET 1 #define ENABLE_SET 2 static int parse_option(const char *arg, int len, unsigned int *colopts, int *group_set) { struct colopt opts[] = { { "always", COL_ENABLED, COL_ENABLE_MASK }, { "never", COL_DISABLED, COL_ENABLE_MASK }, { "auto", COL_AUTO, COL_ENABLE_MASK }, { "plain", COL_PLAIN, COL_LAYOUT_MASK }, { "column", COL_COLUMN, COL_LAYOUT_MASK }, { "row", COL_ROW, COL_LAYOUT_MASK }, { "dense", COL_DENSE, 0 }, }; int i; for (i = 0; i < ARRAY_SIZE(opts); i++) { int set = 1, arg_len = len, name_len; const char *arg_str = arg; if (!opts[i].mask) { if (arg_len > 2 && !strncmp(arg_str, "no", 2)) { arg_str += 2; arg_len -= 2; set = 0; } } name_len = strlen(opts[i].name); if (arg_len != name_len || strncmp(arg_str, opts[i].name, name_len)) continue; switch (opts[i].mask) { case COL_ENABLE_MASK: *group_set |= ENABLE_SET; break; case COL_LAYOUT_MASK: *group_set |= LAYOUT_SET; break; } if (opts[i].mask) *colopts = (*colopts & ~opts[i].mask) | opts[i].value; else { if (set) *colopts |= opts[i].value; else *colopts &= ~opts[i].value; } return 0; } return error("unsupported option '%s'", arg); } static int parse_config(unsigned int *colopts, const char *value) { const char *sep = " ,"; int group_set = 0; while (*value) { int len = strcspn(value, sep); if (len) { if (parse_option(value, len, colopts, &group_set)) return -1; value += len; } value += strspn(value, sep); } /* * If none of "always", "never", and "auto" is specified, then setting * layout implies "always". * * Current value in COL_ENABLE_MASK is disregarded. This means if * you set column.ui = auto and pass --column=row, then "auto" * will become "always". */ if ((group_set & LAYOUT_SET) && !(group_set & ENABLE_SET)) *colopts = (*colopts & ~COL_ENABLE_MASK) | COL_ENABLED; return 0; } static int column_config(const char *var, const char *value, const char *key, unsigned int *colopts) { if (!value) return config_error_nonbool(var); if (parse_config(colopts, value)) return error("invalid column.%s mode %s", key, value); return 0; } int git_column_config(const char *var, const char *value, const char *command, unsigned int *colopts) { const char *it = skip_prefix(var, "column."); if (!it) return 0; if (!strcmp(it, "ui")) return column_config(var, value, "ui", colopts); if (command && !strcmp(it, command)) return column_config(var, value, it, colopts); return 0; } int parseopt_column_callback(const struct option *opt, const char *arg, int unset) { unsigned int *colopts = opt->value; *colopts |= COL_PARSEOPT; *colopts &= ~COL_ENABLE_MASK; if (unset) /* --no-column == never */ return 0; /* --column == always unless "arg" states otherwise */ *colopts |= COL_ENABLED; if (arg) return parse_config(colopts, arg); return 0; } static int fd_out = -1; static struct child_process column_process; int run_column_filter(int colopts, const struct column_options *opts) { const char *av[10]; int ret, ac = 0; struct strbuf sb_colopt = STRBUF_INIT; struct strbuf sb_width = STRBUF_INIT; struct strbuf sb_padding = STRBUF_INIT; if (fd_out != -1) return -1; av[ac++] = "column"; strbuf_addf(&sb_colopt, "--raw-mode=%d", colopts); av[ac++] = sb_colopt.buf; if (opts && opts->width) { strbuf_addf(&sb_width, "--width=%d", opts->width); av[ac++] = sb_width.buf; } if (opts && opts->indent) { av[ac++] = "--indent"; av[ac++] = opts->indent; } if (opts && opts->padding) { strbuf_addf(&sb_padding, "--padding=%d", opts->padding); av[ac++] = sb_padding.buf; } av[ac] = NULL; fflush(stdout); memset(&column_process, 0, sizeof(column_process)); column_process.in = -1; column_process.out = dup(1); column_process.git_cmd = 1; column_process.argv = av; ret = start_command(&column_process); strbuf_release(&sb_colopt); strbuf_release(&sb_width); strbuf_release(&sb_padding); if (ret) return -2; fd_out = dup(1); close(1); dup2(column_process.in, 1); close(column_process.in); return 0; } int stop_column_filter(void) { if (fd_out == -1) return -1; fflush(stdout); close(1); finish_command(&column_process); dup2(fd_out, 1); close(fd_out); fd_out = -1; return 0; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
You can’t perform that action at this time.