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