X-Git-Url: https://git.distorted.org.uk/~mdw/tig/blobdiff_plain/f99c60950a00d72ca0fc7626de6034c704ea3883..c630ef12f388ecb40834d9b4456ce80fc499dedf:/tig.c diff --git a/tig.c b/tig.c index fbc65b9..71ddbb5 100644 --- a/tig.c +++ b/tig.c @@ -51,6 +51,7 @@ #endif static void __NORETURN die(const char *err, ...); +static void warn(const char *msg, ...); static void report(const char *msg, ...); static int read_properties(FILE *pipe, const char *separators, int (*read)(char *, size_t, char *, size_t)); static void set_nonblocking_input(bool loading); @@ -72,6 +73,7 @@ static size_t utf8_length(const char *string, size_t max_width, int *coloffset, #define REVGRAPH_MERGE 'M' #define REVGRAPH_BRANCH '+' #define REVGRAPH_COMMIT '*' +#define REVGRAPH_BOUND '^' #define REVGRAPH_LINE '|' #define SIZEOF_REVGRAPH 19 /* Size of revision ancestry graphics. */ @@ -105,13 +107,13 @@ static size_t utf8_length(const char *string, size_t max_width, int *coloffset, "git ls-remote $(git rev-parse --git-dir) 2>/dev/null" #define TIG_DIFF_CMD \ - "git show --no-color --root --patch-with-stat --find-copies-harder -B -C %s 2>/dev/null" + "git show --no-color --root --patch-with-stat --find-copies-harder -C %s 2>/dev/null" #define TIG_LOG_CMD \ "git log --no-color --cc --stat -n100 %s 2>/dev/null" #define TIG_MAIN_CMD \ - "git log --no-color --topo-order --pretty=raw %s 2>/dev/null" + "git log --no-color --topo-order --boundary --pretty=raw %s 2>/dev/null" #define TIG_TREE_CMD \ "git ls-tree %s %s" @@ -352,6 +354,7 @@ sq_quote(char buf[SIZEOF_STR], size_t bufsize, const char *src) REQ_(TOGGLE_REV_GRAPH, "Toggle revision graph visualization"), \ REQ_(STATUS_UPDATE, "Update file status"), \ REQ_(STATUS_MERGE, "Merge file using external tool"), \ + REQ_(TREE_PARENT, "Switch to parent directory in tree view"), \ REQ_(EDIT, "Open in editor"), \ REQ_(NONE, "Do nothing") @@ -406,22 +409,14 @@ get_request(const char *name) static const char usage[] = "tig " TIG_VERSION " (" __DATE__ ")\n" "\n" -"Usage: tig [options]\n" -" or: tig [options] [--] [git log options]\n" -" or: tig [options] log [git log options]\n" -" or: tig [options] diff [git diff options]\n" -" or: tig [options] show [git show options]\n" -" or: tig [options] < [git command output]\n" +"Usage: tig [options] [revs] [--] [paths]\n" +" or: tig show [options] [revs] [--] [paths]\n" +" or: tig status\n" +" or: tig < [git command output]\n" "\n" "Options:\n" -" -l Start up in log view\n" -" -d Start up in diff view\n" -" -S Start up in status view\n" -" -n[I], --line-number[=I] Show line numbers with given interval\n" -" -b[N], --tab-size[=N] Set number of spaces for tab expansion\n" -" -- Mark end of tig options\n" -" -v, --version Show version and exit\n" -" -h, --help Show help message and exit\n"; +" -v, --version Show version and exit\n" +" -h, --help Show help message and exit\n"; /* Option and state variables. */ static bool opt_line_number = FALSE; @@ -489,16 +484,32 @@ check_option(char *opt, char short_name, char *name, enum option_type type, ...) static bool parse_options(int argc, char *argv[]) { + char *altargv[1024]; + int altargc = 0; + char *subcommand = NULL; int i; for (i = 1; i < argc; i++) { char *opt = argv[i]; if (!strcmp(opt, "log") || - !strcmp(opt, "diff") || - !strcmp(opt, "show")) { + !strcmp(opt, "diff")) { + subcommand = opt; opt_request = opt[0] == 'l' ? REQ_VIEW_LOG : REQ_VIEW_DIFF; + warn("`tig %s' has been deprecated", opt); + break; + } + + if (!strcmp(opt, "show")) { + subcommand = opt; + opt_request = REQ_VIEW_DIFF; + break; + } + + if (!strcmp(opt, "status")) { + subcommand = opt; + opt_request = REQ_VIEW_STATUS; break; } @@ -521,48 +532,58 @@ parse_options(int argc, char *argv[]) } if (!strcmp(opt, "-S")) { + warn("`%s' has been deprecated; use `tig status' instead", opt); opt_request = REQ_VIEW_STATUS; continue; } if (!strcmp(opt, "-l")) { opt_request = REQ_VIEW_LOG; - continue; - } - - if (!strcmp(opt, "-d")) { + } else if (!strcmp(opt, "-d")) { opt_request = REQ_VIEW_DIFF; - continue; - } - - if (check_option(opt, 'n', "line-number", OPT_INT, &opt_num_interval)) { + } else if (check_option(opt, 'n', "line-number", OPT_INT, &opt_num_interval)) { opt_line_number = TRUE; - continue; - } - - if (check_option(opt, 'b', "tab-size", OPT_INT, &opt_tab_size)) { + } else if (check_option(opt, 'b', "tab-size", OPT_INT, &opt_tab_size)) { opt_tab_size = MIN(opt_tab_size, TABSIZE); + } else { + if (altargc >= ARRAY_SIZE(altargv)) + die("maximum number of arguments exceeded"); + altargv[altargc++] = opt; continue; } - die("unknown option '%s'\n\n%s", opt, usage); + warn("`%s' has been deprecated", opt); } + /* Check that no 'alt' arguments occured before a subcommand. */ + if (subcommand && i < argc && altargc > 0) + die("unknown arguments before `%s'", argv[i]); + if (!isatty(STDIN_FILENO)) { opt_request = REQ_VIEW_PAGER; opt_pipe = stdin; - } else if (i < argc) { + } else if (opt_request == REQ_VIEW_STATUS) { + if (argc - i > 1) + warn("ignoring arguments after `%s'", argv[i]); + + } else if (i < argc || altargc > 0) { + int alti = 0; size_t buf_size; if (opt_request == REQ_VIEW_MAIN) /* XXX: This is vulnerable to the user overriding * options required for the main view parser. */ - string_copy(opt_cmd, "git log --no-color --pretty=raw"); + string_copy(opt_cmd, "git log --no-color --pretty=raw --boundary"); else string_copy(opt_cmd, "git"); buf_size = strlen(opt_cmd); + while (buf_size < sizeof(opt_cmd) && alti < altargc) { + opt_cmd[buf_size++] = ' '; + buf_size = sq_quote(opt_cmd, buf_size, altargv[alti++]); + } + while (buf_size < sizeof(opt_cmd) && i < argc) { opt_cmd[buf_size++] = ' '; buf_size = sq_quote(opt_cmd, buf_size, argv[i++]); @@ -626,6 +647,7 @@ LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \ LINE(MAIN_REMOTE, "", COLOR_YELLOW, COLOR_DEFAULT, A_BOLD), \ LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD), \ +LINE(MAIN_REVGRAPH,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ LINE(TREE_DIR, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \ LINE(TREE_FILE, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \ LINE(STAT_SECTION, "", COLOR_CYAN, COLOR_DEFAULT, 0), \ @@ -694,15 +716,15 @@ get_line_info(char *name, int namelen) static void init_colors(void) { - int default_bg = COLOR_BLACK; - int default_fg = COLOR_WHITE; + int default_bg = line_info[LINE_DEFAULT].bg; + int default_fg = line_info[LINE_DEFAULT].fg; enum line_type type; start_color(); - if (use_default_colors() != ERR) { - default_bg = -1; - default_fg = -1; + if (assume_default_colors(default_fg, default_bg) == ERR) { + default_bg = COLOR_BLACK; + default_fg = COLOR_WHITE; } for (type = 0; type < ARRAY_SIZE(line_info); type++) { @@ -787,6 +809,7 @@ static struct keybinding default_keybindings[] = { { ':', REQ_PROMPT }, { 'u', REQ_STATUS_UPDATE }, { 'M', REQ_STATUS_MERGE }, + { ',', REQ_TREE_PARENT }, { 'e', REQ_EDIT }, /* Using the ncurses SIGWINCH handler. */ @@ -1267,29 +1290,47 @@ read_option(char *opt, size_t optlen, char *value, size_t valuelen) return OK; } -static int -load_options(void) +static void +load_option_file(const char *path) { - char *home = getenv("HOME"); - char buf[SIZEOF_STR]; FILE *file; + /* It's ok that the file doesn't exist. */ + file = fopen(path, "r"); + if (!file) + return; + config_lineno = 0; config_errors = FALSE; - add_builtin_run_requests(); + if (read_properties(file, " \t", read_option) == ERR || + config_errors == TRUE) + fprintf(stderr, "Errors while loading %s.\n", path); +} - if (!home || !string_format(buf, "%s/.tigrc", home)) - return ERR; +static int +load_options(void) +{ + char *home = getenv("HOME"); + char *tigrc_user = getenv("TIGRC_USER"); + char *tigrc_system = getenv("TIGRC_SYSTEM"); + char buf[SIZEOF_STR]; - /* It's ok that the file doesn't exist. */ - file = fopen(buf, "r"); - if (!file) - return OK; + add_builtin_run_requests(); - if (read_properties(file, " \t", read_option) == ERR || - config_errors == TRUE) - fprintf(stderr, "Errors while loading %s.\n", buf); + if (!tigrc_system) { + if (!string_format(buf, "%s/tigrc", SYSCONFDIR)) + return ERR; + tigrc_system = buf; + } + load_option_file(tigrc_system); + + if (!tigrc_user) { + if (!home || !string_format(buf, "%s/.tigrc", home)) + return ERR; + tigrc_user = buf; + } + load_option_file(tigrc_user); return OK; } @@ -3019,6 +3060,16 @@ tree_request(struct view *view, enum request request, struct line *line) { enum open_flags flags; + if (request == REQ_TREE_PARENT) { + if (*opt_path) { + /* fake 'cd ..' */ + request = REQ_ENTER; + line = &view->line[1]; + } else { + /* quit view if at top of tree */ + return REQ_VIEW_CLOSE; + } + } if (request != REQ_ENTER) return request; @@ -3114,12 +3165,13 @@ struct status { struct { mode_t mode; char rev[SIZEOF_REV]; + char name[SIZEOF_STR]; } old; struct { mode_t mode; char rev[SIZEOF_REV]; + char name[SIZEOF_STR]; } new; - char name[SIZEOF_STR]; }; static struct status stage_status; @@ -3137,7 +3189,7 @@ status_get_diff(struct status *file, char *buf, size_t bufsize) char *new_rev = buf + 56; char *status = buf + 97; - if (bufsize != 99 || + if (bufsize < 99 || old_mode[-1] != ':' || new_mode[-1] != ' ' || old_rev[-1] != ' ' || @@ -3153,7 +3205,7 @@ status_get_diff(struct status *file, char *buf, size_t bufsize) file->old.mode = strtoul(old_mode, NULL, 8); file->new.mode = strtoul(new_mode, NULL, 8); - file->name[0] = 0; + file->old.name[0] = file->new.name[0] = 0; return TRUE; } @@ -3220,7 +3272,7 @@ status_run(struct view *view, const char cmd[], bool diff, enum line_type type) unmerged = file; } else if (unmerged) { - int collapse = !strcmp(buf, unmerged->name); + int collapse = !strcmp(buf, unmerged->new.name); unmerged = NULL; if (collapse) { @@ -3231,10 +3283,26 @@ status_run(struct view *view, const char cmd[], bool diff, enum line_type type) } } + /* Grab the old name for rename/copy. */ + if (!*file->old.name && + (file->status == 'R' || file->status == 'C')) { + sepsize = sep - buf + 1; + string_ncopy(file->old.name, buf, sepsize); + bufsize -= sepsize; + memmove(buf, sep + 1, bufsize); + + sep = memchr(buf, 0, bufsize); + if (!sep) + break; + sepsize = sep - buf + 1; + } + /* git-ls-files just delivers a NUL separated * list of file names similar to the second half * of the git-diff-* output. */ - string_ncopy(file->name, buf, sepsize); + string_ncopy(file->new.name, buf, sepsize); + if (!*file->old.name) + string_copy(file->old.name, file->new.name); bufsize -= sepsize; memmove(buf, sep + 1, bufsize); file = NULL; @@ -3255,16 +3323,16 @@ error_out: } /* Don't show unmerged entries in the staged section. */ -#define STATUS_DIFF_INDEX_CMD "git diff-index -z --diff-filter=ACDMRTXB --cached HEAD" -#define STATUS_DIFF_FILES_CMD "git diff-files -z" +#define STATUS_DIFF_INDEX_CMD "git diff-index -z --diff-filter=ACDMRTXB --cached -M HEAD" +#define STATUS_DIFF_FILES_CMD "git update-index -q --refresh && git diff-files -z" #define STATUS_LIST_OTHER_CMD \ "git ls-files -z --others --exclude-per-directory=.gitignore" #define STATUS_DIFF_INDEX_SHOW_CMD \ - "git diff-index --root --patch-with-stat --find-copies-harder -B -C --cached HEAD -- %s 2>/dev/null" + "git diff-index --root --patch-with-stat -C -M --cached HEAD -- %s %s 2>/dev/null" #define STATUS_DIFF_FILES_SHOW_CMD \ - "git diff-files --root --patch-with-stat --find-copies-harder -B -C -- %s 2>/dev/null" + "git diff-files --root --patch-with-stat -C -M -- %s %s 2>/dev/null" /* First parse staged info using git-diff-index(1), then parse unstaged * info using git-diff-files(1), and finally untracked files using @@ -3366,7 +3434,7 @@ status_draw(struct view *view, struct line *line, unsigned int lineno, bool sele if (!selected) wattrset(view->win, A_NORMAL); wmove(view->win, lineno, 4); - waddstr(view->win, status->name); + waddstr(view->win, status->new.name); return TRUE; } @@ -3375,7 +3443,8 @@ static enum request status_enter(struct view *view, struct line *line) { struct status *status = line->data; - char path[SIZEOF_STR] = ""; + char oldpath[SIZEOF_STR] = ""; + char newpath[SIZEOF_STR] = ""; char *info; size_t cmdsize = 0; @@ -3385,8 +3454,15 @@ status_enter(struct view *view, struct line *line) return REQ_NONE; } - if (status && sq_quote(path, 0, status->name) >= sizeof(path)) - return REQ_QUIT; + if (status) { + if (sq_quote(oldpath, 0, status->old.name) >= sizeof(oldpath)) + return REQ_QUIT; + /* Diffs for unmerged entries are empty when pasing the + * new path, so leave it empty. */ + if (status->status != 'U' && + sq_quote(newpath, 0, status->new.name) >= sizeof(newpath)) + return REQ_QUIT; + } if (opt_cdup[0] && line->type != LINE_STAT_UNTRACKED && @@ -3396,7 +3472,7 @@ status_enter(struct view *view, struct line *line) switch (line->type) { case LINE_STAT_STAGED: if (!string_format_from(opt_cmd, &cmdsize, - STATUS_DIFF_INDEX_SHOW_CMD, path)) + STATUS_DIFF_INDEX_SHOW_CMD, oldpath, newpath)) return REQ_QUIT; if (status) info = "Staged changes to %s"; @@ -3406,7 +3482,7 @@ status_enter(struct view *view, struct line *line) case LINE_STAT_UNSTAGED: if (!string_format_from(opt_cmd, &cmdsize, - STATUS_DIFF_FILES_SHOW_CMD, path)) + STATUS_DIFF_FILES_SHOW_CMD, oldpath, newpath)) return REQ_QUIT; if (status) info = "Unstaged changes to %s"; @@ -3424,7 +3500,7 @@ status_enter(struct view *view, struct line *line) return REQ_NONE; } - opt_pipe = fopen(status->name, "r"); + opt_pipe = fopen(status->new.name, "r"); info = "Untracked file %s"; break; @@ -3441,7 +3517,7 @@ status_enter(struct view *view, struct line *line) } stage_line_type = line->type; - string_format(VIEW(REQ_VIEW_STAGE)->ref, info, stage_status.name); + string_format(VIEW(REQ_VIEW_STAGE)->ref, info, stage_status.new.name); } return REQ_NONE; @@ -3468,7 +3544,7 @@ status_update_file(struct view *view, struct status *status, enum line_type type if (!string_format_from(buf, &bufsize, "%06o %s\t%s%c", status->old.mode, status->old.rev, - status->name, 0)) + status->old.name, 0)) return FALSE; string_add(cmd, cmdsize, "git update-index -z --index-info"); @@ -3476,7 +3552,7 @@ status_update_file(struct view *view, struct status *status, enum line_type type case LINE_STAT_UNSTAGED: case LINE_STAT_UNTRACKED: - if (!string_format_from(buf, &bufsize, "%s%c", status->name, 0)) + if (!string_format_from(buf, &bufsize, "%s%c", status->new.name, 0)) return FALSE; string_add(cmd, cmdsize, "git update-index -z --add --remove --stdin"); @@ -3540,14 +3616,14 @@ status_request(struct view *view, enum request request, struct line *line) report("Merging only possible for files with unmerged status ('U')."); return REQ_NONE; } - open_mergetool(status->name); + open_mergetool(status->new.name); break; case REQ_EDIT: if (!status) return request; - open_editor(status->status != '?', status->name); + open_editor(status->status != '?', status->new.name); break; case REQ_ENTER: @@ -3578,7 +3654,7 @@ status_select(struct view *view, struct line *line) char *text; char *key; - if (status && !string_format(file, "'%s'", status->name)) + if (status && !string_format(file, "'%s'", status->new.name)) return; if (!status && line[1].type == LINE_STAT_NONE) @@ -3631,7 +3707,7 @@ status_grep(struct view *view, struct line *line) char *text; switch (state) { - case S_NAME: text = status->name; break; + case S_NAME: text = status->new.name; break; case S_STATUS: buf[0] = status->status; text = buf; @@ -3796,10 +3872,10 @@ stage_request(struct view *view, enum request request, struct line *line) break; case REQ_EDIT: - if (!stage_status.name[0]) + if (!stage_status.new.name[0]) return request; - open_editor(stage_status.status != '?', stage_status.name); + open_editor(stage_status.status != '?', stage_status.new.name); break; case REQ_ENTER: @@ -3847,6 +3923,7 @@ struct rev_graph { size_t size; struct commit *commit; size_t pos; + unsigned int boundary:1; }; /* Parents of the commit being visualized. */ @@ -3919,7 +3996,9 @@ get_rev_graph_symbol(struct rev_graph *graph) { chtype symbol; - if (graph->parents->size == 0) + if (graph->boundary) + symbol = REVGRAPH_BOUND; + else if (graph->parents->size == 0) symbol = REVGRAPH_INIT; else if (graph_parent_is_merge(graph)) symbol = REVGRAPH_MERGE; @@ -4001,7 +4080,7 @@ prepare_rev_graph(struct rev_graph *graph) } /* Interleave the new revision parent(s). */ - for (i = 0; i < graph->parents->size; i++) + for (i = 0; !graph->boundary && i < graph->parents->size; i++) push_rev_graph(graph->next, graph->parents->rev[i]); /* Lastly, put any remaining revisions. */ @@ -4085,12 +4164,12 @@ main_draw(struct view *view, struct line *line, unsigned int lineno, bool select } col += AUTHOR_COLS; - if (type != LINE_CURSOR) - wattrset(view->win, A_NORMAL); if (opt_rev_graph && commit->graph_size) { size_t i; + if (type != LINE_CURSOR) + wattrset(view->win, get_line_attr(LINE_MAIN_REVGRAPH)); wmove(view->win, lineno, col); /* Using waddch() instead of waddnstr() ensures that * they'll be rendered correctly for the cursor line. */ @@ -4100,6 +4179,8 @@ main_draw(struct view *view, struct line *line, unsigned int lineno, bool select waddch(view->win, ' '); col += commit->graph_size + 1; } + if (type != LINE_CURSOR) + wattrset(view->win, A_NORMAL); wmove(view->win, lineno, col); @@ -4159,7 +4240,13 @@ main_read(struct view *view, char *line) if (!commit) return FALSE; - string_copy_rev(commit->id, line + STRING_SIZE("commit ")); + line += STRING_SIZE("commit "); + if (*line == '-') { + graph->boundary = 1; + line++; + } + + string_copy_rev(commit->id, line); commit->refs = get_refs(commit->id); graph->commit = commit; add_line_data(view, commit, LINE_MAIN_COMMIT); @@ -4886,6 +4973,18 @@ die(const char *err, ...) exit(1); } +static void +warn(const char *msg, ...) +{ + va_list args; + + va_start(args, msg); + fputs("tig warning: ", stderr); + vfprintf(stderr, msg, args); + fputs("\n", stderr); + va_end(args); +} + int main(int argc, char *argv[]) {