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