1 /* Copyright (c) 2006-2010 Jonas Fonseca <fonseca@diku.dk>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
18 static void __NORETURN
die(const char *err
, ...);
19 static void warn(const char *msg
, ...);
20 static void report(const char *msg
, ...);
24 char id
[SIZEOF_REV
]; /* Commit SHA1 ID */
25 unsigned int head
:1; /* Is it the current HEAD? */
26 unsigned int tag
:1; /* Is it a tag? */
27 unsigned int ltag
:1; /* If so, is the tag local? */
28 unsigned int remote
:1; /* Is it a remote ref? */
29 unsigned int tracked
:1; /* Is it the remote for the current HEAD? */
30 char name
[1]; /* Ref name; tag or head names are shortened. */
34 char id
[SIZEOF_REV
]; /* Commit SHA1 ID */
35 size_t size
; /* Number of refs. */
36 struct ref
**refs
; /* References for this ID. */
39 static struct ref
*get_ref_head();
40 static struct ref_list
*get_ref_list(const char *id
);
41 static void foreach_ref(bool (*visitor
)(void *data
, const struct ref
*ref
), void *data
);
42 static int load_refs(void);
51 typedef enum input_status (*input_handler
)(void *data
, char *buf
, int c
);
53 static char *prompt_input(const char *prompt
, input_handler handler
, void *data
);
54 static bool prompt_yesno(const char *prompt
);
62 static bool prompt_menu(const char *prompt
, const struct menu_item
*items
, int *selected
);
70 static const struct enum_map graphic_map
[] = {
71 #define GRAPHIC_(name) ENUM_MAP(#name, GRAPHIC_##name)
86 #define DATE_(name) DATE_##name
91 static const struct enum_map date_map
[] = {
92 #define DATE_(name) ENUM_MAP(#name, DATE_##name)
102 static inline int timecmp(const struct time
*t1
, const struct time
*t2
)
104 return t1
->sec
- t2
->sec
;
108 mkdate(const struct time
*time
, enum date date
)
110 static char buf
[DATE_COLS
+ 1];
111 static const struct enum_map reldate
[] = {
112 { "second", 1, 60 * 2 },
113 { "minute", 60, 60 * 60 * 2 },
114 { "hour", 60 * 60, 60 * 60 * 24 * 2 },
115 { "day", 60 * 60 * 24, 60 * 60 * 24 * 7 * 2 },
116 { "week", 60 * 60 * 24 * 7, 60 * 60 * 24 * 7 * 5 },
117 { "month", 60 * 60 * 24 * 30, 60 * 60 * 24 * 30 * 12 },
121 if (!date
|| !time
|| !time
->sec
)
124 if (date
== DATE_RELATIVE
) {
126 time_t date
= time
->sec
+ time
->tz
;
130 gettimeofday(&now
, NULL
);
131 seconds
= now
.tv_sec
< date
? date
- now
.tv_sec
: now
.tv_sec
- date
;
132 for (i
= 0; i
< ARRAY_SIZE(reldate
); i
++) {
133 if (seconds
>= reldate
[i
].value
)
136 seconds
/= reldate
[i
].namelen
;
137 if (!string_format(buf
, "%ld %s%s %s",
138 seconds
, reldate
[i
].name
,
139 seconds
> 1 ? "s" : "",
140 now
.tv_sec
>= date
? "ago" : "ahead"))
146 if (date
== DATE_LOCAL
) {
147 time_t date
= time
->sec
+ time
->tz
;
148 localtime_r(&date
, &tm
);
151 gmtime_r(&time
->sec
, &tm
);
153 return strftime(buf
, sizeof(buf
), DATE_FORMAT
, &tm
) ? buf
: NULL
;
157 #define AUTHOR_VALUES \
163 #define AUTHOR_(name) AUTHOR_##name
166 AUTHOR_DEFAULT
= AUTHOR_FULL
169 static const struct enum_map author_map
[] = {
170 #define AUTHOR_(name) ENUM_MAP(#name, AUTHOR_##name)
176 get_author_initials(const char *author
)
178 static char initials
[AUTHOR_COLS
* 6 + 1];
180 const char *end
= strchr(author
, '\0');
182 #define is_initial_sep(c) (isspace(c) || ispunct(c) || (c) == '@' || (c) == '-')
184 memset(initials
, 0, sizeof(initials
));
185 while (author
< end
) {
189 while (is_initial_sep(*author
))
192 bytes
= utf8_char_length(author
, end
);
193 if (bytes
< sizeof(initials
) - 1 - pos
) {
195 initials
[pos
++] = *author
++;
199 for (i
= pos
; author
< end
&& !is_initial_sep(*author
); author
++) {
200 if (i
< sizeof(initials
) - 1)
201 initials
[i
++] = *author
;
216 /* XXX: Keep the view request first and in sync with views[]. */ \
217 REQ_GROUP("View switching") \
218 REQ_(VIEW_MAIN, "Show main view"), \
219 REQ_(VIEW_DIFF, "Show diff view"), \
220 REQ_(VIEW_LOG, "Show log view"), \
221 REQ_(VIEW_TREE, "Show tree view"), \
222 REQ_(VIEW_BLOB, "Show blob view"), \
223 REQ_(VIEW_BLAME, "Show blame view"), \
224 REQ_(VIEW_BRANCH, "Show branch view"), \
225 REQ_(VIEW_HELP, "Show help page"), \
226 REQ_(VIEW_PAGER, "Show pager view"), \
227 REQ_(VIEW_STATUS, "Show status view"), \
228 REQ_(VIEW_STAGE, "Show stage view"), \
230 REQ_GROUP("View manipulation") \
231 REQ_(ENTER, "Enter current line and scroll"), \
232 REQ_(NEXT, "Move to next"), \
233 REQ_(PREVIOUS, "Move to previous"), \
234 REQ_(PARENT, "Move to parent"), \
235 REQ_(VIEW_NEXT, "Move focus to next view"), \
236 REQ_(REFRESH, "Reload and refresh"), \
237 REQ_(MAXIMIZE, "Maximize the current view"), \
238 REQ_(VIEW_CLOSE, "Close the current view"), \
239 REQ_(QUIT, "Close all views and quit"), \
241 REQ_GROUP("View specific requests") \
242 REQ_(STATUS_UPDATE, "Update file status"), \
243 REQ_(STATUS_REVERT, "Revert file changes"), \
244 REQ_(STATUS_MERGE, "Merge file using external tool"), \
245 REQ_(STAGE_NEXT, "Find next chunk to stage"), \
247 REQ_GROUP("Cursor navigation") \
248 REQ_(MOVE_UP, "Move cursor one line up"), \
249 REQ_(MOVE_DOWN, "Move cursor one line down"), \
250 REQ_(MOVE_PAGE_DOWN, "Move cursor one page down"), \
251 REQ_(MOVE_PAGE_UP, "Move cursor one page up"), \
252 REQ_(MOVE_FIRST_LINE, "Move cursor to first line"), \
253 REQ_(MOVE_LAST_LINE, "Move cursor to last line"), \
255 REQ_GROUP("Scrolling") \
256 REQ_(SCROLL_FIRST_COL, "Scroll to the first line columns"), \
257 REQ_(SCROLL_LEFT, "Scroll two columns left"), \
258 REQ_(SCROLL_RIGHT, "Scroll two columns right"), \
259 REQ_(SCROLL_LINE_UP, "Scroll one line up"), \
260 REQ_(SCROLL_LINE_DOWN, "Scroll one line down"), \
261 REQ_(SCROLL_PAGE_UP, "Scroll one page up"), \
262 REQ_(SCROLL_PAGE_DOWN, "Scroll one page down"), \
264 REQ_GROUP("Searching") \
265 REQ_(SEARCH, "Search the view"), \
266 REQ_(SEARCH_BACK, "Search backwards in the view"), \
267 REQ_(FIND_NEXT, "Find next search match"), \
268 REQ_(FIND_PREV, "Find previous search match"), \
270 REQ_GROUP("Option manipulation") \
271 REQ_(OPTIONS, "Open option menu"), \
272 REQ_(TOGGLE_LINENO, "Toggle line numbers"), \
273 REQ_(TOGGLE_DATE, "Toggle date display"), \
274 REQ_(TOGGLE_AUTHOR, "Toggle author display"), \
275 REQ_(TOGGLE_REV_GRAPH, "Toggle revision graph visualization"), \
276 REQ_(TOGGLE_GRAPHIC, "Toggle (line) graphics mode"), \
277 REQ_(TOGGLE_REFS, "Toggle reference display (tags/branches)"), \
278 REQ_(TOGGLE_SORT_ORDER, "Toggle ascending/descending sort order"), \
279 REQ_(TOGGLE_SORT_FIELD, "Toggle field to sort by"), \
282 REQ_(PROMPT, "Bring up the prompt"), \
283 REQ_(SCREEN_REDRAW, "Redraw the screen"), \
284 REQ_(SHOW_VERSION, "Show version information"), \
285 REQ_(STOP_LOADING, "Stop all loading views"), \
286 REQ_(EDIT, "Open in editor"), \
287 REQ_(NONE, "Do nothing")
290 /* User action requests. */
292 #define REQ_GROUP(help)
293 #define REQ_(req, help) REQ_##req
295 /* Offset all requests to avoid conflicts with ncurses getch values. */
296 REQ_UNKNOWN
= KEY_MAX
+ 1,
304 struct request_info
{
305 enum request request
;
311 static const struct request_info req_info
[] = {
312 #define REQ_GROUP(help) { 0, NULL, 0, (help) },
313 #define REQ_(req, help) { REQ_##req, (#req), STRING_SIZE(#req), (help) }
320 get_request(const char *name
)
322 int namelen
= strlen(name
);
325 for (i
= 0; i
< ARRAY_SIZE(req_info
); i
++)
326 if (enum_equals(req_info
[i
], name
, namelen
))
327 return req_info
[i
].request
;
337 /* Option and state variables. */
338 static enum graphic opt_line_graphics
= GRAPHIC_DEFAULT
;
339 static enum date opt_date
= DATE_DEFAULT
;
340 static enum author opt_author
= AUTHOR_DEFAULT
;
341 static bool opt_rev_graph
= TRUE
;
342 static bool opt_line_number
= FALSE
;
343 static bool opt_show_refs
= TRUE
;
344 static bool opt_untracked_dirs_content
= TRUE
;
345 static int opt_num_interval
= 5;
346 static double opt_hscroll
= 0.50;
347 static double opt_scale_split_view
= 2.0 / 3.0;
348 static int opt_tab_size
= 8;
349 static int opt_author_cols
= AUTHOR_COLS
;
350 static char opt_path
[SIZEOF_STR
] = "";
351 static char opt_file
[SIZEOF_STR
] = "";
352 static char opt_ref
[SIZEOF_REF
] = "";
353 static char opt_head
[SIZEOF_REF
] = "";
354 static char opt_remote
[SIZEOF_REF
] = "";
355 static char opt_encoding
[20] = "UTF-8";
356 static iconv_t opt_iconv_in
= ICONV_NONE
;
357 static iconv_t opt_iconv_out
= ICONV_NONE
;
358 static char opt_search
[SIZEOF_STR
] = "";
359 static char opt_cdup
[SIZEOF_STR
] = "";
360 static char opt_prefix
[SIZEOF_STR
] = "";
361 static char opt_git_dir
[SIZEOF_STR
] = "";
362 static signed char opt_is_inside_work_tree
= -1; /* set to TRUE or FALSE */
363 static char opt_editor
[SIZEOF_STR
] = "";
364 static FILE *opt_tty
= NULL
;
365 static const char **opt_diff_argv
= NULL
;
366 static const char **opt_rev_argv
= NULL
;
367 static const char **opt_file_argv
= NULL
;
368 static const char **opt_blame_argv
= NULL
;
370 #define is_initial_commit() (!get_ref_head())
371 #define is_head_commit(rev) (!strcmp((rev), "HEAD") || (get_ref_head() && !strcmp(rev, get_ref_head()->id)))
375 * Line-oriented content detection.
379 LINE(DIFF_HEADER, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
380 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
381 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
382 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
383 LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
384 LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
385 LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
386 LINE(DIFF_COPY_FROM, "copy from", COLOR_YELLOW, COLOR_DEFAULT, 0), \
387 LINE(DIFF_COPY_TO, "copy to", COLOR_YELLOW, COLOR_DEFAULT, 0), \
388 LINE(DIFF_RENAME_FROM, "rename from", COLOR_YELLOW, COLOR_DEFAULT, 0), \
389 LINE(DIFF_RENAME_TO, "rename to", COLOR_YELLOW, COLOR_DEFAULT, 0), \
390 LINE(DIFF_SIMILARITY, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
391 LINE(DIFF_DISSIMILARITY,"dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
392 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
393 LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
394 LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
395 LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
396 LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
397 LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
398 LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
399 LINE(PP_REFS, "Refs: ", COLOR_RED, COLOR_DEFAULT, 0), \
400 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
401 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
402 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
403 LINE(AUTHOR, "author ", COLOR_GREEN, COLOR_DEFAULT, 0), \
404 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
405 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
406 LINE(ACKED, " Acked-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
407 LINE(TESTED, " Tested-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
408 LINE(REVIEWED, " Reviewed-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
409 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
410 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
411 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
412 LINE(DELIMITER, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
413 LINE(DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
414 LINE(MODE, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
415 LINE(LINE_NUMBER, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
416 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
417 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
418 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
419 LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
420 LINE(MAIN_LOCAL_TAG,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
421 LINE(MAIN_REMOTE, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
422 LINE(MAIN_TRACKED, "", COLOR_YELLOW, COLOR_DEFAULT, A_BOLD), \
423 LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
424 LINE(MAIN_HEAD, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD), \
425 LINE(MAIN_REVGRAPH,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
426 LINE(TREE_HEAD, "", COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD), \
427 LINE(TREE_DIR, "", COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL), \
428 LINE(TREE_FILE, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
429 LINE(STAT_HEAD, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
430 LINE(STAT_SECTION, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
431 LINE(STAT_NONE, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
432 LINE(STAT_STAGED, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
433 LINE(STAT_UNSTAGED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
434 LINE(STAT_UNTRACKED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
435 LINE(HELP_KEYMAP, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
436 LINE(HELP_GROUP, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
437 LINE(BLAME_ID, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
438 LINE(GRAPH_LINE_0, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
439 LINE(GRAPH_LINE_1, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
440 LINE(GRAPH_LINE_2, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
441 LINE(GRAPH_LINE_3, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
442 LINE(GRAPH_LINE_4, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
443 LINE(GRAPH_LINE_5, "", COLOR_WHITE, COLOR_DEFAULT, 0), \
444 LINE(GRAPH_LINE_6, "", COLOR_RED, COLOR_DEFAULT, 0), \
445 LINE(GRAPH_COMMIT, "", COLOR_BLUE, COLOR_DEFAULT, 0)
448 #define LINE(type, line, fg, bg, attr) \
456 const char *name
; /* Option name. */
457 int namelen
; /* Size of option name. */
458 const char *line
; /* The start of line to match. */
459 int linelen
; /* Size of string to match. */
460 int fg
, bg
, attr
; /* Color and text attributes for the lines. */
463 static struct line_info line_info
[] = {
464 #define LINE(type, line, fg, bg, attr) \
465 { #type, STRING_SIZE(#type), (line), STRING_SIZE(line), (fg), (bg), (attr) }
470 static enum line_type
471 get_line_type(const char *line
)
473 int linelen
= strlen(line
);
476 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++)
477 /* Case insensitive search matches Signed-off-by lines better. */
478 if (linelen
>= line_info
[type
].linelen
&&
479 !strncasecmp(line_info
[type
].line
, line
, line_info
[type
].linelen
))
486 get_line_attr(enum line_type type
)
488 assert(type
< ARRAY_SIZE(line_info
));
489 return COLOR_PAIR(type
) | line_info
[type
].attr
;
492 static struct line_info
*
493 get_line_info(const char *name
)
495 size_t namelen
= strlen(name
);
498 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++)
499 if (enum_equals(line_info
[type
], name
, namelen
))
500 return &line_info
[type
];
508 int default_bg
= line_info
[LINE_DEFAULT
].bg
;
509 int default_fg
= line_info
[LINE_DEFAULT
].fg
;
514 if (assume_default_colors(default_fg
, default_bg
) == ERR
) {
515 default_bg
= COLOR_BLACK
;
516 default_fg
= COLOR_WHITE
;
519 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++) {
520 struct line_info
*info
= &line_info
[type
];
521 int bg
= info
->bg
== COLOR_DEFAULT
? default_bg
: info
->bg
;
522 int fg
= info
->fg
== COLOR_DEFAULT
? default_fg
: info
->fg
;
524 init_pair(type
, fg
, bg
);
532 unsigned int selected
:1;
533 unsigned int dirty
:1;
534 unsigned int cleareol
:1;
535 unsigned int other
:16;
537 void *data
; /* User data */
547 enum request request
;
550 static struct keybinding default_keybindings
[] = {
552 { 'm', REQ_VIEW_MAIN
},
553 { 'd', REQ_VIEW_DIFF
},
554 { 'l', REQ_VIEW_LOG
},
555 { 't', REQ_VIEW_TREE
},
556 { 'f', REQ_VIEW_BLOB
},
557 { 'B', REQ_VIEW_BLAME
},
558 { 'H', REQ_VIEW_BRANCH
},
559 { 'p', REQ_VIEW_PAGER
},
560 { 'h', REQ_VIEW_HELP
},
561 { 'S', REQ_VIEW_STATUS
},
562 { 'c', REQ_VIEW_STAGE
},
564 /* View manipulation */
565 { 'q', REQ_VIEW_CLOSE
},
566 { KEY_TAB
, REQ_VIEW_NEXT
},
567 { KEY_RETURN
, REQ_ENTER
},
568 { KEY_UP
, REQ_PREVIOUS
},
569 { KEY_CTL('P'), REQ_PREVIOUS
},
570 { KEY_DOWN
, REQ_NEXT
},
571 { KEY_CTL('N'), REQ_NEXT
},
572 { 'R', REQ_REFRESH
},
573 { KEY_F(5), REQ_REFRESH
},
574 { 'O', REQ_MAXIMIZE
},
576 /* Cursor navigation */
577 { 'k', REQ_MOVE_UP
},
578 { 'j', REQ_MOVE_DOWN
},
579 { KEY_HOME
, REQ_MOVE_FIRST_LINE
},
580 { KEY_END
, REQ_MOVE_LAST_LINE
},
581 { KEY_NPAGE
, REQ_MOVE_PAGE_DOWN
},
582 { KEY_CTL('D'), REQ_MOVE_PAGE_DOWN
},
583 { ' ', REQ_MOVE_PAGE_DOWN
},
584 { KEY_PPAGE
, REQ_MOVE_PAGE_UP
},
585 { KEY_CTL('U'), REQ_MOVE_PAGE_UP
},
586 { 'b', REQ_MOVE_PAGE_UP
},
587 { '-', REQ_MOVE_PAGE_UP
},
590 { '|', REQ_SCROLL_FIRST_COL
},
591 { KEY_LEFT
, REQ_SCROLL_LEFT
},
592 { KEY_RIGHT
, REQ_SCROLL_RIGHT
},
593 { KEY_IC
, REQ_SCROLL_LINE_UP
},
594 { KEY_CTL('Y'), REQ_SCROLL_LINE_UP
},
595 { KEY_DC
, REQ_SCROLL_LINE_DOWN
},
596 { KEY_CTL('E'), REQ_SCROLL_LINE_DOWN
},
597 { 'w', REQ_SCROLL_PAGE_UP
},
598 { 's', REQ_SCROLL_PAGE_DOWN
},
602 { '?', REQ_SEARCH_BACK
},
603 { 'n', REQ_FIND_NEXT
},
604 { 'N', REQ_FIND_PREV
},
608 { 'z', REQ_STOP_LOADING
},
609 { 'v', REQ_SHOW_VERSION
},
610 { 'r', REQ_SCREEN_REDRAW
},
611 { KEY_CTL('L'), REQ_SCREEN_REDRAW
},
612 { 'o', REQ_OPTIONS
},
613 { '.', REQ_TOGGLE_LINENO
},
614 { 'D', REQ_TOGGLE_DATE
},
615 { 'A', REQ_TOGGLE_AUTHOR
},
616 { 'g', REQ_TOGGLE_REV_GRAPH
},
617 { '~', REQ_TOGGLE_GRAPHIC
},
618 { 'F', REQ_TOGGLE_REFS
},
619 { 'I', REQ_TOGGLE_SORT_ORDER
},
620 { 'i', REQ_TOGGLE_SORT_FIELD
},
622 { 'u', REQ_STATUS_UPDATE
},
623 { '!', REQ_STATUS_REVERT
},
624 { 'M', REQ_STATUS_MERGE
},
625 { '@', REQ_STAGE_NEXT
},
630 #define KEYMAP_INFO \
645 #define KEYMAP_(name) KEYMAP_##name
650 static const struct enum_map keymap_table
[] = {
651 #define KEYMAP_(name) ENUM_MAP(#name, KEYMAP_##name)
656 #define set_keymap(map, name) map_enum(map, keymap_table, name)
658 struct keybinding_table
{
659 struct keybinding
*data
;
663 static struct keybinding_table keybindings
[ARRAY_SIZE(keymap_table
)];
666 add_keybinding(enum keymap keymap
, enum request request
, int key
)
668 struct keybinding_table
*table
= &keybindings
[keymap
];
671 for (i
= 0; i
< keybindings
[keymap
].size
; i
++) {
672 if (keybindings
[keymap
].data
[i
].alias
== key
) {
673 keybindings
[keymap
].data
[i
].request
= request
;
678 table
->data
= realloc(table
->data
, (table
->size
+ 1) * sizeof(*table
->data
));
680 die("Failed to allocate keybinding");
681 table
->data
[table
->size
].alias
= key
;
682 table
->data
[table
->size
++].request
= request
;
684 if (request
== REQ_NONE
&& keymap
== KEYMAP_GENERIC
) {
687 for (i
= 0; i
< ARRAY_SIZE(default_keybindings
); i
++)
688 if (default_keybindings
[i
].alias
== key
)
689 default_keybindings
[i
].request
= REQ_NONE
;
693 /* Looks for a key binding first in the given map, then in the generic map, and
694 * lastly in the default keybindings. */
696 get_keybinding(enum keymap keymap
, int key
)
700 for (i
= 0; i
< keybindings
[keymap
].size
; i
++)
701 if (keybindings
[keymap
].data
[i
].alias
== key
)
702 return keybindings
[keymap
].data
[i
].request
;
704 for (i
= 0; i
< keybindings
[KEYMAP_GENERIC
].size
; i
++)
705 if (keybindings
[KEYMAP_GENERIC
].data
[i
].alias
== key
)
706 return keybindings
[KEYMAP_GENERIC
].data
[i
].request
;
708 for (i
= 0; i
< ARRAY_SIZE(default_keybindings
); i
++)
709 if (default_keybindings
[i
].alias
== key
)
710 return default_keybindings
[i
].request
;
712 return (enum request
) key
;
721 static const struct key key_table
[] = {
722 { "Enter", KEY_RETURN
},
724 { "Backspace", KEY_BACKSPACE
},
726 { "Escape", KEY_ESC
},
727 { "Left", KEY_LEFT
},
728 { "Right", KEY_RIGHT
},
730 { "Down", KEY_DOWN
},
731 { "Insert", KEY_IC
},
732 { "Delete", KEY_DC
},
734 { "Home", KEY_HOME
},
736 { "PageUp", KEY_PPAGE
},
737 { "PageDown", KEY_NPAGE
},
747 { "F10", KEY_F(10) },
748 { "F11", KEY_F(11) },
749 { "F12", KEY_F(12) },
753 get_key_value(const char *name
)
757 for (i
= 0; i
< ARRAY_SIZE(key_table
); i
++)
758 if (!strcasecmp(key_table
[i
].name
, name
))
759 return key_table
[i
].value
;
761 if (strlen(name
) == 2 && name
[0] == '^' && isprint(*name
))
762 return (int)name
[1] & 0x1f;
763 if (strlen(name
) == 1 && isprint(*name
))
769 get_key_name(int key_value
)
771 static char key_char
[] = "'X'\0";
772 const char *seq
= NULL
;
775 for (key
= 0; key
< ARRAY_SIZE(key_table
); key
++)
776 if (key_table
[key
].value
== key_value
)
777 seq
= key_table
[key
].name
;
779 if (seq
== NULL
&& key_value
< 0x7f) {
780 char *s
= key_char
+ 1;
782 if (key_value
>= 0x20) {
786 *s
++ = 0x40 | (key_value
& 0x1f);
793 return seq
? seq
: "(no key)";
797 append_key(char *buf
, size_t *pos
, const struct keybinding
*keybinding
)
799 const char *sep
= *pos
> 0 ? ", " : "";
800 const char *keyname
= get_key_name(keybinding
->alias
);
802 return string_nformat(buf
, BUFSIZ
, pos
, "%s%s", sep
, keyname
);
806 append_keymap_request_keys(char *buf
, size_t *pos
, enum request request
,
807 enum keymap keymap
, bool all
)
811 for (i
= 0; i
< keybindings
[keymap
].size
; i
++) {
812 if (keybindings
[keymap
].data
[i
].request
== request
) {
813 if (!append_key(buf
, pos
, &keybindings
[keymap
].data
[i
]))
823 #define get_key(keymap, request) get_keys(keymap, request, FALSE)
826 get_keys(enum keymap keymap
, enum request request
, bool all
)
828 static char buf
[BUFSIZ
];
834 if (!append_keymap_request_keys(buf
, &pos
, request
, keymap
, all
))
835 return "Too many keybindings!";
839 if (keymap
!= KEYMAP_GENERIC
) {
840 /* Only the generic keymap includes the default keybindings when
841 * listing all keys. */
845 if (!append_keymap_request_keys(buf
, &pos
, request
, KEYMAP_GENERIC
, all
))
846 return "Too many keybindings!";
851 for (i
= 0; i
< ARRAY_SIZE(default_keybindings
); i
++) {
852 if (default_keybindings
[i
].request
== request
) {
853 if (!append_key(buf
, &pos
, &default_keybindings
[i
]))
854 return "Too many keybindings!";
869 static struct run_request
*run_request
;
870 static size_t run_requests
;
872 DEFINE_ALLOCATOR(realloc_run_requests
, struct run_request
, 8)
875 add_run_request(enum keymap keymap
, int key
, const char **argv
)
877 struct run_request
*req
;
879 if (!realloc_run_requests(&run_request
, run_requests
, 1))
882 req
= &run_request
[run_requests
];
883 req
->keymap
= keymap
;
887 if (!argv_copy(&req
->argv
, argv
))
890 return REQ_NONE
+ ++run_requests
;
893 static struct run_request
*
894 get_run_request(enum request request
)
896 if (request
<= REQ_NONE
)
898 return &run_request
[request
- REQ_NONE
- 1];
902 add_builtin_run_requests(void)
904 const char *cherry_pick
[] = { "git", "cherry-pick", "%(commit)", NULL
};
905 const char *checkout
[] = { "git", "checkout", "%(branch)", NULL
};
906 const char *commit
[] = { "git", "commit", NULL
};
907 const char *gc
[] = { "git", "gc", NULL
};
908 struct run_request reqs
[] = {
909 { KEYMAP_MAIN
, 'C', cherry_pick
},
910 { KEYMAP_STATUS
, 'C', commit
},
911 { KEYMAP_BRANCH
, 'C', checkout
},
912 { KEYMAP_GENERIC
, 'G', gc
},
916 for (i
= 0; i
< ARRAY_SIZE(reqs
); i
++) {
917 enum request req
= get_keybinding(reqs
[i
].keymap
, reqs
[i
].key
);
919 if (req
!= reqs
[i
].key
)
921 req
= add_run_request(reqs
[i
].keymap
, reqs
[i
].key
, reqs
[i
].argv
);
923 add_keybinding(reqs
[i
].keymap
, req
, reqs
[i
].key
);
928 * User config file handling.
931 #define OPT_ERR_INFO \
932 OPT_ERR_(INTEGER_VALUE_OUT_OF_BOUND, "Integer value out of bound"), \
933 OPT_ERR_(INVALID_STEP_VALUE, "Invalid step value"), \
934 OPT_ERR_(NO_OPTION_VALUE, "No option value"), \
935 OPT_ERR_(NO_VALUE_ASSIGNED, "No value assigned"), \
936 OPT_ERR_(OBSOLETE_REQUEST_NAME, "Obsolete request name"), \
937 OPT_ERR_(OUT_OF_MEMORY, "Out of memory"), \
938 OPT_ERR_(TOO_MANY_OPTION_ARGUMENTS, "Too many option arguments"), \
939 OPT_ERR_(UNKNOWN_ATTRIBUTE, "Unknown attribute"), \
940 OPT_ERR_(UNKNOWN_COLOR, "Unknown color"), \
941 OPT_ERR_(UNKNOWN_COLOR_NAME, "Unknown color name"), \
942 OPT_ERR_(UNKNOWN_KEY, "Unknown key"), \
943 OPT_ERR_(UNKNOWN_KEY_MAP, "Unknown key map"), \
944 OPT_ERR_(UNKNOWN_OPTION_COMMAND, "Unknown option command"), \
945 OPT_ERR_(UNKNOWN_REQUEST_NAME, "Unknown request name"), \
946 OPT_ERR_(UNKNOWN_VARIABLE_NAME, "Unknown variable name"), \
947 OPT_ERR_(UNMATCHED_QUOTATION, "Unmatched quotation"), \
948 OPT_ERR_(WRONG_NUMBER_OF_ARGUMENTS, "Wrong number of arguments"),
951 #define OPT_ERR_(name, msg) OPT_ERR_ ## name
957 static const char *option_errors
[] = {
958 #define OPT_ERR_(name, msg) msg
963 static const struct enum_map color_map
[] = {
964 #define COLOR_MAP(name) ENUM_MAP(#name, COLOR_##name)
976 static const struct enum_map attr_map
[] = {
977 #define ATTR_MAP(name) ENUM_MAP(#name, A_##name)
987 #define set_attribute(attr, name) map_enum(attr, attr_map, name)
989 static enum option_code
990 parse_step(double *opt
, const char *arg
)
993 if (!strchr(arg
, '%'))
996 /* "Shift down" so 100% and 1 does not conflict. */
997 *opt
= (*opt
- 1) / 100;
1000 return OPT_ERR_INVALID_STEP_VALUE
;
1004 return OPT_ERR_INVALID_STEP_VALUE
;
1009 static enum option_code
1010 parse_int(int *opt
, const char *arg
, int min
, int max
)
1012 int value
= atoi(arg
);
1014 if (min
<= value
&& value
<= max
) {
1019 return OPT_ERR_INTEGER_VALUE_OUT_OF_BOUND
;
1023 set_color(int *color
, const char *name
)
1025 if (map_enum(color
, color_map
, name
))
1027 if (!prefixcmp(name
, "color"))
1028 return parse_int(color
, name
+ 5, 0, 255) == OK
;
1032 /* Wants: object fgcolor bgcolor [attribute] */
1033 static enum option_code
1034 option_color_command(int argc
, const char *argv
[])
1036 struct line_info
*info
;
1039 return OPT_ERR_WRONG_NUMBER_OF_ARGUMENTS
;
1041 info
= get_line_info(argv
[0]);
1043 static const struct enum_map obsolete
[] = {
1044 ENUM_MAP("main-delim", LINE_DELIMITER
),
1045 ENUM_MAP("main-date", LINE_DATE
),
1046 ENUM_MAP("main-author", LINE_AUTHOR
),
1050 if (!map_enum(&index
, obsolete
, argv
[0]))
1051 return OPT_ERR_UNKNOWN_COLOR_NAME
;
1052 info
= &line_info
[index
];
1055 if (!set_color(&info
->fg
, argv
[1]) ||
1056 !set_color(&info
->bg
, argv
[2]))
1057 return OPT_ERR_UNKNOWN_COLOR
;
1060 while (argc
-- > 3) {
1063 if (!set_attribute(&attr
, argv
[argc
]))
1064 return OPT_ERR_UNKNOWN_ATTRIBUTE
;
1071 static enum option_code
1072 parse_bool(bool *opt
, const char *arg
)
1074 *opt
= (!strcmp(arg
, "1") || !strcmp(arg
, "true") || !strcmp(arg
, "yes"))
1079 static enum option_code
1080 parse_enum_do(unsigned int *opt
, const char *arg
,
1081 const struct enum_map
*map
, size_t map_size
)
1085 assert(map_size
> 1);
1087 if (map_enum_do(map
, map_size
, (int *) opt
, arg
))
1090 parse_bool(&is_true
, arg
);
1091 *opt
= is_true
? map
[1].value
: map
[0].value
;
1095 #define parse_enum(opt, arg, map) \
1096 parse_enum_do(opt, arg, map, ARRAY_SIZE(map))
1098 static enum option_code
1099 parse_string(char *opt
, const char *arg
, size_t optsize
)
1101 int arglen
= strlen(arg
);
1106 if (arglen
== 1 || arg
[arglen
- 1] != arg
[0])
1107 return OPT_ERR_UNMATCHED_QUOTATION
;
1108 arg
+= 1; arglen
-= 2;
1110 string_ncopy_do(opt
, optsize
, arg
, arglen
);
1115 static enum option_code
1116 parse_args(const char ***args
, const char *argv
[])
1118 if (*args
== NULL
&& !argv_copy(args
, argv
))
1119 return OPT_ERR_OUT_OF_MEMORY
;
1123 /* Wants: name = value */
1124 static enum option_code
1125 option_set_command(int argc
, const char *argv
[])
1128 return OPT_ERR_WRONG_NUMBER_OF_ARGUMENTS
;
1130 if (strcmp(argv
[1], "="))
1131 return OPT_ERR_NO_VALUE_ASSIGNED
;
1133 if (!strcmp(argv
[0], "blame-options"))
1134 return parse_args(&opt_blame_argv
, argv
+ 2);
1137 return OPT_ERR_WRONG_NUMBER_OF_ARGUMENTS
;
1139 if (!strcmp(argv
[0], "show-author"))
1140 return parse_enum(&opt_author
, argv
[2], author_map
);
1142 if (!strcmp(argv
[0], "show-date"))
1143 return parse_enum(&opt_date
, argv
[2], date_map
);
1145 if (!strcmp(argv
[0], "show-rev-graph"))
1146 return parse_bool(&opt_rev_graph
, argv
[2]);
1148 if (!strcmp(argv
[0], "show-refs"))
1149 return parse_bool(&opt_show_refs
, argv
[2]);
1151 if (!strcmp(argv
[0], "show-line-numbers"))
1152 return parse_bool(&opt_line_number
, argv
[2]);
1154 if (!strcmp(argv
[0], "line-graphics"))
1155 return parse_enum(&opt_line_graphics
, argv
[2], graphic_map
);
1157 if (!strcmp(argv
[0], "line-number-interval"))
1158 return parse_int(&opt_num_interval
, argv
[2], 1, 1024);
1160 if (!strcmp(argv
[0], "author-width"))
1161 return parse_int(&opt_author_cols
, argv
[2], 0, 1024);
1163 if (!strcmp(argv
[0], "horizontal-scroll"))
1164 return parse_step(&opt_hscroll
, argv
[2]);
1166 if (!strcmp(argv
[0], "split-view-height"))
1167 return parse_step(&opt_scale_split_view
, argv
[2]);
1169 if (!strcmp(argv
[0], "tab-size"))
1170 return parse_int(&opt_tab_size
, argv
[2], 1, 1024);
1172 if (!strcmp(argv
[0], "commit-encoding"))
1173 return parse_string(opt_encoding
, argv
[2], sizeof(opt_encoding
));
1175 if (!strcmp(argv
[0], "status-untracked-dirs"))
1176 return parse_bool(&opt_untracked_dirs_content
, argv
[2]);
1178 return OPT_ERR_UNKNOWN_VARIABLE_NAME
;
1181 /* Wants: mode request key */
1182 static enum option_code
1183 option_bind_command(int argc
, const char *argv
[])
1185 enum request request
;
1190 return OPT_ERR_WRONG_NUMBER_OF_ARGUMENTS
;
1192 if (!set_keymap(&keymap
, argv
[0]))
1193 return OPT_ERR_UNKNOWN_KEY_MAP
;
1195 key
= get_key_value(argv
[1]);
1197 return OPT_ERR_UNKNOWN_KEY
;
1199 request
= get_request(argv
[2]);
1200 if (request
== REQ_UNKNOWN
) {
1201 static const struct enum_map obsolete
[] = {
1202 ENUM_MAP("cherry-pick", REQ_NONE
),
1203 ENUM_MAP("screen-resize", REQ_NONE
),
1204 ENUM_MAP("tree-parent", REQ_PARENT
),
1208 if (map_enum(&alias
, obsolete
, argv
[2])) {
1209 if (alias
!= REQ_NONE
)
1210 add_keybinding(keymap
, alias
, key
);
1211 return OPT_ERR_OBSOLETE_REQUEST_NAME
;
1214 if (request
== REQ_UNKNOWN
&& *argv
[2]++ == '!')
1215 request
= add_run_request(keymap
, key
, argv
+ 2);
1216 if (request
== REQ_UNKNOWN
)
1217 return OPT_ERR_UNKNOWN_REQUEST_NAME
;
1219 add_keybinding(keymap
, request
, key
);
1224 static enum option_code
1225 set_option(const char *opt
, char *value
)
1227 const char *argv
[SIZEOF_ARG
];
1230 if (!argv_from_string(argv
, &argc
, value
))
1231 return OPT_ERR_TOO_MANY_OPTION_ARGUMENTS
;
1233 if (!strcmp(opt
, "color"))
1234 return option_color_command(argc
, argv
);
1236 if (!strcmp(opt
, "set"))
1237 return option_set_command(argc
, argv
);
1239 if (!strcmp(opt
, "bind"))
1240 return option_bind_command(argc
, argv
);
1242 return OPT_ERR_UNKNOWN_OPTION_COMMAND
;
1245 struct config_state
{
1251 read_option(char *opt
, size_t optlen
, char *value
, size_t valuelen
, void *data
)
1253 struct config_state
*config
= data
;
1254 enum option_code status
= OPT_ERR_NO_OPTION_VALUE
;
1258 /* Check for comment markers, since read_properties() will
1259 * only ensure opt and value are split at first " \t". */
1260 optlen
= strcspn(opt
, "#");
1264 if (opt
[optlen
] == 0) {
1265 /* Look for comment endings in the value. */
1266 size_t len
= strcspn(value
, "#");
1268 if (len
< valuelen
) {
1270 value
[valuelen
] = 0;
1273 status
= set_option(opt
, value
);
1276 if (status
!= OPT_OK
) {
1277 warn("Error on line %d, near '%.*s': %s",
1278 config
->lineno
, (int) optlen
, opt
, option_errors
[status
]);
1279 config
->errors
= TRUE
;
1282 /* Always keep going if errors are encountered. */
1287 load_option_file(const char *path
)
1289 struct config_state config
= { 0, FALSE
};
1292 /* It's OK that the file doesn't exist. */
1293 if (!io_open(&io
, "%s", path
))
1296 if (io_load(&io
, " \t", read_option
, &config
) == ERR
||
1297 config
.errors
== TRUE
)
1298 warn("Errors while loading %s.", path
);
1304 const char *home
= getenv("HOME");
1305 const char *tigrc_user
= getenv("TIGRC_USER");
1306 const char *tigrc_system
= getenv("TIGRC_SYSTEM");
1307 const char *tig_diff_opts
= getenv("TIG_DIFF_OPTS");
1308 char buf
[SIZEOF_STR
];
1311 tigrc_system
= SYSCONFDIR
"/tigrc";
1312 load_option_file(tigrc_system
);
1315 if (!home
|| !string_format(buf
, "%s/.tigrc", home
))
1319 load_option_file(tigrc_user
);
1321 /* Add _after_ loading config files to avoid adding run requests
1322 * that conflict with keybindings. */
1323 add_builtin_run_requests();
1325 if (!opt_diff_argv
&& tig_diff_opts
&& *tig_diff_opts
) {
1326 static const char *diff_opts
[SIZEOF_ARG
] = { NULL
};
1329 if (!string_format(buf
, "%s", tig_diff_opts
) ||
1330 !argv_from_string(diff_opts
, &argc
, buf
))
1331 die("TIG_DIFF_OPTS contains too many arguments");
1332 else if (!argv_copy(&opt_diff_argv
, diff_opts
))
1333 die("Failed to format TIG_DIFF_OPTS arguments");
1347 /* The display array of active views and the index of the current view. */
1348 static struct view
*display
[2];
1349 static WINDOW
*display_win
[2];
1350 static WINDOW
*display_title
[2];
1351 static unsigned int current_view
;
1353 #define foreach_displayed_view(view, i) \
1354 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
1356 #define displayed_views() (display[1] != NULL ? 2 : 1)
1358 /* Current head and commit ID */
1359 static char ref_blob
[SIZEOF_REF
] = "";
1360 static char ref_commit
[SIZEOF_REF
] = "HEAD";
1361 static char ref_head
[SIZEOF_REF
] = "HEAD";
1362 static char ref_branch
[SIZEOF_REF
] = "";
1379 enum view_type type
; /* View type */
1380 const char *name
; /* View name */
1381 const char *id
; /* Points to either of ref_{head,commit,blob} */
1383 struct view_ops
*ops
; /* View operations */
1385 enum keymap keymap
; /* What keymap does this view have */
1386 bool git_dir
; /* Whether the view requires a git directory. */
1388 char ref
[SIZEOF_REF
]; /* Hovered commit reference */
1389 char vid
[SIZEOF_REF
]; /* View ID. Set to id member when updating. */
1391 int height
, width
; /* The width and height of the main window */
1392 WINDOW
*win
; /* The main window */
1395 unsigned long offset
; /* Offset of the window top */
1396 unsigned long yoffset
; /* Offset from the window side. */
1397 unsigned long lineno
; /* Current line number */
1398 unsigned long p_offset
; /* Previous offset of the window top */
1399 unsigned long p_yoffset
;/* Previous offset from the window side */
1400 unsigned long p_lineno
; /* Previous current line number */
1401 bool p_restore
; /* Should the previous position be restored. */
1404 char grep
[SIZEOF_STR
]; /* Search string */
1405 regex_t
*regex
; /* Pre-compiled regexp */
1407 /* If non-NULL, points to the view that opened this view. If this view
1408 * is closed tig will switch back to the parent view. */
1409 struct view
*parent
;
1413 size_t lines
; /* Total number of lines */
1414 struct line
*line
; /* Line index */
1415 unsigned int digits
; /* Number of digits in the lines member. */
1418 struct line
*curline
; /* Line currently being drawn. */
1419 enum line_type curtype
; /* Attribute currently used for drawing. */
1420 unsigned long col
; /* Column when drawing. */
1421 bool has_scrolled
; /* View was scrolled. */
1424 const char **argv
; /* Shell command arguments. */
1425 const char *dir
; /* Directory from which to execute. */
1433 OPEN_DEFAULT
= 0, /* Use default view switching. */
1434 OPEN_SPLIT
= 1, /* Split current view. */
1435 OPEN_RELOAD
= 4, /* Reload view even if it is the current. */
1436 OPEN_REFRESH
= 16, /* Refresh view using previous command. */
1437 OPEN_PREPARED
= 32, /* Open already prepared command. */
1441 /* What type of content being displayed. Used in the title bar. */
1443 /* Default command arguments. */
1445 /* Open and reads in all view content. */
1446 bool (*open
)(struct view
*view
, enum open_flags flags
);
1447 /* Read one line; updates view->line. */
1448 bool (*read
)(struct view
*view
, char *data
);
1449 /* Draw one line; @lineno must be < view->height. */
1450 bool (*draw
)(struct view
*view
, struct line
*line
, unsigned int lineno
);
1451 /* Depending on view handle a special requests. */
1452 enum request (*request
)(struct view
*view
, enum request request
, struct line
*line
);
1453 /* Search for regexp in a line. */
1454 bool (*grep
)(struct view
*view
, struct line
*line
);
1456 void (*select
)(struct view
*view
, struct line
*line
);
1457 /* Prepare view for loading */
1458 bool (*prepare
)(struct view
*view
);
1461 static struct view_ops blame_ops
;
1462 static struct view_ops blob_ops
;
1463 static struct view_ops diff_ops
;
1464 static struct view_ops help_ops
;
1465 static struct view_ops log_ops
;
1466 static struct view_ops main_ops
;
1467 static struct view_ops pager_ops
;
1468 static struct view_ops stage_ops
;
1469 static struct view_ops status_ops
;
1470 static struct view_ops tree_ops
;
1471 static struct view_ops branch_ops
;
1473 #define VIEW_STR(type, name, ref, ops, map, git) \
1474 { type, name, ref, ops, map, git }
1476 #define VIEW_(id, name, ops, git, ref) \
1477 VIEW_STR(VIEW_##id, name, ref, ops, KEYMAP_##id, git)
1479 static struct view views
[] = {
1480 VIEW_(MAIN
, "main", &main_ops
, TRUE
, ref_head
),
1481 VIEW_(DIFF
, "diff", &diff_ops
, TRUE
, ref_commit
),
1482 VIEW_(LOG
, "log", &log_ops
, TRUE
, ref_head
),
1483 VIEW_(TREE
, "tree", &tree_ops
, TRUE
, ref_commit
),
1484 VIEW_(BLOB
, "blob", &blob_ops
, TRUE
, ref_blob
),
1485 VIEW_(BLAME
, "blame", &blame_ops
, TRUE
, ref_commit
),
1486 VIEW_(BRANCH
, "branch", &branch_ops
, TRUE
, ref_head
),
1487 VIEW_(HELP
, "help", &help_ops
, FALSE
, ""),
1488 VIEW_(PAGER
, "pager", &pager_ops
, FALSE
, ""),
1489 VIEW_(STATUS
, "status", &status_ops
, TRUE
, ""),
1490 VIEW_(STAGE
, "stage", &stage_ops
, TRUE
, ""),
1493 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
1495 #define foreach_view(view, i) \
1496 for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++)
1498 #define view_is_displayed(view) \
1499 (view == display[0] || view == display[1])
1502 view_request(struct view
*view
, enum request request
)
1504 if (!view
|| !view
->lines
)
1506 return view
->ops
->request(view
, request
, &view
->line
[view
->lineno
]);
1515 set_view_attr(struct view
*view
, enum line_type type
)
1517 if (!view
->curline
->selected
&& view
->curtype
!= type
) {
1518 (void) wattrset(view
->win
, get_line_attr(type
));
1519 wchgat(view
->win
, -1, 0, type
, NULL
);
1520 view
->curtype
= type
;
1524 #define VIEW_MAX_LEN(view) ((view)->width + (view)->yoffset - (view)->col)
1527 draw_chars(struct view
*view
, enum line_type type
, const char *string
,
1528 int max_len
, bool use_tilde
)
1530 static char out_buffer
[BUFSIZ
* 2];
1533 int trimmed
= FALSE
;
1534 size_t skip
= view
->yoffset
> view
->col
? view
->yoffset
- view
->col
: 0;
1539 len
= utf8_length(&string
, skip
, &col
, max_len
, &trimmed
, use_tilde
, opt_tab_size
);
1541 set_view_attr(view
, type
);
1543 if (opt_iconv_out
!= ICONV_NONE
) {
1544 ICONV_CONST
char *inbuf
= (ICONV_CONST
char *) string
;
1545 size_t inlen
= len
+ 1;
1547 char *outbuf
= out_buffer
;
1548 size_t outlen
= sizeof(out_buffer
);
1552 ret
= iconv(opt_iconv_out
, &inbuf
, &inlen
, &outbuf
, &outlen
);
1553 if (ret
!= (size_t) -1) {
1554 string
= out_buffer
;
1555 len
= sizeof(out_buffer
) - outlen
;
1559 waddnstr(view
->win
, string
, len
);
1561 if (trimmed
&& use_tilde
) {
1562 set_view_attr(view
, LINE_DELIMITER
);
1563 waddch(view
->win
, '~');
1572 draw_space(struct view
*view
, enum line_type type
, int max
, int spaces
)
1574 static char space
[] = " ";
1577 spaces
= MIN(max
, spaces
);
1579 while (spaces
> 0) {
1580 int len
= MIN(spaces
, sizeof(space
) - 1);
1582 col
+= draw_chars(view
, type
, space
, len
, FALSE
);
1590 draw_text(struct view
*view
, enum line_type type
, const char *string
)
1592 char text
[SIZEOF_STR
];
1595 size_t pos
= string_expand(text
, sizeof(text
), string
, opt_tab_size
);
1597 view
->col
+= draw_chars(view
, type
, text
, VIEW_MAX_LEN(view
), TRUE
);
1599 } while (*string
&& VIEW_MAX_LEN(view
) > 0);
1601 return VIEW_MAX_LEN(view
) <= 0;
1605 draw_graphic(struct view
*view
, enum line_type type
, const chtype graphic
[], size_t size
, bool separator
)
1607 size_t skip
= view
->yoffset
> view
->col
? view
->yoffset
- view
->col
: 0;
1608 int max
= VIEW_MAX_LEN(view
);
1614 set_view_attr(view
, type
);
1615 /* Using waddch() instead of waddnstr() ensures that
1616 * they'll be rendered correctly for the cursor line. */
1617 for (i
= skip
; i
< size
; i
++)
1618 waddch(view
->win
, graphic
[i
]);
1622 if (size
< max
&& skip
<= size
)
1623 waddch(view
->win
, ' ');
1627 return VIEW_MAX_LEN(view
) <= 0;
1631 draw_field(struct view
*view
, enum line_type type
, const char *text
, int len
, bool trim
)
1633 int max
= MIN(VIEW_MAX_LEN(view
), len
);
1637 col
= draw_chars(view
, type
, text
, max
- 1, trim
);
1639 col
= draw_space(view
, type
, max
- 1, max
- 1);
1642 view
->col
+= draw_space(view
, LINE_DEFAULT
, max
- col
, max
- col
);
1643 return VIEW_MAX_LEN(view
) <= 0;
1647 draw_date(struct view
*view
, struct time
*time
)
1649 const char *date
= mkdate(time
, opt_date
);
1650 int cols
= opt_date
== DATE_SHORT
? DATE_SHORT_COLS
: DATE_COLS
;
1652 return draw_field(view
, LINE_DATE
, date
, cols
, FALSE
);
1656 draw_author(struct view
*view
, const char *author
)
1658 bool trim
= opt_author_cols
== 0 || opt_author_cols
> 5;
1659 bool abbreviate
= opt_author
== AUTHOR_ABBREVIATED
|| !trim
;
1661 if (abbreviate
&& author
)
1662 author
= get_author_initials(author
);
1664 return draw_field(view
, LINE_AUTHOR
, author
, opt_author_cols
, trim
);
1668 draw_mode(struct view
*view
, mode_t mode
)
1674 else if (S_ISLNK(mode
))
1676 else if (S_ISGITLINK(mode
))
1678 else if (S_ISREG(mode
) && mode
& S_IXUSR
)
1680 else if (S_ISREG(mode
))
1685 return draw_field(view
, LINE_MODE
, str
, STRING_SIZE("-rw-r--r-- "), FALSE
);
1689 draw_lineno(struct view
*view
, unsigned int lineno
)
1692 int digits3
= view
->digits
< 3 ? 3 : view
->digits
;
1693 int max
= MIN(VIEW_MAX_LEN(view
), digits3
);
1695 chtype separator
= opt_line_graphics
? ACS_VLINE
: '|';
1697 lineno
+= view
->offset
+ 1;
1698 if (lineno
== 1 || (lineno
% opt_num_interval
) == 0) {
1699 static char fmt
[] = "%1ld";
1701 fmt
[1] = '0' + (view
->digits
<= 9 ? digits3
: 1);
1702 if (string_format(number
, fmt
, lineno
))
1706 view
->col
+= draw_chars(view
, LINE_LINE_NUMBER
, text
, max
, TRUE
);
1708 view
->col
+= draw_space(view
, LINE_LINE_NUMBER
, max
, digits3
);
1709 return draw_graphic(view
, LINE_DEFAULT
, &separator
, 1, TRUE
);
1713 draw_view_line(struct view
*view
, unsigned int lineno
)
1716 bool selected
= (view
->offset
+ lineno
== view
->lineno
);
1718 assert(view_is_displayed(view
));
1720 if (view
->offset
+ lineno
>= view
->lines
)
1723 line
= &view
->line
[view
->offset
+ lineno
];
1725 wmove(view
->win
, lineno
, 0);
1727 wclrtoeol(view
->win
);
1729 view
->curline
= line
;
1730 view
->curtype
= LINE_NONE
;
1731 line
->selected
= FALSE
;
1732 line
->dirty
= line
->cleareol
= 0;
1735 set_view_attr(view
, LINE_CURSOR
);
1736 line
->selected
= TRUE
;
1737 view
->ops
->select(view
, line
);
1740 return view
->ops
->draw(view
, line
, lineno
);
1744 redraw_view_dirty(struct view
*view
)
1749 for (lineno
= 0; lineno
< view
->height
; lineno
++) {
1750 if (view
->offset
+ lineno
>= view
->lines
)
1752 if (!view
->line
[view
->offset
+ lineno
].dirty
)
1755 if (!draw_view_line(view
, lineno
))
1761 wnoutrefresh(view
->win
);
1765 redraw_view_from(struct view
*view
, int lineno
)
1767 assert(0 <= lineno
&& lineno
< view
->height
);
1769 for (; lineno
< view
->height
; lineno
++) {
1770 if (!draw_view_line(view
, lineno
))
1774 wnoutrefresh(view
->win
);
1778 redraw_view(struct view
*view
)
1781 redraw_view_from(view
, 0);
1786 update_view_title(struct view
*view
)
1788 char buf
[SIZEOF_STR
];
1789 char state
[SIZEOF_STR
];
1790 size_t bufpos
= 0, statelen
= 0;
1791 WINDOW
*window
= display
[0] == view
? display_title
[0] : display_title
[1];
1793 assert(view_is_displayed(view
));
1795 if (view
->type
!= VIEW_STATUS
&& view
->lines
) {
1796 unsigned int view_lines
= view
->offset
+ view
->height
;
1797 unsigned int lines
= view
->lines
1798 ? MIN(view_lines
, view
->lines
) * 100 / view
->lines
1801 string_format_from(state
, &statelen
, " - %s %d of %d (%d%%)",
1810 time_t secs
= time(NULL
) - view
->start_time
;
1812 /* Three git seconds are a long time ... */
1814 string_format_from(state
, &statelen
, " loading %lds", secs
);
1817 string_format_from(buf
, &bufpos
, "[%s]", view
->name
);
1818 if (*view
->ref
&& bufpos
< view
->width
) {
1819 size_t refsize
= strlen(view
->ref
);
1820 size_t minsize
= bufpos
+ 1 + /* abbrev= */ 7 + 1 + statelen
;
1822 if (minsize
< view
->width
)
1823 refsize
= view
->width
- minsize
+ 7;
1824 string_format_from(buf
, &bufpos
, " %.*s", (int) refsize
, view
->ref
);
1827 if (statelen
&& bufpos
< view
->width
) {
1828 string_format_from(buf
, &bufpos
, "%s", state
);
1831 if (view
== display
[current_view
])
1832 wbkgdset(window
, get_line_attr(LINE_TITLE_FOCUS
));
1834 wbkgdset(window
, get_line_attr(LINE_TITLE_BLUR
));
1836 mvwaddnstr(window
, 0, 0, buf
, bufpos
);
1838 wnoutrefresh(window
);
1842 apply_step(double step
, int value
)
1846 value
*= step
+ 0.01;
1847 return value
? value
: 1;
1851 resize_display(void)
1854 struct view
*base
= display
[0];
1855 struct view
*view
= display
[1] ? display
[1] : display
[0];
1857 /* Setup window dimensions */
1859 getmaxyx(stdscr
, base
->height
, base
->width
);
1861 /* Make room for the status window. */
1865 /* Horizontal split. */
1866 view
->width
= base
->width
;
1867 view
->height
= apply_step(opt_scale_split_view
, base
->height
);
1868 view
->height
= MAX(view
->height
, MIN_VIEW_HEIGHT
);
1869 view
->height
= MIN(view
->height
, base
->height
- MIN_VIEW_HEIGHT
);
1870 base
->height
-= view
->height
;
1872 /* Make room for the title bar. */
1876 /* Make room for the title bar. */
1881 foreach_displayed_view (view
, i
) {
1882 if (!display_win
[i
]) {
1883 display_win
[i
] = newwin(view
->height
, view
->width
, offset
, 0);
1884 if (!display_win
[i
])
1885 die("Failed to create %s view", view
->name
);
1887 scrollok(display_win
[i
], FALSE
);
1889 display_title
[i
] = newwin(1, view
->width
, offset
+ view
->height
, 0);
1890 if (!display_title
[i
])
1891 die("Failed to create title window");
1894 wresize(display_win
[i
], view
->height
, view
->width
);
1895 mvwin(display_win
[i
], offset
, 0);
1896 mvwin(display_title
[i
], offset
+ view
->height
, 0);
1899 view
->win
= display_win
[i
];
1901 offset
+= view
->height
+ 1;
1906 redraw_display(bool clear
)
1911 foreach_displayed_view (view
, i
) {
1915 update_view_title(view
);
1924 #define TOGGLE_MENU \
1925 TOGGLE_(LINENO, '.', "line numbers", &opt_line_number, NULL) \
1926 TOGGLE_(DATE, 'D', "dates", &opt_date, date_map) \
1927 TOGGLE_(AUTHOR, 'A', "author names", &opt_author, author_map) \
1928 TOGGLE_(GRAPHIC, '~', "graphics", &opt_line_graphics, graphic_map) \
1929 TOGGLE_(REV_GRAPH, 'g', "revision graph", &opt_rev_graph, NULL) \
1930 TOGGLE_(REFS, 'F', "reference display", &opt_show_refs, NULL)
1933 toggle_option(enum request request
)
1936 enum request request
;
1937 const struct enum_map
*map
;
1940 #define TOGGLE_(id, key, help, value, map) { REQ_TOGGLE_ ## id, map, ARRAY_SIZE(map) },
1944 const struct menu_item menu
[] = {
1945 #define TOGGLE_(id, key, help, value, map) { key, help, value },
1952 if (request
== REQ_OPTIONS
) {
1953 if (!prompt_menu("Toggle option", menu
, &i
))
1956 while (i
< ARRAY_SIZE(data
) && data
[i
].request
!= request
)
1958 if (i
>= ARRAY_SIZE(data
))
1959 die("Invalid request (%d)", request
);
1962 if (data
[i
].map
!= NULL
) {
1963 unsigned int *opt
= menu
[i
].data
;
1965 *opt
= (*opt
+ 1) % data
[i
].map_size
;
1966 redraw_display(FALSE
);
1967 report("Displaying %s %s", enum_name(data
[i
].map
[*opt
]), menu
[i
].text
);
1970 bool *option
= menu
[i
].data
;
1973 redraw_display(FALSE
);
1974 report("%sabling %s", *option
? "En" : "Dis", menu
[i
].text
);
1979 maximize_view(struct view
*view
)
1981 memset(display
, 0, sizeof(display
));
1983 display
[current_view
] = view
;
1985 redraw_display(FALSE
);
1995 goto_view_line(struct view
*view
, unsigned long offset
, unsigned long lineno
)
1997 if (lineno
>= view
->lines
)
1998 lineno
= view
->lines
> 0 ? view
->lines
- 1 : 0;
2000 if (offset
> lineno
|| offset
+ view
->height
<= lineno
) {
2001 unsigned long half
= view
->height
/ 2;
2004 offset
= lineno
- half
;
2009 if (offset
!= view
->offset
|| lineno
!= view
->lineno
) {
2010 view
->offset
= offset
;
2011 view
->lineno
= lineno
;
2018 /* Scrolling backend */
2020 do_scroll_view(struct view
*view
, int lines
)
2022 bool redraw_current_line
= FALSE
;
2024 /* The rendering expects the new offset. */
2025 view
->offset
+= lines
;
2027 assert(0 <= view
->offset
&& view
->offset
< view
->lines
);
2030 /* Move current line into the view. */
2031 if (view
->lineno
< view
->offset
) {
2032 view
->lineno
= view
->offset
;
2033 redraw_current_line
= TRUE
;
2034 } else if (view
->lineno
>= view
->offset
+ view
->height
) {
2035 view
->lineno
= view
->offset
+ view
->height
- 1;
2036 redraw_current_line
= TRUE
;
2039 assert(view
->offset
<= view
->lineno
&& view
->lineno
< view
->lines
);
2041 /* Redraw the whole screen if scrolling is pointless. */
2042 if (view
->height
< ABS(lines
)) {
2046 int line
= lines
> 0 ? view
->height
- lines
: 0;
2047 int end
= line
+ ABS(lines
);
2049 scrollok(view
->win
, TRUE
);
2050 wscrl(view
->win
, lines
);
2051 scrollok(view
->win
, FALSE
);
2053 while (line
< end
&& draw_view_line(view
, line
))
2056 if (redraw_current_line
)
2057 draw_view_line(view
, view
->lineno
- view
->offset
);
2058 wnoutrefresh(view
->win
);
2061 view
->has_scrolled
= TRUE
;
2065 /* Scroll frontend */
2067 scroll_view(struct view
*view
, enum request request
)
2071 assert(view_is_displayed(view
));
2074 case REQ_SCROLL_FIRST_COL
:
2076 redraw_view_from(view
, 0);
2079 case REQ_SCROLL_LEFT
:
2080 if (view
->yoffset
== 0) {
2081 report("Cannot scroll beyond the first column");
2084 if (view
->yoffset
<= apply_step(opt_hscroll
, view
->width
))
2087 view
->yoffset
-= apply_step(opt_hscroll
, view
->width
);
2088 redraw_view_from(view
, 0);
2091 case REQ_SCROLL_RIGHT
:
2092 view
->yoffset
+= apply_step(opt_hscroll
, view
->width
);
2096 case REQ_SCROLL_PAGE_DOWN
:
2097 lines
= view
->height
;
2098 case REQ_SCROLL_LINE_DOWN
:
2099 if (view
->offset
+ lines
> view
->lines
)
2100 lines
= view
->lines
- view
->offset
;
2102 if (lines
== 0 || view
->offset
+ view
->height
>= view
->lines
) {
2103 report("Cannot scroll beyond the last line");
2108 case REQ_SCROLL_PAGE_UP
:
2109 lines
= view
->height
;
2110 case REQ_SCROLL_LINE_UP
:
2111 if (lines
> view
->offset
)
2112 lines
= view
->offset
;
2115 report("Cannot scroll beyond the first line");
2123 die("request %d not handled in switch", request
);
2126 do_scroll_view(view
, lines
);
2131 move_view(struct view
*view
, enum request request
)
2133 int scroll_steps
= 0;
2137 case REQ_MOVE_FIRST_LINE
:
2138 steps
= -view
->lineno
;
2141 case REQ_MOVE_LAST_LINE
:
2142 steps
= view
->lines
- view
->lineno
- 1;
2145 case REQ_MOVE_PAGE_UP
:
2146 steps
= view
->height
> view
->lineno
2147 ? -view
->lineno
: -view
->height
;
2150 case REQ_MOVE_PAGE_DOWN
:
2151 steps
= view
->lineno
+ view
->height
>= view
->lines
2152 ? view
->lines
- view
->lineno
- 1 : view
->height
;
2164 die("request %d not handled in switch", request
);
2167 if (steps
<= 0 && view
->lineno
== 0) {
2168 report("Cannot move beyond the first line");
2171 } else if (steps
>= 0 && view
->lineno
+ 1 >= view
->lines
) {
2172 report("Cannot move beyond the last line");
2176 /* Move the current line */
2177 view
->lineno
+= steps
;
2178 assert(0 <= view
->lineno
&& view
->lineno
< view
->lines
);
2180 /* Check whether the view needs to be scrolled */
2181 if (view
->lineno
< view
->offset
||
2182 view
->lineno
>= view
->offset
+ view
->height
) {
2183 scroll_steps
= steps
;
2184 if (steps
< 0 && -steps
> view
->offset
) {
2185 scroll_steps
= -view
->offset
;
2187 } else if (steps
> 0) {
2188 if (view
->lineno
== view
->lines
- 1 &&
2189 view
->lines
> view
->height
) {
2190 scroll_steps
= view
->lines
- view
->offset
- 1;
2191 if (scroll_steps
>= view
->height
)
2192 scroll_steps
-= view
->height
- 1;
2197 if (!view_is_displayed(view
)) {
2198 view
->offset
+= scroll_steps
;
2199 assert(0 <= view
->offset
&& view
->offset
< view
->lines
);
2200 view
->ops
->select(view
, &view
->line
[view
->lineno
]);
2204 /* Repaint the old "current" line if we be scrolling */
2205 if (ABS(steps
) < view
->height
)
2206 draw_view_line(view
, view
->lineno
- steps
- view
->offset
);
2209 do_scroll_view(view
, scroll_steps
);
2213 /* Draw the current line */
2214 draw_view_line(view
, view
->lineno
- view
->offset
);
2216 wnoutrefresh(view
->win
);
2225 static void search_view(struct view
*view
, enum request request
);
2228 grep_text(struct view
*view
, const char *text
[])
2233 for (i
= 0; text
[i
]; i
++)
2235 regexec(view
->regex
, text
[i
], 1, &pmatch
, 0) != REG_NOMATCH
)
2241 select_view_line(struct view
*view
, unsigned long lineno
)
2243 unsigned long old_lineno
= view
->lineno
;
2244 unsigned long old_offset
= view
->offset
;
2246 if (goto_view_line(view
, view
->offset
, lineno
)) {
2247 if (view_is_displayed(view
)) {
2248 if (old_offset
!= view
->offset
) {
2251 draw_view_line(view
, old_lineno
- view
->offset
);
2252 draw_view_line(view
, view
->lineno
- view
->offset
);
2253 wnoutrefresh(view
->win
);
2256 view
->ops
->select(view
, &view
->line
[view
->lineno
]);
2262 find_next(struct view
*view
, enum request request
)
2264 unsigned long lineno
= view
->lineno
;
2269 report("No previous search");
2271 search_view(view
, request
);
2281 case REQ_SEARCH_BACK
:
2290 if (request
== REQ_FIND_NEXT
|| request
== REQ_FIND_PREV
)
2291 lineno
+= direction
;
2293 /* Note, lineno is unsigned long so will wrap around in which case it
2294 * will become bigger than view->lines. */
2295 for (; lineno
< view
->lines
; lineno
+= direction
) {
2296 if (view
->ops
->grep(view
, &view
->line
[lineno
])) {
2297 select_view_line(view
, lineno
);
2298 report("Line %ld matches '%s'", lineno
+ 1, view
->grep
);
2303 report("No match found for '%s'", view
->grep
);
2307 search_view(struct view
*view
, enum request request
)
2312 regfree(view
->regex
);
2315 view
->regex
= calloc(1, sizeof(*view
->regex
));
2320 regex_err
= regcomp(view
->regex
, opt_search
, REG_EXTENDED
);
2321 if (regex_err
!= 0) {
2322 char buf
[SIZEOF_STR
] = "unknown error";
2324 regerror(regex_err
, view
->regex
, buf
, sizeof(buf
));
2325 report("Search failed: %s", buf
);
2329 string_copy(view
->grep
, opt_search
);
2331 find_next(view
, request
);
2335 * Incremental updating
2339 reset_view(struct view
*view
)
2343 for (i
= 0; i
< view
->lines
; i
++)
2344 free(view
->line
[i
].data
);
2347 view
->p_offset
= view
->offset
;
2348 view
->p_yoffset
= view
->yoffset
;
2349 view
->p_lineno
= view
->lineno
;
2357 view
->update_secs
= 0;
2361 format_arg(const char *name
)
2367 const char *value_if_empty
;
2369 #define FORMAT_VAR(name, value, value_if_empty) \
2370 { name, STRING_SIZE(name), value, value_if_empty }
2371 FORMAT_VAR("%(directory)", opt_path
, "."),
2372 FORMAT_VAR("%(file)", opt_file
, ""),
2373 FORMAT_VAR("%(ref)", opt_ref
, "HEAD"),
2374 FORMAT_VAR("%(head)", ref_head
, ""),
2375 FORMAT_VAR("%(commit)", ref_commit
, ""),
2376 FORMAT_VAR("%(blob)", ref_blob
, ""),
2377 FORMAT_VAR("%(branch)", ref_branch
, ""),
2381 for (i
= 0; i
< ARRAY_SIZE(vars
); i
++)
2382 if (!strncmp(name
, vars
[i
].name
, vars
[i
].namelen
))
2383 return *vars
[i
].value
? vars
[i
].value
: vars
[i
].value_if_empty
;
2385 report("Unknown replacement: `%s`", name
);
2390 format_argv(const char ***dst_argv
, const char *src_argv
[], bool replace
, bool first
)
2392 char buf
[SIZEOF_STR
];
2395 argv_free(*dst_argv
);
2397 for (argc
= 0; src_argv
[argc
]; argc
++) {
2398 const char *arg
= src_argv
[argc
];
2401 if (!strcmp(arg
, "%(fileargs)")) {
2402 if (!argv_append_array(dst_argv
, opt_file_argv
))
2406 } else if (!strcmp(arg
, "%(diffargs)")) {
2407 if (!argv_append_array(dst_argv
, opt_diff_argv
))
2411 } else if (!strcmp(arg
, "%(blameargs)")) {
2412 if (!argv_append_array(dst_argv
, opt_blame_argv
))
2416 } else if (!strcmp(arg
, "%(revargs)") ||
2417 (first
&& !strcmp(arg
, "%(commit)"))) {
2418 if (!argv_append_array(dst_argv
, opt_rev_argv
))
2424 char *next
= strstr(arg
, "%(");
2425 int len
= next
- arg
;
2428 if (!next
|| !replace
) {
2433 value
= format_arg(next
);
2440 if (!string_format_from(buf
, &bufpos
, "%.*s%s", len
, arg
, value
))
2443 arg
= next
&& replace
? strchr(next
, ')') + 1 : NULL
;
2446 if (!argv_append(dst_argv
, buf
))
2450 return src_argv
[argc
] == NULL
;
2454 restore_view_position(struct view
*view
)
2456 if (!view
->p_restore
|| (view
->pipe
&& view
->lines
<= view
->p_lineno
))
2459 /* Changing the view position cancels the restoring. */
2460 /* FIXME: Changing back to the first line is not detected. */
2461 if (view
->offset
!= 0 || view
->lineno
!= 0) {
2462 view
->p_restore
= FALSE
;
2466 if (goto_view_line(view
, view
->p_offset
, view
->p_lineno
) &&
2467 view_is_displayed(view
))
2470 view
->yoffset
= view
->p_yoffset
;
2471 view
->p_restore
= FALSE
;
2477 end_update(struct view
*view
, bool force
)
2481 while (!view
->ops
->read(view
, NULL
))
2485 io_kill(view
->pipe
);
2486 io_done(view
->pipe
);
2491 setup_update(struct view
*view
, const char *vid
)
2494 string_copy_rev(view
->vid
, vid
);
2495 view
->pipe
= &view
->io
;
2496 view
->start_time
= time(NULL
);
2500 prepare_io(struct view
*view
, const char *dir
, const char *argv
[], bool replace
)
2503 return format_argv(&view
->argv
, argv
, replace
, !view
->prev
);
2507 prepare_update(struct view
*view
, const char *argv
[], const char *dir
)
2510 end_update(view
, TRUE
);
2511 return prepare_io(view
, dir
, argv
, FALSE
);
2515 start_update(struct view
*view
, const char **argv
, const char *dir
)
2518 io_done(view
->pipe
);
2519 return prepare_io(view
, dir
, argv
, FALSE
) &&
2520 io_run(&view
->io
, IO_RD
, dir
, view
->argv
);
2524 prepare_update_file(struct view
*view
, const char *name
)
2527 end_update(view
, TRUE
);
2528 argv_free(view
->argv
);
2529 return io_open(&view
->io
, "%s/%s", opt_cdup
[0] ? opt_cdup
: ".", name
);
2533 view_open(struct view
*view
, enum open_flags flags
)
2535 bool reload
= !!(flags
& (OPEN_RELOAD
| OPEN_REFRESH
| OPEN_PREPARED
));
2536 bool refresh
= flags
& (OPEN_REFRESH
| OPEN_PREPARED
);
2538 if (!reload
&& !strcmp(view
->vid
, view
->id
))
2542 end_update(view
, TRUE
);
2545 if (view
->ops
->prepare
) {
2546 if (!view
->ops
->prepare(view
))
2548 } else if (!prepare_io(view
, NULL
, view
->ops
->argv
, TRUE
)) {
2552 /* Put the current ref_* value to the view title ref
2553 * member. This is needed by the blob view. Most other
2554 * views sets it automatically after loading because the
2555 * first line is a commit line. */
2556 string_copy_rev(view
->ref
, view
->id
);
2559 if (view
->argv
&& view
->argv
[0] &&
2560 !io_run(&view
->io
, IO_RD
, view
->dir
, view
->argv
))
2563 setup_update(view
, view
->id
);
2569 update_view(struct view
*view
)
2571 char out_buffer
[BUFSIZ
* 2];
2573 /* Clear the view and redraw everything since the tree sorting
2574 * might have rearranged things. */
2575 bool redraw
= view
->lines
== 0;
2576 bool can_read
= TRUE
;
2581 if (!io_can_read(view
->pipe
, FALSE
)) {
2582 if (view
->lines
== 0 && view_is_displayed(view
)) {
2583 time_t secs
= time(NULL
) - view
->start_time
;
2585 if (secs
> 1 && secs
> view
->update_secs
) {
2586 if (view
->update_secs
== 0)
2588 update_view_title(view
);
2589 view
->update_secs
= secs
;
2595 for (; (line
= io_get(view
->pipe
, '\n', can_read
)); can_read
= FALSE
) {
2596 if (opt_iconv_in
!= ICONV_NONE
) {
2597 ICONV_CONST
char *inbuf
= line
;
2598 size_t inlen
= strlen(line
) + 1;
2600 char *outbuf
= out_buffer
;
2601 size_t outlen
= sizeof(out_buffer
);
2605 ret
= iconv(opt_iconv_in
, &inbuf
, &inlen
, &outbuf
, &outlen
);
2606 if (ret
!= (size_t) -1)
2610 if (!view
->ops
->read(view
, line
)) {
2611 report("Allocation failure");
2612 end_update(view
, TRUE
);
2618 unsigned long lines
= view
->lines
;
2621 for (digits
= 0; lines
; digits
++)
2624 /* Keep the displayed view in sync with line number scaling. */
2625 if (digits
!= view
->digits
) {
2626 view
->digits
= digits
;
2627 if (opt_line_number
|| view
->type
== VIEW_BLAME
)
2632 if (io_error(view
->pipe
)) {
2633 report("Failed to read: %s", io_strerror(view
->pipe
));
2634 end_update(view
, TRUE
);
2636 } else if (io_eof(view
->pipe
)) {
2637 if (view_is_displayed(view
))
2639 end_update(view
, FALSE
);
2642 if (restore_view_position(view
))
2645 if (!view_is_displayed(view
))
2649 redraw_view_from(view
, 0);
2651 redraw_view_dirty(view
);
2653 /* Update the title _after_ the redraw so that if the redraw picks up a
2654 * commit reference in view->ref it'll be available here. */
2655 update_view_title(view
);
2659 DEFINE_ALLOCATOR(realloc_lines
, struct line
, 256)
2661 static struct line
*
2662 add_line_data(struct view
*view
, void *data
, enum line_type type
)
2666 if (!realloc_lines(&view
->line
, view
->lines
, 1))
2669 line
= &view
->line
[view
->lines
++];
2670 memset(line
, 0, sizeof(*line
));
2678 static struct line
*
2679 add_line_text(struct view
*view
, const char *text
, enum line_type type
)
2681 char *data
= text
? strdup(text
) : NULL
;
2683 return data
? add_line_data(view
, data
, type
) : NULL
;
2686 static struct line
*
2687 add_line_format(struct view
*view
, enum line_type type
, const char *fmt
, ...)
2689 char buf
[SIZEOF_STR
];
2692 va_start(args
, fmt
);
2693 if (vsnprintf(buf
, sizeof(buf
), fmt
, args
) >= sizeof(buf
))
2697 return buf
[0] ? add_line_text(view
, buf
, type
) : NULL
;
2705 open_view(struct view
*prev
, enum request request
, enum open_flags flags
)
2707 bool split
= !!(flags
& OPEN_SPLIT
);
2708 bool reload
= !!(flags
& (OPEN_RELOAD
| OPEN_REFRESH
| OPEN_PREPARED
));
2709 bool nomaximize
= !!(flags
& OPEN_REFRESH
);
2710 struct view
*view
= VIEW(request
);
2711 int nviews
= displayed_views();
2712 struct view
*base_view
= display
[0];
2714 if (view
== prev
&& nviews
== 1 && !reload
) {
2715 report("Already in %s view", view
->name
);
2719 if (view
->git_dir
&& !opt_git_dir
[0]) {
2720 report("The %s view is disabled in pager view", view
->name
);
2727 view
->parent
= prev
;
2728 } else if (!nomaximize
) {
2729 /* Maximize the current view. */
2730 memset(display
, 0, sizeof(display
));
2732 display
[current_view
] = view
;
2735 /* No prev signals that this is the first loaded view. */
2736 if (prev
&& view
!= prev
) {
2740 /* Resize the view when switching between split- and full-screen,
2741 * or when switching between two different full-screen views. */
2742 if (nviews
!= displayed_views() ||
2743 (nviews
== 1 && base_view
!= display
[0]))
2746 if (view
->ops
->open
) {
2748 end_update(view
, TRUE
);
2749 if (!view
->ops
->open(view
, flags
)) {
2750 report("Failed to load %s view", view
->name
);
2753 restore_view_position(view
);
2756 if (split
&& prev
->lineno
- prev
->offset
>= prev
->height
) {
2757 /* Take the title line into account. */
2758 int lines
= prev
->lineno
- prev
->offset
- prev
->height
+ 1;
2760 /* Scroll the view that was split if the current line is
2761 * outside the new limited view. */
2762 do_scroll_view(prev
, lines
);
2765 if (prev
&& view
!= prev
&& split
&& view_is_displayed(prev
)) {
2766 /* "Blur" the previous view. */
2767 update_view_title(prev
);
2770 if (view
->pipe
&& view
->lines
== 0) {
2771 /* Clear the old view and let the incremental updating refill
2774 view
->p_restore
= flags
& (OPEN_RELOAD
| OPEN_REFRESH
);
2776 } else if (view_is_displayed(view
)) {
2783 open_external_viewer(const char *argv
[], const char *dir
)
2785 def_prog_mode(); /* save current tty modes */
2786 endwin(); /* restore original tty modes */
2787 io_run_fg(argv
, dir
);
2788 fprintf(stderr
, "Press Enter to continue");
2791 redraw_display(TRUE
);
2795 open_mergetool(const char *file
)
2797 const char *mergetool_argv
[] = { "git", "mergetool", file
, NULL
};
2799 open_external_viewer(mergetool_argv
, opt_cdup
);
2803 open_editor(const char *file
)
2805 const char *editor_argv
[] = { "vi", file
, NULL
};
2808 editor
= getenv("GIT_EDITOR");
2809 if (!editor
&& *opt_editor
)
2810 editor
= opt_editor
;
2812 editor
= getenv("VISUAL");
2814 editor
= getenv("EDITOR");
2818 editor_argv
[0] = editor
;
2819 open_external_viewer(editor_argv
, opt_cdup
);
2823 open_run_request(enum request request
)
2825 struct run_request
*req
= get_run_request(request
);
2826 const char **argv
= NULL
;
2829 report("Unknown run request");
2833 if (format_argv(&argv
, req
->argv
, TRUE
, FALSE
))
2834 open_external_viewer(argv
, NULL
);
2841 * User request switch noodle
2845 view_driver(struct view
*view
, enum request request
)
2849 if (request
== REQ_NONE
)
2852 if (request
> REQ_NONE
) {
2853 open_run_request(request
);
2854 view_request(view
, REQ_REFRESH
);
2858 request
= view_request(view
, request
);
2859 if (request
== REQ_NONE
)
2865 case REQ_MOVE_PAGE_UP
:
2866 case REQ_MOVE_PAGE_DOWN
:
2867 case REQ_MOVE_FIRST_LINE
:
2868 case REQ_MOVE_LAST_LINE
:
2869 move_view(view
, request
);
2872 case REQ_SCROLL_FIRST_COL
:
2873 case REQ_SCROLL_LEFT
:
2874 case REQ_SCROLL_RIGHT
:
2875 case REQ_SCROLL_LINE_DOWN
:
2876 case REQ_SCROLL_LINE_UP
:
2877 case REQ_SCROLL_PAGE_DOWN
:
2878 case REQ_SCROLL_PAGE_UP
:
2879 scroll_view(view
, request
);
2882 case REQ_VIEW_BLAME
:
2884 report("No file chosen, press %s to open tree view",
2885 get_key(view
->keymap
, REQ_VIEW_TREE
));
2888 open_view(view
, request
, OPEN_DEFAULT
);
2893 report("No file chosen, press %s to open tree view",
2894 get_key(view
->keymap
, REQ_VIEW_TREE
));
2897 open_view(view
, request
, OPEN_DEFAULT
);
2900 case REQ_VIEW_PAGER
:
2902 if (!io_open(&VIEW(REQ_VIEW_PAGER
)->io
, ""))
2903 die("Failed to open stdin");
2904 open_view(view
, request
, OPEN_PREPARED
);
2908 if (!VIEW(REQ_VIEW_PAGER
)->pipe
&& !VIEW(REQ_VIEW_PAGER
)->lines
) {
2909 report("No pager content, press %s to run command from prompt",
2910 get_key(view
->keymap
, REQ_PROMPT
));
2913 open_view(view
, request
, OPEN_DEFAULT
);
2916 case REQ_VIEW_STAGE
:
2917 if (!VIEW(REQ_VIEW_STAGE
)->lines
) {
2918 report("No stage content, press %s to open the status view and choose file",
2919 get_key(view
->keymap
, REQ_VIEW_STATUS
));
2922 open_view(view
, request
, OPEN_DEFAULT
);
2925 case REQ_VIEW_STATUS
:
2926 if (opt_is_inside_work_tree
== FALSE
) {
2927 report("The status view requires a working tree");
2930 open_view(view
, request
, OPEN_DEFAULT
);
2938 case REQ_VIEW_BRANCH
:
2939 open_view(view
, request
, OPEN_DEFAULT
);
2944 request
= request
== REQ_NEXT
? REQ_MOVE_DOWN
: REQ_MOVE_UP
;
2949 view
= view
->parent
;
2950 line
= view
->lineno
;
2951 move_view(view
, request
);
2952 if (view_is_displayed(view
))
2953 update_view_title(view
);
2954 if (line
!= view
->lineno
)
2955 view_request(view
, REQ_ENTER
);
2957 move_view(view
, request
);
2963 int nviews
= displayed_views();
2964 int next_view
= (current_view
+ 1) % nviews
;
2966 if (next_view
== current_view
) {
2967 report("Only one view is displayed");
2971 current_view
= next_view
;
2972 /* Blur out the title of the previous view. */
2973 update_view_title(view
);
2978 report("Refreshing is not yet supported for the %s view", view
->name
);
2982 if (displayed_views() == 2)
2983 maximize_view(view
);
2987 case REQ_TOGGLE_LINENO
:
2988 case REQ_TOGGLE_DATE
:
2989 case REQ_TOGGLE_AUTHOR
:
2990 case REQ_TOGGLE_GRAPHIC
:
2991 case REQ_TOGGLE_REV_GRAPH
:
2992 case REQ_TOGGLE_REFS
:
2993 toggle_option(request
);
2996 case REQ_TOGGLE_SORT_FIELD
:
2997 case REQ_TOGGLE_SORT_ORDER
:
2998 report("Sorting is not yet supported for the %s view", view
->name
);
3002 case REQ_SEARCH_BACK
:
3003 search_view(view
, request
);
3008 find_next(view
, request
);
3011 case REQ_STOP_LOADING
:
3012 foreach_view(view
, i
) {
3014 report("Stopped loading the %s view", view
->name
),
3015 end_update(view
, TRUE
);
3019 case REQ_SHOW_VERSION
:
3020 report("tig-%s (built %s)", TIG_VERSION
, __DATE__
);
3023 case REQ_SCREEN_REDRAW
:
3024 redraw_display(TRUE
);
3028 report("Nothing to edit");
3032 report("Nothing to enter");
3035 case REQ_VIEW_CLOSE
:
3036 /* XXX: Mark closed views by letting view->prev point to the
3037 * view itself. Parents to closed view should never be
3039 if (view
->prev
&& view
->prev
!= view
) {
3040 maximize_view(view
->prev
);
3049 report("Unknown key, press %s for help",
3050 get_key(view
->keymap
, REQ_VIEW_HELP
));
3059 * View backend utilities
3069 const enum sort_field
*fields
;
3070 size_t size
, current
;
3074 #define SORT_STATE(fields) { fields, ARRAY_SIZE(fields), 0 }
3075 #define get_sort_field(state) ((state).fields[(state).current])
3076 #define sort_order(state, result) ((state).reverse ? -(result) : (result))
3079 sort_view(struct view
*view
, enum request request
, struct sort_state
*state
,
3080 int (*compare
)(const void *, const void *))
3083 case REQ_TOGGLE_SORT_FIELD
:
3084 state
->current
= (state
->current
+ 1) % state
->size
;
3087 case REQ_TOGGLE_SORT_ORDER
:
3088 state
->reverse
= !state
->reverse
;
3091 die("Not a sort request");
3094 qsort(view
->line
, view
->lines
, sizeof(*view
->line
), compare
);
3098 DEFINE_ALLOCATOR(realloc_authors
, const char *, 256)
3100 /* Small author cache to reduce memory consumption. It uses binary
3101 * search to lookup or find place to position new entries. No entries
3102 * are ever freed. */
3104 get_author(const char *name
)
3106 static const char **authors
;
3107 static size_t authors_size
;
3108 int from
= 0, to
= authors_size
- 1;
3110 while (from
<= to
) {
3111 size_t pos
= (to
+ from
) / 2;
3112 int cmp
= strcmp(name
, authors
[pos
]);
3115 return authors
[pos
];
3123 if (!realloc_authors(&authors
, authors_size
, 1))
3125 name
= strdup(name
);
3129 memmove(authors
+ from
+ 1, authors
+ from
, (authors_size
- from
) * sizeof(*authors
));
3130 authors
[from
] = name
;
3137 parse_timesec(struct time
*time
, const char *sec
)
3139 time
->sec
= (time_t) atol(sec
);
3143 parse_timezone(struct time
*time
, const char *zone
)
3147 tz
= ('0' - zone
[1]) * 60 * 60 * 10;
3148 tz
+= ('0' - zone
[2]) * 60 * 60;
3149 tz
+= ('0' - zone
[3]) * 60 * 10;
3150 tz
+= ('0' - zone
[4]) * 60;
3159 /* Parse author lines where the name may be empty:
3160 * author <email@address.tld> 1138474660 +0100
3163 parse_author_line(char *ident
, const char **author
, struct time
*time
)
3165 char *nameend
= strchr(ident
, '<');
3166 char *emailend
= strchr(ident
, '>');
3168 if (nameend
&& emailend
)
3169 *nameend
= *emailend
= 0;
3170 ident
= chomp_string(ident
);
3173 ident
= chomp_string(nameend
+ 1);
3178 *author
= get_author(ident
);
3180 /* Parse epoch and timezone */
3181 if (emailend
&& emailend
[1] == ' ') {
3182 char *secs
= emailend
+ 2;
3183 char *zone
= strchr(secs
, ' ');
3185 parse_timesec(time
, secs
);
3187 if (zone
&& strlen(zone
) == STRING_SIZE(" +0700"))
3188 parse_timezone(time
, zone
+ 1);
3197 pager_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
3199 if (opt_line_number
&& draw_lineno(view
, lineno
))
3202 draw_text(view
, line
->type
, line
->data
);
3207 add_describe_ref(char *buf
, size_t *bufpos
, const char *commit_id
, const char *sep
)
3209 const char *describe_argv
[] = { "git", "describe", commit_id
, NULL
};
3210 char ref
[SIZEOF_STR
];
3212 if (!io_run_buf(describe_argv
, ref
, sizeof(ref
)) || !*ref
)
3215 /* This is the only fatal call, since it can "corrupt" the buffer. */
3216 if (!string_nformat(buf
, SIZEOF_STR
, bufpos
, "%s%s", sep
, ref
))
3223 add_pager_refs(struct view
*view
, struct line
*line
)
3225 char buf
[SIZEOF_STR
];
3226 char *commit_id
= (char *)line
->data
+ STRING_SIZE("commit ");
3227 struct ref_list
*list
;
3228 size_t bufpos
= 0, i
;
3229 const char *sep
= "Refs: ";
3230 bool is_tag
= FALSE
;
3232 assert(line
->type
== LINE_COMMIT
);
3234 list
= get_ref_list(commit_id
);
3236 if (view
->type
== VIEW_DIFF
)
3237 goto try_add_describe_ref
;
3241 for (i
= 0; i
< list
->size
; i
++) {
3242 struct ref
*ref
= list
->refs
[i
];
3243 const char *fmt
= ref
->tag
? "%s[%s]" :
3244 ref
->remote
? "%s<%s>" : "%s%s";
3246 if (!string_format_from(buf
, &bufpos
, fmt
, sep
, ref
->name
))
3253 if (!is_tag
&& view
->type
== VIEW_DIFF
) {
3254 try_add_describe_ref
:
3255 /* Add <tag>-g<commit_id> "fake" reference. */
3256 if (!add_describe_ref(buf
, &bufpos
, commit_id
, sep
))
3263 add_line_text(view
, buf
, LINE_PP_REFS
);
3267 pager_read(struct view
*view
, char *data
)
3274 line
= add_line_text(view
, data
, get_line_type(data
));
3278 if (line
->type
== LINE_COMMIT
&&
3279 (view
->type
== VIEW_DIFF
||
3280 view
->type
== VIEW_LOG
))
3281 add_pager_refs(view
, line
);
3287 pager_request(struct view
*view
, enum request request
, struct line
*line
)
3291 if (request
!= REQ_ENTER
)
3294 if (line
->type
== LINE_COMMIT
&&
3295 (view
->type
== VIEW_LOG
||
3296 view
->type
== VIEW_PAGER
)) {
3297 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
);
3301 /* Always scroll the view even if it was split. That way
3302 * you can use Enter to scroll through the log view and
3303 * split open each commit diff. */
3304 scroll_view(view
, REQ_SCROLL_LINE_DOWN
);
3306 /* FIXME: A minor workaround. Scrolling the view will call report("")
3307 * but if we are scrolling a non-current view this won't properly
3308 * update the view title. */
3310 update_view_title(view
);
3316 pager_grep(struct view
*view
, struct line
*line
)
3318 const char *text
[] = { line
->data
, NULL
};
3320 return grep_text(view
, text
);
3324 pager_select(struct view
*view
, struct line
*line
)
3326 if (line
->type
== LINE_COMMIT
) {
3327 char *text
= (char *)line
->data
+ STRING_SIZE("commit ");
3329 if (view
->type
!= VIEW_PAGER
)
3330 string_copy_rev(view
->ref
, text
);
3331 string_copy_rev(ref_commit
, text
);
3335 static struct view_ops pager_ops
= {
3346 static const char *log_argv
[SIZEOF_ARG
] = {
3347 "git", "log", "--no-color", "--cc", "--stat", "-n100", "%(head)", NULL
3351 log_request(struct view
*view
, enum request request
, struct line
*line
)
3356 open_view(view
, REQ_VIEW_LOG
, OPEN_REFRESH
);
3359 return pager_request(view
, request
, line
);
3363 static struct view_ops log_ops
= {
3374 static const char *diff_argv
[SIZEOF_ARG
] = {
3375 "git", "show", "--pretty=fuller", "--no-color", "--root",
3376 "--patch-with-stat", "--find-copies-harder", "-C",
3377 "%(diffargs)", "%(commit)", "--", "%(fileargs)", NULL
3381 diff_read(struct view
*view
, char *data
)
3384 /* Fall back to retry if no diff will be shown. */
3385 if (view
->lines
== 0 && opt_file_argv
) {
3386 int pos
= argv_size(view
->argv
)
3387 - argv_size(opt_file_argv
) - 1;
3389 if (pos
> 0 && !strcmp(view
->argv
[pos
], "--")) {
3390 for (; view
->argv
[pos
]; pos
++) {
3391 free((void *) view
->argv
[pos
]);
3392 view
->argv
[pos
] = NULL
;
3396 io_done(view
->pipe
);
3397 if (io_run(&view
->io
, IO_RD
, view
->dir
, view
->argv
))
3404 return pager_read(view
, data
);
3407 static struct view_ops diff_ops
= {
3422 static bool help_keymap_hidden
[ARRAY_SIZE(keymap_table
)];
3425 help_open_keymap_title(struct view
*view
, enum keymap keymap
)
3429 line
= add_line_format(view
, LINE_HELP_KEYMAP
, "[%c] %s bindings",
3430 help_keymap_hidden
[keymap
] ? '+' : '-',
3431 enum_name(keymap_table
[keymap
]));
3433 line
->other
= keymap
;
3435 return help_keymap_hidden
[keymap
];
3439 help_open_keymap(struct view
*view
, enum keymap keymap
)
3441 const char *group
= NULL
;
3442 char buf
[SIZEOF_STR
];
3444 bool add_title
= TRUE
;
3447 for (i
= 0; i
< ARRAY_SIZE(req_info
); i
++) {
3448 const char *key
= NULL
;
3450 if (req_info
[i
].request
== REQ_NONE
)
3453 if (!req_info
[i
].request
) {
3454 group
= req_info
[i
].help
;
3458 key
= get_keys(keymap
, req_info
[i
].request
, TRUE
);
3462 if (add_title
&& help_open_keymap_title(view
, keymap
))
3467 add_line_text(view
, group
, LINE_HELP_GROUP
);
3471 add_line_format(view
, LINE_DEFAULT
, " %-25s %-20s %s", key
,
3472 enum_name(req_info
[i
]), req_info
[i
].help
);
3475 group
= "External commands:";
3477 for (i
= 0; i
< run_requests
; i
++) {
3478 struct run_request
*req
= get_run_request(REQ_NONE
+ i
+ 1);
3482 if (!req
|| req
->keymap
!= keymap
)
3485 key
= get_key_name(req
->key
);
3487 key
= "(no key defined)";
3489 if (add_title
&& help_open_keymap_title(view
, keymap
))
3492 add_line_text(view
, group
, LINE_HELP_GROUP
);
3496 for (bufpos
= 0, argc
= 0; req
->argv
[argc
]; argc
++)
3497 if (!string_format_from(buf
, &bufpos
, "%s%s",
3498 argc
? " " : "", req
->argv
[argc
]))
3501 add_line_format(view
, LINE_DEFAULT
, " %-25s `%s`", key
, buf
);
3506 help_open(struct view
*view
, enum open_flags flags
)
3511 add_line_text(view
, "Quick reference for tig keybindings:", LINE_DEFAULT
);
3512 add_line_text(view
, "", LINE_DEFAULT
);
3514 for (keymap
= 0; keymap
< ARRAY_SIZE(keymap_table
); keymap
++)
3515 help_open_keymap(view
, keymap
);
3521 help_request(struct view
*view
, enum request request
, struct line
*line
)
3525 if (line
->type
== LINE_HELP_KEYMAP
) {
3526 help_keymap_hidden
[line
->other
] =
3527 !help_keymap_hidden
[line
->other
];
3528 view
->p_restore
= TRUE
;
3529 open_view(view
, REQ_VIEW_HELP
, OPEN_REFRESH
);
3534 return pager_request(view
, request
, line
);
3538 static struct view_ops help_ops
= {
3554 struct tree_stack_entry
{
3555 struct tree_stack_entry
*prev
; /* Entry below this in the stack */
3556 unsigned long lineno
; /* Line number to restore */
3557 char *name
; /* Position of name in opt_path */
3560 /* The top of the path stack. */
3561 static struct tree_stack_entry
*tree_stack
= NULL
;
3562 unsigned long tree_lineno
= 0;
3565 pop_tree_stack_entry(void)
3567 struct tree_stack_entry
*entry
= tree_stack
;
3569 tree_lineno
= entry
->lineno
;
3571 tree_stack
= entry
->prev
;
3576 push_tree_stack_entry(const char *name
, unsigned long lineno
)
3578 struct tree_stack_entry
*entry
= calloc(1, sizeof(*entry
));
3579 size_t pathlen
= strlen(opt_path
);
3584 entry
->prev
= tree_stack
;
3585 entry
->name
= opt_path
+ pathlen
;
3588 if (!string_format_from(opt_path
, &pathlen
, "%s/", name
)) {
3589 pop_tree_stack_entry();
3593 /* Move the current line to the first tree entry. */
3595 entry
->lineno
= lineno
;
3598 /* Parse output from git-ls-tree(1):
3600 * 100644 blob f931e1d229c3e185caad4449bf5b66ed72462657 tig.c
3603 #define SIZEOF_TREE_ATTR \
3604 STRING_SIZE("100644 blob f931e1d229c3e185caad4449bf5b66ed72462657\t")
3606 #define SIZEOF_TREE_MODE \
3607 STRING_SIZE("100644 ")
3609 #define TREE_ID_OFFSET \
3610 STRING_SIZE("100644 blob ")
3613 char id
[SIZEOF_REV
];
3615 struct time time
; /* Date from the author ident. */
3616 const char *author
; /* Author of the commit. */
3621 tree_path(const struct line
*line
)
3623 return ((struct tree_entry
*) line
->data
)->name
;
3627 tree_compare_entry(const struct line
*line1
, const struct line
*line2
)
3629 if (line1
->type
!= line2
->type
)
3630 return line1
->type
== LINE_TREE_DIR
? -1 : 1;
3631 return strcmp(tree_path(line1
), tree_path(line2
));
3634 static const enum sort_field tree_sort_fields
[] = {
3635 ORDERBY_NAME
, ORDERBY_DATE
, ORDERBY_AUTHOR
3637 static struct sort_state tree_sort_state
= SORT_STATE(tree_sort_fields
);
3640 tree_compare(const void *l1
, const void *l2
)
3642 const struct line
*line1
= (const struct line
*) l1
;
3643 const struct line
*line2
= (const struct line
*) l2
;
3644 const struct tree_entry
*entry1
= ((const struct line
*) l1
)->data
;
3645 const struct tree_entry
*entry2
= ((const struct line
*) l2
)->data
;
3647 if (line1
->type
== LINE_TREE_HEAD
)
3649 if (line2
->type
== LINE_TREE_HEAD
)
3652 switch (get_sort_field(tree_sort_state
)) {
3654 return sort_order(tree_sort_state
, timecmp(&entry1
->time
, &entry2
->time
));
3656 case ORDERBY_AUTHOR
:
3657 return sort_order(tree_sort_state
, strcmp(entry1
->author
, entry2
->author
));
3661 return sort_order(tree_sort_state
, tree_compare_entry(line1
, line2
));
3666 static struct line
*
3667 tree_entry(struct view
*view
, enum line_type type
, const char *path
,
3668 const char *mode
, const char *id
)
3670 struct tree_entry
*entry
= calloc(1, sizeof(*entry
) + strlen(path
));
3671 struct line
*line
= entry
? add_line_data(view
, entry
, type
) : NULL
;
3673 if (!entry
|| !line
) {
3678 strncpy(entry
->name
, path
, strlen(path
));
3680 entry
->mode
= strtoul(mode
, NULL
, 8);
3682 string_copy_rev(entry
->id
, id
);
3688 tree_read_date(struct view
*view
, char *text
, bool *read_date
)
3690 static const char *author_name
;
3691 static struct time author_time
;
3693 if (!text
&& *read_date
) {
3698 char *path
= *opt_path
? opt_path
: ".";
3699 /* Find next entry to process */
3700 const char *log_file
[] = {
3701 "git", "log", "--no-color", "--pretty=raw",
3702 "--cc", "--raw", view
->id
, "--", path
, NULL
3706 tree_entry(view
, LINE_TREE_HEAD
, opt_path
, NULL
, NULL
);
3707 report("Tree is empty");
3711 if (!start_update(view
, log_file
, opt_cdup
)) {
3712 report("Failed to load tree data");
3719 } else if (*text
== 'a' && get_line_type(text
) == LINE_AUTHOR
) {
3720 parse_author_line(text
+ STRING_SIZE("author "),
3721 &author_name
, &author_time
);
3723 } else if (*text
== ':') {
3725 size_t annotated
= 1;
3728 pos
= strchr(text
, '\t');
3732 if (*opt_path
&& !strncmp(text
, opt_path
, strlen(opt_path
)))
3733 text
+= strlen(opt_path
);
3734 pos
= strchr(text
, '/');
3738 for (i
= 1; i
< view
->lines
; i
++) {
3739 struct line
*line
= &view
->line
[i
];
3740 struct tree_entry
*entry
= line
->data
;
3742 annotated
+= !!entry
->author
;
3743 if (entry
->author
|| strcmp(entry
->name
, text
))
3746 entry
->author
= author_name
;
3747 entry
->time
= author_time
;
3752 if (annotated
== view
->lines
)
3753 io_kill(view
->pipe
);
3759 tree_read(struct view
*view
, char *text
)
3761 static bool read_date
= FALSE
;
3762 struct tree_entry
*data
;
3763 struct line
*entry
, *line
;
3764 enum line_type type
;
3765 size_t textlen
= text
? strlen(text
) : 0;
3766 char *path
= text
+ SIZEOF_TREE_ATTR
;
3768 if (read_date
|| !text
)
3769 return tree_read_date(view
, text
, &read_date
);
3771 if (textlen
<= SIZEOF_TREE_ATTR
)
3773 if (view
->lines
== 0 &&
3774 !tree_entry(view
, LINE_TREE_HEAD
, opt_path
, NULL
, NULL
))
3777 /* Strip the path part ... */
3779 size_t pathlen
= textlen
- SIZEOF_TREE_ATTR
;
3780 size_t striplen
= strlen(opt_path
);
3782 if (pathlen
> striplen
)
3783 memmove(path
, path
+ striplen
,
3784 pathlen
- striplen
+ 1);
3786 /* Insert "link" to parent directory. */
3787 if (view
->lines
== 1 &&
3788 !tree_entry(view
, LINE_TREE_DIR
, "..", "040000", view
->ref
))
3792 type
= text
[SIZEOF_TREE_MODE
] == 't' ? LINE_TREE_DIR
: LINE_TREE_FILE
;
3793 entry
= tree_entry(view
, type
, path
, text
, text
+ TREE_ID_OFFSET
);
3798 /* Skip "Directory ..." and ".." line. */
3799 for (line
= &view
->line
[1 + !!*opt_path
]; line
< entry
; line
++) {
3800 if (tree_compare_entry(line
, entry
) <= 0)
3803 memmove(line
+ 1, line
, (entry
- line
) * sizeof(*entry
));
3807 for (; line
<= entry
; line
++)
3808 line
->dirty
= line
->cleareol
= 1;
3812 if (tree_lineno
> view
->lineno
) {
3813 view
->lineno
= tree_lineno
;
3821 tree_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
3823 struct tree_entry
*entry
= line
->data
;
3825 if (line
->type
== LINE_TREE_HEAD
) {
3826 if (draw_text(view
, line
->type
, "Directory path /"))
3829 if (draw_mode(view
, entry
->mode
))
3832 if (opt_author
&& draw_author(view
, entry
->author
))
3835 if (opt_date
&& draw_date(view
, &entry
->time
))
3839 draw_text(view
, line
->type
, entry
->name
);
3844 open_blob_editor(const char *id
)
3846 const char *blob_argv
[] = { "git", "cat-file", "blob", id
, NULL
};
3847 char file
[SIZEOF_STR
] = "/tmp/tigblob.XXXXXX";
3848 int fd
= mkstemp(file
);
3851 report("Failed to create temporary file");
3852 else if (!io_run_append(blob_argv
, fd
))
3853 report("Failed to save blob data to file");
3861 tree_request(struct view
*view
, enum request request
, struct line
*line
)
3863 enum open_flags flags
;
3864 struct tree_entry
*entry
= line
->data
;
3867 case REQ_VIEW_BLAME
:
3868 if (line
->type
!= LINE_TREE_FILE
) {
3869 report("Blame only supported for files");
3873 string_copy(opt_ref
, view
->vid
);
3877 if (line
->type
!= LINE_TREE_FILE
) {
3878 report("Edit only supported for files");
3879 } else if (!is_head_commit(view
->vid
)) {
3880 open_blob_editor(entry
->id
);
3882 open_editor(opt_file
);
3886 case REQ_TOGGLE_SORT_FIELD
:
3887 case REQ_TOGGLE_SORT_ORDER
:
3888 sort_view(view
, request
, &tree_sort_state
, tree_compare
);
3893 /* quit view if at top of tree */
3894 return REQ_VIEW_CLOSE
;
3897 line
= &view
->line
[1];
3907 /* Cleanup the stack if the tree view is at a different tree. */
3908 while (!*opt_path
&& tree_stack
)
3909 pop_tree_stack_entry();
3911 switch (line
->type
) {
3913 /* Depending on whether it is a subdirectory or parent link
3914 * mangle the path buffer. */
3915 if (line
== &view
->line
[1] && *opt_path
) {
3916 pop_tree_stack_entry();
3919 const char *basename
= tree_path(line
);
3921 push_tree_stack_entry(basename
, view
->lineno
);
3924 /* Trees and subtrees share the same ID, so they are not not
3925 * unique like blobs. */
3926 flags
= OPEN_RELOAD
;
3927 request
= REQ_VIEW_TREE
;
3930 case LINE_TREE_FILE
:
3931 flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
3932 request
= REQ_VIEW_BLOB
;
3939 open_view(view
, request
, flags
);
3940 if (request
== REQ_VIEW_TREE
)
3941 view
->lineno
= tree_lineno
;
3947 tree_grep(struct view
*view
, struct line
*line
)
3949 struct tree_entry
*entry
= line
->data
;
3950 const char *text
[] = {
3952 opt_author
? entry
->author
: "",
3953 mkdate(&entry
->time
, opt_date
),
3957 return grep_text(view
, text
);
3961 tree_select(struct view
*view
, struct line
*line
)
3963 struct tree_entry
*entry
= line
->data
;
3965 if (line
->type
== LINE_TREE_FILE
) {
3966 string_copy_rev(ref_blob
, entry
->id
);
3967 string_format(opt_file
, "%s%s", opt_path
, tree_path(line
));
3969 } else if (line
->type
!= LINE_TREE_DIR
) {
3973 string_copy_rev(view
->ref
, entry
->id
);
3977 tree_prepare(struct view
*view
)
3979 if (view
->lines
== 0 && opt_prefix
[0]) {
3980 char *pos
= opt_prefix
;
3982 while (pos
&& *pos
) {
3983 char *end
= strchr(pos
, '/');
3987 push_tree_stack_entry(pos
, 0);
3995 } else if (strcmp(view
->vid
, view
->id
)) {
3999 return prepare_io(view
, opt_cdup
, view
->ops
->argv
, TRUE
);
4002 static const char *tree_argv
[SIZEOF_ARG
] = {
4003 "git", "ls-tree", "%(commit)", "%(directory)", NULL
4006 static struct view_ops tree_ops
= {
4019 blob_read(struct view
*view
, char *line
)
4023 return add_line_text(view
, line
, LINE_DEFAULT
) != NULL
;
4027 blob_request(struct view
*view
, enum request request
, struct line
*line
)
4031 open_blob_editor(view
->vid
);
4034 return pager_request(view
, request
, line
);
4038 static const char *blob_argv
[SIZEOF_ARG
] = {
4039 "git", "cat-file", "blob", "%(blob)", NULL
4042 static struct view_ops blob_ops
= {
4056 * Loading the blame view is a two phase job:
4058 * 1. File content is read either using opt_file from the
4059 * filesystem or using git-cat-file.
4060 * 2. Then blame information is incrementally added by
4061 * reading output from git-blame.
4064 struct blame_commit
{
4065 char id
[SIZEOF_REV
]; /* SHA1 ID. */
4066 char title
[128]; /* First line of the commit message. */
4067 const char *author
; /* Author of the commit. */
4068 struct time time
; /* Date from the author ident. */
4069 char filename
[128]; /* Name of file. */
4070 char parent_id
[SIZEOF_REV
]; /* Parent/previous SHA1 ID. */
4071 char parent_filename
[128]; /* Parent/previous name of file. */
4075 struct blame_commit
*commit
;
4076 unsigned long lineno
;
4081 blame_open(struct view
*view
, enum open_flags flags
)
4083 char path
[SIZEOF_STR
];
4086 if (!view
->prev
&& *opt_prefix
) {
4087 string_copy(path
, opt_file
);
4088 if (!string_format(opt_file
, "%s%s", opt_prefix
, path
))
4092 if (*opt_ref
|| !io_open(&view
->io
, "%s%s", opt_cdup
, opt_file
)) {
4093 const char *blame_cat_file_argv
[] = {
4094 "git", "cat-file", "blob", path
, NULL
4097 if (!string_format(path
, "%s:%s", opt_ref
, opt_file
) ||
4098 !start_update(view
, blame_cat_file_argv
, opt_cdup
))
4102 /* First pass: remove multiple references to the same commit. */
4103 for (i
= 0; i
< view
->lines
; i
++) {
4104 struct blame
*blame
= view
->line
[i
].data
;
4106 if (blame
->commit
&& blame
->commit
->id
[0])
4107 blame
->commit
->id
[0] = 0;
4109 blame
->commit
= NULL
;
4112 /* Second pass: free existing references. */
4113 for (i
= 0; i
< view
->lines
; i
++) {
4114 struct blame
*blame
= view
->line
[i
].data
;
4117 free(blame
->commit
);
4120 setup_update(view
, opt_file
);
4121 string_format(view
->ref
, "%s ...", opt_file
);
4126 static struct blame_commit
*
4127 get_blame_commit(struct view
*view
, const char *id
)
4131 for (i
= 0; i
< view
->lines
; i
++) {
4132 struct blame
*blame
= view
->line
[i
].data
;
4137 if (!strncmp(blame
->commit
->id
, id
, SIZEOF_REV
- 1))
4138 return blame
->commit
;
4142 struct blame_commit
*commit
= calloc(1, sizeof(*commit
));
4145 string_ncopy(commit
->id
, id
, SIZEOF_REV
);
4151 parse_number(const char **posref
, size_t *number
, size_t min
, size_t max
)
4153 const char *pos
= *posref
;
4156 pos
= strchr(pos
+ 1, ' ');
4157 if (!pos
|| !isdigit(pos
[1]))
4159 *number
= atoi(pos
+ 1);
4160 if (*number
< min
|| *number
> max
)
4167 static struct blame_commit
*
4168 parse_blame_commit(struct view
*view
, const char *text
, int *blamed
)
4170 struct blame_commit
*commit
;
4171 struct blame
*blame
;
4172 const char *pos
= text
+ SIZEOF_REV
- 2;
4173 size_t orig_lineno
= 0;
4177 if (strlen(text
) <= SIZEOF_REV
|| pos
[1] != ' ')
4180 if (!parse_number(&pos
, &orig_lineno
, 1, 9999999) ||
4181 !parse_number(&pos
, &lineno
, 1, view
->lines
) ||
4182 !parse_number(&pos
, &group
, 1, view
->lines
- lineno
+ 1))
4185 commit
= get_blame_commit(view
, text
);
4191 struct line
*line
= &view
->line
[lineno
+ group
- 1];
4194 blame
->commit
= commit
;
4195 blame
->lineno
= orig_lineno
+ group
- 1;
4203 blame_read_file(struct view
*view
, const char *line
, bool *read_file
)
4206 const char *blame_argv
[] = {
4207 "git", "blame", "%(blameargs)", "--incremental",
4208 *opt_ref
? opt_ref
: "--incremental", "--", opt_file
, NULL
4211 if (view
->lines
== 0 && !view
->prev
)
4212 die("No blame exist for %s", view
->vid
);
4214 if (view
->lines
== 0 || !start_update(view
, blame_argv
, opt_cdup
)) {
4215 report("Failed to load blame data");
4223 size_t linelen
= strlen(line
);
4224 struct blame
*blame
= malloc(sizeof(*blame
) + linelen
);
4229 blame
->commit
= NULL
;
4230 strncpy(blame
->text
, line
, linelen
);
4231 blame
->text
[linelen
] = 0;
4232 return add_line_data(view
, blame
, LINE_BLAME_ID
) != NULL
;
4237 match_blame_header(const char *name
, char **line
)
4239 size_t namelen
= strlen(name
);
4240 bool matched
= !strncmp(name
, *line
, namelen
);
4249 blame_read(struct view
*view
, char *line
)
4251 static struct blame_commit
*commit
= NULL
;
4252 static int blamed
= 0;
4253 static bool read_file
= TRUE
;
4256 return blame_read_file(view
, line
, &read_file
);
4263 string_format(view
->ref
, "%s", view
->vid
);
4264 if (view_is_displayed(view
)) {
4265 update_view_title(view
);
4266 redraw_view_from(view
, 0);
4272 commit
= parse_blame_commit(view
, line
, &blamed
);
4273 string_format(view
->ref
, "%s %2d%%", view
->vid
,
4274 view
->lines
? blamed
* 100 / view
->lines
: 0);
4276 } else if (match_blame_header("author ", &line
)) {
4277 commit
->author
= get_author(line
);
4279 } else if (match_blame_header("author-time ", &line
)) {
4280 parse_timesec(&commit
->time
, line
);
4282 } else if (match_blame_header("author-tz ", &line
)) {
4283 parse_timezone(&commit
->time
, line
);
4285 } else if (match_blame_header("summary ", &line
)) {
4286 string_ncopy(commit
->title
, line
, strlen(line
));
4288 } else if (match_blame_header("previous ", &line
)) {
4289 if (strlen(line
) <= SIZEOF_REV
)
4291 string_copy_rev(commit
->parent_id
, line
);
4293 string_ncopy(commit
->parent_filename
, line
, strlen(line
));
4295 } else if (match_blame_header("filename ", &line
)) {
4296 string_ncopy(commit
->filename
, line
, strlen(line
));
4304 blame_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
4306 struct blame
*blame
= line
->data
;
4307 struct time
*time
= NULL
;
4308 const char *id
= NULL
, *author
= NULL
;
4310 if (blame
->commit
&& *blame
->commit
->filename
) {
4311 id
= blame
->commit
->id
;
4312 author
= blame
->commit
->author
;
4313 time
= &blame
->commit
->time
;
4316 if (opt_date
&& draw_date(view
, time
))
4319 if (opt_author
&& draw_author(view
, author
))
4322 if (draw_field(view
, LINE_BLAME_ID
, id
, ID_COLS
, FALSE
))
4325 if (draw_lineno(view
, lineno
))
4328 draw_text(view
, LINE_DEFAULT
, blame
->text
);
4333 check_blame_commit(struct blame
*blame
, bool check_null_id
)
4336 report("Commit data not loaded yet");
4337 else if (check_null_id
&& !strcmp(blame
->commit
->id
, NULL_ID
))
4338 report("No commit exist for the selected line");
4345 setup_blame_parent_line(struct view
*view
, struct blame
*blame
)
4347 char from
[SIZEOF_REF
+ SIZEOF_STR
];
4348 char to
[SIZEOF_REF
+ SIZEOF_STR
];
4349 const char *diff_tree_argv
[] = {
4350 "git", "diff", "--no-textconv", "--no-extdiff", "--no-color",
4351 "-U0", from
, to
, "--", NULL
4354 int parent_lineno
= -1;
4355 int blamed_lineno
= -1;
4358 if (!string_format(from
, "%s:%s", opt_ref
, opt_file
) ||
4359 !string_format(to
, "%s:%s", blame
->commit
->id
, blame
->commit
->filename
) ||
4360 !io_run(&io
, IO_RD
, NULL
, diff_tree_argv
))
4363 while ((line
= io_get(&io
, '\n', TRUE
))) {
4365 char *pos
= strchr(line
, '+');
4367 parent_lineno
= atoi(line
+ 4);
4369 blamed_lineno
= atoi(pos
+ 1);
4371 } else if (*line
== '+' && parent_lineno
!= -1) {
4372 if (blame
->lineno
== blamed_lineno
- 1 &&
4373 !strcmp(blame
->text
, line
+ 1)) {
4374 view
->lineno
= parent_lineno
? parent_lineno
- 1 : 0;
4385 blame_request(struct view
*view
, enum request request
, struct line
*line
)
4387 enum open_flags flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
4388 struct blame
*blame
= line
->data
;
4391 case REQ_VIEW_BLAME
:
4392 if (check_blame_commit(blame
, TRUE
)) {
4393 string_copy(opt_ref
, blame
->commit
->id
);
4394 string_copy(opt_file
, blame
->commit
->filename
);
4396 view
->lineno
= blame
->lineno
;
4397 open_view(view
, REQ_VIEW_BLAME
, OPEN_REFRESH
);
4402 if (!check_blame_commit(blame
, TRUE
))
4404 if (!*blame
->commit
->parent_id
) {
4405 report("The selected commit has no parents");
4407 string_copy_rev(opt_ref
, blame
->commit
->parent_id
);
4408 string_copy(opt_file
, blame
->commit
->parent_filename
);
4409 setup_blame_parent_line(view
, blame
);
4410 open_view(view
, REQ_VIEW_BLAME
, OPEN_REFRESH
);
4415 if (!check_blame_commit(blame
, FALSE
))
4418 if (view_is_displayed(VIEW(REQ_VIEW_DIFF
)) &&
4419 !strcmp(blame
->commit
->id
, VIEW(REQ_VIEW_DIFF
)->ref
))
4422 if (!strcmp(blame
->commit
->id
, NULL_ID
)) {
4423 struct view
*diff
= VIEW(REQ_VIEW_DIFF
);
4424 const char *diff_index_argv
[] = {
4425 "git", "diff-index", "--root", "--patch-with-stat",
4426 "-C", "-M", "HEAD", "--", view
->vid
, NULL
4429 if (!*blame
->commit
->parent_id
) {
4430 diff_index_argv
[1] = "diff";
4431 diff_index_argv
[2] = "--no-color";
4432 diff_index_argv
[6] = "--";
4433 diff_index_argv
[7] = "/dev/null";
4436 if (!prepare_update(diff
, diff_index_argv
, NULL
)) {
4437 report("Failed to allocate diff command");
4440 flags
|= OPEN_PREPARED
;
4443 open_view(view
, REQ_VIEW_DIFF
, flags
);
4444 if (VIEW(REQ_VIEW_DIFF
)->pipe
&& !strcmp(blame
->commit
->id
, NULL_ID
))
4445 string_copy_rev(VIEW(REQ_VIEW_DIFF
)->ref
, NULL_ID
);
4456 blame_grep(struct view
*view
, struct line
*line
)
4458 struct blame
*blame
= line
->data
;
4459 struct blame_commit
*commit
= blame
->commit
;
4460 const char *text
[] = {
4462 commit
? commit
->title
: "",
4463 commit
? commit
->id
: "",
4464 commit
&& opt_author
? commit
->author
: "",
4465 commit
? mkdate(&commit
->time
, opt_date
) : "",
4469 return grep_text(view
, text
);
4473 blame_select(struct view
*view
, struct line
*line
)
4475 struct blame
*blame
= line
->data
;
4476 struct blame_commit
*commit
= blame
->commit
;
4481 if (!strcmp(commit
->id
, NULL_ID
))
4482 string_ncopy(ref_commit
, "HEAD", 4);
4484 string_copy_rev(ref_commit
, commit
->id
);
4487 static struct view_ops blame_ops
= {
4503 const char *author
; /* Author of the last commit. */
4504 struct time time
; /* Date of the last activity. */
4505 const struct ref
*ref
; /* Name and commit ID information. */
4508 static const struct ref branch_all
;
4510 static const enum sort_field branch_sort_fields
[] = {
4511 ORDERBY_NAME
, ORDERBY_DATE
, ORDERBY_AUTHOR
4513 static struct sort_state branch_sort_state
= SORT_STATE(branch_sort_fields
);
4516 branch_compare(const void *l1
, const void *l2
)
4518 const struct branch
*branch1
= ((const struct line
*) l1
)->data
;
4519 const struct branch
*branch2
= ((const struct line
*) l2
)->data
;
4521 switch (get_sort_field(branch_sort_state
)) {
4523 return sort_order(branch_sort_state
, timecmp(&branch1
->time
, &branch2
->time
));
4525 case ORDERBY_AUTHOR
:
4526 return sort_order(branch_sort_state
, strcmp(branch1
->author
, branch2
->author
));
4530 return sort_order(branch_sort_state
, strcmp(branch1
->ref
->name
, branch2
->ref
->name
));
4535 branch_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
4537 struct branch
*branch
= line
->data
;
4538 enum line_type type
= branch
->ref
->head
? LINE_MAIN_HEAD
: LINE_DEFAULT
;
4540 if (opt_date
&& draw_date(view
, &branch
->time
))
4543 if (opt_author
&& draw_author(view
, branch
->author
))
4546 draw_text(view
, type
, branch
->ref
== &branch_all
? "All branches" : branch
->ref
->name
);
4551 branch_request(struct view
*view
, enum request request
, struct line
*line
)
4553 struct branch
*branch
= line
->data
;
4558 open_view(view
, REQ_VIEW_BRANCH
, OPEN_REFRESH
);
4561 case REQ_TOGGLE_SORT_FIELD
:
4562 case REQ_TOGGLE_SORT_ORDER
:
4563 sort_view(view
, request
, &branch_sort_state
, branch_compare
);
4568 const struct ref
*ref
= branch
->ref
;
4569 const char *all_branches_argv
[] = {
4570 "git", "log", "--no-color", "--pretty=raw", "--parents",
4572 ref
== &branch_all
? "--all" : ref
->name
, NULL
4574 struct view
*main_view
= VIEW(REQ_VIEW_MAIN
);
4576 if (!prepare_update(main_view
, all_branches_argv
, NULL
))
4577 report("Failed to load view of all branches");
4579 open_view(view
, REQ_VIEW_MAIN
, OPEN_PREPARED
| OPEN_SPLIT
);
4588 branch_read(struct view
*view
, char *line
)
4590 static char id
[SIZEOF_REV
];
4591 struct branch
*reference
;
4597 switch (get_line_type(line
)) {
4599 string_copy_rev(id
, line
+ STRING_SIZE("commit "));
4603 for (i
= 0, reference
= NULL
; i
< view
->lines
; i
++) {
4604 struct branch
*branch
= view
->line
[i
].data
;
4606 if (strcmp(branch
->ref
->id
, id
))
4609 view
->line
[i
].dirty
= TRUE
;
4611 branch
->author
= reference
->author
;
4612 branch
->time
= reference
->time
;
4616 parse_author_line(line
+ STRING_SIZE("author "),
4617 &branch
->author
, &branch
->time
);
4629 branch_open_visitor(void *data
, const struct ref
*ref
)
4631 struct view
*view
= data
;
4632 struct branch
*branch
;
4634 if (ref
->tag
|| ref
->ltag
|| ref
->remote
)
4637 branch
= calloc(1, sizeof(*branch
));
4642 return !!add_line_data(view
, branch
, LINE_DEFAULT
);
4646 branch_open(struct view
*view
, enum open_flags flags
)
4648 const char *branch_log
[] = {
4649 "git", "log", "--no-color", "--pretty=raw",
4650 "--simplify-by-decoration", "--all", NULL
4653 if (!start_update(view
, branch_log
, NULL
)) {
4654 report("Failed to load branch data");
4658 setup_update(view
, view
->id
);
4659 branch_open_visitor(view
, &branch_all
);
4660 foreach_ref(branch_open_visitor
, view
);
4661 view
->p_restore
= TRUE
;
4667 branch_grep(struct view
*view
, struct line
*line
)
4669 struct branch
*branch
= line
->data
;
4670 const char *text
[] = {
4676 return grep_text(view
, text
);
4680 branch_select(struct view
*view
, struct line
*line
)
4682 struct branch
*branch
= line
->data
;
4684 string_copy_rev(view
->ref
, branch
->ref
->id
);
4685 string_copy_rev(ref_commit
, branch
->ref
->id
);
4686 string_copy_rev(ref_head
, branch
->ref
->id
);
4687 string_copy_rev(ref_branch
, branch
->ref
->name
);
4690 static struct view_ops branch_ops
= {
4709 char rev
[SIZEOF_REV
];
4710 char name
[SIZEOF_STR
];
4714 char rev
[SIZEOF_REV
];
4715 char name
[SIZEOF_STR
];
4719 static char status_onbranch
[SIZEOF_STR
];
4720 static struct status stage_status
;
4721 static enum line_type stage_line_type
;
4722 static size_t stage_chunks
;
4723 static int *stage_chunk
;
4725 DEFINE_ALLOCATOR(realloc_ints
, int, 32)
4727 /* This should work even for the "On branch" line. */
4729 status_has_none(struct view
*view
, struct line
*line
)
4731 return line
< view
->line
+ view
->lines
&& !line
[1].data
;
4734 /* Get fields from the diff line:
4735 * :100644 100644 06a5d6ae9eca55be2e0e585a152e6b1336f2b20e 0000000000000000000000000000000000000000 M
4738 status_get_diff(struct status
*file
, const char *buf
, size_t bufsize
)
4740 const char *old_mode
= buf
+ 1;
4741 const char *new_mode
= buf
+ 8;
4742 const char *old_rev
= buf
+ 15;
4743 const char *new_rev
= buf
+ 56;
4744 const char *status
= buf
+ 97;
4747 old_mode
[-1] != ':' ||
4748 new_mode
[-1] != ' ' ||
4749 old_rev
[-1] != ' ' ||
4750 new_rev
[-1] != ' ' ||
4754 file
->status
= *status
;
4756 string_copy_rev(file
->old
.rev
, old_rev
);
4757 string_copy_rev(file
->new.rev
, new_rev
);
4759 file
->old
.mode
= strtoul(old_mode
, NULL
, 8);
4760 file
->new.mode
= strtoul(new_mode
, NULL
, 8);
4762 file
->old
.name
[0] = file
->new.name
[0] = 0;
4768 status_run(struct view
*view
, const char *argv
[], char status
, enum line_type type
)
4770 struct status
*unmerged
= NULL
;
4774 if (!io_run(&io
, IO_RD
, opt_cdup
, argv
))
4777 add_line_data(view
, NULL
, type
);
4779 while ((buf
= io_get(&io
, 0, TRUE
))) {
4780 struct status
*file
= unmerged
;
4783 file
= calloc(1, sizeof(*file
));
4784 if (!file
|| !add_line_data(view
, file
, type
))
4788 /* Parse diff info part. */
4790 file
->status
= status
;
4792 string_copy(file
->old
.rev
, NULL_ID
);
4794 } else if (!file
->status
|| file
== unmerged
) {
4795 if (!status_get_diff(file
, buf
, strlen(buf
)))
4798 buf
= io_get(&io
, 0, TRUE
);
4802 /* Collapse all modified entries that follow an
4803 * associated unmerged entry. */
4804 if (unmerged
== file
) {
4805 unmerged
->status
= 'U';
4807 } else if (file
->status
== 'U') {
4812 /* Grab the old name for rename/copy. */
4813 if (!*file
->old
.name
&&
4814 (file
->status
== 'R' || file
->status
== 'C')) {
4815 string_ncopy(file
->old
.name
, buf
, strlen(buf
));
4817 buf
= io_get(&io
, 0, TRUE
);
4822 /* git-ls-files just delivers a NUL separated list of
4823 * file names similar to the second half of the
4824 * git-diff-* output. */
4825 string_ncopy(file
->new.name
, buf
, strlen(buf
));
4826 if (!*file
->old
.name
)
4827 string_copy(file
->old
.name
, file
->new.name
);
4831 if (io_error(&io
)) {
4837 if (!view
->line
[view
->lines
- 1].data
)
4838 add_line_data(view
, NULL
, LINE_STAT_NONE
);
4844 /* Don't show unmerged entries in the staged section. */
4845 static const char *status_diff_index_argv
[] = {
4846 "git", "diff-index", "-z", "--diff-filter=ACDMRTXB",
4847 "--cached", "-M", "HEAD", NULL
4850 static const char *status_diff_files_argv
[] = {
4851 "git", "diff-files", "-z", NULL
4854 static const char *status_list_other_argv
[] = {
4855 "git", "ls-files", "-z", "--others", "--exclude-standard", opt_prefix
, NULL
, NULL
,
4858 static const char *status_list_no_head_argv
[] = {
4859 "git", "ls-files", "-z", "--cached", "--exclude-standard", NULL
4862 static const char *update_index_argv
[] = {
4863 "git", "update-index", "-q", "--unmerged", "--refresh", NULL
4866 /* Restore the previous line number to stay in the context or select a
4867 * line with something that can be updated. */
4869 status_restore(struct view
*view
)
4871 if (view
->p_lineno
>= view
->lines
)
4872 view
->p_lineno
= view
->lines
- 1;
4873 while (view
->p_lineno
< view
->lines
&& !view
->line
[view
->p_lineno
].data
)
4875 while (view
->p_lineno
> 0 && !view
->line
[view
->p_lineno
].data
)
4878 /* If the above fails, always skip the "On branch" line. */
4879 if (view
->p_lineno
< view
->lines
)
4880 view
->lineno
= view
->p_lineno
;
4884 if (view
->lineno
< view
->offset
)
4885 view
->offset
= view
->lineno
;
4886 else if (view
->offset
+ view
->height
<= view
->lineno
)
4887 view
->offset
= view
->lineno
- view
->height
+ 1;
4889 view
->p_restore
= FALSE
;
4893 status_update_onbranch(void)
4895 static const char *paths
[][2] = {
4896 { "rebase-apply/rebasing", "Rebasing" },
4897 { "rebase-apply/applying", "Applying mailbox" },
4898 { "rebase-apply/", "Rebasing mailbox" },
4899 { "rebase-merge/interactive", "Interactive rebase" },
4900 { "rebase-merge/", "Rebase merge" },
4901 { "MERGE_HEAD", "Merging" },
4902 { "BISECT_LOG", "Bisecting" },
4903 { "HEAD", "On branch" },
4905 char buf
[SIZEOF_STR
];
4909 if (is_initial_commit()) {
4910 string_copy(status_onbranch
, "Initial commit");
4914 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
4915 char *head
= opt_head
;
4917 if (!string_format(buf
, "%s/%s", opt_git_dir
, paths
[i
][0]) ||
4918 lstat(buf
, &stat
) < 0)
4924 if (io_open(&io
, "%s/rebase-merge/head-name", opt_git_dir
) &&
4925 io_read_buf(&io
, buf
, sizeof(buf
))) {
4927 if (!prefixcmp(head
, "refs/heads/"))
4928 head
+= STRING_SIZE("refs/heads/");
4932 if (!string_format(status_onbranch
, "%s %s", paths
[i
][1], head
))
4933 string_copy(status_onbranch
, opt_head
);
4937 string_copy(status_onbranch
, "Not currently on any branch");
4940 /* First parse staged info using git-diff-index(1), then parse unstaged
4941 * info using git-diff-files(1), and finally untracked files using
4942 * git-ls-files(1). */
4944 status_open(struct view
*view
, enum open_flags flags
)
4948 add_line_data(view
, NULL
, LINE_STAT_HEAD
);
4949 status_update_onbranch();
4951 io_run_bg(update_index_argv
);
4953 if (is_initial_commit()) {
4954 if (!status_run(view
, status_list_no_head_argv
, 'A', LINE_STAT_STAGED
))
4956 } else if (!status_run(view
, status_diff_index_argv
, 0, LINE_STAT_STAGED
)) {
4960 if (!opt_untracked_dirs_content
)
4961 status_list_other_argv
[ARRAY_SIZE(status_list_other_argv
) - 2] = "--directory";
4963 if (!status_run(view
, status_diff_files_argv
, 0, LINE_STAT_UNSTAGED
) ||
4964 !status_run(view
, status_list_other_argv
, '?', LINE_STAT_UNTRACKED
))
4967 /* Restore the exact position or use the specialized restore
4969 if (!view
->p_restore
)
4970 status_restore(view
);
4975 status_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
4977 struct status
*status
= line
->data
;
4978 enum line_type type
;
4982 switch (line
->type
) {
4983 case LINE_STAT_STAGED
:
4984 type
= LINE_STAT_SECTION
;
4985 text
= "Changes to be committed:";
4988 case LINE_STAT_UNSTAGED
:
4989 type
= LINE_STAT_SECTION
;
4990 text
= "Changed but not updated:";
4993 case LINE_STAT_UNTRACKED
:
4994 type
= LINE_STAT_SECTION
;
4995 text
= "Untracked files:";
4998 case LINE_STAT_NONE
:
4999 type
= LINE_DEFAULT
;
5000 text
= " (no files)";
5003 case LINE_STAT_HEAD
:
5004 type
= LINE_STAT_HEAD
;
5005 text
= status_onbranch
;
5012 static char buf
[] = { '?', ' ', ' ', ' ', 0 };
5014 buf
[0] = status
->status
;
5015 if (draw_text(view
, line
->type
, buf
))
5017 type
= LINE_DEFAULT
;
5018 text
= status
->new.name
;
5021 draw_text(view
, type
, text
);
5026 status_load_error(struct view
*view
, struct view
*stage
, const char *path
)
5028 if (displayed_views() == 2 || display
[current_view
] != view
)
5029 maximize_view(view
);
5030 report("Failed to load '%s': %s", path
, io_strerror(&stage
->io
));
5035 status_enter(struct view
*view
, struct line
*line
)
5037 struct status
*status
= line
->data
;
5038 const char *oldpath
= status
? status
->old
.name
: NULL
;
5039 /* Diffs for unmerged entries are empty when passing the new
5040 * path, so leave it empty. */
5041 const char *newpath
= status
&& status
->status
!= 'U' ? status
->new.name
: NULL
;
5043 enum open_flags split
;
5044 struct view
*stage
= VIEW(REQ_VIEW_STAGE
);
5046 if (line
->type
== LINE_STAT_NONE
||
5047 (!status
&& line
[1].type
== LINE_STAT_NONE
)) {
5048 report("No file to diff");
5052 switch (line
->type
) {
5053 case LINE_STAT_STAGED
:
5054 if (is_initial_commit()) {
5055 const char *no_head_diff_argv
[] = {
5056 "git", "diff", "--no-color", "--patch-with-stat",
5057 "--", "/dev/null", newpath
, NULL
5060 if (!prepare_update(stage
, no_head_diff_argv
, opt_cdup
))
5061 return status_load_error(view
, stage
, newpath
);
5063 const char *index_show_argv
[] = {
5064 "git", "diff-index", "--root", "--patch-with-stat",
5065 "-C", "-M", "--cached", "HEAD", "--",
5066 oldpath
, newpath
, NULL
5069 if (!prepare_update(stage
, index_show_argv
, opt_cdup
))
5070 return status_load_error(view
, stage
, newpath
);
5074 info
= "Staged changes to %s";
5076 info
= "Staged changes";
5079 case LINE_STAT_UNSTAGED
:
5081 const char *files_show_argv
[] = {
5082 "git", "diff-files", "--root", "--patch-with-stat",
5083 "-C", "-M", "--", oldpath
, newpath
, NULL
5086 if (!prepare_update(stage
, files_show_argv
, opt_cdup
))
5087 return status_load_error(view
, stage
, newpath
);
5089 info
= "Unstaged changes to %s";
5091 info
= "Unstaged changes";
5094 case LINE_STAT_UNTRACKED
:
5096 report("No file to show");
5100 if (!suffixcmp(status
->new.name
, -1, "/")) {
5101 report("Cannot display a directory");
5105 if (!prepare_update_file(stage
, newpath
))
5106 return status_load_error(view
, stage
, newpath
);
5107 info
= "Untracked file %s";
5110 case LINE_STAT_HEAD
:
5114 die("line type %d not handled in switch", line
->type
);
5117 split
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
5118 open_view(view
, REQ_VIEW_STAGE
, OPEN_PREPARED
| split
);
5119 if (view_is_displayed(VIEW(REQ_VIEW_STAGE
))) {
5121 stage_status
= *status
;
5123 memset(&stage_status
, 0, sizeof(stage_status
));
5126 stage_line_type
= line
->type
;
5128 string_format(VIEW(REQ_VIEW_STAGE
)->ref
, info
, stage_status
.new.name
);
5135 status_exists(struct status
*status
, enum line_type type
)
5137 struct view
*view
= VIEW(REQ_VIEW_STATUS
);
5138 unsigned long lineno
;
5140 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
5141 struct line
*line
= &view
->line
[lineno
];
5142 struct status
*pos
= line
->data
;
5144 if (line
->type
!= type
)
5146 if (!pos
&& (!status
|| !status
->status
) && line
[1].data
) {
5147 select_view_line(view
, lineno
);
5150 if (pos
&& !strcmp(status
->new.name
, pos
->new.name
)) {
5151 select_view_line(view
, lineno
);
5161 status_update_prepare(struct io
*io
, enum line_type type
)
5163 const char *staged_argv
[] = {
5164 "git", "update-index", "-z", "--index-info", NULL
5166 const char *others_argv
[] = {
5167 "git", "update-index", "-z", "--add", "--remove", "--stdin", NULL
5171 case LINE_STAT_STAGED
:
5172 return io_run(io
, IO_WR
, opt_cdup
, staged_argv
);
5174 case LINE_STAT_UNSTAGED
:
5175 case LINE_STAT_UNTRACKED
:
5176 return io_run(io
, IO_WR
, opt_cdup
, others_argv
);
5179 die("line type %d not handled in switch", type
);
5185 status_update_write(struct io
*io
, struct status
*status
, enum line_type type
)
5187 char buf
[SIZEOF_STR
];
5191 case LINE_STAT_STAGED
:
5192 if (!string_format_from(buf
, &bufsize
, "%06o %s\t%s%c",
5195 status
->old
.name
, 0))
5199 case LINE_STAT_UNSTAGED
:
5200 case LINE_STAT_UNTRACKED
:
5201 if (!string_format_from(buf
, &bufsize
, "%s%c", status
->new.name
, 0))
5206 die("line type %d not handled in switch", type
);
5209 return io_write(io
, buf
, bufsize
);
5213 status_update_file(struct status
*status
, enum line_type type
)
5218 if (!status_update_prepare(&io
, type
))
5221 result
= status_update_write(&io
, status
, type
);
5222 return io_done(&io
) && result
;
5226 status_update_files(struct view
*view
, struct line
*line
)
5228 char buf
[sizeof(view
->ref
)];
5231 struct line
*pos
= view
->line
+ view
->lines
;
5234 int cursor_y
= -1, cursor_x
= -1;
5236 if (!status_update_prepare(&io
, line
->type
))
5239 for (pos
= line
; pos
< view
->line
+ view
->lines
&& pos
->data
; pos
++)
5242 string_copy(buf
, view
->ref
);
5243 getsyx(cursor_y
, cursor_x
);
5244 for (file
= 0, done
= 5; result
&& file
< files
; line
++, file
++) {
5245 int almost_done
= file
* 100 / files
;
5247 if (almost_done
> done
) {
5249 string_format(view
->ref
, "updating file %u of %u (%d%% done)",
5251 update_view_title(view
);
5252 setsyx(cursor_y
, cursor_x
);
5255 result
= status_update_write(&io
, line
->data
, line
->type
);
5257 string_copy(view
->ref
, buf
);
5259 return io_done(&io
) && result
;
5263 status_update(struct view
*view
)
5265 struct line
*line
= &view
->line
[view
->lineno
];
5267 assert(view
->lines
);
5270 /* This should work even for the "On branch" line. */
5271 if (line
< view
->line
+ view
->lines
&& !line
[1].data
) {
5272 report("Nothing to update");
5276 if (!status_update_files(view
, line
+ 1)) {
5277 report("Failed to update file status");
5281 } else if (!status_update_file(line
->data
, line
->type
)) {
5282 report("Failed to update file status");
5290 status_revert(struct status
*status
, enum line_type type
, bool has_none
)
5292 if (!status
|| type
!= LINE_STAT_UNSTAGED
) {
5293 if (type
== LINE_STAT_STAGED
) {
5294 report("Cannot revert changes to staged files");
5295 } else if (type
== LINE_STAT_UNTRACKED
) {
5296 report("Cannot revert changes to untracked files");
5297 } else if (has_none
) {
5298 report("Nothing to revert");
5300 report("Cannot revert changes to multiple files");
5303 } else if (prompt_yesno("Are you sure you want to revert changes?")) {
5304 char mode
[10] = "100644";
5305 const char *reset_argv
[] = {
5306 "git", "update-index", "--cacheinfo", mode
,
5307 status
->old
.rev
, status
->old
.name
, NULL
5309 const char *checkout_argv
[] = {
5310 "git", "checkout", "--", status
->old
.name
, NULL
5313 if (status
->status
== 'U') {
5314 string_format(mode
, "%5o", status
->old
.mode
);
5316 if (status
->old
.mode
== 0 && status
->new.mode
== 0) {
5317 reset_argv
[2] = "--force-remove";
5318 reset_argv
[3] = status
->old
.name
;
5319 reset_argv
[4] = NULL
;
5322 if (!io_run_fg(reset_argv
, opt_cdup
))
5324 if (status
->old
.mode
== 0 && status
->new.mode
== 0)
5328 return io_run_fg(checkout_argv
, opt_cdup
);
5335 status_request(struct view
*view
, enum request request
, struct line
*line
)
5337 struct status
*status
= line
->data
;
5340 case REQ_STATUS_UPDATE
:
5341 if (!status_update(view
))
5345 case REQ_STATUS_REVERT
:
5346 if (!status_revert(status
, line
->type
, status_has_none(view
, line
)))
5350 case REQ_STATUS_MERGE
:
5351 if (!status
|| status
->status
!= 'U') {
5352 report("Merging only possible for files with unmerged status ('U').");
5355 open_mergetool(status
->new.name
);
5361 if (status
->status
== 'D') {
5362 report("File has been deleted.");
5366 open_editor(status
->new.name
);
5369 case REQ_VIEW_BLAME
:
5375 /* After returning the status view has been split to
5376 * show the stage view. No further reloading is
5378 return status_enter(view
, line
);
5381 /* Simply reload the view. */
5388 open_view(view
, REQ_VIEW_STATUS
, OPEN_RELOAD
);
5394 status_select(struct view
*view
, struct line
*line
)
5396 struct status
*status
= line
->data
;
5397 char file
[SIZEOF_STR
] = "all files";
5401 if (status
&& !string_format(file
, "'%s'", status
->new.name
))
5404 if (!status
&& line
[1].type
== LINE_STAT_NONE
)
5407 switch (line
->type
) {
5408 case LINE_STAT_STAGED
:
5409 text
= "Press %s to unstage %s for commit";
5412 case LINE_STAT_UNSTAGED
:
5413 text
= "Press %s to stage %s for commit";
5416 case LINE_STAT_UNTRACKED
:
5417 text
= "Press %s to stage %s for addition";
5420 case LINE_STAT_HEAD
:
5421 case LINE_STAT_NONE
:
5422 text
= "Nothing to update";
5426 die("line type %d not handled in switch", line
->type
);
5429 if (status
&& status
->status
== 'U') {
5430 text
= "Press %s to resolve conflict in %s";
5431 key
= get_key(KEYMAP_STATUS
, REQ_STATUS_MERGE
);
5434 key
= get_key(KEYMAP_STATUS
, REQ_STATUS_UPDATE
);
5437 string_format(view
->ref
, text
, key
, file
);
5439 string_copy(opt_file
, status
->new.name
);
5443 status_grep(struct view
*view
, struct line
*line
)
5445 struct status
*status
= line
->data
;
5448 const char buf
[2] = { status
->status
, 0 };
5449 const char *text
[] = { status
->new.name
, buf
, NULL
};
5451 return grep_text(view
, text
);
5457 static struct view_ops status_ops
= {
5470 stage_diff_write(struct io
*io
, struct line
*line
, struct line
*end
)
5472 while (line
< end
) {
5473 if (!io_write(io
, line
->data
, strlen(line
->data
)) ||
5474 !io_write(io
, "\n", 1))
5477 if (line
->type
== LINE_DIFF_CHUNK
||
5478 line
->type
== LINE_DIFF_HEADER
)
5485 static struct line
*
5486 stage_diff_find(struct view
*view
, struct line
*line
, enum line_type type
)
5488 for (; view
->line
< line
; line
--)
5489 if (line
->type
== type
)
5496 stage_apply_chunk(struct view
*view
, struct line
*chunk
, bool revert
)
5498 const char *apply_argv
[SIZEOF_ARG
] = {
5499 "git", "apply", "--whitespace=nowarn", NULL
5501 struct line
*diff_hdr
;
5505 diff_hdr
= stage_diff_find(view
, chunk
, LINE_DIFF_HEADER
);
5510 apply_argv
[argc
++] = "--cached";
5511 if (revert
|| stage_line_type
== LINE_STAT_STAGED
)
5512 apply_argv
[argc
++] = "-R";
5513 apply_argv
[argc
++] = "-";
5514 apply_argv
[argc
++] = NULL
;
5515 if (!io_run(&io
, IO_WR
, opt_cdup
, apply_argv
))
5518 if (!stage_diff_write(&io
, diff_hdr
, chunk
) ||
5519 !stage_diff_write(&io
, chunk
, view
->line
+ view
->lines
))
5523 io_run_bg(update_index_argv
);
5525 return chunk
? TRUE
: FALSE
;
5529 stage_update(struct view
*view
, struct line
*line
)
5531 struct line
*chunk
= NULL
;
5533 if (!is_initial_commit() && stage_line_type
!= LINE_STAT_UNTRACKED
)
5534 chunk
= stage_diff_find(view
, line
, LINE_DIFF_CHUNK
);
5537 if (!stage_apply_chunk(view
, chunk
, FALSE
)) {
5538 report("Failed to apply chunk");
5542 } else if (!stage_status
.status
) {
5543 view
= VIEW(REQ_VIEW_STATUS
);
5545 for (line
= view
->line
; line
< view
->line
+ view
->lines
; line
++)
5546 if (line
->type
== stage_line_type
)
5549 if (!status_update_files(view
, line
+ 1)) {
5550 report("Failed to update files");
5554 } else if (!status_update_file(&stage_status
, stage_line_type
)) {
5555 report("Failed to update file");
5563 stage_revert(struct view
*view
, struct line
*line
)
5565 struct line
*chunk
= NULL
;
5567 if (!is_initial_commit() && stage_line_type
== LINE_STAT_UNSTAGED
)
5568 chunk
= stage_diff_find(view
, line
, LINE_DIFF_CHUNK
);
5571 if (!prompt_yesno("Are you sure you want to revert changes?"))
5574 if (!stage_apply_chunk(view
, chunk
, TRUE
)) {
5575 report("Failed to revert chunk");
5581 return status_revert(stage_status
.status
? &stage_status
: NULL
,
5582 stage_line_type
, FALSE
);
5588 stage_next(struct view
*view
, struct line
*line
)
5592 if (!stage_chunks
) {
5593 for (line
= view
->line
; line
< view
->line
+ view
->lines
; line
++) {
5594 if (line
->type
!= LINE_DIFF_CHUNK
)
5597 if (!realloc_ints(&stage_chunk
, stage_chunks
, 1)) {
5598 report("Allocation failure");
5602 stage_chunk
[stage_chunks
++] = line
- view
->line
;
5606 for (i
= 0; i
< stage_chunks
; i
++) {
5607 if (stage_chunk
[i
] > view
->lineno
) {
5608 do_scroll_view(view
, stage_chunk
[i
] - view
->lineno
);
5609 report("Chunk %d of %d", i
+ 1, stage_chunks
);
5614 report("No next chunk found");
5618 stage_request(struct view
*view
, enum request request
, struct line
*line
)
5621 case REQ_STATUS_UPDATE
:
5622 if (!stage_update(view
, line
))
5626 case REQ_STATUS_REVERT
:
5627 if (!stage_revert(view
, line
))
5631 case REQ_STAGE_NEXT
:
5632 if (stage_line_type
== LINE_STAT_UNTRACKED
) {
5633 report("File is untracked; press %s to add",
5634 get_key(KEYMAP_STAGE
, REQ_STATUS_UPDATE
));
5637 stage_next(view
, line
);
5641 if (!stage_status
.new.name
[0])
5643 if (stage_status
.status
== 'D') {
5644 report("File has been deleted.");
5648 open_editor(stage_status
.new.name
);
5652 /* Reload everything ... */
5655 case REQ_VIEW_BLAME
:
5656 if (stage_status
.new.name
[0]) {
5657 string_copy(opt_file
, stage_status
.new.name
);
5663 return pager_request(view
, request
, line
);
5669 VIEW(REQ_VIEW_STATUS
)->p_restore
= TRUE
;
5670 open_view(view
, REQ_VIEW_STATUS
, OPEN_REFRESH
);
5672 /* Check whether the staged entry still exists, and close the
5673 * stage view if it doesn't. */
5674 if (!status_exists(&stage_status
, stage_line_type
)) {
5675 status_restore(VIEW(REQ_VIEW_STATUS
));
5676 return REQ_VIEW_CLOSE
;
5679 if (stage_line_type
== LINE_STAT_UNTRACKED
) {
5680 if (!suffixcmp(stage_status
.new.name
, -1, "/")) {
5681 report("Cannot display a directory");
5685 if (!prepare_update_file(view
, stage_status
.new.name
)) {
5686 report("Failed to open file: %s", strerror(errno
));
5690 open_view(view
, REQ_VIEW_STAGE
, OPEN_REFRESH
);
5695 static struct view_ops stage_ops
= {
5711 static const enum line_type graph_colors
[] = {
5721 static enum line_type
get_graph_color(struct graph_symbol
*symbol
)
5724 return LINE_GRAPH_COMMIT
;
5725 assert(symbol
->color
< ARRAY_SIZE(graph_colors
));
5726 return graph_colors
[symbol
->color
];
5730 draw_graph_utf8(struct view
*view
, struct graph_symbol
*symbol
, enum line_type color
, bool first
)
5732 const char *chars
= graph_symbol_to_utf8(symbol
);
5734 return draw_text(view
, color
, chars
+ !!first
);
5738 draw_graph_ascii(struct view
*view
, struct graph_symbol
*symbol
, enum line_type color
, bool first
)
5740 const char *chars
= graph_symbol_to_ascii(symbol
);
5742 return draw_text(view
, color
, chars
+ !!first
);
5746 draw_graph_chtype(struct view
*view
, struct graph_symbol
*symbol
, enum line_type color
, bool first
)
5748 const chtype
*chars
= graph_symbol_to_chtype(symbol
);
5750 return draw_graphic(view
, color
, chars
+ !!first
, 2 - !!first
, FALSE
);
5753 typedef bool (*draw_graph_fn
)(struct view
*, struct graph_symbol
*, enum line_type
, bool);
5755 static bool draw_graph(struct view
*view
, struct graph_canvas
*canvas
)
5757 static const draw_graph_fn fns
[] = {
5762 draw_graph_fn fn
= fns
[opt_line_graphics
];
5765 for (i
= 0; i
< canvas
->size
; i
++) {
5766 struct graph_symbol
*symbol
= &canvas
->symbols
[i
];
5767 enum line_type color
= get_graph_color(symbol
);
5769 if (fn(view
, symbol
, color
, i
== 0))
5773 return draw_text(view
, LINE_MAIN_REVGRAPH
, " ");
5781 char id
[SIZEOF_REV
]; /* SHA1 ID. */
5782 char title
[128]; /* First line of the commit message. */
5783 const char *author
; /* Author of the commit. */
5784 struct time time
; /* Date from the author ident. */
5785 struct ref_list
*refs
; /* Repository references. */
5786 struct graph_canvas graph
; /* Ancestry chain graphics. */
5789 static const char *main_argv
[SIZEOF_ARG
] = {
5790 "git", "log", "--no-color", "--pretty=raw", "--parents",
5791 "--topo-order", "%(diffargs)", "%(revargs)",
5792 "--", "%(fileargs)", NULL
5796 main_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
5798 struct commit
*commit
= line
->data
;
5800 if (!commit
->author
)
5803 if (opt_date
&& draw_date(view
, &commit
->time
))
5806 if (opt_author
&& draw_author(view
, commit
->author
))
5809 if (opt_rev_graph
&& draw_graph(view
, &commit
->graph
))
5812 if (opt_show_refs
&& commit
->refs
) {
5815 for (i
= 0; i
< commit
->refs
->size
; i
++) {
5816 struct ref
*ref
= commit
->refs
->refs
[i
];
5817 enum line_type type
;
5820 type
= LINE_MAIN_HEAD
;
5822 type
= LINE_MAIN_LOCAL_TAG
;
5824 type
= LINE_MAIN_TAG
;
5825 else if (ref
->tracked
)
5826 type
= LINE_MAIN_TRACKED
;
5827 else if (ref
->remote
)
5828 type
= LINE_MAIN_REMOTE
;
5830 type
= LINE_MAIN_REF
;
5832 if (draw_text(view
, type
, "[") ||
5833 draw_text(view
, type
, ref
->name
) ||
5834 draw_text(view
, type
, "]"))
5837 if (draw_text(view
, LINE_DEFAULT
, " "))
5842 draw_text(view
, LINE_DEFAULT
, commit
->title
);
5846 /* Reads git log --pretty=raw output and parses it into the commit struct. */
5848 main_read(struct view
*view
, char *line
)
5850 static struct graph graph
;
5851 enum line_type type
;
5852 struct commit
*commit
;
5855 if (!view
->lines
&& !view
->prev
)
5856 die("No revisions match the given arguments.");
5857 if (view
->lines
> 0) {
5858 commit
= view
->line
[view
->lines
- 1].data
;
5859 view
->line
[view
->lines
- 1].dirty
= 1;
5860 if (!commit
->author
) {
5870 type
= get_line_type(line
);
5871 if (type
== LINE_COMMIT
) {
5874 commit
= calloc(1, sizeof(struct commit
));
5878 line
+= STRING_SIZE("commit ");
5879 is_boundary
= *line
== '-';
5883 string_copy_rev(commit
->id
, line
);
5884 commit
->refs
= get_ref_list(commit
->id
);
5885 add_line_data(view
, commit
, LINE_MAIN_COMMIT
);
5886 graph_add_commit(&graph
, &commit
->graph
, commit
->id
, line
, is_boundary
);
5892 commit
= view
->line
[view
->lines
- 1].data
;
5896 if (!graph
.has_parents
)
5897 graph_add_parent(&graph
, line
+ STRING_SIZE("parent "));
5901 parse_author_line(line
+ STRING_SIZE("author "),
5902 &commit
->author
, &commit
->time
);
5903 graph_render_parents(&graph
);
5907 /* Fill in the commit title if it has not already been set. */
5908 if (commit
->title
[0])
5911 /* Require titles to start with a non-space character at the
5912 * offset used by git log. */
5913 if (strncmp(line
, " ", 4))
5916 /* Well, if the title starts with a whitespace character,
5917 * try to be forgiving. Otherwise we end up with no title. */
5918 while (isspace(*line
))
5922 /* FIXME: More graceful handling of titles; append "..." to
5923 * shortened titles, etc. */
5925 string_expand(commit
->title
, sizeof(commit
->title
), line
, 1);
5926 view
->line
[view
->lines
- 1].dirty
= 1;
5933 main_request(struct view
*view
, enum request request
, struct line
*line
)
5935 enum open_flags flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
5939 if (view_is_displayed(view
) && display
[0] != view
)
5940 maximize_view(view
);
5941 open_view(view
, REQ_VIEW_DIFF
, flags
);
5945 open_view(view
, REQ_VIEW_MAIN
, OPEN_REFRESH
);
5955 grep_refs(struct ref_list
*list
, regex_t
*regex
)
5960 if (!opt_show_refs
|| !list
)
5963 for (i
= 0; i
< list
->size
; i
++) {
5964 if (regexec(regex
, list
->refs
[i
]->name
, 1, &pmatch
, 0) != REG_NOMATCH
)
5972 main_grep(struct view
*view
, struct line
*line
)
5974 struct commit
*commit
= line
->data
;
5975 const char *text
[] = {
5977 opt_author
? commit
->author
: "",
5978 mkdate(&commit
->time
, opt_date
),
5982 return grep_text(view
, text
) || grep_refs(commit
->refs
, view
->regex
);
5986 main_select(struct view
*view
, struct line
*line
)
5988 struct commit
*commit
= line
->data
;
5990 string_copy_rev(view
->ref
, commit
->id
);
5991 string_copy_rev(ref_commit
, view
->ref
);
5994 static struct view_ops main_ops
= {
6010 /* Whether or not the curses interface has been initialized. */
6011 static bool cursed
= FALSE
;
6013 /* Terminal hacks and workarounds. */
6014 static bool use_scroll_redrawwin
;
6015 static bool use_scroll_status_wclear
;
6017 /* The status window is used for polling keystrokes. */
6018 static WINDOW
*status_win
;
6020 /* Reading from the prompt? */
6021 static bool input_mode
= FALSE
;
6023 static bool status_empty
= FALSE
;
6025 /* Update status and title window. */
6027 report(const char *msg
, ...)
6029 struct view
*view
= display
[current_view
];
6035 char buf
[SIZEOF_STR
];
6038 va_start(args
, msg
);
6039 if (vsnprintf(buf
, sizeof(buf
), msg
, args
) >= sizeof(buf
)) {
6040 buf
[sizeof(buf
) - 1] = 0;
6041 buf
[sizeof(buf
) - 2] = '.';
6042 buf
[sizeof(buf
) - 3] = '.';
6043 buf
[sizeof(buf
) - 4] = '.';
6049 if (!status_empty
|| *msg
) {
6052 va_start(args
, msg
);
6054 wmove(status_win
, 0, 0);
6055 if (view
->has_scrolled
&& use_scroll_status_wclear
)
6058 vwprintw(status_win
, msg
, args
);
6059 status_empty
= FALSE
;
6061 status_empty
= TRUE
;
6063 wclrtoeol(status_win
);
6064 wnoutrefresh(status_win
);
6069 update_view_title(view
);
6078 /* Initialize the curses library */
6079 if (isatty(STDIN_FILENO
)) {
6080 cursed
= !!initscr();
6083 /* Leave stdin and stdout alone when acting as a pager. */
6084 opt_tty
= fopen("/dev/tty", "r+");
6086 die("Failed to open /dev/tty");
6087 cursed
= !!newterm(NULL
, opt_tty
, opt_tty
);
6091 die("Failed to initialize curses");
6093 nonl(); /* Disable conversion and detect newlines from input. */
6094 cbreak(); /* Take input chars one at a time, no wait for \n */
6095 noecho(); /* Don't echo input */
6096 leaveok(stdscr
, FALSE
);
6101 getmaxyx(stdscr
, y
, x
);
6102 status_win
= newwin(1, x
, y
- 1, 0);
6104 die("Failed to create status window");
6106 /* Enable keyboard mapping */
6107 keypad(status_win
, TRUE
);
6108 wbkgdset(status_win
, get_line_attr(LINE_STATUS
));
6110 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
6111 set_tabsize(opt_tab_size
);
6113 TABSIZE
= opt_tab_size
;
6116 term
= getenv("XTERM_VERSION") ? NULL
: getenv("COLORTERM");
6117 if (term
&& !strcmp(term
, "gnome-terminal")) {
6118 /* In the gnome-terminal-emulator, the message from
6119 * scrolling up one line when impossible followed by
6120 * scrolling down one line causes corruption of the
6121 * status line. This is fixed by calling wclear. */
6122 use_scroll_status_wclear
= TRUE
;
6123 use_scroll_redrawwin
= FALSE
;
6125 } else if (term
&& !strcmp(term
, "xrvt-xpm")) {
6126 /* No problems with full optimizations in xrvt-(unicode)
6128 use_scroll_status_wclear
= use_scroll_redrawwin
= FALSE
;
6131 /* When scrolling in (u)xterm the last line in the
6132 * scrolling direction will update slowly. */
6133 use_scroll_redrawwin
= TRUE
;
6134 use_scroll_status_wclear
= FALSE
;
6139 get_input(int prompt_position
)
6142 int i
, key
, cursor_y
, cursor_x
;
6144 if (prompt_position
)
6148 bool loading
= FALSE
;
6150 foreach_view (view
, i
) {
6152 if (view_is_displayed(view
) && view
->has_scrolled
&&
6153 use_scroll_redrawwin
)
6154 redrawwin(view
->win
);
6155 view
->has_scrolled
= FALSE
;
6160 /* Update the cursor position. */
6161 if (prompt_position
) {
6162 getbegyx(status_win
, cursor_y
, cursor_x
);
6163 cursor_x
= prompt_position
;
6165 view
= display
[current_view
];
6166 getbegyx(view
->win
, cursor_y
, cursor_x
);
6167 cursor_x
= view
->width
- 1;
6168 cursor_y
+= view
->lineno
- view
->offset
;
6170 setsyx(cursor_y
, cursor_x
);
6172 /* Refresh, accept single keystroke of input */
6174 nodelay(status_win
, loading
);
6175 key
= wgetch(status_win
);
6177 /* wgetch() with nodelay() enabled returns ERR when
6178 * there's no input. */
6181 } else if (key
== KEY_RESIZE
) {
6184 getmaxyx(stdscr
, height
, width
);
6186 wresize(status_win
, 1, width
);
6187 mvwin(status_win
, height
- 1, 0);
6188 wnoutrefresh(status_win
);
6190 redraw_display(TRUE
);
6200 prompt_input(const char *prompt
, input_handler handler
, void *data
)
6202 enum input_status status
= INPUT_OK
;
6203 static char buf
[SIZEOF_STR
];
6208 while (status
== INPUT_OK
|| status
== INPUT_SKIP
) {
6211 mvwprintw(status_win
, 0, 0, "%s%.*s", prompt
, pos
, buf
);
6212 wclrtoeol(status_win
);
6214 key
= get_input(pos
+ 1);
6219 status
= pos
? INPUT_STOP
: INPUT_CANCEL
;
6226 status
= INPUT_CANCEL
;
6230 status
= INPUT_CANCEL
;
6234 if (pos
>= sizeof(buf
)) {
6235 report("Input string too long");
6239 status
= handler(data
, buf
, key
);
6240 if (status
== INPUT_OK
)
6241 buf
[pos
++] = (char) key
;
6245 /* Clear the status window */
6246 status_empty
= FALSE
;
6249 if (status
== INPUT_CANCEL
)
6257 static enum input_status
6258 prompt_yesno_handler(void *data
, char *buf
, int c
)
6260 if (c
== 'y' || c
== 'Y')
6262 if (c
== 'n' || c
== 'N')
6263 return INPUT_CANCEL
;
6268 prompt_yesno(const char *prompt
)
6270 char prompt2
[SIZEOF_STR
];
6272 if (!string_format(prompt2
, "%s [Yy/Nn]", prompt
))
6275 return !!prompt_input(prompt2
, prompt_yesno_handler
, NULL
);
6278 static enum input_status
6279 read_prompt_handler(void *data
, char *buf
, int c
)
6281 return isprint(c
) ? INPUT_OK
: INPUT_SKIP
;
6285 read_prompt(const char *prompt
)
6287 return prompt_input(prompt
, read_prompt_handler
, NULL
);
6290 static bool prompt_menu(const char *prompt
, const struct menu_item
*items
, int *selected
)
6292 enum input_status status
= INPUT_OK
;
6295 while (items
[size
].text
)
6298 while (status
== INPUT_OK
) {
6299 const struct menu_item
*item
= &items
[*selected
];
6303 mvwprintw(status_win
, 0, 0, "%s (%d of %d) ",
6304 prompt
, *selected
+ 1, size
);
6306 wprintw(status_win
, "[%c] ", (char) item
->hotkey
);
6307 wprintw(status_win
, "%s", item
->text
);
6308 wclrtoeol(status_win
);
6310 key
= get_input(COLS
- 1);
6315 status
= INPUT_STOP
;
6320 *selected
= *selected
- 1;
6322 *selected
= size
- 1;
6327 *selected
= (*selected
+ 1) % size
;
6331 status
= INPUT_CANCEL
;
6335 for (i
= 0; items
[i
].text
; i
++)
6336 if (items
[i
].hotkey
== key
) {
6338 status
= INPUT_STOP
;
6344 /* Clear the status window */
6345 status_empty
= FALSE
;
6348 return status
!= INPUT_CANCEL
;
6352 * Repository properties
6355 static struct ref
**refs
= NULL
;
6356 static size_t refs_size
= 0;
6357 static struct ref
*refs_head
= NULL
;
6359 static struct ref_list
**ref_lists
= NULL
;
6360 static size_t ref_lists_size
= 0;
6362 DEFINE_ALLOCATOR(realloc_refs
, struct ref
*, 256)
6363 DEFINE_ALLOCATOR(realloc_refs_list
, struct ref
*, 8)
6364 DEFINE_ALLOCATOR(realloc_ref_lists
, struct ref_list
*, 8)
6367 compare_refs(const void *ref1_
, const void *ref2_
)
6369 const struct ref
*ref1
= *(const struct ref
**)ref1_
;
6370 const struct ref
*ref2
= *(const struct ref
**)ref2_
;
6372 if (ref1
->tag
!= ref2
->tag
)
6373 return ref2
->tag
- ref1
->tag
;
6374 if (ref1
->ltag
!= ref2
->ltag
)
6375 return ref2
->ltag
- ref2
->ltag
;
6376 if (ref1
->head
!= ref2
->head
)
6377 return ref2
->head
- ref1
->head
;
6378 if (ref1
->tracked
!= ref2
->tracked
)
6379 return ref2
->tracked
- ref1
->tracked
;
6380 if (ref1
->remote
!= ref2
->remote
)
6381 return ref2
->remote
- ref1
->remote
;
6382 return strcmp(ref1
->name
, ref2
->name
);
6386 foreach_ref(bool (*visitor
)(void *data
, const struct ref
*ref
), void *data
)
6390 for (i
= 0; i
< refs_size
; i
++)
6391 if (!visitor(data
, refs
[i
]))
6401 static struct ref_list
*
6402 get_ref_list(const char *id
)
6404 struct ref_list
*list
;
6407 for (i
= 0; i
< ref_lists_size
; i
++)
6408 if (!strcmp(id
, ref_lists
[i
]->id
))
6409 return ref_lists
[i
];
6411 if (!realloc_ref_lists(&ref_lists
, ref_lists_size
, 1))
6413 list
= calloc(1, sizeof(*list
));
6417 for (i
= 0; i
< refs_size
; i
++) {
6418 if (!strcmp(id
, refs
[i
]->id
) &&
6419 realloc_refs_list(&list
->refs
, list
->size
, 1))
6420 list
->refs
[list
->size
++] = refs
[i
];
6428 qsort(list
->refs
, list
->size
, sizeof(*list
->refs
), compare_refs
);
6429 ref_lists
[ref_lists_size
++] = list
;
6434 read_ref(char *id
, size_t idlen
, char *name
, size_t namelen
, void *data
)
6436 struct ref
*ref
= NULL
;
6439 bool remote
= FALSE
;
6440 bool tracked
= FALSE
;
6442 int from
= 0, to
= refs_size
- 1;
6444 if (!prefixcmp(name
, "refs/tags/")) {
6445 if (!suffixcmp(name
, namelen
, "^{}")) {
6453 namelen
-= STRING_SIZE("refs/tags/");
6454 name
+= STRING_SIZE("refs/tags/");
6456 } else if (!prefixcmp(name
, "refs/remotes/")) {
6458 namelen
-= STRING_SIZE("refs/remotes/");
6459 name
+= STRING_SIZE("refs/remotes/");
6460 tracked
= !strcmp(opt_remote
, name
);
6462 } else if (!prefixcmp(name
, "refs/heads/")) {
6463 namelen
-= STRING_SIZE("refs/heads/");
6464 name
+= STRING_SIZE("refs/heads/");
6465 if (!strncmp(opt_head
, name
, namelen
))
6468 } else if (!strcmp(name
, "HEAD")) {
6471 namelen
= strlen(opt_head
);
6476 /* If we are reloading or it's an annotated tag, replace the
6477 * previous SHA1 with the resolved commit id; relies on the fact
6478 * git-ls-remote lists the commit id of an annotated tag right
6479 * before the commit id it points to. */
6480 while (from
<= to
) {
6481 size_t pos
= (to
+ from
) / 2;
6482 int cmp
= strcmp(name
, refs
[pos
]->name
);
6496 if (!realloc_refs(&refs
, refs_size
, 1))
6498 ref
= calloc(1, sizeof(*ref
) + namelen
);
6501 memmove(refs
+ from
+ 1, refs
+ from
,
6502 (refs_size
- from
) * sizeof(*refs
));
6504 strncpy(ref
->name
, name
, namelen
);
6511 ref
->remote
= remote
;
6512 ref
->tracked
= tracked
;
6513 string_copy_rev(ref
->id
, id
);
6523 const char *head_argv
[] = {
6524 "git", "symbolic-ref", "HEAD", NULL
6526 static const char *ls_remote_argv
[SIZEOF_ARG
] = {
6527 "git", "ls-remote", opt_git_dir
, NULL
6529 static bool init
= FALSE
;
6533 if (!argv_from_env(ls_remote_argv
, "TIG_LS_REMOTE"))
6534 die("TIG_LS_REMOTE contains too many arguments");
6541 if (io_run_buf(head_argv
, opt_head
, sizeof(opt_head
)) &&
6542 !prefixcmp(opt_head
, "refs/heads/")) {
6543 char *offset
= opt_head
+ STRING_SIZE("refs/heads/");
6545 memmove(opt_head
, offset
, strlen(offset
) + 1);
6549 for (i
= 0; i
< refs_size
; i
++)
6552 if (io_run_load(ls_remote_argv
, "\t", read_ref
, NULL
) == ERR
)
6555 /* Update the ref lists to reflect changes. */
6556 for (i
= 0; i
< ref_lists_size
; i
++) {
6557 struct ref_list
*list
= ref_lists
[i
];
6560 for (old
= new = 0; old
< list
->size
; old
++)
6561 if (!strcmp(list
->id
, list
->refs
[old
]->id
))
6562 list
->refs
[new++] = list
->refs
[old
];
6570 set_remote_branch(const char *name
, const char *value
, size_t valuelen
)
6572 if (!strcmp(name
, ".remote")) {
6573 string_ncopy(opt_remote
, value
, valuelen
);
6575 } else if (*opt_remote
&& !strcmp(name
, ".merge")) {
6576 size_t from
= strlen(opt_remote
);
6578 if (!prefixcmp(value
, "refs/heads/"))
6579 value
+= STRING_SIZE("refs/heads/");
6581 if (!string_format_from(opt_remote
, &from
, "/%s", value
))
6587 set_repo_config_option(char *name
, char *value
, enum option_code (*cmd
)(int, const char **))
6589 const char *argv
[SIZEOF_ARG
] = { name
, "=" };
6590 int argc
= 1 + (cmd
== option_set_command
);
6591 enum option_code error
;
6593 if (!argv_from_string(argv
, &argc
, value
))
6594 error
= OPT_ERR_TOO_MANY_OPTION_ARGUMENTS
;
6596 error
= cmd(argc
, argv
);
6598 if (error
!= OPT_OK
)
6599 warn("Option 'tig.%s': %s", name
, option_errors
[error
]);
6603 set_environment_variable(const char *name
, const char *value
)
6605 size_t len
= strlen(name
) + 1 + strlen(value
) + 1;
6606 char *env
= malloc(len
);
6609 string_nformat(env
, len
, NULL
, "%s=%s", name
, value
) &&
6617 set_work_tree(const char *value
)
6619 char cwd
[SIZEOF_STR
];
6621 if (!getcwd(cwd
, sizeof(cwd
)))
6622 die("Failed to get cwd path: %s", strerror(errno
));
6623 if (chdir(opt_git_dir
) < 0)
6624 die("Failed to chdir(%s): %s", strerror(errno
));
6625 if (!getcwd(opt_git_dir
, sizeof(opt_git_dir
)))
6626 die("Failed to get git path: %s", strerror(errno
));
6628 die("Failed to chdir(%s): %s", cwd
, strerror(errno
));
6629 if (chdir(value
) < 0)
6630 die("Failed to chdir(%s): %s", value
, strerror(errno
));
6631 if (!getcwd(cwd
, sizeof(cwd
)))
6632 die("Failed to get cwd path: %s", strerror(errno
));
6633 if (!set_environment_variable("GIT_WORK_TREE", cwd
))
6634 die("Failed to set GIT_WORK_TREE to '%s'", cwd
);
6635 if (!set_environment_variable("GIT_DIR", opt_git_dir
))
6636 die("Failed to set GIT_DIR to '%s'", opt_git_dir
);
6637 opt_is_inside_work_tree
= TRUE
;
6641 read_repo_config_option(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
6643 if (!strcmp(name
, "i18n.commitencoding"))
6644 string_ncopy(opt_encoding
, value
, valuelen
);
6646 else if (!strcmp(name
, "core.editor"))
6647 string_ncopy(opt_editor
, value
, valuelen
);
6649 else if (!strcmp(name
, "core.worktree"))
6650 set_work_tree(value
);
6652 else if (!prefixcmp(name
, "tig.color."))
6653 set_repo_config_option(name
+ 10, value
, option_color_command
);
6655 else if (!prefixcmp(name
, "tig.bind."))
6656 set_repo_config_option(name
+ 9, value
, option_bind_command
);
6658 else if (!prefixcmp(name
, "tig."))
6659 set_repo_config_option(name
+ 4, value
, option_set_command
);
6661 else if (*opt_head
&& !prefixcmp(name
, "branch.") &&
6662 !strncmp(name
+ 7, opt_head
, strlen(opt_head
)))
6663 set_remote_branch(name
+ 7 + strlen(opt_head
), value
, valuelen
);
6669 load_git_config(void)
6671 const char *config_list_argv
[] = { "git", "config", "--list", NULL
};
6673 return io_run_load(config_list_argv
, "=", read_repo_config_option
, NULL
);
6677 read_repo_info(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
6679 if (!opt_git_dir
[0]) {
6680 string_ncopy(opt_git_dir
, name
, namelen
);
6682 } else if (opt_is_inside_work_tree
== -1) {
6683 /* This can be 3 different values depending on the
6684 * version of git being used. If git-rev-parse does not
6685 * understand --is-inside-work-tree it will simply echo
6686 * the option else either "true" or "false" is printed.
6687 * Default to true for the unknown case. */
6688 opt_is_inside_work_tree
= strcmp(name
, "false") ? TRUE
: FALSE
;
6690 } else if (*name
== '.') {
6691 string_ncopy(opt_cdup
, name
, namelen
);
6694 string_ncopy(opt_prefix
, name
, namelen
);
6701 load_repo_info(void)
6703 const char *rev_parse_argv
[] = {
6704 "git", "rev-parse", "--git-dir", "--is-inside-work-tree",
6705 "--show-cdup", "--show-prefix", NULL
6708 return io_run_load(rev_parse_argv
, "=", read_repo_info
, NULL
);
6716 static const char usage
[] =
6717 "tig " TIG_VERSION
" (" __DATE__
")\n"
6719 "Usage: tig [options] [revs] [--] [paths]\n"
6720 " or: tig show [options] [revs] [--] [paths]\n"
6721 " or: tig blame [options] [rev] [--] path\n"
6723 " or: tig < [git command output]\n"
6726 " -v, --version Show version and exit\n"
6727 " -h, --help Show help message and exit";
6729 static void __NORETURN
6732 /* XXX: Restore tty modes and let the OS cleanup the rest! */
6738 static void __NORETURN
6739 die(const char *err
, ...)
6745 va_start(args
, err
);
6746 fputs("tig: ", stderr
);
6747 vfprintf(stderr
, err
, args
);
6748 fputs("\n", stderr
);
6755 warn(const char *msg
, ...)
6759 va_start(args
, msg
);
6760 fputs("tig warning: ", stderr
);
6761 vfprintf(stderr
, msg
, args
);
6762 fputs("\n", stderr
);
6767 read_filter_args(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
6769 const char ***filter_args
= data
;
6771 return argv_append(filter_args
, name
) ? OK
: ERR
;
6775 filter_rev_parse(const char ***args
, const char *arg1
, const char *arg2
, const char *argv
[])
6777 const char *rev_parse_argv
[SIZEOF_ARG
] = { "git", "rev-parse", arg1
, arg2
};
6778 const char **all_argv
= NULL
;
6780 if (!argv_append_array(&all_argv
, rev_parse_argv
) ||
6781 !argv_append_array(&all_argv
, argv
) ||
6782 !io_run_load(all_argv
, "\n", read_filter_args
, args
) == ERR
)
6783 die("Failed to split arguments");
6784 argv_free(all_argv
);
6789 filter_options(const char *argv
[])
6791 filter_rev_parse(&opt_file_argv
, "--no-revs", "--no-flags", argv
);
6792 filter_rev_parse(&opt_diff_argv
, "--no-revs", "--flags", argv
);
6793 filter_rev_parse(&opt_rev_argv
, "--symbolic", "--revs-only", argv
);
6797 parse_options(int argc
, const char *argv
[])
6799 enum request request
= REQ_VIEW_MAIN
;
6800 const char *subcommand
;
6801 bool seen_dashdash
= FALSE
;
6802 const char **filter_argv
= NULL
;
6805 if (!isatty(STDIN_FILENO
))
6806 return REQ_VIEW_PAGER
;
6809 return REQ_VIEW_MAIN
;
6811 subcommand
= argv
[1];
6812 if (!strcmp(subcommand
, "status")) {
6814 warn("ignoring arguments after `%s'", subcommand
);
6815 return REQ_VIEW_STATUS
;
6817 } else if (!strcmp(subcommand
, "blame")) {
6818 filter_rev_parse(&opt_file_argv
, "--no-revs", "--no-flags", argv
+ 2);
6819 filter_rev_parse(&opt_blame_argv
, "--no-revs", "--flags", argv
+ 2);
6820 filter_rev_parse(&opt_rev_argv
, "--symbolic", "--revs-only", argv
+ 2);
6822 if (!opt_file_argv
|| opt_file_argv
[1] || (opt_rev_argv
&& opt_rev_argv
[1]))
6823 die("invalid number of options to blame\n\n%s", usage
);
6826 string_ncopy(opt_ref
, opt_rev_argv
[0], strlen(opt_rev_argv
[0]));
6829 string_ncopy(opt_file
, opt_file_argv
[0], strlen(opt_file_argv
[0]));
6830 return REQ_VIEW_BLAME
;
6832 } else if (!strcmp(subcommand
, "show")) {
6833 request
= REQ_VIEW_DIFF
;
6839 for (i
= 1 + !!subcommand
; i
< argc
; i
++) {
6840 const char *opt
= argv
[i
];
6842 if (seen_dashdash
) {
6843 argv_append(&opt_file_argv
, opt
);
6846 } else if (!strcmp(opt
, "--")) {
6847 seen_dashdash
= TRUE
;
6850 } else if (!strcmp(opt
, "-v") || !strcmp(opt
, "--version")) {
6851 printf("tig version %s\n", TIG_VERSION
);
6854 } else if (!strcmp(opt
, "-h") || !strcmp(opt
, "--help")) {
6855 printf("%s\n", usage
);
6858 } else if (!strcmp(opt
, "--all")) {
6859 argv_append(&opt_rev_argv
, opt
);
6863 if (!argv_append(&filter_argv
, opt
))
6864 die("command too long");
6868 filter_options(filter_argv
);
6874 main(int argc
, const char *argv
[])
6876 const char *codeset
= "UTF-8";
6877 enum request request
= parse_options(argc
, argv
);
6880 signal(SIGINT
, quit
);
6881 signal(SIGPIPE
, SIG_IGN
);
6883 if (setlocale(LC_ALL
, "")) {
6884 codeset
= nl_langinfo(CODESET
);
6887 if (load_repo_info() == ERR
)
6888 die("Failed to load repo info.");
6890 if (load_options() == ERR
)
6891 die("Failed to load user config.");
6893 if (load_git_config() == ERR
)
6894 die("Failed to load repo config.");
6896 /* Require a git repository unless when running in pager mode. */
6897 if (!opt_git_dir
[0] && request
!= REQ_VIEW_PAGER
)
6898 die("Not a git repository");
6900 if (*opt_encoding
&& strcmp(codeset
, "UTF-8")) {
6901 opt_iconv_in
= iconv_open("UTF-8", opt_encoding
);
6902 if (opt_iconv_in
== ICONV_NONE
)
6903 die("Failed to initialize character set conversion");
6906 if (codeset
&& strcmp(codeset
, "UTF-8")) {
6907 opt_iconv_out
= iconv_open(codeset
, "UTF-8");
6908 if (opt_iconv_out
== ICONV_NONE
)
6909 die("Failed to initialize character set conversion");
6912 if (load_refs() == ERR
)
6913 die("Failed to load refs.");
6917 while (view_driver(display
[current_view
], request
)) {
6918 int key
= get_input(0);
6920 view
= display
[current_view
];
6921 request
= get_keybinding(view
->keymap
, key
);
6923 /* Some low-level request handling. This keeps access to
6924 * status_win restricted. */
6927 report("Unknown key, press %s for help",
6928 get_key(view
->keymap
, REQ_VIEW_HELP
));
6932 char *cmd
= read_prompt(":");
6934 if (cmd
&& isdigit(*cmd
)) {
6935 int lineno
= view
->lineno
+ 1;
6937 if (parse_int(&lineno
, cmd
, 1, view
->lines
+ 1) == OK
) {
6938 select_view_line(view
, lineno
- 1);
6941 report("Unable to parse '%s' as a line number", cmd
);
6945 struct view
*next
= VIEW(REQ_VIEW_PAGER
);
6946 const char *argv
[SIZEOF_ARG
] = { "git" };
6949 /* When running random commands, initially show the
6950 * command in the title. However, it maybe later be
6951 * overwritten if a commit line is selected. */
6952 string_ncopy(next
->ref
, cmd
, strlen(cmd
));
6954 if (!argv_from_string(argv
, &argc
, cmd
)) {
6955 report("Too many arguments");
6956 } else if (!prepare_update(next
, argv
, NULL
)) {
6957 report("Failed to format command");
6959 open_view(view
, REQ_VIEW_PAGER
, OPEN_PREPARED
);
6967 case REQ_SEARCH_BACK
:
6969 const char *prompt
= request
== REQ_SEARCH
? "/" : "?";
6970 char *search
= read_prompt(prompt
);
6973 string_ncopy(opt_search
, search
, strlen(search
));
6974 else if (*opt_search
)
6975 request
= request
== REQ_SEARCH
?