Rearrange pager_enter logic
[tig] / tig.c
CommitLineData
4a2909a7
JF
1/* Copyright (c) 2006 Jonas Fonseca <fonseca@diku.dk>
2 * See license info at the bottom. */
b801d8b2
JF
3/**
4 * TIG(1)
5 * ======
6 *
7 * NAME
8 * ----
9 * tig - text-mode interface for git
10 *
11 * SYNOPSIS
12 * --------
13 * [verse]
03a93dbb
JF
14 * tig [options]
15 * tig [options] [--] [git log options]
b76c2afc
JF
16 * tig [options] log [git log options]
17 * tig [options] diff [git diff options]
8855ada4
JF
18 * tig [options] show [git show options]
19 * tig [options] < [git command output]
b801d8b2
JF
20 *
21 * DESCRIPTION
22 * -----------
6706b2ba
JF
23 * Browse changes in a git repository. Additionally, tig(1) can also act
24 * as a pager for output of various git commands.
25 *
57bdf034 26 * When browsing repositories, tig(1) uses the underlying git commands
468876c9
JF
27 * to present the user with various views, such as summarized commit log
28 * and showing the commit with the log message, diffstat, and the diff.
6706b2ba 29 *
468876c9
JF
30 * Using tig(1) as a pager, it will display input from stdin and try
31 * to colorize it.
b801d8b2
JF
32 **/
33
b76c2afc 34#ifndef VERSION
8c11094f 35#define VERSION "tig-0.3"
b76c2afc
JF
36#endif
37
8855ada4
JF
38#ifndef DEBUG
39#define NDEBUG
40#endif
41
22f66b0a 42#include <assert.h>
4c6fabc2 43#include <errno.h>
22f66b0a
JF
44#include <ctype.h>
45#include <signal.h>
b801d8b2 46#include <stdarg.h>
b801d8b2 47#include <stdio.h>
22f66b0a 48#include <stdlib.h>
b801d8b2 49#include <string.h>
6908bdbd 50#include <unistd.h>
b76c2afc 51#include <time.h>
b801d8b2
JF
52
53#include <curses.h>
b801d8b2
JF
54
55static void die(const char *err, ...);
56static void report(const char *msg, ...);
1ba2ae4b 57static void set_nonblocking_input(bool loading);
10e290ee 58static size_t utf8_length(const char *string, size_t max_width, int *coloffset, int *trimmed);
6b161b31
JF
59
60#define ABS(x) ((x) >= 0 ? (x) : -(x))
61#define MIN(x, y) ((x) < (y) ? (x) : (y))
62
63#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
64#define STRING_SIZE(x) (sizeof(x) - 1)
b76c2afc 65
2e8488b4
JF
66#define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
67#define SIZEOF_CMD 1024 /* Size of command buffer. */
b801d8b2 68
82e78006
JF
69/* This color name can be used to refer to the default term colors. */
70#define COLOR_DEFAULT (-1)
78c70acd 71
67e48ac5 72#define TIG_HELP "(d)iff, (l)og, (m)ain, (q)uit, (h)elp"
468876c9 73
82e78006 74/* The format and size of the date column in the main view. */
4c6fabc2 75#define DATE_FORMAT "%Y-%m-%d %H:%M"
6b161b31 76#define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
4c6fabc2 77
10e290ee
JF
78#define AUTHOR_COLS 20
79
a28bcc22 80/* The default interval between line numbers. */
4a2909a7 81#define NUMBER_INTERVAL 1
82e78006 82
6706b2ba
JF
83#define TABSIZE 8
84
a28bcc22
JF
85#define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
86
8855ada4 87/* Some ascii-shorthands fitted into the ncurses namespace. */
a28bcc22
JF
88#define KEY_TAB '\t'
89#define KEY_RETURN '\r'
4a2909a7
JF
90#define KEY_ESC 27
91
6706b2ba 92
a28bcc22 93/* User action requests. */
4a2909a7 94enum request {
a28bcc22
JF
95 /* Offset all requests to avoid conflicts with ncurses getch values. */
96 REQ_OFFSET = KEY_MAX + 1,
97
4a2909a7 98 /* XXX: Keep the view request first and in sync with views[]. */
2e8488b4 99 REQ_VIEW_MAIN,
4a2909a7
JF
100 REQ_VIEW_DIFF,
101 REQ_VIEW_LOG,
2e8488b4 102 REQ_VIEW_HELP,
6908bdbd 103 REQ_VIEW_PAGER,
4a2909a7 104
6b161b31 105 REQ_ENTER,
03a93dbb
JF
106 REQ_QUIT,
107 REQ_PROMPT,
4a2909a7 108 REQ_SCREEN_REDRAW,
fac7db6c 109 REQ_SCREEN_RESIZE,
4a2909a7 110 REQ_SCREEN_UPDATE,
03a93dbb
JF
111 REQ_SHOW_VERSION,
112 REQ_STOP_LOADING,
4a2909a7 113 REQ_TOGGLE_LINE_NUMBERS,
03a93dbb 114 REQ_VIEW_NEXT,
4f9b667a 115 REQ_VIEW_CLOSE,
b3a54cba
JF
116 REQ_NEXT,
117 REQ_PREVIOUS,
4a2909a7
JF
118
119 REQ_MOVE_UP,
120 REQ_MOVE_DOWN,
121 REQ_MOVE_PAGE_UP,
122 REQ_MOVE_PAGE_DOWN,
123 REQ_MOVE_FIRST_LINE,
124 REQ_MOVE_LAST_LINE,
125
126 REQ_SCROLL_LINE_UP,
127 REQ_SCROLL_LINE_DOWN,
128 REQ_SCROLL_PAGE_UP,
129 REQ_SCROLL_PAGE_DOWN,
130};
131
c34d9c9f 132struct ref {
468876c9
JF
133 char *name; /* Ref name; tag or head names are shortened. */
134 char id[41]; /* Commit SHA1 ID */
135 unsigned int tag:1; /* Is it a tag? */
136 unsigned int next:1; /* For ref lists: are there more refs? */
c34d9c9f
JF
137};
138
ff26aa29 139static struct ref **get_refs(char *id);
4c6fabc2 140
6706b2ba 141
03a93dbb
JF
142/*
143 * String helpers
144 */
78c70acd 145
82e78006 146static inline void
4685845e 147string_ncopy(char *dst, const char *src, int dstlen)
82e78006
JF
148{
149 strncpy(dst, src, dstlen - 1);
150 dst[dstlen - 1] = 0;
03a93dbb 151
82e78006
JF
152}
153
154/* Shorthand for safely copying into a fixed buffer. */
155#define string_copy(dst, src) \
156 string_ncopy(dst, src, sizeof(dst))
157
6706b2ba 158
03a93dbb
JF
159/* Shell quoting
160 *
161 * NOTE: The following is a slightly modified copy of the git project's shell
162 * quoting routines found in the quote.c file.
163 *
164 * Help to copy the thing properly quoted for the shell safety. any single
165 * quote is replaced with '\'', any exclamation point is replaced with '\!',
166 * and the whole thing is enclosed in a
167 *
168 * E.g.
169 * original sq_quote result
170 * name ==> name ==> 'name'
171 * a b ==> a b ==> 'a b'
172 * a'b ==> a'\''b ==> 'a'\''b'
173 * a!b ==> a'\!'b ==> 'a'\!'b'
174 */
175
176static size_t
177sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
178{
179 char c;
180
a9274b95 181#define BUFPUT(x) do { if (bufsize < SIZEOF_CMD) buf[bufsize++] = (x); } while (0)
03a93dbb
JF
182
183 BUFPUT('\'');
184 while ((c = *src++)) {
185 if (c == '\'' || c == '!') {
186 BUFPUT('\'');
187 BUFPUT('\\');
188 BUFPUT(c);
189 BUFPUT('\'');
190 } else {
191 BUFPUT(c);
192 }
193 }
194 BUFPUT('\'');
195
196 return bufsize;
197}
198
82e78006 199
b76c2afc
JF
200/**
201 * OPTIONS
202 * -------
203 **/
204
4b8c01a3
JF
205static const char usage[] =
206VERSION " (" __DATE__ ")\n"
207"\n"
208"Usage: tig [options]\n"
209" or: tig [options] [--] [git log options]\n"
210" or: tig [options] log [git log options]\n"
211" or: tig [options] diff [git diff options]\n"
212" or: tig [options] show [git show options]\n"
213" or: tig [options] < [git command output]\n"
214"\n"
215"Options:\n"
216" -l Start up in log view\n"
217" -d Start up in diff view\n"
218" -n[I], --line-number[=I] Show line numbers with given interval\n"
219" -t[N], --tab-size[=N] Set number of spaces for tab expansion\n"
220" -- Mark end of tig options\n"
221" -v, --version Show version and exit\n"
222" -h, --help Show help message and exit\n";
223
6706b2ba
JF
224/* Option and state variables. */
225static bool opt_line_number = FALSE;
2e8488b4 226static int opt_num_interval = NUMBER_INTERVAL;
6706b2ba 227static int opt_tab_size = TABSIZE;
6b161b31 228static enum request opt_request = REQ_VIEW_MAIN;
03a93dbb 229static char opt_cmd[SIZEOF_CMD] = "";
6908bdbd 230static FILE *opt_pipe = NULL;
b76c2afc 231
b76c2afc 232/* Returns the index of log or diff command or -1 to exit. */
8855ada4 233static bool
b76c2afc
JF
234parse_options(int argc, char *argv[])
235{
236 int i;
237
238 for (i = 1; i < argc; i++) {
239 char *opt = argv[i];
240
241 /**
b76c2afc 242 * -l::
1ba2ae4b 243 * Start up in log view using the internal log command.
b76c2afc 244 **/
6b161b31 245 if (!strcmp(opt, "-l")) {
4a2909a7 246 opt_request = REQ_VIEW_LOG;
6b161b31
JF
247 continue;
248 }
b76c2afc
JF
249
250 /**
251 * -d::
1ba2ae4b 252 * Start up in diff view using the internal diff command.
b76c2afc 253 **/
6b161b31 254 if (!strcmp(opt, "-d")) {
4a2909a7 255 opt_request = REQ_VIEW_DIFF;
6b161b31
JF
256 continue;
257 }
b76c2afc
JF
258
259 /**
2e8488b4 260 * -n[INTERVAL], --line-number[=INTERVAL]::
b76c2afc 261 * Prefix line numbers in log and diff view.
2e8488b4 262 * Optionally, with interval different than each line.
b76c2afc 263 **/
6b161b31 264 if (!strncmp(opt, "-n", 2) ||
8855ada4 265 !strncmp(opt, "--line-number", 13)) {
2e8488b4
JF
266 char *num = opt;
267
268 if (opt[1] == 'n') {
269 num = opt + 2;
270
271 } else if (opt[STRING_SIZE("--line-number")] == '=') {
272 num = opt + STRING_SIZE("--line-number=");
273 }
274
275 if (isdigit(*num))
276 opt_num_interval = atoi(num);
277
6b161b31
JF
278 opt_line_number = TRUE;
279 continue;
280 }
b76c2afc
JF
281
282 /**
6706b2ba
JF
283 * -t[NSPACES], --tab-size[=NSPACES]::
284 * Set the number of spaces tabs should be expanded to.
285 **/
286 if (!strncmp(opt, "-t", 2) ||
287 !strncmp(opt, "--tab-size", 10)) {
288 char *num = opt;
289
290 if (opt[1] == 't') {
291 num = opt + 2;
292
293 } else if (opt[STRING_SIZE("--tab-size")] == '=') {
294 num = opt + STRING_SIZE("--tab-size=");
295 }
296
297 if (isdigit(*num))
298 opt_tab_size = MIN(atoi(num), TABSIZE);
299 continue;
300 }
301
302 /**
b76c2afc
JF
303 * -v, --version::
304 * Show version and exit.
305 **/
6b161b31 306 if (!strcmp(opt, "-v") ||
8855ada4 307 !strcmp(opt, "--version")) {
b76c2afc 308 printf("tig version %s\n", VERSION);
8855ada4 309 return FALSE;
6b161b31 310 }
b76c2afc
JF
311
312 /**
4b8c01a3
JF
313 * -h, --help::
314 * Show help message and exit.
315 **/
316 if (!strcmp(opt, "-h") ||
317 !strcmp(opt, "--help")) {
318 printf(usage);
319 return FALSE;
320 }
321
322 /**
03a93dbb 323 * \--::
0721c53a 324 * End of tig(1) options. Useful when specifying command
d44f34bf 325 * options for the main view. Example:
03a93dbb 326 *
889cb462 327 * $ tig -- --since=1.month
b76c2afc 328 **/
6908bdbd
JF
329 if (!strcmp(opt, "--")) {
330 i++;
331 break;
332 }
03a93dbb 333
8855ada4 334 /**
d44f34bf 335 * log [git log options]::
8855ada4
JF
336 * Open log view using the given git log options.
337 *
d44f34bf 338 * diff [git diff options]::
8855ada4
JF
339 * Open diff view using the given git diff options.
340 *
d44f34bf 341 * show [git show options]::
8855ada4
JF
342 * Open diff view using the given git show options.
343 **/
344 if (!strcmp(opt, "log") ||
345 !strcmp(opt, "diff") ||
346 !strcmp(opt, "show")) {
347 opt_request = opt[0] == 'l'
348 ? REQ_VIEW_LOG : REQ_VIEW_DIFF;
349 break;
350 }
351
d44f34bf 352 /**
57bdf034 353 * [git log options]::
d44f34bf
JF
354 * tig(1) will stop the option parsing when the first
355 * command line parameter not starting with "-" is
57bdf034
JF
356 * encountered. All options including this one will be
357 * passed to git log when loading the main view.
358 * This makes it possible to say:
889cb462 359 *
03a93dbb 360 * $ tig tag-1.0..HEAD
d44f34bf 361 **/
03a93dbb 362 if (opt[0] && opt[0] != '-')
6908bdbd 363 break;
6b161b31
JF
364
365 die("unknown command '%s'", opt);
b76c2afc
JF
366 }
367
6908bdbd 368 if (!isatty(STDIN_FILENO)) {
8855ada4
JF
369 /**
370 * Pager mode
371 * ~~~~~~~~~~
372 * If stdin is a pipe, any log or diff options will be ignored and the
373 * pager view will be opened loading data from stdin. The pager mode
374 * can be used for colorizing output from various git commands.
375 *
376 * Example on how to colorize the output of git-show(1):
377 *
378 * $ git show | tig
379 **/
6908bdbd
JF
380 opt_request = REQ_VIEW_PAGER;
381 opt_pipe = stdin;
382
383 } else if (i < argc) {
384 size_t buf_size;
385
8855ada4
JF
386 /**
387 * Git command options
388 * ~~~~~~~~~~~~~~~~~~~
8855ada4
JF
389 * All git command options specified on the command line will
390 * be passed to the given command and all will be shell quoted
468876c9 391 * before they are passed to the shell.
8855ada4 392 *
468876c9
JF
393 * NOTE: If you specify options for the main view, you should
394 * not use the `--pretty` option as this option will be set
395 * automatically to the format expected by the main view.
8855ada4
JF
396 *
397 * Example on how to open the log view and show both author and
398 * committer information:
399 *
400 * $ tig log --pretty=fuller
468876c9 401 *
0721c53a
JF
402 * See the <<refspec, "Specifying revisions">> section below
403 * for an introduction to revision options supported by the git
404 * commands. For details on specific git command options, refer
405 * to the man page of the command in question.
8855ada4
JF
406 **/
407
6908bdbd 408 if (opt_request == REQ_VIEW_MAIN)
8855ada4
JF
409 /* XXX: This is vulnerable to the user overriding
410 * options required for the main view parser. */
6908bdbd
JF
411 string_copy(opt_cmd, "git log --stat --pretty=raw");
412 else
413 string_copy(opt_cmd, "git");
414 buf_size = strlen(opt_cmd);
415
416 while (buf_size < sizeof(opt_cmd) && i < argc) {
417 opt_cmd[buf_size++] = ' ';
418 buf_size = sq_quote(opt_cmd, buf_size, argv[i++]);
419 }
420
421 if (buf_size >= sizeof(opt_cmd))
422 die("command too long");
423
424 opt_cmd[buf_size] = 0;
425
426 }
427
8855ada4 428 return TRUE;
b76c2afc
JF
429}
430
431
432/*
433 * Line-oriented content detection.
6b161b31
JF
434 */
435
2e8488b4 436#define LINE_INFO \
6b161b31
JF
437/* Line type String to match Foreground Background Attributes
438 * --------- --------------- ---------- ---------- ---------- */ \
a28bcc22
JF
439/* Diff markup */ \
440LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
8855ada4 441LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
a28bcc22
JF
442LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
443LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
444LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
3af8774e
JF
445LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
446LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
a28bcc22
JF
447LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
448LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
449LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
450LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
451/* Pretty print commit header */ \
6908bdbd 452LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
8855ada4 453LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
6908bdbd
JF
454LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
455LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
8855ada4
JF
456LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
457LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
a28bcc22
JF
458/* Raw commit header */ \
459LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
460LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
461LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
8855ada4 462LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
a28bcc22
JF
463LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
464/* Misc */ \
465LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
466LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
467/* UI colors */ \
468LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
469LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
470LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
6b161b31
JF
471LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
472LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
a28bcc22
JF
473LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
474LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
475LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
c34d9c9f
JF
476LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
477LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
478LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD),
2e8488b4 479
78c70acd 480enum line_type {
2e8488b4
JF
481#define LINE(type, line, fg, bg, attr) \
482 LINE_##type
483 LINE_INFO
484#undef LINE
78c70acd
JF
485};
486
487struct line_info {
4685845e 488 const char *line; /* The start of line to match. */
2e8488b4
JF
489 int linelen; /* Size of string to match. */
490 int fg, bg, attr; /* Color and text attributes for the lines. */
78c70acd
JF
491};
492
2e8488b4 493static struct line_info line_info[] = {
78c70acd 494#define LINE(type, line, fg, bg, attr) \
a28bcc22 495 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
2e8488b4
JF
496 LINE_INFO
497#undef LINE
78c70acd
JF
498};
499
2e8488b4
JF
500static enum line_type
501get_line_type(char *line)
78c70acd
JF
502{
503 int linelen = strlen(line);
a28bcc22 504 enum line_type type;
78c70acd 505
a28bcc22 506 for (type = 0; type < ARRAY_SIZE(line_info); type++)
2e8488b4 507 /* Case insensitive search matches Signed-off-by lines better. */
a28bcc22
JF
508 if (linelen >= line_info[type].linelen &&
509 !strncasecmp(line_info[type].line, line, line_info[type].linelen))
510 return type;
78c70acd 511
2e8488b4 512 return LINE_DEFAULT;
78c70acd
JF
513}
514
2e8488b4 515static inline int
78c70acd
JF
516get_line_attr(enum line_type type)
517{
2e8488b4
JF
518 assert(type < ARRAY_SIZE(line_info));
519 return COLOR_PAIR(type) | line_info[type].attr;
78c70acd
JF
520}
521
522static void
523init_colors(void)
524{
82e78006
JF
525 int default_bg = COLOR_BLACK;
526 int default_fg = COLOR_WHITE;
a28bcc22 527 enum line_type type;
78c70acd
JF
528
529 start_color();
530
531 if (use_default_colors() != ERR) {
82e78006
JF
532 default_bg = -1;
533 default_fg = -1;
78c70acd
JF
534 }
535
a28bcc22
JF
536 for (type = 0; type < ARRAY_SIZE(line_info); type++) {
537 struct line_info *info = &line_info[type];
82e78006
JF
538 int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
539 int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
78c70acd 540
a28bcc22 541 init_pair(type, fg, bg);
78c70acd
JF
542 }
543}
544
fe7233c3
JF
545struct line {
546 enum line_type type;
547 void *data; /* User data */
548};
549
78c70acd 550
1ba2ae4b
JF
551/**
552 * ENVIRONMENT VARIABLES
553 * ---------------------
c34d9c9f
JF
554 * Several options related to the interface with git can be configured
555 * via environment options.
556 *
557 * Repository references
558 * ~~~~~~~~~~~~~~~~~~~~~
559 * Commits that are referenced by tags and branch heads will be marked
560 * by the reference name surrounded by '[' and ']':
561 *
468876c9 562 * 2006-03-26 19:42 Petr Baudis | [cogito-0.17.1] Cogito 0.17.1
c34d9c9f
JF
563 *
564 * If you want to filter out certain directories under `.git/refs/`, say
565 * `tmp` you can do it by setting the following variable:
566 *
6706b2ba 567 * $ TIG_LS_REMOTE="git ls-remote . | sed /\/tmp\//d" tig
c34d9c9f
JF
568 *
569 * Or set the variable permanently in your environment.
570 *
571 * TIG_LS_REMOTE::
6706b2ba
JF
572 * Set command for retrieving all repository references. The command
573 * should output data in the same format as git-ls-remote(1).
c34d9c9f
JF
574 **/
575
576#define TIG_LS_REMOTE \
1b4b0bd9 577 "git ls-remote . 2>/dev/null"
c34d9c9f
JF
578
579/**
0721c53a 580 * [[view-commands]]
c34d9c9f
JF
581 * View commands
582 * ~~~~~~~~~~~~~
1ba2ae4b 583 * It is possible to alter which commands are used for the different views.
6706b2ba
JF
584 * If for example you prefer commits in the main view to be sorted by date
585 * and only show 500 commits, use:
1ba2ae4b
JF
586 *
587 * $ TIG_MAIN_CMD="git log --date-order -n500 --pretty=raw %s" tig
588 *
589 * Or set the variable permanently in your environment.
590 *
591 * Notice, how `%s` is used to specify the commit reference. There can
592 * be a maximum of 5 `%s` ref specifications.
593 *
594 * TIG_DIFF_CMD::
595 * The command used for the diff view. By default, git show is used
596 * as a backend.
597 *
598 * TIG_LOG_CMD::
6706b2ba
JF
599 * The command used for the log view. If you prefer to have both
600 * author and committer shown in the log view be sure to pass
601 * `--pretty=fuller` to git log.
1ba2ae4b
JF
602 *
603 * TIG_MAIN_CMD::
604 * The command used for the main view. Note, you must always specify
605 * the option: `--pretty=raw` since the main view parser expects to
606 * read that format.
607 **/
608
609#define TIG_DIFF_CMD \
610 "git show --patch-with-stat --find-copies-harder -B -C %s"
611
612#define TIG_LOG_CMD \
613 "git log --cc --stat -n100 %s"
614
615#define TIG_MAIN_CMD \
616 "git log --topo-order --stat --pretty=raw %s"
617
0721c53a 618/* ... silently ignore that the following are also exported. */
1ba2ae4b
JF
619
620#define TIG_HELP_CMD \
1b4b0bd9 621 "man tig 2>/dev/null"
1ba2ae4b
JF
622
623#define TIG_PAGER_CMD \
624 ""
625
c2124ccd 626
468876c9
JF
627/**
628 * The viewer
629 * ----------
c2124ccd
JF
630 * The display consists of a status window on the last line of the screen and
631 * one or more views. The default is to only show one view at the time but it
632 * is possible to split both the main and log view to also show the commit
633 * diff.
468876c9 634 *
c2124ccd
JF
635 * If you are in the log view and press 'Enter' when the current line is a
636 * commit line, such as:
468876c9 637 *
c2124ccd 638 * commit 4d55caff4cc89335192f3e566004b4ceef572521
468876c9 639 *
c2124ccd
JF
640 * You will split the view so that the log view is displayed in the top window
641 * and the diff view in the bottom window. You can switch between the two
642 * views by pressing 'Tab'. To maximize the log view again, simply press 'l'.
643 **/
644
645struct view;
fe7233c3 646struct view_ops;
c2124ccd
JF
647
648/* The display array of active views and the index of the current view. */
649static struct view *display[2];
650static unsigned int current_view;
651
652#define foreach_view(view, i) \
653 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
654
9f41488f 655#define displayed_views() (display[1] != NULL ? 2 : 1)
c2124ccd
JF
656
657/**
658 * Current head and commit ID
659 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
660 * The viewer keeps track of both what head and commit ID you are currently
661 * viewing. The commit ID will follow the cursor line and change everytime time
662 * you highlight a different commit. Whenever you reopen the diff view it
663 * will be reloaded, if the commit ID changed.
468876c9 664 *
c2124ccd
JF
665 * The head ID is used when opening the main and log view to indicate from
666 * what revision to show history.
468876c9 667 **/
b801d8b2 668
c2124ccd
JF
669static char ref_commit[SIZEOF_REF] = "HEAD";
670static char ref_head[SIZEOF_REF] = "HEAD";
671
b801d8b2 672struct view {
03a93dbb 673 const char *name; /* View name */
4685845e
TH
674 const char *cmd_fmt; /* Default command line format */
675 const char *cmd_env; /* Command line set via environment */
676 const char *id; /* Points to either of ref_{head,commit} */
6b161b31 677
fe7233c3 678 struct view_ops *ops; /* View operations */
22f66b0a 679
03a93dbb 680 char cmd[SIZEOF_CMD]; /* Command buffer */
49f2b43f
JF
681 char ref[SIZEOF_REF]; /* Hovered commit reference */
682 char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */
2e8488b4 683
8855ada4
JF
684 int height, width; /* The width and height of the main window */
685 WINDOW *win; /* The main window */
686 WINDOW *title; /* The title window living below the main window */
b801d8b2
JF
687
688 /* Navigation */
689 unsigned long offset; /* Offset of the window top */
690 unsigned long lineno; /* Current line number */
691
f6da0b66
JF
692 /* If non-NULL, points to the view that opened this view. If this view
693 * is closed tig will switch back to the parent view. */
694 struct view *parent;
695
b801d8b2
JF
696 /* Buffering */
697 unsigned long lines; /* Total number of lines */
fe7233c3 698 struct line *line; /* Line index */
8855ada4 699 unsigned int digits; /* Number of digits in the lines member. */
b801d8b2
JF
700
701 /* Loading */
702 FILE *pipe;
2e8488b4 703 time_t start_time;
b801d8b2
JF
704};
705
fe7233c3
JF
706struct view_ops {
707 /* What type of content being displayed. Used in the title bar. */
708 const char *type;
709 /* Draw one line; @lineno must be < view->height. */
710 bool (*draw)(struct view *view, struct line *line, unsigned int lineno);
711 /* Read one line; updates view->line. */
712 bool (*read)(struct view *view, struct line *prev, char *data);
713 /* Depending on view, change display based on current line. */
714 bool (*enter)(struct view *view, struct line *line);
715};
716
6b161b31
JF
717static struct view_ops pager_ops;
718static struct view_ops main_ops;
a28bcc22 719
95d7ddcd
JF
720#define VIEW_STR(name, cmd, env, ref, ops) \
721 { name, cmd, #env, ref, ops }
1ba2ae4b 722
95d7ddcd
JF
723#define VIEW_(id, name, ops, ref) \
724 VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, ops)
1ba2ae4b 725
c2124ccd
JF
726/**
727 * Views
728 * ~~~~~
729 * tig(1) presents various 'views' of a repository. Each view is based on output
730 * from an external command, most often 'git log', 'git diff', or 'git show'.
731 *
732 * The main view::
733 * Is the default view, and it shows a one line summary of each commit
734 * in the chosen list of revisions. The summary includes commit date,
735 * author, and the first line of the log message. Additionally, any
736 * repository references, such as tags, will be shown.
737 *
738 * The log view::
739 * Presents a more rich view of the revision log showing the whole log
740 * message and the diffstat.
741 *
742 * The diff view::
743 * Shows either the diff of the current working tree, that is, what
744 * has changed since the last commit, or the commit diff complete
745 * with log message, diffstat and diff.
746 *
747 * The pager view::
748 * Is used for displaying both input from stdin and output from git
749 * commands entered in the internal prompt.
750 *
751 * The help view::
752 * Displays the information from the tig(1) man page. For the help view
753 * to work you need to have the tig(1) man page installed.
754 **/
755
b801d8b2 756static struct view views[] = {
95d7ddcd
JF
757 VIEW_(MAIN, "main", &main_ops, ref_head),
758 VIEW_(DIFF, "diff", &pager_ops, ref_commit),
759 VIEW_(LOG, "log", &pager_ops, ref_head),
760 VIEW_(HELP, "help", &pager_ops, "static"),
761 VIEW_(PAGER, "pager", &pager_ops, "static"),
b801d8b2
JF
762};
763
a28bcc22
JF
764#define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
765
4c6fabc2 766
fe7233c3
JF
767static bool
768draw_view_line(struct view *view, unsigned int lineno)
769{
770 if (view->offset + lineno >= view->lines)
771 return FALSE;
772
773 return view->ops->draw(view, &view->line[view->offset + lineno], lineno);
774}
775
b801d8b2 776static void
82e78006 777redraw_view_from(struct view *view, int lineno)
b801d8b2 778{
82e78006 779 assert(0 <= lineno && lineno < view->height);
b801d8b2 780
82e78006 781 for (; lineno < view->height; lineno++) {
fe7233c3 782 if (!draw_view_line(view, lineno))
fd85fef1 783 break;
b801d8b2
JF
784 }
785
786 redrawwin(view->win);
787 wrefresh(view->win);
788}
789
b76c2afc 790static void
82e78006
JF
791redraw_view(struct view *view)
792{
793 wclear(view->win);
794 redraw_view_from(view, 0);
795}
796
c2124ccd
JF
797
798/**
799 * Title windows
800 * ~~~~~~~~~~~~~
801 * Each view has a title window which shows the name of the view, current
802 * commit ID if available, and where the view is positioned:
803 *
804 * [main] c622eefaa485995320bc743431bae0d497b1d875 - commit 1 of 61 (1%)
805 *
806 * By default, the title of the current view is highlighted using bold font.
807 **/
808
6b161b31 809static void
81030ec8
JF
810update_view_title(struct view *view)
811{
812 if (view == display[current_view])
813 wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS));
814 else
815 wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR));
816
817 werase(view->title);
818 wmove(view->title, 0, 0);
819
81030ec8
JF
820 if (*view->ref)
821 wprintw(view->title, "[%s] %s", view->name, view->ref);
822 else
823 wprintw(view->title, "[%s]", view->name);
824
c19f8017
JF
825 if (view->lines || view->pipe) {
826 unsigned int lines = view->lines
827 ? (view->lineno + 1) * 100 / view->lines
828 : 0;
829
81030ec8
JF
830 wprintw(view->title, " - %s %d of %d (%d%%)",
831 view->ops->type,
832 view->lineno + 1,
833 view->lines,
c19f8017 834 lines);
81030ec8
JF
835 }
836
f97f4012
JF
837 if (view->pipe) {
838 time_t secs = time(NULL) - view->start_time;
839
840 /* Three git seconds are a long time ... */
841 if (secs > 2)
842 wprintw(view->title, " %lds", secs);
843 }
844
845
81030ec8
JF
846 wrefresh(view->title);
847}
848
849static void
6b161b31 850resize_display(void)
b76c2afc 851{
03a93dbb 852 int offset, i;
6b161b31
JF
853 struct view *base = display[0];
854 struct view *view = display[1] ? display[1] : display[0];
b76c2afc 855
6b161b31 856 /* Setup window dimensions */
b76c2afc 857
03a93dbb 858 getmaxyx(stdscr, base->height, base->width);
b76c2afc 859
6b161b31 860 /* Make room for the status window. */
03a93dbb 861 base->height -= 1;
6b161b31
JF
862
863 if (view != base) {
03a93dbb
JF
864 /* Horizontal split. */
865 view->width = base->width;
6b161b31
JF
866 view->height = SCALE_SPLIT_VIEW(base->height);
867 base->height -= view->height;
868
869 /* Make room for the title bar. */
870 view->height -= 1;
871 }
872
873 /* Make room for the title bar. */
874 base->height -= 1;
875
876 offset = 0;
877
878 foreach_view (view, i) {
b76c2afc 879 if (!view->win) {
c19f8017 880 view->win = newwin(view->height, 0, offset, 0);
6b161b31
JF
881 if (!view->win)
882 die("Failed to create %s view", view->name);
883
884 scrollok(view->win, TRUE);
885
886 view->title = newwin(1, 0, offset + view->height, 0);
887 if (!view->title)
888 die("Failed to create title window");
889
890 } else {
c19f8017 891 wresize(view->win, view->height, view->width);
6b161b31
JF
892 mvwin(view->win, offset, 0);
893 mvwin(view->title, offset + view->height, 0);
894 wrefresh(view->win);
a28bcc22 895 }
a28bcc22 896
6b161b31 897 offset += view->height + 1;
b76c2afc 898 }
6b161b31 899}
b76c2afc 900
6b161b31 901static void
20bb5e18
JF
902redraw_display(void)
903{
904 struct view *view;
905 int i;
906
907 foreach_view (view, i) {
908 redraw_view(view);
909 update_view_title(view);
910 }
911}
912
913
2e8488b4
JF
914/*
915 * Navigation
916 */
917
4a2909a7 918/* Scrolling backend */
b801d8b2 919static void
b3a54cba 920do_scroll_view(struct view *view, int lines, bool redraw)
b801d8b2 921{
fd85fef1
JF
922 /* The rendering expects the new offset. */
923 view->offset += lines;
924
925 assert(0 <= view->offset && view->offset < view->lines);
926 assert(lines);
b801d8b2 927
82e78006 928 /* Redraw the whole screen if scrolling is pointless. */
4c6fabc2 929 if (view->height < ABS(lines)) {
b76c2afc
JF
930 redraw_view(view);
931
932 } else {
22f66b0a 933 int line = lines > 0 ? view->height - lines : 0;
82e78006 934 int end = line + ABS(lines);
fd85fef1
JF
935
936 wscrl(view->win, lines);
937
22f66b0a 938 for (; line < end; line++) {
fe7233c3 939 if (!draw_view_line(view, line))
fd85fef1
JF
940 break;
941 }
942 }
943
944 /* Move current line into the view. */
945 if (view->lineno < view->offset) {
946 view->lineno = view->offset;
fe7233c3 947 draw_view_line(view, 0);
fd85fef1
JF
948
949 } else if (view->lineno >= view->offset + view->height) {
6706b2ba
JF
950 if (view->lineno == view->offset + view->height) {
951 /* Clear the hidden line so it doesn't show if the view
952 * is scrolled up. */
953 wmove(view->win, view->height, 0);
954 wclrtoeol(view->win);
955 }
fd85fef1 956 view->lineno = view->offset + view->height - 1;
fe7233c3 957 draw_view_line(view, view->lineno - view->offset);
fd85fef1
JF
958 }
959
4c6fabc2 960 assert(view->offset <= view->lineno && view->lineno < view->lines);
fd85fef1 961
b3a54cba
JF
962 if (!redraw)
963 return;
964
fd85fef1
JF
965 redrawwin(view->win);
966 wrefresh(view->win);
9d3f5834 967 report("");
fd85fef1 968}
78c70acd 969
4a2909a7 970/* Scroll frontend */
fd85fef1 971static void
6b161b31 972scroll_view(struct view *view, enum request request)
fd85fef1
JF
973{
974 int lines = 1;
b801d8b2
JF
975
976 switch (request) {
4a2909a7 977 case REQ_SCROLL_PAGE_DOWN:
fd85fef1 978 lines = view->height;
4a2909a7 979 case REQ_SCROLL_LINE_DOWN:
b801d8b2 980 if (view->offset + lines > view->lines)
bde3653a 981 lines = view->lines - view->offset;
b801d8b2 982
fd85fef1 983 if (lines == 0 || view->offset + view->height >= view->lines) {
eb98559e 984 report("Cannot scroll beyond the last line");
b801d8b2
JF
985 return;
986 }
987 break;
988
4a2909a7 989 case REQ_SCROLL_PAGE_UP:
fd85fef1 990 lines = view->height;
4a2909a7 991 case REQ_SCROLL_LINE_UP:
b801d8b2
JF
992 if (lines > view->offset)
993 lines = view->offset;
994
995 if (lines == 0) {
eb98559e 996 report("Cannot scroll beyond the first line");
b801d8b2
JF
997 return;
998 }
999
fd85fef1 1000 lines = -lines;
b801d8b2 1001 break;
03a93dbb 1002
6b161b31
JF
1003 default:
1004 die("request %d not handled in switch", request);
b801d8b2
JF
1005 }
1006
b3a54cba 1007 do_scroll_view(view, lines, TRUE);
fd85fef1 1008}
b801d8b2 1009
4a2909a7 1010/* Cursor moving */
fd85fef1 1011static void
b3a54cba 1012move_view(struct view *view, enum request request, bool redraw)
fd85fef1
JF
1013{
1014 int steps;
b801d8b2 1015
fd85fef1 1016 switch (request) {
4a2909a7 1017 case REQ_MOVE_FIRST_LINE:
78c70acd
JF
1018 steps = -view->lineno;
1019 break;
1020
4a2909a7 1021 case REQ_MOVE_LAST_LINE:
78c70acd
JF
1022 steps = view->lines - view->lineno - 1;
1023 break;
1024
4a2909a7 1025 case REQ_MOVE_PAGE_UP:
78c70acd
JF
1026 steps = view->height > view->lineno
1027 ? -view->lineno : -view->height;
1028 break;
1029
4a2909a7 1030 case REQ_MOVE_PAGE_DOWN:
78c70acd
JF
1031 steps = view->lineno + view->height >= view->lines
1032 ? view->lines - view->lineno - 1 : view->height;
1033 break;
1034
4a2909a7 1035 case REQ_MOVE_UP:
fd85fef1
JF
1036 steps = -1;
1037 break;
b801d8b2 1038
4a2909a7 1039 case REQ_MOVE_DOWN:
fd85fef1
JF
1040 steps = 1;
1041 break;
6b161b31
JF
1042
1043 default:
1044 die("request %d not handled in switch", request);
78c70acd 1045 }
b801d8b2 1046
4c6fabc2 1047 if (steps <= 0 && view->lineno == 0) {
eb98559e 1048 report("Cannot move beyond the first line");
78c70acd 1049 return;
b801d8b2 1050
6908bdbd 1051 } else if (steps >= 0 && view->lineno + 1 >= view->lines) {
eb98559e 1052 report("Cannot move beyond the last line");
78c70acd 1053 return;
fd85fef1
JF
1054 }
1055
4c6fabc2 1056 /* Move the current line */
fd85fef1 1057 view->lineno += steps;
4c6fabc2
JF
1058 assert(0 <= view->lineno && view->lineno < view->lines);
1059
1060 /* Repaint the old "current" line if we be scrolling */
2e8488b4
JF
1061 if (ABS(steps) < view->height) {
1062 int prev_lineno = view->lineno - steps - view->offset;
1063
1064 wmove(view->win, prev_lineno, 0);
1065 wclrtoeol(view->win);
fe7233c3 1066 draw_view_line(view, prev_lineno);
2e8488b4 1067 }
fd85fef1 1068
4c6fabc2 1069 /* Check whether the view needs to be scrolled */
fd85fef1
JF
1070 if (view->lineno < view->offset ||
1071 view->lineno >= view->offset + view->height) {
1072 if (steps < 0 && -steps > view->offset) {
1073 steps = -view->offset;
b76c2afc
JF
1074
1075 } else if (steps > 0) {
1076 if (view->lineno == view->lines - 1 &&
1077 view->lines > view->height) {
1078 steps = view->lines - view->offset - 1;
1079 if (steps >= view->height)
1080 steps -= view->height - 1;
1081 }
b801d8b2 1082 }
78c70acd 1083
b3a54cba 1084 do_scroll_view(view, steps, redraw);
fd85fef1 1085 return;
b801d8b2
JF
1086 }
1087
4c6fabc2 1088 /* Draw the current line */
fe7233c3 1089 draw_view_line(view, view->lineno - view->offset);
fd85fef1 1090
b3a54cba
JF
1091 if (!redraw)
1092 return;
1093
b801d8b2
JF
1094 redrawwin(view->win);
1095 wrefresh(view->win);
9d3f5834 1096 report("");
b801d8b2
JF
1097}
1098
b801d8b2 1099
2e8488b4
JF
1100/*
1101 * Incremental updating
1102 */
b801d8b2 1103
199d1288
JF
1104static void
1105end_update(struct view *view)
1106{
1107 if (!view->pipe)
1108 return;
1109 set_nonblocking_input(FALSE);
1110 if (view->pipe == stdin)
1111 fclose(view->pipe);
1112 else
1113 pclose(view->pipe);
1114 view->pipe = NULL;
1115}
1116
03a93dbb 1117static bool
b801d8b2
JF
1118begin_update(struct view *view)
1119{
4685845e 1120 const char *id = view->id;
fd85fef1 1121
199d1288
JF
1122 if (view->pipe)
1123 end_update(view);
1124
03a93dbb
JF
1125 if (opt_cmd[0]) {
1126 string_copy(view->cmd, opt_cmd);
1127 opt_cmd[0] = 0;
8855ada4
JF
1128 /* When running random commands, the view ref could have become
1129 * invalid so clear it. */
1130 view->ref[0] = 0;
03a93dbb 1131 } else {
4685845e 1132 const char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt;
1ba2ae4b
JF
1133
1134 if (snprintf(view->cmd, sizeof(view->cmd), format,
1135 id, id, id, id, id) >= sizeof(view->cmd))
03a93dbb
JF
1136 return FALSE;
1137 }
b801d8b2 1138
6908bdbd
JF
1139 /* Special case for the pager view. */
1140 if (opt_pipe) {
1141 view->pipe = opt_pipe;
1142 opt_pipe = NULL;
1143 } else {
1144 view->pipe = popen(view->cmd, "r");
1145 }
1146
2e8488b4
JF
1147 if (!view->pipe)
1148 return FALSE;
b801d8b2 1149
6b161b31 1150 set_nonblocking_input(TRUE);
b801d8b2
JF
1151
1152 view->offset = 0;
1153 view->lines = 0;
1154 view->lineno = 0;
49f2b43f 1155 string_copy(view->vid, id);
b801d8b2 1156
2e8488b4
JF
1157 if (view->line) {
1158 int i;
1159
1160 for (i = 0; i < view->lines; i++)
fe7233c3
JF
1161 if (view->line[i].data)
1162 free(view->line[i].data);
2e8488b4
JF
1163
1164 free(view->line);
1165 view->line = NULL;
1166 }
1167
1168 view->start_time = time(NULL);
1169
b801d8b2
JF
1170 return TRUE;
1171}
1172
03a93dbb 1173static bool
b801d8b2
JF
1174update_view(struct view *view)
1175{
1176 char buffer[BUFSIZ];
1177 char *line;
fe7233c3 1178 struct line *tmp;
82e78006
JF
1179 /* The number of lines to read. If too low it will cause too much
1180 * redrawing (and possible flickering), if too high responsiveness
1181 * will suffer. */
8855ada4 1182 unsigned long lines = view->height;
82e78006 1183 int redraw_from = -1;
b801d8b2
JF
1184
1185 if (!view->pipe)
1186 return TRUE;
1187
82e78006
JF
1188 /* Only redraw if lines are visible. */
1189 if (view->offset + view->height >= view->lines)
1190 redraw_from = view->lines - view->offset;
b801d8b2
JF
1191
1192 tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines));
1193 if (!tmp)
1194 goto alloc_error;
1195
1196 view->line = tmp;
1197
1198 while ((line = fgets(buffer, sizeof(buffer), view->pipe))) {
c34d9c9f 1199 int linelen = strlen(line);
b801d8b2 1200
fe7233c3
JF
1201 struct line *prev = view->lines
1202 ? &view->line[view->lines - 1]
1203 : NULL;
1204
b801d8b2
JF
1205 if (linelen)
1206 line[linelen - 1] = 0;
1207
fe7233c3 1208 if (!view->ops->read(view, prev, line))
b801d8b2 1209 goto alloc_error;
fd85fef1
JF
1210
1211 if (lines-- == 1)
1212 break;
b801d8b2
JF
1213 }
1214
8855ada4
JF
1215 {
1216 int digits;
1217
1218 lines = view->lines;
1219 for (digits = 0; lines; digits++)
1220 lines /= 10;
1221
1222 /* Keep the displayed view in sync with line number scaling. */
1223 if (digits != view->digits) {
1224 view->digits = digits;
1225 redraw_from = 0;
1226 }
1227 }
1228
82e78006
JF
1229 if (redraw_from >= 0) {
1230 /* If this is an incremental update, redraw the previous line
a28bcc22
JF
1231 * since for commits some members could have changed when
1232 * loading the main view. */
82e78006
JF
1233 if (redraw_from > 0)
1234 redraw_from--;
1235
1236 /* Incrementally draw avoids flickering. */
1237 redraw_view_from(view, redraw_from);
4c6fabc2 1238 }
b801d8b2 1239
eb98559e
JF
1240 /* Update the title _after_ the redraw so that if the redraw picks up a
1241 * commit reference in view->ref it'll be available here. */
1242 update_view_title(view);
1243
b801d8b2 1244 if (ferror(view->pipe)) {
03a93dbb 1245 report("Failed to read: %s", strerror(errno));
b801d8b2
JF
1246 goto end;
1247
1248 } else if (feof(view->pipe)) {
a28bcc22 1249 if (view == VIEW(REQ_VIEW_HELP)) {
4685845e 1250 const char *msg = TIG_HELP;
468876c9
JF
1251
1252 if (view->lines == 0) {
1253 /* Slightly ugly, but abusing view->ref keeps
1254 * the error message. */
1255 string_copy(view->ref, "No help available");
1256 msg = "The tig(1) manpage is not installed";
1257 }
1258
1259 report("%s", msg);
2e8488b4
JF
1260 goto end;
1261 }
1262
f97f4012 1263 report("");
b801d8b2
JF
1264 goto end;
1265 }
1266
1267 return TRUE;
1268
1269alloc_error:
2e8488b4 1270 report("Allocation failure");
b801d8b2
JF
1271
1272end:
1273 end_update(view);
1274 return FALSE;
1275}
1276
49f2b43f
JF
1277enum open_flags {
1278 OPEN_DEFAULT = 0, /* Use default view switching. */
1279 OPEN_SPLIT = 1, /* Split current view. */
1280 OPEN_BACKGROUNDED = 2, /* Backgrounded. */
1281 OPEN_RELOAD = 4, /* Reload view even if it is the current. */
1282};
1283
6b161b31 1284static void
49f2b43f 1285open_view(struct view *prev, enum request request, enum open_flags flags)
b801d8b2 1286{
49f2b43f
JF
1287 bool backgrounded = !!(flags & OPEN_BACKGROUNDED);
1288 bool split = !!(flags & OPEN_SPLIT);
1289 bool reload = !!(flags & OPEN_RELOAD);
a28bcc22 1290 struct view *view = VIEW(request);
9f41488f 1291 int nviews = displayed_views();
6e950a52 1292 struct view *base_view = display[0];
b801d8b2 1293
49f2b43f 1294 if (view == prev && nviews == 1 && !reload) {
6b161b31
JF
1295 report("Already in %s view", view->name);
1296 return;
1297 }
b801d8b2 1298
8855ada4 1299 if ((reload || strcmp(view->vid, view->id)) &&
03a93dbb 1300 !begin_update(view)) {
6b161b31
JF
1301 report("Failed to load %s view", view->name);
1302 return;
1303 }
a28bcc22 1304
6b161b31
JF
1305 if (split) {
1306 display[current_view + 1] = view;
1307 if (!backgrounded)
a28bcc22 1308 current_view++;
6b161b31
JF
1309 } else {
1310 /* Maximize the current view. */
1311 memset(display, 0, sizeof(display));
1312 current_view = 0;
1313 display[current_view] = view;
a28bcc22 1314 }
b801d8b2 1315
6e950a52
JF
1316 /* Resize the view when switching between split- and full-screen,
1317 * or when switching between two different full-screen views. */
1318 if (nviews != displayed_views() ||
1319 (nviews == 1 && base_view != display[0]))
a006db63 1320 resize_display();
b801d8b2 1321
a8891802 1322 if (split && prev->lineno - prev->offset >= prev->height) {
03a93dbb 1323 /* Take the title line into account. */
eb98559e 1324 int lines = prev->lineno - prev->offset - prev->height + 1;
03a93dbb
JF
1325
1326 /* Scroll the view that was split if the current line is
1327 * outside the new limited view. */
b3a54cba 1328 do_scroll_view(prev, lines, TRUE);
03a93dbb
JF
1329 }
1330
6b161b31 1331 if (prev && view != prev) {
6b161b31 1332 /* Continue loading split views in the background. */
03a93dbb 1333 if (!split)
6b161b31 1334 end_update(prev);
f0b3ab80
JF
1335 else if (!backgrounded)
1336 /* "Blur" the previous view. */
1337 update_view_title(prev);
1338
f6da0b66 1339 view->parent = prev;
b801d8b2
JF
1340 }
1341
03a93dbb
JF
1342 if (view->pipe) {
1343 /* Clear the old view and let the incremental updating refill
1344 * the screen. */
1345 wclear(view->win);
f97f4012 1346 report("");
03a93dbb
JF
1347 } else {
1348 redraw_view(view);
468876c9
JF
1349 if (view == VIEW(REQ_VIEW_HELP))
1350 report("%s", TIG_HELP);
1351 else
1352 report("");
03a93dbb 1353 }
6706b2ba
JF
1354
1355 /* If the view is backgrounded the above calls to report()
1356 * won't redraw the view title. */
1357 if (backgrounded)
1358 update_view_title(view);
b801d8b2
JF
1359}
1360
1361
6b161b31
JF
1362/*
1363 * User request switch noodle
1364 */
1365
b801d8b2 1366static int
6b161b31 1367view_driver(struct view *view, enum request request)
b801d8b2 1368{
b801d8b2
JF
1369 int i;
1370
1371 switch (request) {
4a2909a7
JF
1372 case REQ_MOVE_UP:
1373 case REQ_MOVE_DOWN:
1374 case REQ_MOVE_PAGE_UP:
1375 case REQ_MOVE_PAGE_DOWN:
1376 case REQ_MOVE_FIRST_LINE:
1377 case REQ_MOVE_LAST_LINE:
b3a54cba 1378 move_view(view, request, TRUE);
fd85fef1
JF
1379 break;
1380
4a2909a7
JF
1381 case REQ_SCROLL_LINE_DOWN:
1382 case REQ_SCROLL_LINE_UP:
1383 case REQ_SCROLL_PAGE_DOWN:
1384 case REQ_SCROLL_PAGE_UP:
a28bcc22 1385 scroll_view(view, request);
b801d8b2
JF
1386 break;
1387
4a2909a7 1388 case REQ_VIEW_MAIN:
4a2909a7 1389 case REQ_VIEW_DIFF:
2e8488b4
JF
1390 case REQ_VIEW_LOG:
1391 case REQ_VIEW_HELP:
6908bdbd 1392 case REQ_VIEW_PAGER:
49f2b43f 1393 open_view(view, request, OPEN_DEFAULT);
b801d8b2
JF
1394 break;
1395
b3a54cba
JF
1396 case REQ_NEXT:
1397 case REQ_PREVIOUS:
1398 request = request == REQ_NEXT ? REQ_MOVE_DOWN : REQ_MOVE_UP;
1399
1400 if (view == VIEW(REQ_VIEW_DIFF) &&
1401 view->parent == VIEW(REQ_VIEW_MAIN)) {
f0b3ab80 1402 bool redraw = display[1] == view;
b3a54cba
JF
1403
1404 view = view->parent;
1405 move_view(view, request, redraw);
f0b3ab80
JF
1406 if (redraw)
1407 update_view_title(view);
b3a54cba
JF
1408 } else {
1409 move_view(view, request, TRUE);
1410 break;
1411 }
6706b2ba
JF
1412 /* Fall-through */
1413
6b161b31 1414 case REQ_ENTER:
6908bdbd
JF
1415 if (!view->lines) {
1416 report("Nothing to enter");
1417 break;
1418 }
fe7233c3 1419 return view->ops->enter(view, &view->line[view->lineno]);
6b161b31 1420
03a93dbb
JF
1421 case REQ_VIEW_NEXT:
1422 {
9f41488f 1423 int nviews = displayed_views();
03a93dbb
JF
1424 int next_view = (current_view + 1) % nviews;
1425
1426 if (next_view == current_view) {
1427 report("Only one view is displayed");
1428 break;
1429 }
1430
1431 current_view = next_view;
1432 /* Blur out the title of the previous view. */
1433 update_view_title(view);
6734f6b9 1434 report("");
03a93dbb
JF
1435 break;
1436 }
4a2909a7 1437 case REQ_TOGGLE_LINE_NUMBERS:
b76c2afc 1438 opt_line_number = !opt_line_number;
20bb5e18 1439 redraw_display();
b801d8b2
JF
1440 break;
1441
03a93dbb 1442 case REQ_PROMPT:
8855ada4 1443 /* Always reload^Wrerun commands from the prompt. */
49f2b43f 1444 open_view(view, opt_request, OPEN_RELOAD);
03a93dbb
JF
1445 break;
1446
4a2909a7 1447 case REQ_STOP_LOADING:
03a93dbb 1448 foreach_view (view, i) {
2e8488b4 1449 if (view->pipe)
6706b2ba 1450 report("Stopped loaded the %s view", view->name),
03a93dbb
JF
1451 end_update(view);
1452 }
b801d8b2
JF
1453 break;
1454
4a2909a7 1455 case REQ_SHOW_VERSION:
6cb291b7 1456 report("%s (built %s)", VERSION, __DATE__);
b801d8b2
JF
1457 return TRUE;
1458
fac7db6c
JF
1459 case REQ_SCREEN_RESIZE:
1460 resize_display();
1461 /* Fall-through */
4a2909a7 1462 case REQ_SCREEN_REDRAW:
20bb5e18 1463 redraw_display();
4a2909a7
JF
1464 break;
1465
1466 case REQ_SCREEN_UPDATE:
b801d8b2
JF
1467 doupdate();
1468 return TRUE;
1469
4f9b667a 1470 case REQ_VIEW_CLOSE:
f6da0b66 1471 if (view->parent) {
4f9b667a
JF
1472 memset(display, 0, sizeof(display));
1473 current_view = 0;
f6da0b66
JF
1474 display[current_view] = view->parent;
1475 view->parent = NULL;
4f9b667a
JF
1476 resize_display();
1477 redraw_display();
1478 break;
1479 }
1480 /* Fall-through */
b801d8b2
JF
1481 case REQ_QUIT:
1482 return FALSE;
1483
1484 default:
2e8488b4 1485 /* An unknown key will show most commonly used commands. */
468876c9 1486 report("Unknown key, press 'h' for help");
b801d8b2
JF
1487 return TRUE;
1488 }
1489
1490 return TRUE;
1491}
1492
1493
1494/*
ff26aa29 1495 * Pager backend
b801d8b2
JF
1496 */
1497
6b161b31 1498static bool
fe7233c3 1499pager_draw(struct view *view, struct line *line, unsigned int lineno)
b801d8b2 1500{
fe7233c3
JF
1501 char *text = line->data;
1502 enum line_type type = line->type;
1503 int textlen = strlen(text);
78c70acd 1504 int attr;
b801d8b2 1505
6706b2ba
JF
1506 wmove(view->win, lineno, 0);
1507
fd85fef1 1508 if (view->offset + lineno == view->lineno) {
8855ada4 1509 if (type == LINE_COMMIT) {
fe7233c3 1510 string_copy(view->ref, text + 7);
03a93dbb
JF
1511 string_copy(ref_commit, view->ref);
1512 }
8855ada4 1513
78c70acd 1514 type = LINE_CURSOR;
6706b2ba 1515 wchgat(view->win, -1, 0, type, NULL);
fd85fef1
JF
1516 }
1517
78c70acd 1518 attr = get_line_attr(type);
b801d8b2 1519 wattrset(view->win, attr);
b76c2afc 1520
6706b2ba
JF
1521 if (opt_line_number || opt_tab_size < TABSIZE) {
1522 static char spaces[] = " ";
1523 int col_offset = 0, col = 0;
1524
1525 if (opt_line_number) {
1526 unsigned long real_lineno = view->offset + lineno + 1;
82e78006 1527
6706b2ba
JF
1528 if (real_lineno == 1 ||
1529 (real_lineno % opt_num_interval) == 0) {
1530 wprintw(view->win, "%.*d", view->digits, real_lineno);
8855ada4 1531
6706b2ba
JF
1532 } else {
1533 waddnstr(view->win, spaces,
1534 MIN(view->digits, STRING_SIZE(spaces)));
1535 }
1536 waddstr(view->win, ": ");
1537 col_offset = view->digits + 2;
1538 }
8855ada4 1539
fe7233c3 1540 while (text && col_offset + col < view->width) {
6706b2ba 1541 int cols_max = view->width - col_offset - col;
fe7233c3 1542 char *pos = text;
6706b2ba 1543 int cols;
4c6fabc2 1544
fe7233c3
JF
1545 if (*text == '\t') {
1546 text++;
6706b2ba 1547 assert(sizeof(spaces) > TABSIZE);
fe7233c3 1548 pos = spaces;
6706b2ba 1549 cols = opt_tab_size - (col % opt_tab_size);
82e78006 1550
b76c2afc 1551 } else {
fe7233c3
JF
1552 text = strchr(text, '\t');
1553 cols = line ? text - pos : strlen(pos);
b76c2afc 1554 }
6706b2ba 1555
fe7233c3 1556 waddnstr(view->win, pos, MIN(cols, cols_max));
6706b2ba 1557 col += cols;
b76c2afc 1558 }
b76c2afc
JF
1559
1560 } else {
6706b2ba 1561 int col = 0, pos = 0;
b801d8b2 1562
fe7233c3
JF
1563 for (; pos < textlen && col < view->width; pos++, col++)
1564 if (text[pos] == '\t')
6706b2ba
JF
1565 col += TABSIZE - (col % TABSIZE) - 1;
1566
fe7233c3 1567 waddnstr(view->win, text, pos);
6706b2ba 1568 }
2e8488b4 1569
b801d8b2
JF
1570 return TRUE;
1571}
1572
6b161b31 1573static bool
fe7233c3 1574pager_read(struct view *view, struct line *prev, char *line)
22f66b0a 1575{
6706b2ba
JF
1576 /* Compress empty lines in the help view. */
1577 if (view == VIEW(REQ_VIEW_HELP) &&
fe7233c3 1578 !*line && prev && !*((char *) prev->data))
6706b2ba
JF
1579 return TRUE;
1580
fe7233c3
JF
1581 view->line[view->lines].data = strdup(line);
1582 if (!view->line[view->lines].data)
22f66b0a
JF
1583 return FALSE;
1584
fe7233c3
JF
1585 view->line[view->lines].type = get_line_type(line);
1586
22f66b0a
JF
1587 view->lines++;
1588 return TRUE;
1589}
1590
6b161b31 1591static bool
fe7233c3 1592pager_enter(struct view *view, struct line *line)
6b161b31 1593{
91e8e277 1594 int split = 0;
6b161b31 1595
9fbbd28f
JF
1596 if (line->type == LINE_COMMIT &&
1597 (view == VIEW(REQ_VIEW_LOG) ||
1598 view == VIEW(REQ_VIEW_PAGER))) {
91e8e277
JF
1599 open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT);
1600 split = 1;
67e48ac5
JF
1601 }
1602
91e8e277
JF
1603 /* Always scroll the view even if it was split. That way
1604 * you can use Enter to scroll through the log view and
1605 * split open each commit diff. */
1606 scroll_view(view, REQ_SCROLL_LINE_DOWN);
1607
1608 /* FIXME: A minor workaround. Scrolling the view will call report("")
1609 * but if we are scolling a non-current view this won't properly update
1610 * the view title. */
1611 if (split)
1612 update_view_title(view);
6b161b31
JF
1613
1614 return TRUE;
1615}
1616
6b161b31 1617static struct view_ops pager_ops = {
6734f6b9 1618 "line",
6b161b31
JF
1619 pager_draw,
1620 pager_read,
1621 pager_enter,
1622};
1623
80ce96ea 1624
ff26aa29
JF
1625/*
1626 * Main view backend
1627 */
1628
1629struct commit {
1630 char id[41]; /* SHA1 ID. */
1631 char title[75]; /* The first line of the commit message. */
1632 char author[75]; /* The author of the commit. */
1633 struct tm time; /* Date from the author ident. */
1634 struct ref **refs; /* Repository references; tags & branch heads. */
1635};
c34d9c9f 1636
6b161b31 1637static bool
fe7233c3 1638main_draw(struct view *view, struct line *line, unsigned int lineno)
22f66b0a 1639{
2e8488b4 1640 char buf[DATE_COLS + 1];
fe7233c3 1641 struct commit *commit = line->data;
78c70acd 1642 enum line_type type;
6706b2ba 1643 int col = 0;
b76c2afc 1644 size_t timelen;
10e290ee
JF
1645 size_t authorlen;
1646 int trimmed;
22f66b0a 1647
4c6fabc2
JF
1648 if (!*commit->author)
1649 return FALSE;
22f66b0a 1650
6706b2ba
JF
1651 wmove(view->win, lineno, col);
1652
22f66b0a 1653 if (view->offset + lineno == view->lineno) {
03a93dbb 1654 string_copy(view->ref, commit->id);
49f2b43f 1655 string_copy(ref_commit, view->ref);
78c70acd 1656 type = LINE_CURSOR;
6706b2ba
JF
1657 wattrset(view->win, get_line_attr(type));
1658 wchgat(view->win, -1, 0, type, NULL);
1659
78c70acd 1660 } else {
b76c2afc 1661 type = LINE_MAIN_COMMIT;
6706b2ba 1662 wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
b76c2afc
JF
1663 }
1664
4c6fabc2 1665 timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time);
b76c2afc 1666 waddnstr(view->win, buf, timelen);
4c6fabc2 1667 waddstr(view->win, " ");
b76c2afc 1668
6706b2ba
JF
1669 col += DATE_COLS;
1670 wmove(view->win, lineno, col);
1671 if (type != LINE_CURSOR)
1672 wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
b76c2afc 1673
10e290ee
JF
1674 /* FIXME: Make this optional, and add i18n.commitEncoding support. */
1675 authorlen = utf8_length(commit->author, AUTHOR_COLS - 2, &col, &trimmed);
1676
1677 if (trimmed) {
1678 waddnstr(view->win, commit->author, authorlen);
6706b2ba
JF
1679 if (type != LINE_CURSOR)
1680 wattrset(view->win, get_line_attr(LINE_MAIN_DELIM));
b76c2afc
JF
1681 waddch(view->win, '~');
1682 } else {
1683 waddstr(view->win, commit->author);
22f66b0a
JF
1684 }
1685
10e290ee 1686 col += AUTHOR_COLS;
6706b2ba
JF
1687 if (type != LINE_CURSOR)
1688 wattrset(view->win, A_NORMAL);
1689
1690 mvwaddch(view->win, lineno, col, ACS_LTEE);
1691 wmove(view->win, lineno, col + 2);
1692 col += 2;
c34d9c9f
JF
1693
1694 if (commit->refs) {
1695 size_t i = 0;
1696
1697 do {
6706b2ba
JF
1698 if (type == LINE_CURSOR)
1699 ;
1700 else if (commit->refs[i]->tag)
c34d9c9f
JF
1701 wattrset(view->win, get_line_attr(LINE_MAIN_TAG));
1702 else
1703 wattrset(view->win, get_line_attr(LINE_MAIN_REF));
1704 waddstr(view->win, "[");
1705 waddstr(view->win, commit->refs[i]->name);
1706 waddstr(view->win, "]");
6706b2ba
JF
1707 if (type != LINE_CURSOR)
1708 wattrset(view->win, A_NORMAL);
c34d9c9f 1709 waddstr(view->win, " ");
6706b2ba 1710 col += strlen(commit->refs[i]->name) + STRING_SIZE("[] ");
c34d9c9f
JF
1711 } while (commit->refs[i++]->next);
1712 }
1713
6706b2ba
JF
1714 if (type != LINE_CURSOR)
1715 wattrset(view->win, get_line_attr(type));
1716
1717 {
1718 int titlelen = strlen(commit->title);
1719
1720 if (col + titlelen > view->width)
1721 titlelen = view->width - col;
1722
1723 waddnstr(view->win, commit->title, titlelen);
1724 }
22f66b0a
JF
1725
1726 return TRUE;
1727}
1728
4c6fabc2 1729/* Reads git log --pretty=raw output and parses it into the commit struct. */
6b161b31 1730static bool
fe7233c3 1731main_read(struct view *view, struct line *prev, char *line)
22f66b0a 1732{
78c70acd
JF
1733 enum line_type type = get_line_type(line);
1734 struct commit *commit;
22f66b0a 1735
78c70acd
JF
1736 switch (type) {
1737 case LINE_COMMIT:
22f66b0a
JF
1738 commit = calloc(1, sizeof(struct commit));
1739 if (!commit)
1740 return FALSE;
1741
4c6fabc2 1742 line += STRING_SIZE("commit ");
b76c2afc 1743
fe7233c3 1744 view->line[view->lines++].data = commit;
82e78006 1745 string_copy(commit->id, line);
c34d9c9f 1746 commit->refs = get_refs(commit->id);
78c70acd 1747 break;
22f66b0a 1748
8855ada4 1749 case LINE_AUTHOR:
b76c2afc 1750 {
4c6fabc2 1751 char *ident = line + STRING_SIZE("author ");
b76c2afc
JF
1752 char *end = strchr(ident, '<');
1753
fe7233c3
JF
1754 if (!prev)
1755 break;
1756
1757 commit = prev->data;
1758
b76c2afc
JF
1759 if (end) {
1760 for (; end > ident && isspace(end[-1]); end--) ;
1761 *end = 0;
1762 }
1763
82e78006 1764 string_copy(commit->author, ident);
b76c2afc 1765
4c6fabc2 1766 /* Parse epoch and timezone */
b76c2afc
JF
1767 if (end) {
1768 char *secs = strchr(end + 1, '>');
1769 char *zone;
1770 time_t time;
1771
1772 if (!secs || secs[1] != ' ')
1773 break;
1774
1775 secs += 2;
1776 time = (time_t) atol(secs);
1777 zone = strchr(secs, ' ');
4c6fabc2 1778 if (zone && strlen(zone) == STRING_SIZE(" +0700")) {
b76c2afc
JF
1779 long tz;
1780
1781 zone++;
1782 tz = ('0' - zone[1]) * 60 * 60 * 10;
1783 tz += ('0' - zone[2]) * 60 * 60;
1784 tz += ('0' - zone[3]) * 60;
1785 tz += ('0' - zone[4]) * 60;
1786
1787 if (zone[0] == '-')
1788 tz = -tz;
1789
1790 time -= tz;
1791 }
1792 gmtime_r(&time, &commit->time);
1793 }
1794 break;
1795 }
78c70acd 1796 default:
fe7233c3 1797 if (!prev)
2e8488b4
JF
1798 break;
1799
fe7233c3
JF
1800 commit = prev->data;
1801
2e8488b4 1802 /* Fill in the commit title if it has not already been set. */
2e8488b4
JF
1803 if (commit->title[0])
1804 break;
1805
1806 /* Require titles to start with a non-space character at the
1807 * offset used by git log. */
eb98559e
JF
1808 /* FIXME: More gracefull handling of titles; append "..." to
1809 * shortened titles, etc. */
2e8488b4 1810 if (strncmp(line, " ", 4) ||
eb98559e 1811 isspace(line[4]))
82e78006
JF
1812 break;
1813
1814 string_copy(commit->title, line + 4);
22f66b0a
JF
1815 }
1816
1817 return TRUE;
1818}
1819
6b161b31 1820static bool
fe7233c3 1821main_enter(struct view *view, struct line *line)
b801d8b2 1822{
b3a54cba
JF
1823 enum open_flags flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT;
1824
1825 open_view(view, REQ_VIEW_DIFF, flags);
6b161b31 1826 return TRUE;
b801d8b2
JF
1827}
1828
6b161b31 1829static struct view_ops main_ops = {
6734f6b9 1830 "commit",
6b161b31
JF
1831 main_draw,
1832 main_read,
1833 main_enter,
1834};
2e8488b4 1835
c34d9c9f 1836
468876c9
JF
1837/**
1838 * KEYS
1839 * ----
1840 * Below the default key bindings are shown.
1841 **/
1842
1843struct keymap {
1844 int alias;
1845 int request;
1846};
1847
3a91b75e 1848static struct keymap keymap[] = {
468876c9
JF
1849 /**
1850 * View switching
1851 * ~~~~~~~~~~~~~~
1852 * m::
1853 * Switch to main view.
1854 * d::
1855 * Switch to diff view.
1856 * l::
1857 * Switch to log view.
1858 * p::
1859 * Switch to pager view.
1860 * h::
1861 * Show man page.
b3a54cba
JF
1862 **/
1863 { 'm', REQ_VIEW_MAIN },
1864 { 'd', REQ_VIEW_DIFF },
1865 { 'l', REQ_VIEW_LOG },
1866 { 'p', REQ_VIEW_PAGER },
1867 { 'h', REQ_VIEW_HELP },
1868
1869 /**
1870 * View manipulation
1871 * ~~~~~~~~~~~~~~~~~
4f9b667a 1872 * q::
f6da0b66
JF
1873 * Close view, if multiple views are open it will jump back to the
1874 * previous view in the view stack. If it is the last open view it
1875 * will quit. Use 'Q' to quit all views at once.
67e48ac5
JF
1876 * Enter::
1877 * This key is "context sensitive" depending on what view you are
1878 * currently in. When in log view on a commit line or in the main
1879 * view, split the view and show the commit diff. In the diff view
1880 * pressing Enter will simply scroll the view one line down.
468876c9
JF
1881 * Tab::
1882 * Switch to next view.
b3a54cba
JF
1883 * Up::
1884 * This key is "context sensitive" and will move the cursor one
1885 * line up. However, uf you opened a diff view from the main view
1886 * (split- or full-screen) it will change the cursor to point to
1887 * the previous commit in the main view and update the diff view
1888 * to display it.
1889 * Down::
1890 * Similar to 'Up' but will move down.
468876c9 1891 **/
4f9b667a 1892 { 'q', REQ_VIEW_CLOSE },
468876c9
JF
1893 { KEY_TAB, REQ_VIEW_NEXT },
1894 { KEY_RETURN, REQ_ENTER },
b3a54cba
JF
1895 { KEY_UP, REQ_PREVIOUS },
1896 { KEY_DOWN, REQ_NEXT },
468876c9
JF
1897
1898 /**
1899 * Cursor navigation
1900 * ~~~~~~~~~~~~~~~~~
b3a54cba 1901 * j::
57bdf034 1902 * Move cursor one line up.
468876c9 1903 * k::
b3a54cba 1904 * Move cursor one line down.
468876c9 1905 * PgUp::
c622eefa 1906 * b::
0df811cb 1907 * -::
57bdf034 1908 * Move cursor one page up.
468876c9 1909 * PgDown::
c622eefa 1910 * Space::
468876c9
JF
1911 * Move cursor one page down.
1912 * Home::
1913 * Jump to first line.
1914 * End::
1915 * Jump to last line.
1916 **/
b3a54cba
JF
1917 { 'k', REQ_MOVE_UP },
1918 { 'j', REQ_MOVE_DOWN },
468876c9
JF
1919 { KEY_HOME, REQ_MOVE_FIRST_LINE },
1920 { KEY_END, REQ_MOVE_LAST_LINE },
1921 { KEY_NPAGE, REQ_MOVE_PAGE_DOWN },
c622eefa 1922 { ' ', REQ_MOVE_PAGE_DOWN },
468876c9 1923 { KEY_PPAGE, REQ_MOVE_PAGE_UP },
c622eefa 1924 { 'b', REQ_MOVE_PAGE_UP },
0df811cb 1925 { '-', REQ_MOVE_PAGE_UP },
468876c9
JF
1926
1927 /**
1928 * Scrolling
1929 * ~~~~~~~~~
1930 * Insert::
1931 * Scroll view one line up.
1932 * Delete::
1933 * Scroll view one line down.
1934 * w::
1935 * Scroll view one page up.
1936 * s::
1937 * Scroll view one page down.
1938 **/
1939 { KEY_IC, REQ_SCROLL_LINE_UP },
1940 { KEY_DC, REQ_SCROLL_LINE_DOWN },
1941 { 'w', REQ_SCROLL_PAGE_UP },
1942 { 's', REQ_SCROLL_PAGE_DOWN },
1943
1944 /**
1945 * Misc
1946 * ~~~~
d9d1c722
JF
1947 * Q::
1948 * Quit.
468876c9
JF
1949 * r::
1950 * Redraw screen.
1951 * z::
1952 * Stop all background loading. This can be useful if you use
1953 * tig(1) in a repository with a long history without limiting
0721c53a 1954 * the revision log.
468876c9
JF
1955 * v::
1956 * Show version.
1957 * n::
1958 * Toggle line numbers on/off.
1959 * ':'::
1960 * Open prompt. This allows you to specify what git command
1961 * to run. Example:
1962 *
1963 * :log -p
1964 **/
d9d1c722 1965 { 'Q', REQ_QUIT },
468876c9
JF
1966 { 'z', REQ_STOP_LOADING },
1967 { 'v', REQ_SHOW_VERSION },
1968 { 'r', REQ_SCREEN_REDRAW },
1969 { 'n', REQ_TOGGLE_LINE_NUMBERS },
1970 { ':', REQ_PROMPT },
1971
1972 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
1973 { ERR, REQ_SCREEN_UPDATE },
1974
1975 /* Use the ncurses SIGWINCH handler. */
1976 { KEY_RESIZE, REQ_SCREEN_RESIZE },
1977};
1978
1979static enum request
1980get_request(int key)
1981{
1982 int i;
1983
1984 for (i = 0; i < ARRAY_SIZE(keymap); i++)
1985 if (keymap[i].alias == key)
1986 return keymap[i].request;
1987
1988 return (enum request) key;
1989}
1990
1991
6b161b31 1992/*
10e290ee
JF
1993 * Unicode / UTF-8 handling
1994 *
1995 * NOTE: Much of the following code for dealing with unicode is derived from
1996 * ELinks' UTF-8 code developed by Scrool <scroolik@gmail.com>. Origin file is
1997 * src/intl/charset.c from the utf8 branch commit elinks-0.11.0-g31f2c28.
1998 */
1999
2000/* I've (over)annotated a lot of code snippets because I am not entirely
2001 * confident that the approach taken by this small UTF-8 interface is correct.
2002 * --jonas */
2003
2004static inline int
2005unicode_width(unsigned long c)
2006{
2007 if (c >= 0x1100 &&
2008 (c <= 0x115f /* Hangul Jamo */
2009 || c == 0x2329
2010 || c == 0x232a
2011 || (c >= 0x2e80 && c <= 0xa4cf && c != 0x303f)
f97f4012 2012 /* CJK ... Yi */
10e290ee
JF
2013 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
2014 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
2015 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
2016 || (c >= 0xff00 && c <= 0xff60) /* Fullwidth Forms */
2017 || (c >= 0xffe0 && c <= 0xffe6)
2018 || (c >= 0x20000 && c <= 0x2fffd)
2019 || (c >= 0x30000 && c <= 0x3fffd)))
2020 return 2;
2021
2022 return 1;
2023}
2024
2025/* Number of bytes used for encoding a UTF-8 character indexed by first byte.
2026 * Illegal bytes are set one. */
2027static const unsigned char utf8_bytes[256] = {
2028 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
2029 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
2030 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
2031 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
2032 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
2033 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
2034 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,
2035 3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4, 5,5,5,5,6,6,1,1,
2036};
2037
2038/* Decode UTF-8 multi-byte representation into a unicode character. */
2039static inline unsigned long
2040utf8_to_unicode(const char *string, size_t length)
2041{
2042 unsigned long unicode;
2043
2044 switch (length) {
2045 case 1:
2046 unicode = string[0];
2047 break;
2048 case 2:
2049 unicode = (string[0] & 0x1f) << 6;
2050 unicode += (string[1] & 0x3f);
2051 break;
2052 case 3:
2053 unicode = (string[0] & 0x0f) << 12;
2054 unicode += ((string[1] & 0x3f) << 6);
2055 unicode += (string[2] & 0x3f);
2056 break;
2057 case 4:
2058 unicode = (string[0] & 0x0f) << 18;
2059 unicode += ((string[1] & 0x3f) << 12);
2060 unicode += ((string[2] & 0x3f) << 6);
2061 unicode += (string[3] & 0x3f);
2062 break;
2063 case 5:
2064 unicode = (string[0] & 0x0f) << 24;
2065 unicode += ((string[1] & 0x3f) << 18);
2066 unicode += ((string[2] & 0x3f) << 12);
2067 unicode += ((string[3] & 0x3f) << 6);
2068 unicode += (string[4] & 0x3f);
2069 break;
68b6e0eb 2070 case 6:
10e290ee
JF
2071 unicode = (string[0] & 0x01) << 30;
2072 unicode += ((string[1] & 0x3f) << 24);
2073 unicode += ((string[2] & 0x3f) << 18);
2074 unicode += ((string[3] & 0x3f) << 12);
2075 unicode += ((string[4] & 0x3f) << 6);
2076 unicode += (string[5] & 0x3f);
2077 break;
2078 default:
2079 die("Invalid unicode length");
2080 }
2081
2082 /* Invalid characters could return the special 0xfffd value but NUL
2083 * should be just as good. */
2084 return unicode > 0xffff ? 0 : unicode;
2085}
2086
2087/* Calculates how much of string can be shown within the given maximum width
2088 * and sets trimmed parameter to non-zero value if all of string could not be
2089 * shown.
2090 *
2091 * Additionally, adds to coloffset how many many columns to move to align with
2092 * the expected position. Takes into account how multi-byte and double-width
2093 * characters will effect the cursor position.
2094 *
2095 * Returns the number of bytes to output from string to satisfy max_width. */
2096static size_t
2097utf8_length(const char *string, size_t max_width, int *coloffset, int *trimmed)
2098{
2099 const char *start = string;
2100 const char *end = strchr(string, '\0');
2101 size_t mbwidth = 0;
2102 size_t width = 0;
2103
2104 *trimmed = 0;
2105
2106 while (string < end) {
2107 int c = *(unsigned char *) string;
2108 unsigned char bytes = utf8_bytes[c];
2109 size_t ucwidth;
2110 unsigned long unicode;
2111
2112 if (string + bytes > end)
2113 break;
2114
2115 /* Change representation to figure out whether
2116 * it is a single- or double-width character. */
2117
2118 unicode = utf8_to_unicode(string, bytes);
2119 /* FIXME: Graceful handling of invalid unicode character. */
2120 if (!unicode)
2121 break;
2122
2123 ucwidth = unicode_width(unicode);
2124 width += ucwidth;
2125 if (width > max_width) {
2126 *trimmed = 1;
2127 break;
2128 }
2129
2130 /* The column offset collects the differences between the
2131 * number of bytes encoding a character and the number of
2132 * columns will be used for rendering said character.
2133 *
2134 * So if some character A is encoded in 2 bytes, but will be
2135 * represented on the screen using only 1 byte this will and up
2136 * adding 1 to the multi-byte column offset.
2137 *
2138 * Assumes that no double-width character can be encoding in
2139 * less than two bytes. */
2140 if (bytes > ucwidth)
2141 mbwidth += bytes - ucwidth;
2142
2143 string += bytes;
2144 }
2145
2146 *coloffset += mbwidth;
2147
2148 return string - start;
2149}
2150
2151
2152/*
6b161b31
JF
2153 * Status management
2154 */
2e8488b4 2155
8855ada4 2156/* Whether or not the curses interface has been initialized. */
68b6e0eb 2157static bool cursed = FALSE;
8855ada4 2158
6b161b31
JF
2159/* The status window is used for polling keystrokes. */
2160static WINDOW *status_win;
4a2909a7 2161
2e8488b4 2162/* Update status and title window. */
4a2909a7
JF
2163static void
2164report(const char *msg, ...)
2165{
6706b2ba
JF
2166 static bool empty = TRUE;
2167 struct view *view = display[current_view];
b76c2afc 2168
6706b2ba
JF
2169 if (!empty || *msg) {
2170 va_list args;
4a2909a7 2171
6706b2ba 2172 va_start(args, msg);
4b76734f 2173
6706b2ba
JF
2174 werase(status_win);
2175 wmove(status_win, 0, 0);
2176 if (*msg) {
2177 vwprintw(status_win, msg, args);
2178 empty = FALSE;
2179 } else {
2180 empty = TRUE;
2181 }
2182 wrefresh(status_win);
b801d8b2 2183
6706b2ba
JF
2184 va_end(args);
2185 }
2186
2187 update_view_title(view);
2188
2189 /* Move the cursor to the right-most column of the cursor line.
2190 *
2191 * XXX: This could turn out to be a bit expensive, but it ensures that
2192 * the cursor does not jump around. */
2193 if (view->lines) {
2194 wmove(view->win, view->lineno - view->offset, view->width - 1);
2195 wrefresh(view->win);
2196 }
b801d8b2
JF
2197}
2198
6b161b31
JF
2199/* Controls when nodelay should be in effect when polling user input. */
2200static void
1ba2ae4b 2201set_nonblocking_input(bool loading)
b801d8b2 2202{
6706b2ba 2203 static unsigned int loading_views;
b801d8b2 2204
6706b2ba
JF
2205 if ((loading == FALSE && loading_views-- == 1) ||
2206 (loading == TRUE && loading_views++ == 0))
1ba2ae4b 2207 nodelay(status_win, loading);
6b161b31
JF
2208}
2209
2210static void
2211init_display(void)
2212{
2213 int x, y;
b76c2afc 2214
6908bdbd
JF
2215 /* Initialize the curses library */
2216 if (isatty(STDIN_FILENO)) {
8855ada4 2217 cursed = !!initscr();
6908bdbd
JF
2218 } else {
2219 /* Leave stdin and stdout alone when acting as a pager. */
2220 FILE *io = fopen("/dev/tty", "r+");
2221
8855ada4 2222 cursed = !!newterm(NULL, io, io);
6908bdbd
JF
2223 }
2224
8855ada4
JF
2225 if (!cursed)
2226 die("Failed to initialize curses");
2227
2e8488b4
JF
2228 nonl(); /* Tell curses not to do NL->CR/NL on output */
2229 cbreak(); /* Take input chars one at a time, no wait for \n */
2230 noecho(); /* Don't echo input */
b801d8b2 2231 leaveok(stdscr, TRUE);
b801d8b2
JF
2232
2233 if (has_colors())
2234 init_colors();
2235
2236 getmaxyx(stdscr, y, x);
2237 status_win = newwin(1, 0, y - 1, 0);
2238 if (!status_win)
2239 die("Failed to create status window");
2240
2241 /* Enable keyboard mapping */
2242 keypad(status_win, TRUE);
78c70acd 2243 wbkgdset(status_win, get_line_attr(LINE_STATUS));
6b161b31
JF
2244}
2245
c34d9c9f
JF
2246
2247/*
2248 * Repository references
2249 */
2250
2251static struct ref *refs;
3a91b75e 2252static size_t refs_size;
c34d9c9f 2253
1307df1a
JF
2254/* Id <-> ref store */
2255static struct ref ***id_refs;
2256static size_t id_refs_size;
2257
c34d9c9f
JF
2258static struct ref **
2259get_refs(char *id)
2260{
1307df1a
JF
2261 struct ref ***tmp_id_refs;
2262 struct ref **ref_list = NULL;
2263 size_t ref_list_size = 0;
c34d9c9f
JF
2264 size_t i;
2265
1307df1a
JF
2266 for (i = 0; i < id_refs_size; i++)
2267 if (!strcmp(id, id_refs[i][0]->id))
2268 return id_refs[i];
2269
2270 tmp_id_refs = realloc(id_refs, (id_refs_size + 1) * sizeof(*id_refs));
2271 if (!tmp_id_refs)
2272 return NULL;
2273
2274 id_refs = tmp_id_refs;
2275
c34d9c9f
JF
2276 for (i = 0; i < refs_size; i++) {
2277 struct ref **tmp;
2278
2279 if (strcmp(id, refs[i].id))
2280 continue;
2281
1307df1a 2282 tmp = realloc(ref_list, (ref_list_size + 1) * sizeof(*ref_list));
c34d9c9f 2283 if (!tmp) {
1307df1a
JF
2284 if (ref_list)
2285 free(ref_list);
c34d9c9f
JF
2286 return NULL;
2287 }
2288
1307df1a
JF
2289 ref_list = tmp;
2290 if (ref_list_size > 0)
2291 ref_list[ref_list_size - 1]->next = 1;
2292 ref_list[ref_list_size] = &refs[i];
3af8774e
JF
2293
2294 /* XXX: The properties of the commit chains ensures that we can
2295 * safely modify the shared ref. The repo references will
2296 * always be similar for the same id. */
1307df1a
JF
2297 ref_list[ref_list_size]->next = 0;
2298 ref_list_size++;
c34d9c9f
JF
2299 }
2300
1307df1a
JF
2301 if (ref_list)
2302 id_refs[id_refs_size++] = ref_list;
2303
2304 return ref_list;
c34d9c9f
JF
2305}
2306
2307static int
2308load_refs(void)
2309{
4685845e
TH
2310 const char *cmd_env = getenv("TIG_LS_REMOTE");
2311 const char *cmd = cmd_env && *cmd_env ? cmd_env : TIG_LS_REMOTE;
c34d9c9f
JF
2312 FILE *pipe = popen(cmd, "r");
2313 char buffer[BUFSIZ];
2314 char *line;
2315
2316 if (!pipe)
2317 return ERR;
2318
2319 while ((line = fgets(buffer, sizeof(buffer), pipe))) {
2320 char *name = strchr(line, '\t');
2321 struct ref *ref;
2322 int namelen;
2323 bool tag = FALSE;
6734f6b9 2324 bool tag_commit = FALSE;
c34d9c9f
JF
2325
2326 if (!name)
2327 continue;
2328
2329 *name++ = 0;
2330 namelen = strlen(name) - 1;
6706b2ba
JF
2331
2332 /* Commits referenced by tags has "^{}" appended. */
c34d9c9f
JF
2333 if (name[namelen - 1] == '}') {
2334 while (namelen > 0 && name[namelen] != '^')
2335 namelen--;
6734f6b9
JF
2336 if (namelen > 0)
2337 tag_commit = TRUE;
c34d9c9f
JF
2338 }
2339 name[namelen] = 0;
2340
2341 if (!strncmp(name, "refs/tags/", STRING_SIZE("refs/tags/"))) {
6734f6b9
JF
2342 if (!tag_commit)
2343 continue;
c34d9c9f
JF
2344 name += STRING_SIZE("refs/tags/");
2345 tag = TRUE;
3af8774e
JF
2346
2347 } else if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) {
2348 name += STRING_SIZE("refs/heads/");
2349
2350 } else if (!strcmp(name, "HEAD")) {
2351 continue;
c34d9c9f
JF
2352 }
2353
2354 refs = realloc(refs, sizeof(*refs) * (refs_size + 1));
2355 if (!refs)
2356 return ERR;
2357
2358 ref = &refs[refs_size++];
2359 ref->tag = tag;
2360 ref->name = strdup(name);
2361 if (!ref->name)
2362 return ERR;
2363
2364 string_copy(ref->id, line);
2365 }
2366
2367 if (ferror(pipe))
2368 return ERR;
2369
2370 pclose(pipe);
2371
2372 return OK;
2373}
2374
6b161b31
JF
2375/*
2376 * Main
2377 */
2378
b5c9e67f
TH
2379#if __GNUC__ >= 3
2380#define __NORETURN __attribute__((__noreturn__))
2381#else
2382#define __NORETURN
2383#endif
2384
2385static void __NORETURN
6b161b31
JF
2386quit(int sig)
2387{
8855ada4
JF
2388 /* XXX: Restore tty modes and let the OS cleanup the rest! */
2389 if (cursed)
2390 endwin();
6b161b31
JF
2391 exit(0);
2392}
2393
c6704a4e
JF
2394static void __NORETURN
2395die(const char *err, ...)
6b161b31
JF
2396{
2397 va_list args;
2398
2399 endwin();
2400
2401 va_start(args, err);
2402 fputs("tig: ", stderr);
2403 vfprintf(stderr, err, args);
2404 fputs("\n", stderr);
2405 va_end(args);
2406
2407 exit(1);
2408}
2409
2410int
2411main(int argc, char *argv[])
2412{
1ba2ae4b 2413 struct view *view;
6b161b31 2414 enum request request;
1ba2ae4b 2415 size_t i;
6b161b31
JF
2416
2417 signal(SIGINT, quit);
2418
8855ada4 2419 if (!parse_options(argc, argv))
6b161b31
JF
2420 return 0;
2421
c34d9c9f
JF
2422 if (load_refs() == ERR)
2423 die("Failed to load refs.");
2424
7bb55251
JF
2425 /* Require a git repository unless when running in pager mode. */
2426 if (refs_size == 0 && opt_request != REQ_VIEW_PAGER)
2427 die("Not a git repository");
2428
1ba2ae4b
JF
2429 for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++)
2430 view->cmd_env = getenv(view->cmd_env);
2431
6b161b31
JF
2432 request = opt_request;
2433
2434 init_display();
b801d8b2
JF
2435
2436 while (view_driver(display[current_view], request)) {
6b161b31 2437 int key;
b801d8b2
JF
2438 int i;
2439
6b161b31
JF
2440 foreach_view (view, i)
2441 update_view(view);
b801d8b2
JF
2442
2443 /* Refresh, accept single keystroke of input */
6b161b31
JF
2444 key = wgetch(status_win);
2445 request = get_request(key);
03a93dbb 2446
6706b2ba 2447 /* Some low-level request handling. This keeps access to
fac7db6c
JF
2448 * status_win restricted. */
2449 switch (request) {
2450 case REQ_PROMPT:
6908bdbd
JF
2451 report(":");
2452 /* Temporarily switch to line-oriented and echoed
2453 * input. */
03a93dbb
JF
2454 nocbreak();
2455 echo();
49f2b43f
JF
2456
2457 if (wgetnstr(status_win, opt_cmd + 4, sizeof(opt_cmd) - 4) == OK) {
2458 memcpy(opt_cmd, "git ", 4);
2459 opt_request = REQ_VIEW_PAGER;
2460 } else {
2461 request = ERR;
2462 }
2463
6908bdbd
JF
2464 noecho();
2465 cbreak();
fac7db6c
JF
2466 break;
2467
2468 case REQ_SCREEN_RESIZE:
2469 {
2470 int height, width;
2471
2472 getmaxyx(stdscr, height, width);
2473
2474 /* Resize the status view and let the view driver take
2475 * care of resizing the displayed views. */
2476 wresize(status_win, 1, width);
2477 mvwin(status_win, height - 1, 0);
2478 wrefresh(status_win);
2479 break;
2480 }
2481 default:
2482 break;
03a93dbb 2483 }
b801d8b2
JF
2484 }
2485
2486 quit(0);
2487
2488 return 0;
2489}
2490
2491/**
0721c53a 2492 * [[refspec]]
3a11b38f
JF
2493 * Revision specification
2494 * ----------------------
0721c53a 2495 * This section describes various ways to specify what revisions to display
3a11b38f
JF
2496 * or otherwise limit the view to. tig(1) does not itself parse the described
2497 * revision options so refer to the relevant git man pages for futher
2498 * information. Relevant man pages besides git-log(1) are git-diff(1) and
2499 * git-rev-list(1).
468876c9 2500 *
3a11b38f
JF
2501 * You can tune the interaction with git by making use of the options
2502 * explained in this section. For example, by configuring the environment
2503 * variables described in the <<view-commands, "View commands">> section.
2504 *
2505 * Limit by path name
2506 * ~~~~~~~~~~~~~~~~~~
ab46037d
JF
2507 * If you are interested only in those revisions that made changes to a
2508 * specific file (or even several files) list the files like this:
2509 *
c760e6ba 2510 * $ tig log Makefile README
ab46037d
JF
2511 *
2512 * To avoid ambiguity with repository references such as tag name, be sure
2513 * to separate file names from other git options using "\--". So if you
2514 * have a file named 'master' it will clash with the reference named
2515 * 'master', and thus you will have to use:
2516 *
1b4b0bd9 2517 * $ tig log -- master
ab46037d
JF
2518 *
2519 * NOTE: For the main view, avoiding ambiguity will in some cases require
2520 * you to specify two "\--" options. The first will make tig(1) stop
2521 * option processing and the latter will be passed to git log.
2522 *
468876c9
JF
2523 * Limit by date or number
2524 * ~~~~~~~~~~~~~~~~~~~~~~~
2525 * To speed up interaction with git, you can limit the amount of commits
2526 * to show both for the log and main view. Either limit by date using
2527 * e.g. `--since=1.month` or limit by the number of commits using `-n400`.
2528 *
3a11b38f
JF
2529 * If you are only interested in changed that happened between two dates
2530 * you can use:
2531 *
c760e6ba 2532 * $ tig -- --after="May 5th" --before="2006-05-16 15:44"
468876c9 2533 *
c760e6ba
JF
2534 * NOTE: If you want to avoid having to quote dates containing spaces you
2535 * can use "." instead, e.g. `--after=May.5th`.
3a11b38f
JF
2536 *
2537 * Limiting by commit ranges
2538 * ~~~~~~~~~~~~~~~~~~~~~~~~~
468876c9
JF
2539 * Alternatively, commits can be limited to a specific range, such as
2540 * "all commits between 'tag-1.0' and 'tag-2.0'". For example:
2541 *
2542 * $ tig log tag-1.0..tag-2.0
2543 *
2544 * This way of commit limiting makes it trivial to only browse the commits
2545 * which haven't been pushed to a remote branch. Assuming 'origin' is your
2546 * upstream remote branch, using:
2547 *
2548 * $ tig log origin..HEAD
2549 *
2550 * will list what will be pushed to the remote branch. Optionally, the ending
2551 * 'HEAD' can be left out since it is implied.
2552 *
2553 * Limiting by reachability
2554 * ~~~~~~~~~~~~~~~~~~~~~~~~
2555 * Git interprets the range specifier "tag-1.0..tag-2.0" as
2556 * "all commits reachable from 'tag-2.0' but not from 'tag-1.0'".
3a11b38f
JF
2557 * Where reachability refers to what commits are ancestors (or part of the
2558 * history) of the branch or tagged revision in question.
2559 *
468876c9
JF
2560 * If you prefer to specify which commit to preview in this way use the
2561 * following:
2562 *
2563 * $ tig log tag-2.0 ^tag-1.0
2564 *
57bdf034
JF
2565 * You can think of '^' as a negation operator. Using this alternate syntax,
2566 * it is possible to further prune commits by specifying multiple branch
2567 * cut offs.
468876c9 2568 *
3a11b38f
JF
2569 * Combining revisions specification
2570 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2571 * Revisions options can to some degree be combined, which makes it possible
2572 * to say "show at most 20 commits from within the last month that changed
2573 * files under the Documentation/ directory."
2574 *
2575 * $ tig -- --since=1.month -n20 -- Documentation/
2576 *
2577 * Examining all repository references
2578 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2579 * In some cases, it can be useful to query changes across all references
2580 * in a repository. An example is to ask "did any line of development in
2581 * this repository change a particular file within the last week". This
2582 * can be accomplished using:
2583 *
2584 * $ tig -- --all --since=1.week -- Makefile
2585 *
6706b2ba
JF
2586 * BUGS
2587 * ----
2588 * Known bugs and problems:
2589 *
10e290ee
JF
2590 * - In it's current state tig is pretty much UTF-8 only.
2591 *
6706b2ba 2592 * - If the screen width is very small the main view can draw
468876c9
JF
2593 * outside the current view causing bad wrapping. Same goes
2594 * for title and status windows.
6706b2ba 2595 *
c19f8017
JF
2596 * - The cursor can wrap-around on the last line and cause the
2597 * window to scroll.
2598 *
4c6fabc2
JF
2599 * TODO
2600 * ----
2601 * Features that should be explored.
2602 *
fac7db6c 2603 * - Searching.
4c6fabc2
JF
2604 *
2605 * - Locale support.
2606 *
b801d8b2
JF
2607 * COPYRIGHT
2608 * ---------
4a2909a7 2609 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
b801d8b2
JF
2610 *
2611 * This program is free software; you can redistribute it and/or modify
2612 * it under the terms of the GNU General Public License as published by
2613 * the Free Software Foundation; either version 2 of the License, or
2614 * (at your option) any later version.
2615 *
2616 * SEE ALSO
2617 * --------
4c6fabc2 2618 * [verse]
b801d8b2
JF
2619 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
2620 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
4c6fabc2 2621 * gitk(1): git repository browser written using tcl/tk,
c6704a4e 2622 * qgit(1): git repository browser written using c++/Qt,
4c6fabc2 2623 * gitview(1): git repository browser written using python/gtk.
b801d8b2 2624 **/