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