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