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 replace
:1; /* Is it a replace ref? */
30 unsigned int tracked
:1; /* Is it the remote for the current HEAD? */
31 char name
[1]; /* Ref name; tag or head names are shortened. */
35 char id
[SIZEOF_REV
]; /* Commit SHA1 ID */
36 size_t size
; /* Number of refs. */
37 struct ref
**refs
; /* References for this ID. */
40 static struct ref
*get_ref_head();
41 static struct ref_list
*get_ref_list(const char *id
);
42 static void foreach_ref(bool (*visitor
)(void *data
, const struct ref
*ref
), void *data
);
43 static int load_refs(void);
52 typedef enum input_status (*input_handler
)(void *data
, char *buf
, int c
);
54 static char *prompt_input(const char *prompt
, input_handler handler
, void *data
);
55 static bool prompt_yesno(const char *prompt
);
63 static bool prompt_menu(const char *prompt
, const struct menu_item
*items
, int *selected
);
65 #define GRAPHIC_ENUM(_) \
67 _(GRAPHIC, DEFAULT), \
70 DEFINE_ENUM(graphic
, GRAPHIC_ENUM
);
72 #define DATE_ENUM(_) \
79 DEFINE_ENUM(date
, DATE_ENUM
);
86 static inline int timecmp(const struct time
*t1
, const struct time
*t2
)
88 return t1
->sec
- t2
->sec
;
92 mkdate(const struct time
*time
, enum date date
)
94 static char buf
[DATE_COLS
+ 1];
95 static const struct enum_map reldate
[] = {
96 { "second", 1, 60 * 2 },
97 { "minute", 60, 60 * 60 * 2 },
98 { "hour", 60 * 60, 60 * 60 * 24 * 2 },
99 { "day", 60 * 60 * 24, 60 * 60 * 24 * 7 * 2 },
100 { "week", 60 * 60 * 24 * 7, 60 * 60 * 24 * 7 * 5 },
101 { "month", 60 * 60 * 24 * 30, 60 * 60 * 24 * 30 * 12 },
105 if (!date
|| !time
|| !time
->sec
)
108 if (date
== DATE_RELATIVE
) {
110 time_t date
= time
->sec
+ time
->tz
;
114 gettimeofday(&now
, NULL
);
115 seconds
= now
.tv_sec
< date
? date
- now
.tv_sec
: now
.tv_sec
- date
;
116 for (i
= 0; i
< ARRAY_SIZE(reldate
); i
++) {
117 if (seconds
>= reldate
[i
].value
)
120 seconds
/= reldate
[i
].namelen
;
121 if (!string_format(buf
, "%ld %s%s %s",
122 seconds
, reldate
[i
].name
,
123 seconds
> 1 ? "s" : "",
124 now
.tv_sec
>= date
? "ago" : "ahead"))
130 if (date
== DATE_LOCAL
) {
131 time_t date
= time
->sec
+ time
->tz
;
132 localtime_r(&date
, &tm
);
135 gmtime_r(&time
->sec
, &tm
);
137 return strftime(buf
, sizeof(buf
), DATE_FORMAT
, &tm
) ? buf
: NULL
;
141 #define AUTHOR_ENUM(_) \
144 _(AUTHOR, ABBREVIATED)
146 DEFINE_ENUM(author
, AUTHOR_ENUM
);
149 get_author_initials(const char *author
)
151 static char initials
[AUTHOR_COLS
* 6 + 1];
153 const char *end
= strchr(author
, '\0');
155 #define is_initial_sep(c) (isspace(c) || ispunct(c) || (c) == '@' || (c) == '-')
157 memset(initials
, 0, sizeof(initials
));
158 while (author
< end
) {
162 while (author
< end
&& is_initial_sep(*author
))
165 bytes
= utf8_char_length(author
, end
);
166 if (bytes
>= sizeof(initials
) - 1 - pos
)
169 initials
[pos
++] = *author
++;
173 while (author
< end
&& !is_initial_sep(*author
)) {
174 bytes
= utf8_char_length(author
, end
);
175 if (bytes
>= sizeof(initials
) - 1 - i
) {
176 while (author
< end
&& !is_initial_sep(*author
))
181 initials
[i
++] = *author
++;
191 #define author_trim(cols) (cols == 0 || cols > 5)
194 mkauthor(const char *text
, int cols
, enum author author
)
196 bool trim
= author_trim(cols
);
197 bool abbreviate
= author
== AUTHOR_ABBREVIATED
|| !trim
;
199 if (author
== AUTHOR_NO
)
201 if (abbreviate
&& text
)
202 return get_author_initials(text
);
211 else if (S_ISLNK(mode
))
213 else if (S_ISGITLINK(mode
))
215 else if (S_ISREG(mode
) && mode
& S_IXUSR
)
217 else if (S_ISREG(mode
))
223 #define FILENAME_ENUM(_) \
225 _(FILENAME, ALWAYS), \
228 DEFINE_ENUM(filename
, FILENAME_ENUM
);
236 /* XXX: Keep the view request first and in sync with views[]. */ \
237 REQ_GROUP("View switching") \
238 REQ_(VIEW_MAIN, "Show main view"), \
239 REQ_(VIEW_DIFF, "Show diff view"), \
240 REQ_(VIEW_LOG, "Show log view"), \
241 REQ_(VIEW_TREE, "Show tree view"), \
242 REQ_(VIEW_BLOB, "Show blob view"), \
243 REQ_(VIEW_BLAME, "Show blame view"), \
244 REQ_(VIEW_BRANCH, "Show branch view"), \
245 REQ_(VIEW_HELP, "Show help page"), \
246 REQ_(VIEW_PAGER, "Show pager view"), \
247 REQ_(VIEW_STATUS, "Show status view"), \
248 REQ_(VIEW_STAGE, "Show stage view"), \
250 REQ_GROUP("View manipulation") \
251 REQ_(ENTER, "Enter current line and scroll"), \
252 REQ_(NEXT, "Move to next"), \
253 REQ_(PREVIOUS, "Move to previous"), \
254 REQ_(PARENT, "Move to parent"), \
255 REQ_(VIEW_NEXT, "Move focus to next view"), \
256 REQ_(REFRESH, "Reload and refresh"), \
257 REQ_(MAXIMIZE, "Maximize the current view"), \
258 REQ_(VIEW_CLOSE, "Close the current view"), \
259 REQ_(QUIT, "Close all views and quit"), \
261 REQ_GROUP("View specific requests") \
262 REQ_(STATUS_UPDATE, "Update file status"), \
263 REQ_(STATUS_REVERT, "Revert file changes"), \
264 REQ_(STATUS_MERGE, "Merge file using external tool"), \
265 REQ_(STAGE_UPDATE_LINE, "Update single line"), \
266 REQ_(STAGE_NEXT, "Find next chunk to stage"), \
267 REQ_(DIFF_CONTEXT_DOWN, "Decrease the diff context"), \
268 REQ_(DIFF_CONTEXT_UP, "Increase the diff context"), \
270 REQ_GROUP("Cursor navigation") \
271 REQ_(MOVE_UP, "Move cursor one line up"), \
272 REQ_(MOVE_DOWN, "Move cursor one line down"), \
273 REQ_(MOVE_PAGE_DOWN, "Move cursor one page down"), \
274 REQ_(MOVE_PAGE_UP, "Move cursor one page up"), \
275 REQ_(MOVE_FIRST_LINE, "Move cursor to first line"), \
276 REQ_(MOVE_LAST_LINE, "Move cursor to last line"), \
278 REQ_GROUP("Scrolling") \
279 REQ_(SCROLL_FIRST_COL, "Scroll to the first line columns"), \
280 REQ_(SCROLL_LEFT, "Scroll two columns left"), \
281 REQ_(SCROLL_RIGHT, "Scroll two columns right"), \
282 REQ_(SCROLL_LINE_UP, "Scroll one line up"), \
283 REQ_(SCROLL_LINE_DOWN, "Scroll one line down"), \
284 REQ_(SCROLL_PAGE_UP, "Scroll one page up"), \
285 REQ_(SCROLL_PAGE_DOWN, "Scroll one page down"), \
287 REQ_GROUP("Searching") \
288 REQ_(SEARCH, "Search the view"), \
289 REQ_(SEARCH_BACK, "Search backwards in the view"), \
290 REQ_(FIND_NEXT, "Find next search match"), \
291 REQ_(FIND_PREV, "Find previous search match"), \
293 REQ_GROUP("Option manipulation") \
294 REQ_(OPTIONS, "Open option menu"), \
295 REQ_(TOGGLE_LINENO, "Toggle line numbers"), \
296 REQ_(TOGGLE_DATE, "Toggle date display"), \
297 REQ_(TOGGLE_AUTHOR, "Toggle author display"), \
298 REQ_(TOGGLE_REV_GRAPH, "Toggle revision graph visualization"), \
299 REQ_(TOGGLE_GRAPHIC, "Toggle (line) graphics mode"), \
300 REQ_(TOGGLE_FILENAME, "Toggle file name display"), \
301 REQ_(TOGGLE_REFS, "Toggle reference display (tags/branches)"), \
302 REQ_(TOGGLE_SORT_ORDER, "Toggle ascending/descending sort order"), \
303 REQ_(TOGGLE_SORT_FIELD, "Toggle field to sort by"), \
306 REQ_(PROMPT, "Bring up the prompt"), \
307 REQ_(SCREEN_REDRAW, "Redraw the screen"), \
308 REQ_(SHOW_VERSION, "Show version information"), \
309 REQ_(STOP_LOADING, "Stop all loading views"), \
310 REQ_(EDIT, "Open in editor"), \
311 REQ_(NONE, "Do nothing")
314 /* User action requests. */
316 #define REQ_GROUP(help)
317 #define REQ_(req, help) REQ_##req
319 /* Offset all requests to avoid conflicts with ncurses getch values. */
320 REQ_UNKNOWN
= KEY_MAX
+ 1,
324 /* Internal requests. */
331 struct request_info
{
332 enum request request
;
338 static const struct request_info req_info
[] = {
339 #define REQ_GROUP(help) { 0, NULL, 0, (help) },
340 #define REQ_(req, help) { REQ_##req, (#req), STRING_SIZE(#req), (help) }
347 get_request(const char *name
)
349 int namelen
= strlen(name
);
352 for (i
= 0; i
< ARRAY_SIZE(req_info
); i
++)
353 if (enum_equals(req_info
[i
], name
, namelen
))
354 return req_info
[i
].request
;
364 /* Option and state variables. */
365 static enum graphic opt_line_graphics
= GRAPHIC_DEFAULT
;
366 static enum date opt_date
= DATE_DEFAULT
;
367 static enum author opt_author
= AUTHOR_FULL
;
368 static enum filename opt_filename
= FILENAME_AUTO
;
369 static bool opt_rev_graph
= TRUE
;
370 static bool opt_line_number
= FALSE
;
371 static bool opt_show_refs
= TRUE
;
372 static bool opt_untracked_dirs_content
= TRUE
;
373 static int opt_diff_context
= 3;
374 static char opt_diff_context_arg
[9] = "";
375 static char opt_notes_arg
[SIZEOF_STR
] = "--no-notes";
376 static int opt_num_interval
= 5;
377 static double opt_hscroll
= 0.50;
378 static double opt_scale_split_view
= 2.0 / 3.0;
379 static int opt_tab_size
= 8;
380 static int opt_author_cols
= AUTHOR_COLS
;
381 static int opt_filename_cols
= FILENAME_COLS
;
382 static char opt_path
[SIZEOF_STR
] = "";
383 static char opt_file
[SIZEOF_STR
] = "";
384 static char opt_ref
[SIZEOF_REF
] = "";
385 static unsigned long opt_goto_line
= 0;
386 static char opt_head
[SIZEOF_REF
] = "";
387 static char opt_remote
[SIZEOF_REF
] = "";
388 static char opt_encoding
[20] = ENCODING_UTF8
;
389 static iconv_t opt_iconv_in
= ICONV_NONE
;
390 static iconv_t opt_iconv_out
= ICONV_NONE
;
391 static char opt_search
[SIZEOF_STR
] = "";
392 static char opt_cdup
[SIZEOF_STR
] = "";
393 static char opt_prefix
[SIZEOF_STR
] = "";
394 static char opt_git_dir
[SIZEOF_STR
] = "";
395 static signed char opt_is_inside_work_tree
= -1; /* set to TRUE or FALSE */
396 static char opt_editor
[SIZEOF_STR
] = "";
397 static FILE *opt_tty
= NULL
;
398 static const char **opt_diff_argv
= NULL
;
399 static const char **opt_rev_argv
= NULL
;
400 static const char **opt_file_argv
= NULL
;
401 static const char **opt_blame_argv
= NULL
;
402 static int opt_lineno
= 0;
404 #define is_initial_commit() (!get_ref_head())
405 #define is_head_commit(rev) (!strcmp((rev), "HEAD") || (get_ref_head() && !strcmp(rev, get_ref_head()->id)))
408 update_diff_context_arg(int diff_context
)
410 if (!string_format(opt_diff_context_arg
, "-U%u", diff_context
))
411 string_ncopy(opt_diff_context_arg
, "-U3", 3);
415 * Line-oriented content detection.
419 LINE(DIFF_HEADER, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
420 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
421 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
422 LINE(DIFF_ADD2, " +", COLOR_GREEN, COLOR_DEFAULT, 0), \
423 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
424 LINE(DIFF_DEL2, " -", COLOR_RED, COLOR_DEFAULT, 0), \
425 LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
426 LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
427 LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
428 LINE(DIFF_COPY_FROM, "copy from", COLOR_YELLOW, COLOR_DEFAULT, 0), \
429 LINE(DIFF_COPY_TO, "copy to", COLOR_YELLOW, COLOR_DEFAULT, 0), \
430 LINE(DIFF_RENAME_FROM, "rename from", COLOR_YELLOW, COLOR_DEFAULT, 0), \
431 LINE(DIFF_RENAME_TO, "rename to", COLOR_YELLOW, COLOR_DEFAULT, 0), \
432 LINE(DIFF_SIMILARITY, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
433 LINE(DIFF_DISSIMILARITY,"dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
434 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
435 LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
436 LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
437 LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
438 LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
439 LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
440 LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
441 LINE(PP_REFS, "Refs: ", COLOR_RED, COLOR_DEFAULT, 0), \
442 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
443 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
444 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
445 LINE(AUTHOR, "author ", COLOR_GREEN, COLOR_DEFAULT, 0), \
446 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
447 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
448 LINE(ACKED, " Acked-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
449 LINE(TESTED, " Tested-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
450 LINE(REVIEWED, " Reviewed-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
451 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
452 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
453 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
454 LINE(DELIMITER, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
455 LINE(DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
456 LINE(MODE, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
457 LINE(FILENAME, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
458 LINE(LINE_NUMBER, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
459 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
460 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
461 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
462 LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
463 LINE(MAIN_LOCAL_TAG,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
464 LINE(MAIN_REMOTE, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
465 LINE(MAIN_REPLACE, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
466 LINE(MAIN_TRACKED, "", COLOR_YELLOW, COLOR_DEFAULT, A_BOLD), \
467 LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
468 LINE(MAIN_HEAD, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD), \
469 LINE(MAIN_REVGRAPH,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
470 LINE(TREE_HEAD, "", COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD), \
471 LINE(TREE_DIR, "", COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL), \
472 LINE(TREE_FILE, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
473 LINE(STAT_HEAD, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
474 LINE(STAT_SECTION, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
475 LINE(STAT_NONE, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
476 LINE(STAT_STAGED, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
477 LINE(STAT_UNSTAGED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
478 LINE(STAT_UNTRACKED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
479 LINE(HELP_KEYMAP, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
480 LINE(HELP_GROUP, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
481 LINE(BLAME_ID, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
482 LINE(DIFF_STAT, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
483 LINE(PALETTE_0, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
484 LINE(PALETTE_1, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
485 LINE(PALETTE_2, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
486 LINE(PALETTE_3, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
487 LINE(PALETTE_4, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
488 LINE(PALETTE_5, "", COLOR_WHITE, COLOR_DEFAULT, 0), \
489 LINE(PALETTE_6, "", COLOR_RED, COLOR_DEFAULT, 0), \
490 LINE(GRAPH_COMMIT, "", COLOR_BLUE, COLOR_DEFAULT, 0)
493 #define LINE(type, line, fg, bg, attr) \
501 const char *name
; /* Option name. */
502 int namelen
; /* Size of option name. */
503 const char *line
; /* The start of line to match. */
504 int linelen
; /* Size of string to match. */
505 int fg
, bg
, attr
; /* Color and text attributes for the lines. */
508 static struct line_info line_info
[] = {
509 #define LINE(type, line, fg, bg, attr) \
510 { #type, STRING_SIZE(#type), (line), STRING_SIZE(line), (fg), (bg), (attr) }
515 static struct line_info
*custom_color
;
516 static size_t custom_colors
;
518 DEFINE_ALLOCATOR(realloc_custom_color
, struct line_info
, 8)
520 #define TO_CUSTOM_COLOR_TYPE(type) (LINE_NONE + 1 + (type))
521 #define TO_CUSTOM_COLOR_OFFSET(type) ((type) - LINE_NONE - 1)
523 /* Color IDs must be 1 or higher. [GH #15] */
524 #define COLOR_ID(line_type) ((line_type) + 1)
526 static enum line_type
527 get_line_type(const char *line
)
529 int linelen
= strlen(line
);
532 for (type
= 0; type
< custom_colors
; type
++)
533 /* Case insensitive search matches Signed-off-by lines better. */
534 if (linelen
>= custom_color
[type
].linelen
&&
535 !strncasecmp(custom_color
[type
].line
, line
, custom_color
[type
].linelen
))
536 return TO_CUSTOM_COLOR_TYPE(type
);
538 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++)
539 /* Case insensitive search matches Signed-off-by lines better. */
540 if (linelen
>= line_info
[type
].linelen
&&
541 !strncasecmp(line_info
[type
].line
, line
, line_info
[type
].linelen
))
547 static enum line_type
548 get_line_type_from_ref(const struct ref
*ref
)
551 return LINE_MAIN_HEAD
;
553 return LINE_MAIN_LOCAL_TAG
;
555 return LINE_MAIN_TAG
;
556 else if (ref
->tracked
)
557 return LINE_MAIN_TRACKED
;
558 else if (ref
->remote
)
559 return LINE_MAIN_REMOTE
;
560 else if (ref
->replace
)
561 return LINE_MAIN_REPLACE
;
563 return LINE_MAIN_REF
;
567 get_line_attr(enum line_type type
)
569 if (type
> LINE_NONE
) {
570 assert(TO_CUSTOM_COLOR_OFFSET(type
) < custom_colors
);
571 return COLOR_PAIR(COLOR_ID(type
)) | custom_color
[TO_CUSTOM_COLOR_OFFSET(type
)].attr
;
573 assert(type
< ARRAY_SIZE(line_info
));
574 return COLOR_PAIR(COLOR_ID(type
)) | line_info
[type
].attr
;
577 static struct line_info
*
578 get_line_info(const char *name
)
580 size_t namelen
= strlen(name
);
583 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++)
584 if (enum_equals(line_info
[type
], name
, namelen
))
585 return &line_info
[type
];
590 static struct line_info
*
591 add_custom_color(const char *quoted_line
)
593 struct line_info
*info
;
597 if (!realloc_custom_color(&custom_color
, custom_colors
, 1))
598 die("Failed to alloc custom line info");
600 linelen
= strlen(quoted_line
) - 1;
601 line
= malloc(linelen
);
605 strncpy(line
, quoted_line
+ 1, linelen
);
606 line
[linelen
- 1] = 0;
608 info
= &custom_color
[custom_colors
++];
609 info
->name
= info
->line
= line
;
610 info
->namelen
= info
->linelen
= strlen(line
);
616 init_line_info_color_pair(struct line_info
*info
, enum line_type type
,
617 int default_bg
, int default_fg
)
619 int bg
= info
->bg
== COLOR_DEFAULT
? default_bg
: info
->bg
;
620 int fg
= info
->fg
== COLOR_DEFAULT
? default_fg
: info
->fg
;
622 init_pair(COLOR_ID(type
), fg
, bg
);
628 int default_bg
= line_info
[LINE_DEFAULT
].bg
;
629 int default_fg
= line_info
[LINE_DEFAULT
].fg
;
634 if (assume_default_colors(default_fg
, default_bg
) == ERR
) {
635 default_bg
= COLOR_BLACK
;
636 default_fg
= COLOR_WHITE
;
639 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++) {
640 struct line_info
*info
= &line_info
[type
];
642 init_line_info_color_pair(info
, type
, default_bg
, default_fg
);
645 for (type
= 0; type
< custom_colors
; type
++) {
646 struct line_info
*info
= &custom_color
[type
];
648 init_line_info_color_pair(info
, TO_CUSTOM_COLOR_TYPE(type
),
649 default_bg
, default_fg
);
657 unsigned int selected
:1;
658 unsigned int dirty
:1;
659 unsigned int cleareol
:1;
660 unsigned int other
:16;
662 void *data
; /* User data */
672 enum request request
;
675 static struct keybinding default_keybindings
[] = {
677 { 'm', REQ_VIEW_MAIN
},
678 { 'd', REQ_VIEW_DIFF
},
679 { 'l', REQ_VIEW_LOG
},
680 { 't', REQ_VIEW_TREE
},
681 { 'f', REQ_VIEW_BLOB
},
682 { 'B', REQ_VIEW_BLAME
},
683 { 'H', REQ_VIEW_BRANCH
},
684 { 'p', REQ_VIEW_PAGER
},
685 { 'h', REQ_VIEW_HELP
},
686 { 'S', REQ_VIEW_STATUS
},
687 { 'c', REQ_VIEW_STAGE
},
689 /* View manipulation */
690 { 'q', REQ_VIEW_CLOSE
},
691 { KEY_TAB
, REQ_VIEW_NEXT
},
692 { KEY_RETURN
, REQ_ENTER
},
693 { KEY_UP
, REQ_PREVIOUS
},
694 { KEY_CTL('P'), REQ_PREVIOUS
},
695 { KEY_DOWN
, REQ_NEXT
},
696 { KEY_CTL('N'), REQ_NEXT
},
697 { 'R', REQ_REFRESH
},
698 { KEY_F(5), REQ_REFRESH
},
699 { 'O', REQ_MAXIMIZE
},
703 { 'u', REQ_STATUS_UPDATE
},
704 { '!', REQ_STATUS_REVERT
},
705 { 'M', REQ_STATUS_MERGE
},
706 { KEY_CTL('u'), REQ_STAGE_UPDATE_LINE
},
707 { '@', REQ_STAGE_NEXT
},
708 { '[', REQ_DIFF_CONTEXT_DOWN
},
709 { ']', REQ_DIFF_CONTEXT_UP
},
711 /* Cursor navigation */
712 { 'k', REQ_MOVE_UP
},
713 { 'j', REQ_MOVE_DOWN
},
714 { KEY_HOME
, REQ_MOVE_FIRST_LINE
},
715 { KEY_END
, REQ_MOVE_LAST_LINE
},
716 { KEY_NPAGE
, REQ_MOVE_PAGE_DOWN
},
717 { KEY_CTL('D'), REQ_MOVE_PAGE_DOWN
},
718 { ' ', REQ_MOVE_PAGE_DOWN
},
719 { KEY_PPAGE
, REQ_MOVE_PAGE_UP
},
720 { KEY_CTL('U'), REQ_MOVE_PAGE_UP
},
721 { 'b', REQ_MOVE_PAGE_UP
},
722 { '-', REQ_MOVE_PAGE_UP
},
725 { '|', REQ_SCROLL_FIRST_COL
},
726 { KEY_LEFT
, REQ_SCROLL_LEFT
},
727 { KEY_RIGHT
, REQ_SCROLL_RIGHT
},
728 { KEY_IC
, REQ_SCROLL_LINE_UP
},
729 { KEY_CTL('Y'), REQ_SCROLL_LINE_UP
},
730 { KEY_DC
, REQ_SCROLL_LINE_DOWN
},
731 { KEY_CTL('E'), REQ_SCROLL_LINE_DOWN
},
732 { 'w', REQ_SCROLL_PAGE_UP
},
733 { 's', REQ_SCROLL_PAGE_DOWN
},
737 { '?', REQ_SEARCH_BACK
},
738 { 'n', REQ_FIND_NEXT
},
739 { 'N', REQ_FIND_PREV
},
743 { 'z', REQ_STOP_LOADING
},
744 { 'v', REQ_SHOW_VERSION
},
745 { 'r', REQ_SCREEN_REDRAW
},
746 { KEY_CTL('L'), REQ_SCREEN_REDRAW
},
747 { 'o', REQ_OPTIONS
},
748 { '.', REQ_TOGGLE_LINENO
},
749 { 'D', REQ_TOGGLE_DATE
},
750 { 'A', REQ_TOGGLE_AUTHOR
},
751 { 'g', REQ_TOGGLE_REV_GRAPH
},
752 { '~', REQ_TOGGLE_GRAPHIC
},
753 { '#', REQ_TOGGLE_FILENAME
},
754 { 'F', REQ_TOGGLE_REFS
},
755 { 'I', REQ_TOGGLE_SORT_ORDER
},
756 { 'i', REQ_TOGGLE_SORT_FIELD
},
761 #define KEYMAP_ENUM(_) \
762 _(KEYMAP, GENERIC), \
775 DEFINE_ENUM(keymap
, KEYMAP_ENUM
);
777 #define set_keymap(map, name) map_enum(map, keymap_map, name)
779 struct keybinding_table
{
780 struct keybinding
*data
;
784 static struct keybinding_table keybindings
[ARRAY_SIZE(keymap_map
)];
787 add_keybinding(enum keymap keymap
, enum request request
, int key
)
789 struct keybinding_table
*table
= &keybindings
[keymap
];
792 for (i
= 0; i
< keybindings
[keymap
].size
; i
++) {
793 if (keybindings
[keymap
].data
[i
].alias
== key
) {
794 keybindings
[keymap
].data
[i
].request
= request
;
799 table
->data
= realloc(table
->data
, (table
->size
+ 1) * sizeof(*table
->data
));
801 die("Failed to allocate keybinding");
802 table
->data
[table
->size
].alias
= key
;
803 table
->data
[table
->size
++].request
= request
;
805 if (request
== REQ_NONE
&& keymap
== KEYMAP_GENERIC
) {
808 for (i
= 0; i
< ARRAY_SIZE(default_keybindings
); i
++)
809 if (default_keybindings
[i
].alias
== key
)
810 default_keybindings
[i
].request
= REQ_NONE
;
814 /* Looks for a key binding first in the given map, then in the generic map, and
815 * lastly in the default keybindings. */
817 get_keybinding(enum keymap keymap
, int key
)
821 for (i
= 0; i
< keybindings
[keymap
].size
; i
++)
822 if (keybindings
[keymap
].data
[i
].alias
== key
)
823 return keybindings
[keymap
].data
[i
].request
;
825 for (i
= 0; i
< keybindings
[KEYMAP_GENERIC
].size
; i
++)
826 if (keybindings
[KEYMAP_GENERIC
].data
[i
].alias
== key
)
827 return keybindings
[KEYMAP_GENERIC
].data
[i
].request
;
829 for (i
= 0; i
< ARRAY_SIZE(default_keybindings
); i
++)
830 if (default_keybindings
[i
].alias
== key
)
831 return default_keybindings
[i
].request
;
833 return (enum request
) key
;
842 static const struct key key_table
[] = {
843 { "Enter", KEY_RETURN
},
845 { "Backspace", KEY_BACKSPACE
},
847 { "Escape", KEY_ESC
},
848 { "Left", KEY_LEFT
},
849 { "Right", KEY_RIGHT
},
851 { "Down", KEY_DOWN
},
852 { "Insert", KEY_IC
},
853 { "Delete", KEY_DC
},
855 { "Home", KEY_HOME
},
857 { "PageUp", KEY_PPAGE
},
858 { "PageDown", KEY_NPAGE
},
868 { "F10", KEY_F(10) },
869 { "F11", KEY_F(11) },
870 { "F12", KEY_F(12) },
874 get_key_value(const char *name
)
878 for (i
= 0; i
< ARRAY_SIZE(key_table
); i
++)
879 if (!strcasecmp(key_table
[i
].name
, name
))
880 return key_table
[i
].value
;
882 if (strlen(name
) == 2 && name
[0] == '^' && isprint(*name
))
883 return (int)name
[1] & 0x1f;
884 if (strlen(name
) == 1 && isprint(*name
))
890 get_key_name(int key_value
)
892 static char key_char
[] = "'X'\0";
893 const char *seq
= NULL
;
896 for (key
= 0; key
< ARRAY_SIZE(key_table
); key
++)
897 if (key_table
[key
].value
== key_value
)
898 seq
= key_table
[key
].name
;
900 if (seq
== NULL
&& key_value
< 0x7f) {
901 char *s
= key_char
+ 1;
903 if (key_value
>= 0x20) {
907 *s
++ = 0x40 | (key_value
& 0x1f);
914 return seq
? seq
: "(no key)";
918 append_key(char *buf
, size_t *pos
, const struct keybinding
*keybinding
)
920 const char *sep
= *pos
> 0 ? ", " : "";
921 const char *keyname
= get_key_name(keybinding
->alias
);
923 return string_nformat(buf
, BUFSIZ
, pos
, "%s%s", sep
, keyname
);
927 append_keymap_request_keys(char *buf
, size_t *pos
, enum request request
,
928 enum keymap keymap
, bool all
)
932 for (i
= 0; i
< keybindings
[keymap
].size
; i
++) {
933 if (keybindings
[keymap
].data
[i
].request
== request
) {
934 if (!append_key(buf
, pos
, &keybindings
[keymap
].data
[i
]))
944 #define get_key(keymap, request) get_keys(keymap, request, FALSE)
947 get_keys(enum keymap keymap
, enum request request
, bool all
)
949 static char buf
[BUFSIZ
];
955 if (!append_keymap_request_keys(buf
, &pos
, request
, keymap
, all
))
956 return "Too many keybindings!";
960 if (keymap
!= KEYMAP_GENERIC
) {
961 /* Only the generic keymap includes the default keybindings when
962 * listing all keys. */
966 if (!append_keymap_request_keys(buf
, &pos
, request
, KEYMAP_GENERIC
, all
))
967 return "Too many keybindings!";
972 for (i
= 0; i
< ARRAY_SIZE(default_keybindings
); i
++) {
973 if (default_keybindings
[i
].request
== request
) {
974 if (!append_key(buf
, &pos
, &default_keybindings
[i
]))
975 return "Too many keybindings!";
990 static struct run_request
*run_request
;
991 static size_t run_requests
;
993 DEFINE_ALLOCATOR(realloc_run_requests
, struct run_request
, 8)
996 add_run_request(enum keymap keymap
, int key
, const char **argv
)
998 struct run_request
*req
;
1000 if (!realloc_run_requests(&run_request
, run_requests
, 1))
1003 req
= &run_request
[run_requests
];
1004 req
->keymap
= keymap
;
1008 if (!argv_copy(&req
->argv
, argv
))
1011 return REQ_NONE
+ ++run_requests
;
1014 static struct run_request
*
1015 get_run_request(enum request request
)
1017 if (request
<= REQ_NONE
)
1019 return &run_request
[request
- REQ_NONE
- 1];
1023 add_builtin_run_requests(void)
1025 const char *cherry_pick
[] = { "git", "cherry-pick", "%(commit)", NULL
};
1026 const char *checkout
[] = { "git", "checkout", "%(branch)", NULL
};
1027 const char *commit
[] = { "git", "commit", NULL
};
1028 const char *gc
[] = { "git", "gc", NULL
};
1029 struct run_request reqs
[] = {
1030 { KEYMAP_MAIN
, 'C', cherry_pick
},
1031 { KEYMAP_STATUS
, 'C', commit
},
1032 { KEYMAP_BRANCH
, 'C', checkout
},
1033 { KEYMAP_GENERIC
, 'G', gc
},
1037 for (i
= 0; i
< ARRAY_SIZE(reqs
); i
++) {
1038 enum request req
= get_keybinding(reqs
[i
].keymap
, reqs
[i
].key
);
1040 if (req
!= reqs
[i
].key
)
1042 req
= add_run_request(reqs
[i
].keymap
, reqs
[i
].key
, reqs
[i
].argv
);
1043 if (req
!= REQ_NONE
)
1044 add_keybinding(reqs
[i
].keymap
, req
, reqs
[i
].key
);
1049 * User config file handling.
1052 #define OPT_ERR_INFO \
1053 OPT_ERR_(INTEGER_VALUE_OUT_OF_BOUND, "Integer value out of bound"), \
1054 OPT_ERR_(INVALID_STEP_VALUE, "Invalid step value"), \
1055 OPT_ERR_(NO_OPTION_VALUE, "No option value"), \
1056 OPT_ERR_(NO_VALUE_ASSIGNED, "No value assigned"), \
1057 OPT_ERR_(OBSOLETE_REQUEST_NAME, "Obsolete request name"), \
1058 OPT_ERR_(OUT_OF_MEMORY, "Out of memory"), \
1059 OPT_ERR_(TOO_MANY_OPTION_ARGUMENTS, "Too many option arguments"), \
1060 OPT_ERR_(UNKNOWN_ATTRIBUTE, "Unknown attribute"), \
1061 OPT_ERR_(UNKNOWN_COLOR, "Unknown color"), \
1062 OPT_ERR_(UNKNOWN_COLOR_NAME, "Unknown color name"), \
1063 OPT_ERR_(UNKNOWN_KEY, "Unknown key"), \
1064 OPT_ERR_(UNKNOWN_KEY_MAP, "Unknown key map"), \
1065 OPT_ERR_(UNKNOWN_OPTION_COMMAND, "Unknown option command"), \
1066 OPT_ERR_(UNKNOWN_REQUEST_NAME, "Unknown request name"), \
1067 OPT_ERR_(UNKNOWN_VARIABLE_NAME, "Unknown variable name"), \
1068 OPT_ERR_(UNMATCHED_QUOTATION, "Unmatched quotation"), \
1069 OPT_ERR_(WRONG_NUMBER_OF_ARGUMENTS, "Wrong number of arguments"),
1072 #define OPT_ERR_(name, msg) OPT_ERR_ ## name
1078 static const char *option_errors
[] = {
1079 #define OPT_ERR_(name, msg) msg
1084 static const struct enum_map color_map
[] = {
1085 #define COLOR_MAP(name) ENUM_MAP(#name, COLOR_##name)
1097 static const struct enum_map attr_map
[] = {
1098 #define ATTR_MAP(name) ENUM_MAP(#name, A_##name)
1105 ATTR_MAP(UNDERLINE
),
1108 #define set_attribute(attr, name) map_enum(attr, attr_map, name)
1110 static enum option_code
1111 parse_step(double *opt
, const char *arg
)
1114 if (!strchr(arg
, '%'))
1117 /* "Shift down" so 100% and 1 does not conflict. */
1118 *opt
= (*opt
- 1) / 100;
1121 return OPT_ERR_INVALID_STEP_VALUE
;
1125 return OPT_ERR_INVALID_STEP_VALUE
;
1130 static enum option_code
1131 parse_int(int *opt
, const char *arg
, int min
, int max
)
1133 int value
= atoi(arg
);
1135 if (min
<= value
&& value
<= max
) {
1140 return OPT_ERR_INTEGER_VALUE_OUT_OF_BOUND
;
1144 set_color(int *color
, const char *name
)
1146 if (map_enum(color
, color_map
, name
))
1148 if (!prefixcmp(name
, "color"))
1149 return parse_int(color
, name
+ 5, 0, 255) == OPT_OK
;
1153 /* Wants: object fgcolor bgcolor [attribute] */
1154 static enum option_code
1155 option_color_command(int argc
, const char *argv
[])
1157 struct line_info
*info
;
1160 return OPT_ERR_WRONG_NUMBER_OF_ARGUMENTS
;
1162 if (*argv
[0] == '"' || *argv
[0] == '\'') {
1163 info
= add_custom_color(argv
[0]);
1165 info
= get_line_info(argv
[0]);
1168 static const struct enum_map obsolete
[] = {
1169 ENUM_MAP("main-delim", LINE_DELIMITER
),
1170 ENUM_MAP("main-date", LINE_DATE
),
1171 ENUM_MAP("main-author", LINE_AUTHOR
),
1175 if (!map_enum(&index
, obsolete
, argv
[0]))
1176 return OPT_ERR_UNKNOWN_COLOR_NAME
;
1177 info
= &line_info
[index
];
1180 if (!set_color(&info
->fg
, argv
[1]) ||
1181 !set_color(&info
->bg
, argv
[2]))
1182 return OPT_ERR_UNKNOWN_COLOR
;
1185 while (argc
-- > 3) {
1188 if (!set_attribute(&attr
, argv
[argc
]))
1189 return OPT_ERR_UNKNOWN_ATTRIBUTE
;
1196 static enum option_code
1197 parse_bool(bool *opt
, const char *arg
)
1199 *opt
= (!strcmp(arg
, "1") || !strcmp(arg
, "true") || !strcmp(arg
, "yes"))
1204 static enum option_code
1205 parse_enum_do(unsigned int *opt
, const char *arg
,
1206 const struct enum_map
*map
, size_t map_size
)
1210 assert(map_size
> 1);
1212 if (map_enum_do(map
, map_size
, (int *) opt
, arg
))
1215 parse_bool(&is_true
, arg
);
1216 *opt
= is_true
? map
[1].value
: map
[0].value
;
1220 #define parse_enum(opt, arg, map) \
1221 parse_enum_do(opt, arg, map, ARRAY_SIZE(map))
1223 static enum option_code
1224 parse_string(char *opt
, const char *arg
, size_t optsize
)
1226 int arglen
= strlen(arg
);
1231 if (arglen
== 1 || arg
[arglen
- 1] != arg
[0])
1232 return OPT_ERR_UNMATCHED_QUOTATION
;
1233 arg
+= 1; arglen
-= 2;
1235 string_ncopy_do(opt
, optsize
, arg
, arglen
);
1240 static enum option_code
1241 parse_args(const char ***args
, const char *argv
[])
1243 if (*args
== NULL
&& !argv_copy(args
, argv
))
1244 return OPT_ERR_OUT_OF_MEMORY
;
1248 /* Wants: name = value */
1249 static enum option_code
1250 option_set_command(int argc
, const char *argv
[])
1253 return OPT_ERR_WRONG_NUMBER_OF_ARGUMENTS
;
1255 if (strcmp(argv
[1], "="))
1256 return OPT_ERR_NO_VALUE_ASSIGNED
;
1258 if (!strcmp(argv
[0], "blame-options"))
1259 return parse_args(&opt_blame_argv
, argv
+ 2);
1262 return OPT_ERR_WRONG_NUMBER_OF_ARGUMENTS
;
1264 if (!strcmp(argv
[0], "show-author"))
1265 return parse_enum(&opt_author
, argv
[2], author_map
);
1267 if (!strcmp(argv
[0], "show-date"))
1268 return parse_enum(&opt_date
, argv
[2], date_map
);
1270 if (!strcmp(argv
[0], "show-rev-graph"))
1271 return parse_bool(&opt_rev_graph
, argv
[2]);
1273 if (!strcmp(argv
[0], "show-refs"))
1274 return parse_bool(&opt_show_refs
, argv
[2]);
1276 if (!strcmp(argv
[0], "show-notes")) {
1279 strcpy(opt_notes_arg
, "--notes=");
1280 res
= parse_string(opt_notes_arg
+ 8, argv
[2],
1281 sizeof(opt_notes_arg
) - 8);
1282 if (res
== OPT_OK
&& opt_notes_arg
[8] == '\0')
1283 opt_notes_arg
[7] = '\0';
1287 if (!strcmp(argv
[0], "show-line-numbers"))
1288 return parse_bool(&opt_line_number
, argv
[2]);
1290 if (!strcmp(argv
[0], "line-graphics"))
1291 return parse_enum(&opt_line_graphics
, argv
[2], graphic_map
);
1293 if (!strcmp(argv
[0], "line-number-interval"))
1294 return parse_int(&opt_num_interval
, argv
[2], 1, 1024);
1296 if (!strcmp(argv
[0], "author-width"))
1297 return parse_int(&opt_author_cols
, argv
[2], 0, 1024);
1299 if (!strcmp(argv
[0], "filename-width"))
1300 return parse_int(&opt_filename_cols
, argv
[2], 0, 1024);
1302 if (!strcmp(argv
[0], "show-filename"))
1303 return parse_enum(&opt_filename
, argv
[2], filename_map
);
1305 if (!strcmp(argv
[0], "horizontal-scroll"))
1306 return parse_step(&opt_hscroll
, argv
[2]);
1308 if (!strcmp(argv
[0], "split-view-height"))
1309 return parse_step(&opt_scale_split_view
, argv
[2]);
1311 if (!strcmp(argv
[0], "tab-size"))
1312 return parse_int(&opt_tab_size
, argv
[2], 1, 1024);
1314 if (!strcmp(argv
[0], "diff-context")) {
1315 enum option_code code
= parse_int(&opt_diff_context
, argv
[2], 1, 999999);
1318 update_diff_context_arg(opt_diff_context
);
1322 if (!strcmp(argv
[0], "commit-encoding"))
1323 return parse_string(opt_encoding
, argv
[2], sizeof(opt_encoding
));
1325 if (!strcmp(argv
[0], "status-untracked-dirs"))
1326 return parse_bool(&opt_untracked_dirs_content
, argv
[2]);
1328 return OPT_ERR_UNKNOWN_VARIABLE_NAME
;
1331 /* Wants: mode request key */
1332 static enum option_code
1333 option_bind_command(int argc
, const char *argv
[])
1335 enum request request
;
1340 return OPT_ERR_WRONG_NUMBER_OF_ARGUMENTS
;
1342 if (!set_keymap(&keymap
, argv
[0]))
1343 return OPT_ERR_UNKNOWN_KEY_MAP
;
1345 key
= get_key_value(argv
[1]);
1347 return OPT_ERR_UNKNOWN_KEY
;
1349 request
= get_request(argv
[2]);
1350 if (request
== REQ_UNKNOWN
) {
1351 static const struct enum_map obsolete
[] = {
1352 ENUM_MAP("cherry-pick", REQ_NONE
),
1353 ENUM_MAP("screen-resize", REQ_NONE
),
1354 ENUM_MAP("tree-parent", REQ_PARENT
),
1358 if (map_enum(&alias
, obsolete
, argv
[2])) {
1359 if (alias
!= REQ_NONE
)
1360 add_keybinding(keymap
, alias
, key
);
1361 return OPT_ERR_OBSOLETE_REQUEST_NAME
;
1364 if (request
== REQ_UNKNOWN
&& *argv
[2]++ == '!')
1365 request
= add_run_request(keymap
, key
, argv
+ 2);
1366 if (request
== REQ_UNKNOWN
)
1367 return OPT_ERR_UNKNOWN_REQUEST_NAME
;
1369 add_keybinding(keymap
, request
, key
);
1374 static enum option_code
1375 set_option(const char *opt
, char *value
)
1377 const char *argv
[SIZEOF_ARG
];
1380 if (!argv_from_string(argv
, &argc
, value
))
1381 return OPT_ERR_TOO_MANY_OPTION_ARGUMENTS
;
1383 if (!strcmp(opt
, "color"))
1384 return option_color_command(argc
, argv
);
1386 if (!strcmp(opt
, "set"))
1387 return option_set_command(argc
, argv
);
1389 if (!strcmp(opt
, "bind"))
1390 return option_bind_command(argc
, argv
);
1392 return OPT_ERR_UNKNOWN_OPTION_COMMAND
;
1395 struct config_state
{
1401 read_option(char *opt
, size_t optlen
, char *value
, size_t valuelen
, void *data
)
1403 struct config_state
*config
= data
;
1404 enum option_code status
= OPT_ERR_NO_OPTION_VALUE
;
1408 /* Check for comment markers, since read_properties() will
1409 * only ensure opt and value are split at first " \t". */
1410 optlen
= strcspn(opt
, "#");
1414 if (opt
[optlen
] == 0) {
1415 /* Look for comment endings in the value. */
1416 size_t len
= strcspn(value
, "#");
1418 if (len
< valuelen
) {
1420 value
[valuelen
] = 0;
1423 status
= set_option(opt
, value
);
1426 if (status
!= OPT_OK
) {
1427 warn("Error on line %d, near '%.*s': %s",
1428 config
->lineno
, (int) optlen
, opt
, option_errors
[status
]);
1429 config
->errors
= TRUE
;
1432 /* Always keep going if errors are encountered. */
1437 load_option_file(const char *path
)
1439 struct config_state config
= { 0, FALSE
};
1442 /* It's OK that the file doesn't exist. */
1443 if (!io_open(&io
, "%s", path
))
1446 if (io_load(&io
, " \t", read_option
, &config
) == ERR
||
1447 config
.errors
== TRUE
)
1448 warn("Errors while loading %s.", path
);
1454 const char *home
= getenv("HOME");
1455 const char *tigrc_user
= getenv("TIGRC_USER");
1456 const char *tigrc_system
= getenv("TIGRC_SYSTEM");
1457 const char *tig_diff_opts
= getenv("TIG_DIFF_OPTS");
1458 char buf
[SIZEOF_STR
];
1461 tigrc_system
= SYSCONFDIR
"/tigrc";
1462 load_option_file(tigrc_system
);
1465 if (!home
|| !string_format(buf
, "%s/.tigrc", home
))
1469 load_option_file(tigrc_user
);
1471 /* Add _after_ loading config files to avoid adding run requests
1472 * that conflict with keybindings. */
1473 add_builtin_run_requests();
1475 if (!opt_diff_argv
&& tig_diff_opts
&& *tig_diff_opts
) {
1476 static const char *diff_opts
[SIZEOF_ARG
] = { NULL
};
1479 if (!string_format(buf
, "%s", tig_diff_opts
) ||
1480 !argv_from_string(diff_opts
, &argc
, buf
))
1481 die("TIG_DIFF_OPTS contains too many arguments");
1482 else if (!argv_copy(&opt_diff_argv
, diff_opts
))
1483 die("Failed to format TIG_DIFF_OPTS arguments");
1497 /* The display array of active views and the index of the current view. */
1498 static struct view
*display
[2];
1499 static WINDOW
*display_win
[2];
1500 static WINDOW
*display_title
[2];
1501 static unsigned int current_view
;
1503 #define foreach_displayed_view(view, i) \
1504 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
1506 #define displayed_views() (display[1] != NULL ? 2 : 1)
1508 /* Current head and commit ID */
1509 static char ref_blob
[SIZEOF_REF
] = "";
1510 static char ref_commit
[SIZEOF_REF
] = "HEAD";
1511 static char ref_head
[SIZEOF_REF
] = "HEAD";
1512 static char ref_branch
[SIZEOF_REF
] = "";
1529 enum view_type type
; /* View type */
1530 const char *name
; /* View name */
1531 const char *id
; /* Points to either of ref_{head,commit,blob} */
1533 struct view_ops
*ops
; /* View operations */
1535 enum keymap keymap
; /* What keymap does this view have */
1536 bool git_dir
; /* Whether the view requires a git directory. */
1538 char ref
[SIZEOF_REF
]; /* Hovered commit reference */
1539 char vid
[SIZEOF_REF
]; /* View ID. Set to id member when updating. */
1541 int height
, width
; /* The width and height of the main window */
1542 WINDOW
*win
; /* The main window */
1545 unsigned long offset
; /* Offset of the window top */
1546 unsigned long yoffset
; /* Offset from the window side. */
1547 unsigned long lineno
; /* Current line number */
1548 unsigned long p_offset
; /* Previous offset of the window top */
1549 unsigned long p_yoffset
;/* Previous offset from the window side */
1550 unsigned long p_lineno
; /* Previous current line number */
1551 bool p_restore
; /* Should the previous position be restored. */
1554 char grep
[SIZEOF_STR
]; /* Search string */
1555 regex_t
*regex
; /* Pre-compiled regexp */
1557 /* If non-NULL, points to the view that opened this view. If this view
1558 * is closed tig will switch back to the parent view. */
1559 struct view
*parent
;
1563 size_t lines
; /* Total number of lines */
1564 struct line
*line
; /* Line index */
1565 unsigned int digits
; /* Number of digits in the lines member. */
1568 struct line
*curline
; /* Line currently being drawn. */
1569 enum line_type curtype
; /* Attribute currently used for drawing. */
1570 unsigned long col
; /* Column when drawing. */
1571 bool has_scrolled
; /* View was scrolled. */
1574 const char **argv
; /* Shell command arguments. */
1575 const char *dir
; /* Directory from which to execute. */
1586 OPEN_DEFAULT
= 0, /* Use default view switching. */
1587 OPEN_SPLIT
= 1, /* Split current view. */
1588 OPEN_RELOAD
= 4, /* Reload view even if it is the current. */
1589 OPEN_REFRESH
= 16, /* Refresh view using previous command. */
1590 OPEN_PREPARED
= 32, /* Open already prepared command. */
1591 OPEN_EXTRA
= 64, /* Open extra data from command. */
1595 /* What type of content being displayed. Used in the title bar. */
1597 /* Size of private data. */
1598 size_t private_size
;
1599 /* Open and reads in all view content. */
1600 bool (*open
)(struct view
*view
, enum open_flags flags
);
1601 /* Read one line; updates view->line. */
1602 bool (*read
)(struct view
*view
, char *data
);
1603 /* Draw one line; @lineno must be < view->height. */
1604 bool (*draw
)(struct view
*view
, struct line
*line
, unsigned int lineno
);
1605 /* Depending on view handle a special requests. */
1606 enum request (*request
)(struct view
*view
, enum request request
, struct line
*line
);
1607 /* Search for regexp in a line. */
1608 bool (*grep
)(struct view
*view
, struct line
*line
);
1610 void (*select
)(struct view
*view
, struct line
*line
);
1613 static struct view_ops blame_ops
;
1614 static struct view_ops blob_ops
;
1615 static struct view_ops diff_ops
;
1616 static struct view_ops help_ops
;
1617 static struct view_ops log_ops
;
1618 static struct view_ops main_ops
;
1619 static struct view_ops pager_ops
;
1620 static struct view_ops stage_ops
;
1621 static struct view_ops status_ops
;
1622 static struct view_ops tree_ops
;
1623 static struct view_ops branch_ops
;
1625 #define VIEW_STR(type, name, ref, ops, map, git) \
1626 { type, name, ref, ops, map, git }
1628 #define VIEW_(id, name, ops, git, ref) \
1629 VIEW_STR(VIEW_##id, name, ref, ops, KEYMAP_##id, git)
1631 static struct view views
[] = {
1632 VIEW_(MAIN
, "main", &main_ops
, TRUE
, ref_head
),
1633 VIEW_(DIFF
, "diff", &diff_ops
, TRUE
, ref_commit
),
1634 VIEW_(LOG
, "log", &log_ops
, TRUE
, ref_head
),
1635 VIEW_(TREE
, "tree", &tree_ops
, TRUE
, ref_commit
),
1636 VIEW_(BLOB
, "blob", &blob_ops
, TRUE
, ref_blob
),
1637 VIEW_(BLAME
, "blame", &blame_ops
, TRUE
, ref_commit
),
1638 VIEW_(BRANCH
, "branch", &branch_ops
, TRUE
, ref_head
),
1639 VIEW_(HELP
, "help", &help_ops
, FALSE
, ""),
1640 VIEW_(PAGER
, "pager", &pager_ops
, FALSE
, ""),
1641 VIEW_(STATUS
, "status", &status_ops
, TRUE
, "status"),
1642 VIEW_(STAGE
, "stage", &stage_ops
, TRUE
, "stage"),
1645 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
1647 #define foreach_view(view, i) \
1648 for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++)
1650 #define view_is_displayed(view) \
1651 (view == display[0] || view == display[1])
1654 view_request(struct view
*view
, enum request request
)
1656 if (!view
|| !view
->lines
)
1658 return view
->ops
->request(view
, request
, &view
->line
[view
->lineno
]);
1667 set_view_attr(struct view
*view
, enum line_type type
)
1669 if (!view
->curline
->selected
&& view
->curtype
!= type
) {
1670 (void) wattrset(view
->win
, get_line_attr(type
));
1671 wchgat(view
->win
, -1, 0, COLOR_ID(type
), NULL
);
1672 view
->curtype
= type
;
1676 #define VIEW_MAX_LEN(view) ((view)->width + (view)->yoffset - (view)->col)
1679 draw_chars(struct view
*view
, enum line_type type
, const char *string
,
1680 int max_len
, bool use_tilde
)
1682 static char out_buffer
[BUFSIZ
* 2];
1685 int trimmed
= FALSE
;
1686 size_t skip
= view
->yoffset
> view
->col
? view
->yoffset
- view
->col
: 0;
1689 return VIEW_MAX_LEN(view
) <= 0;
1691 len
= utf8_length(&string
, skip
, &col
, max_len
, &trimmed
, use_tilde
, opt_tab_size
);
1693 set_view_attr(view
, type
);
1695 if (opt_iconv_out
!= ICONV_NONE
) {
1696 size_t inlen
= len
+ 1;
1697 char *instr
= calloc(1, inlen
);
1698 ICONV_CONST
char *inbuf
= (ICONV_CONST
char *) instr
;
1700 return VIEW_MAX_LEN(view
) <= 0;
1702 strncpy(instr
, string
, len
);
1704 char *outbuf
= out_buffer
;
1705 size_t outlen
= sizeof(out_buffer
);
1709 ret
= iconv(opt_iconv_out
, &inbuf
, &inlen
, &outbuf
, &outlen
);
1710 if (ret
!= (size_t) -1) {
1711 string
= out_buffer
;
1712 len
= sizeof(out_buffer
) - outlen
;
1717 waddnstr(view
->win
, string
, len
);
1719 if (trimmed
&& use_tilde
) {
1720 set_view_attr(view
, LINE_DELIMITER
);
1721 waddch(view
->win
, '~');
1727 return VIEW_MAX_LEN(view
) <= 0;
1731 draw_space(struct view
*view
, enum line_type type
, int max
, int spaces
)
1733 static char space
[] = " ";
1735 spaces
= MIN(max
, spaces
);
1737 while (spaces
> 0) {
1738 int len
= MIN(spaces
, sizeof(space
) - 1);
1740 if (draw_chars(view
, type
, space
, len
, FALSE
))
1745 return VIEW_MAX_LEN(view
) <= 0;
1749 draw_text(struct view
*view
, enum line_type type
, const char *string
)
1751 char text
[SIZEOF_STR
];
1754 size_t pos
= string_expand(text
, sizeof(text
), string
, opt_tab_size
);
1756 if (draw_chars(view
, type
, text
, VIEW_MAX_LEN(view
), TRUE
))
1761 return VIEW_MAX_LEN(view
) <= 0;
1765 draw_graphic(struct view
*view
, enum line_type type
, const chtype graphic
[], size_t size
, bool separator
)
1767 size_t skip
= view
->yoffset
> view
->col
? view
->yoffset
- view
->col
: 0;
1768 int max
= VIEW_MAX_LEN(view
);
1774 set_view_attr(view
, type
);
1775 /* Using waddch() instead of waddnstr() ensures that
1776 * they'll be rendered correctly for the cursor line. */
1777 for (i
= skip
; i
< size
; i
++)
1778 waddch(view
->win
, graphic
[i
]);
1782 if (size
< max
&& skip
<= size
)
1783 waddch(view
->win
, ' ');
1787 return VIEW_MAX_LEN(view
) <= 0;
1791 draw_field(struct view
*view
, enum line_type type
, const char *text
, int len
, bool trim
)
1793 int max
= MIN(VIEW_MAX_LEN(view
), len
);
1794 int col
= view
->col
;
1797 return draw_space(view
, type
, max
, max
);
1799 return draw_chars(view
, type
, text
, max
- 1, trim
)
1800 || draw_space(view
, LINE_DEFAULT
, max
- (view
->col
- col
), max
);
1804 draw_date(struct view
*view
, struct time
*time
)
1806 const char *date
= mkdate(time
, opt_date
);
1807 int cols
= opt_date
== DATE_SHORT
? DATE_SHORT_COLS
: DATE_COLS
;
1809 if (opt_date
== DATE_NO
)
1812 return draw_field(view
, LINE_DATE
, date
, cols
, FALSE
);
1816 draw_author(struct view
*view
, const char *author
)
1818 bool trim
= author_trim(opt_author_cols
);
1819 const char *text
= mkauthor(author
, opt_author_cols
, opt_author
);
1821 if (opt_author
== AUTHOR_NO
)
1824 return draw_field(view
, LINE_AUTHOR
, text
, opt_author_cols
, trim
);
1828 draw_filename(struct view
*view
, const char *filename
, bool auto_enabled
)
1830 bool trim
= filename
&& strlen(filename
) >= opt_filename_cols
;
1832 if (opt_filename
== FILENAME_NO
)
1835 if (opt_filename
== FILENAME_AUTO
&& !auto_enabled
)
1838 return draw_field(view
, LINE_FILENAME
, filename
, opt_filename_cols
, trim
);
1842 draw_mode(struct view
*view
, mode_t mode
)
1844 const char *str
= mkmode(mode
);
1846 return draw_field(view
, LINE_MODE
, str
, STRING_SIZE("-rw-r--r-- "), FALSE
);
1850 draw_lineno(struct view
*view
, unsigned int lineno
)
1853 int digits3
= view
->digits
< 3 ? 3 : view
->digits
;
1854 int max
= MIN(VIEW_MAX_LEN(view
), digits3
);
1856 chtype separator
= opt_line_graphics
? ACS_VLINE
: '|';
1858 lineno
+= view
->offset
+ 1;
1859 if (lineno
== 1 || (lineno
% opt_num_interval
) == 0) {
1860 static char fmt
[] = "%1ld";
1862 fmt
[1] = '0' + (view
->digits
<= 9 ? digits3
: 1);
1863 if (string_format(number
, fmt
, lineno
))
1867 draw_chars(view
, LINE_LINE_NUMBER
, text
, max
, TRUE
);
1869 draw_space(view
, LINE_LINE_NUMBER
, max
, digits3
);
1870 return draw_graphic(view
, LINE_DEFAULT
, &separator
, 1, TRUE
);
1874 draw_refs(struct view
*view
, struct ref_list
*refs
)
1878 if (!opt_show_refs
|| !refs
)
1881 for (i
= 0; i
< refs
->size
; i
++) {
1882 struct ref
*ref
= refs
->refs
[i
];
1883 enum line_type type
= get_line_type_from_ref(ref
);
1885 if (draw_text(view
, type
, "[") ||
1886 draw_text(view
, type
, ref
->name
) ||
1887 draw_text(view
, type
, "]"))
1890 if (draw_text(view
, LINE_DEFAULT
, " "))
1898 draw_view_line(struct view
*view
, unsigned int lineno
)
1901 bool selected
= (view
->offset
+ lineno
== view
->lineno
);
1903 assert(view_is_displayed(view
));
1905 if (view
->offset
+ lineno
>= view
->lines
)
1908 line
= &view
->line
[view
->offset
+ lineno
];
1910 wmove(view
->win
, lineno
, 0);
1912 wclrtoeol(view
->win
);
1914 view
->curline
= line
;
1915 view
->curtype
= LINE_NONE
;
1916 line
->selected
= FALSE
;
1917 line
->dirty
= line
->cleareol
= 0;
1920 set_view_attr(view
, LINE_CURSOR
);
1921 line
->selected
= TRUE
;
1922 view
->ops
->select(view
, line
);
1925 return view
->ops
->draw(view
, line
, lineno
);
1929 redraw_view_dirty(struct view
*view
)
1934 for (lineno
= 0; lineno
< view
->height
; lineno
++) {
1935 if (view
->offset
+ lineno
>= view
->lines
)
1937 if (!view
->line
[view
->offset
+ lineno
].dirty
)
1940 if (!draw_view_line(view
, lineno
))
1946 wnoutrefresh(view
->win
);
1950 redraw_view_from(struct view
*view
, int lineno
)
1952 assert(0 <= lineno
&& lineno
< view
->height
);
1954 for (; lineno
< view
->height
; lineno
++) {
1955 if (!draw_view_line(view
, lineno
))
1959 wnoutrefresh(view
->win
);
1963 redraw_view(struct view
*view
)
1966 redraw_view_from(view
, 0);
1971 update_view_title(struct view
*view
)
1973 char buf
[SIZEOF_STR
];
1974 char state
[SIZEOF_STR
];
1975 size_t bufpos
= 0, statelen
= 0;
1976 WINDOW
*window
= display
[0] == view
? display_title
[0] : display_title
[1];
1978 assert(view_is_displayed(view
));
1980 if (view
->type
!= VIEW_STATUS
&& view
->lines
) {
1981 unsigned int view_lines
= view
->offset
+ view
->height
;
1982 unsigned int lines
= view
->lines
1983 ? MIN(view_lines
, view
->lines
) * 100 / view
->lines
1986 string_format_from(state
, &statelen
, " - %s %d of %d (%d%%)",
1995 time_t secs
= time(NULL
) - view
->start_time
;
1997 /* Three git seconds are a long time ... */
1999 string_format_from(state
, &statelen
, " loading %lds", secs
);
2002 string_format_from(buf
, &bufpos
, "[%s]", view
->name
);
2003 if (*view
->ref
&& bufpos
< view
->width
) {
2004 size_t refsize
= strlen(view
->ref
);
2005 size_t minsize
= bufpos
+ 1 + /* abbrev= */ 7 + 1 + statelen
;
2007 if (minsize
< view
->width
)
2008 refsize
= view
->width
- minsize
+ 7;
2009 string_format_from(buf
, &bufpos
, " %.*s", (int) refsize
, view
->ref
);
2012 if (statelen
&& bufpos
< view
->width
) {
2013 string_format_from(buf
, &bufpos
, "%s", state
);
2016 if (view
== display
[current_view
])
2017 wbkgdset(window
, get_line_attr(LINE_TITLE_FOCUS
));
2019 wbkgdset(window
, get_line_attr(LINE_TITLE_BLUR
));
2021 mvwaddnstr(window
, 0, 0, buf
, bufpos
);
2023 wnoutrefresh(window
);
2027 apply_step(double step
, int value
)
2031 value
*= step
+ 0.01;
2032 return value
? value
: 1;
2036 resize_display(void)
2039 struct view
*base
= display
[0];
2040 struct view
*view
= display
[1] ? display
[1] : display
[0];
2042 /* Setup window dimensions */
2044 getmaxyx(stdscr
, base
->height
, base
->width
);
2046 /* Make room for the status window. */
2050 /* Horizontal split. */
2051 view
->width
= base
->width
;
2052 view
->height
= apply_step(opt_scale_split_view
, base
->height
);
2053 view
->height
= MAX(view
->height
, MIN_VIEW_HEIGHT
);
2054 view
->height
= MIN(view
->height
, base
->height
- MIN_VIEW_HEIGHT
);
2055 base
->height
-= view
->height
;
2057 /* Make room for the title bar. */
2061 /* Make room for the title bar. */
2066 foreach_displayed_view (view
, i
) {
2067 if (!display_win
[i
]) {
2068 display_win
[i
] = newwin(view
->height
, view
->width
, offset
, 0);
2069 if (!display_win
[i
])
2070 die("Failed to create %s view", view
->name
);
2072 scrollok(display_win
[i
], FALSE
);
2074 display_title
[i
] = newwin(1, view
->width
, offset
+ view
->height
, 0);
2075 if (!display_title
[i
])
2076 die("Failed to create title window");
2079 wresize(display_win
[i
], view
->height
, view
->width
);
2080 mvwin(display_win
[i
], offset
, 0);
2081 mvwin(display_title
[i
], offset
+ view
->height
, 0);
2084 view
->win
= display_win
[i
];
2086 offset
+= view
->height
+ 1;
2091 redraw_display(bool clear
)
2096 foreach_displayed_view (view
, i
) {
2100 update_view_title(view
);
2109 #define TOGGLE_MENU \
2110 TOGGLE_(LINENO, '.', "line numbers", &opt_line_number, NULL) \
2111 TOGGLE_(DATE, 'D', "dates", &opt_date, date_map) \
2112 TOGGLE_(AUTHOR, 'A', "author names", &opt_author, author_map) \
2113 TOGGLE_(GRAPHIC, '~', "graphics", &opt_line_graphics, graphic_map) \
2114 TOGGLE_(REV_GRAPH, 'g', "revision graph", &opt_rev_graph, NULL) \
2115 TOGGLE_(FILENAME, '#', "file names", &opt_filename, filename_map) \
2116 TOGGLE_(REFS, 'F', "reference display", &opt_show_refs, NULL)
2119 toggle_option(enum request request
)
2122 enum request request
;
2123 const struct enum_map
*map
;
2126 #define TOGGLE_(id, key, help, value, map) { REQ_TOGGLE_ ## id, map, ARRAY_SIZE(map) },
2130 const struct menu_item menu
[] = {
2131 #define TOGGLE_(id, key, help, value, map) { key, help, value },
2138 if (request
== REQ_OPTIONS
) {
2139 if (!prompt_menu("Toggle option", menu
, &i
))
2142 while (i
< ARRAY_SIZE(data
) && data
[i
].request
!= request
)
2144 if (i
>= ARRAY_SIZE(data
))
2145 die("Invalid request (%d)", request
);
2148 if (data
[i
].map
!= NULL
) {
2149 unsigned int *opt
= menu
[i
].data
;
2151 *opt
= (*opt
+ 1) % data
[i
].map_size
;
2152 redraw_display(FALSE
);
2153 report("Displaying %s %s", enum_name(data
[i
].map
[*opt
]), menu
[i
].text
);
2156 bool *option
= menu
[i
].data
;
2159 redraw_display(FALSE
);
2160 report("%sabling %s", *option
? "En" : "Dis", menu
[i
].text
);
2165 maximize_view(struct view
*view
, bool redraw
)
2167 memset(display
, 0, sizeof(display
));
2169 display
[current_view
] = view
;
2172 redraw_display(FALSE
);
2183 goto_view_line(struct view
*view
, unsigned long offset
, unsigned long lineno
)
2185 if (lineno
>= view
->lines
)
2186 lineno
= view
->lines
> 0 ? view
->lines
- 1 : 0;
2188 if (offset
> lineno
|| offset
+ view
->height
<= lineno
) {
2189 unsigned long half
= view
->height
/ 2;
2192 offset
= lineno
- half
;
2197 if (offset
!= view
->offset
|| lineno
!= view
->lineno
) {
2198 view
->offset
= offset
;
2199 view
->lineno
= lineno
;
2206 /* Scrolling backend */
2208 do_scroll_view(struct view
*view
, int lines
)
2210 bool redraw_current_line
= FALSE
;
2212 /* The rendering expects the new offset. */
2213 view
->offset
+= lines
;
2215 assert(0 <= view
->offset
&& view
->offset
< view
->lines
);
2218 /* Move current line into the view. */
2219 if (view
->lineno
< view
->offset
) {
2220 view
->lineno
= view
->offset
;
2221 redraw_current_line
= TRUE
;
2222 } else if (view
->lineno
>= view
->offset
+ view
->height
) {
2223 view
->lineno
= view
->offset
+ view
->height
- 1;
2224 redraw_current_line
= TRUE
;
2227 assert(view
->offset
<= view
->lineno
&& view
->lineno
< view
->lines
);
2229 /* Redraw the whole screen if scrolling is pointless. */
2230 if (view
->height
< ABS(lines
)) {
2234 int line
= lines
> 0 ? view
->height
- lines
: 0;
2235 int end
= line
+ ABS(lines
);
2237 scrollok(view
->win
, TRUE
);
2238 wscrl(view
->win
, lines
);
2239 scrollok(view
->win
, FALSE
);
2241 while (line
< end
&& draw_view_line(view
, line
))
2244 if (redraw_current_line
)
2245 draw_view_line(view
, view
->lineno
- view
->offset
);
2246 wnoutrefresh(view
->win
);
2249 view
->has_scrolled
= TRUE
;
2253 /* Scroll frontend */
2255 scroll_view(struct view
*view
, enum request request
)
2259 assert(view_is_displayed(view
));
2262 case REQ_SCROLL_FIRST_COL
:
2264 redraw_view_from(view
, 0);
2267 case REQ_SCROLL_LEFT
:
2268 if (view
->yoffset
== 0) {
2269 report("Cannot scroll beyond the first column");
2272 if (view
->yoffset
<= apply_step(opt_hscroll
, view
->width
))
2275 view
->yoffset
-= apply_step(opt_hscroll
, view
->width
);
2276 redraw_view_from(view
, 0);
2279 case REQ_SCROLL_RIGHT
:
2280 view
->yoffset
+= apply_step(opt_hscroll
, view
->width
);
2284 case REQ_SCROLL_PAGE_DOWN
:
2285 lines
= view
->height
;
2286 case REQ_SCROLL_LINE_DOWN
:
2287 if (view
->offset
+ lines
> view
->lines
)
2288 lines
= view
->lines
- view
->offset
;
2290 if (lines
== 0 || view
->offset
+ view
->height
>= view
->lines
) {
2291 report("Cannot scroll beyond the last line");
2296 case REQ_SCROLL_PAGE_UP
:
2297 lines
= view
->height
;
2298 case REQ_SCROLL_LINE_UP
:
2299 if (lines
> view
->offset
)
2300 lines
= view
->offset
;
2303 report("Cannot scroll beyond the first line");
2311 die("request %d not handled in switch", request
);
2314 do_scroll_view(view
, lines
);
2319 move_view(struct view
*view
, enum request request
)
2321 int scroll_steps
= 0;
2325 case REQ_MOVE_FIRST_LINE
:
2326 steps
= -view
->lineno
;
2329 case REQ_MOVE_LAST_LINE
:
2330 steps
= view
->lines
- view
->lineno
- 1;
2333 case REQ_MOVE_PAGE_UP
:
2334 steps
= view
->height
> view
->lineno
2335 ? -view
->lineno
: -view
->height
;
2338 case REQ_MOVE_PAGE_DOWN
:
2339 steps
= view
->lineno
+ view
->height
>= view
->lines
2340 ? view
->lines
- view
->lineno
- 1 : view
->height
;
2352 die("request %d not handled in switch", request
);
2355 if (steps
<= 0 && view
->lineno
== 0) {
2356 report("Cannot move beyond the first line");
2359 } else if (steps
>= 0 && view
->lineno
+ 1 >= view
->lines
) {
2360 report("Cannot move beyond the last line");
2364 /* Move the current line */
2365 view
->lineno
+= steps
;
2366 assert(0 <= view
->lineno
&& view
->lineno
< view
->lines
);
2368 /* Check whether the view needs to be scrolled */
2369 if (view
->lineno
< view
->offset
||
2370 view
->lineno
>= view
->offset
+ view
->height
) {
2371 scroll_steps
= steps
;
2372 if (steps
< 0 && -steps
> view
->offset
) {
2373 scroll_steps
= -view
->offset
;
2375 } else if (steps
> 0) {
2376 if (view
->lineno
== view
->lines
- 1 &&
2377 view
->lines
> view
->height
) {
2378 scroll_steps
= view
->lines
- view
->offset
- 1;
2379 if (scroll_steps
>= view
->height
)
2380 scroll_steps
-= view
->height
- 1;
2385 if (!view_is_displayed(view
)) {
2386 view
->offset
+= scroll_steps
;
2387 assert(0 <= view
->offset
&& view
->offset
< view
->lines
);
2388 view
->ops
->select(view
, &view
->line
[view
->lineno
]);
2392 /* Repaint the old "current" line if we be scrolling */
2393 if (ABS(steps
) < view
->height
)
2394 draw_view_line(view
, view
->lineno
- steps
- view
->offset
);
2397 do_scroll_view(view
, scroll_steps
);
2401 /* Draw the current line */
2402 draw_view_line(view
, view
->lineno
- view
->offset
);
2404 wnoutrefresh(view
->win
);
2413 static void search_view(struct view
*view
, enum request request
);
2416 grep_text(struct view
*view
, const char *text
[])
2421 for (i
= 0; text
[i
]; i
++)
2423 regexec(view
->regex
, text
[i
], 1, &pmatch
, 0) != REG_NOMATCH
)
2429 select_view_line(struct view
*view
, unsigned long lineno
)
2431 unsigned long old_lineno
= view
->lineno
;
2432 unsigned long old_offset
= view
->offset
;
2434 if (goto_view_line(view
, view
->offset
, lineno
)) {
2435 if (view_is_displayed(view
)) {
2436 if (old_offset
!= view
->offset
) {
2439 draw_view_line(view
, old_lineno
- view
->offset
);
2440 draw_view_line(view
, view
->lineno
- view
->offset
);
2441 wnoutrefresh(view
->win
);
2444 view
->ops
->select(view
, &view
->line
[view
->lineno
]);
2450 find_next(struct view
*view
, enum request request
)
2452 unsigned long lineno
= view
->lineno
;
2457 report("No previous search");
2459 search_view(view
, request
);
2469 case REQ_SEARCH_BACK
:
2478 if (request
== REQ_FIND_NEXT
|| request
== REQ_FIND_PREV
)
2479 lineno
+= direction
;
2481 /* Note, lineno is unsigned long so will wrap around in which case it
2482 * will become bigger than view->lines. */
2483 for (; lineno
< view
->lines
; lineno
+= direction
) {
2484 if (view
->ops
->grep(view
, &view
->line
[lineno
])) {
2485 select_view_line(view
, lineno
);
2486 report("Line %ld matches '%s'", lineno
+ 1, view
->grep
);
2491 report("No match found for '%s'", view
->grep
);
2495 search_view(struct view
*view
, enum request request
)
2500 regfree(view
->regex
);
2503 view
->regex
= calloc(1, sizeof(*view
->regex
));
2508 regex_err
= regcomp(view
->regex
, opt_search
, REG_EXTENDED
);
2509 if (regex_err
!= 0) {
2510 char buf
[SIZEOF_STR
] = "unknown error";
2512 regerror(regex_err
, view
->regex
, buf
, sizeof(buf
));
2513 report("Search failed: %s", buf
);
2517 string_copy(view
->grep
, opt_search
);
2519 find_next(view
, request
);
2523 * Incremental updating
2527 reset_view(struct view
*view
)
2531 for (i
= 0; i
< view
->lines
; i
++)
2532 free(view
->line
[i
].data
);
2535 view
->p_offset
= view
->offset
;
2536 view
->p_yoffset
= view
->yoffset
;
2537 view
->p_lineno
= view
->lineno
;
2545 view
->update_secs
= 0;
2549 format_arg(const char *name
)
2555 const char *value_if_empty
;
2557 #define FORMAT_VAR(name, value, value_if_empty) \
2558 { name, STRING_SIZE(name), value, value_if_empty }
2559 FORMAT_VAR("%(directory)", opt_path
, "."),
2560 FORMAT_VAR("%(file)", opt_file
, ""),
2561 FORMAT_VAR("%(ref)", opt_ref
, "HEAD"),
2562 FORMAT_VAR("%(head)", ref_head
, ""),
2563 FORMAT_VAR("%(commit)", ref_commit
, ""),
2564 FORMAT_VAR("%(blob)", ref_blob
, ""),
2565 FORMAT_VAR("%(branch)", ref_branch
, ""),
2569 for (i
= 0; i
< ARRAY_SIZE(vars
); i
++)
2570 if (!strncmp(name
, vars
[i
].name
, vars
[i
].namelen
))
2571 return *vars
[i
].value
? vars
[i
].value
: vars
[i
].value_if_empty
;
2573 report("Unknown replacement: `%s`", name
);
2578 format_argv(const char ***dst_argv
, const char *src_argv
[], bool first
)
2580 char buf
[SIZEOF_STR
];
2583 argv_free(*dst_argv
);
2585 for (argc
= 0; src_argv
[argc
]; argc
++) {
2586 const char *arg
= src_argv
[argc
];
2589 if (!strcmp(arg
, "%(fileargs)")) {
2590 if (!argv_append_array(dst_argv
, opt_file_argv
))
2594 } else if (!strcmp(arg
, "%(diffargs)")) {
2595 if (!argv_append_array(dst_argv
, opt_diff_argv
))
2599 } else if (!strcmp(arg
, "%(blameargs)")) {
2600 if (!argv_append_array(dst_argv
, opt_blame_argv
))
2604 } else if (!strcmp(arg
, "%(revargs)") ||
2605 (first
&& !strcmp(arg
, "%(commit)"))) {
2606 if (!argv_append_array(dst_argv
, opt_rev_argv
))
2612 char *next
= strstr(arg
, "%(");
2613 int len
= next
- arg
;
2621 value
= format_arg(next
);
2628 if (!string_format_from(buf
, &bufpos
, "%.*s%s", len
, arg
, value
))
2631 arg
= next
? strchr(next
, ')') + 1 : NULL
;
2634 if (!argv_append(dst_argv
, buf
))
2638 return src_argv
[argc
] == NULL
;
2642 restore_view_position(struct view
*view
)
2644 /* A view without a previous view is the first view */
2645 if (!view
->prev
&& opt_lineno
&& opt_lineno
<= view
->lines
) {
2646 select_view_line(view
, opt_lineno
- 1);
2650 if (!view
->p_restore
|| (view
->pipe
&& view
->lines
<= view
->p_lineno
))
2653 /* Changing the view position cancels the restoring. */
2654 /* FIXME: Changing back to the first line is not detected. */
2655 if (view
->offset
!= 0 || view
->lineno
!= 0) {
2656 view
->p_restore
= FALSE
;
2660 if (goto_view_line(view
, view
->p_offset
, view
->p_lineno
) &&
2661 view_is_displayed(view
))
2664 view
->yoffset
= view
->p_yoffset
;
2665 view
->p_restore
= FALSE
;
2671 end_update(struct view
*view
, bool force
)
2675 while (!view
->ops
->read(view
, NULL
))
2679 io_kill(view
->pipe
);
2680 io_done(view
->pipe
);
2685 setup_update(struct view
*view
, const char *vid
)
2688 string_copy_rev(view
->vid
, vid
);
2689 view
->pipe
= &view
->io
;
2690 view
->start_time
= time(NULL
);
2694 begin_update(struct view
*view
, const char *dir
, const char **argv
, enum open_flags flags
)
2696 bool extra
= !!(flags
& (OPEN_EXTRA
));
2697 bool reload
= !!(flags
& (OPEN_RELOAD
| OPEN_REFRESH
| OPEN_PREPARED
| OPEN_EXTRA
));
2698 bool refresh
= flags
& (OPEN_REFRESH
| OPEN_PREPARED
);
2700 if (!reload
&& !strcmp(view
->vid
, view
->id
))
2705 io_done(view
->pipe
);
2707 end_update(view
, TRUE
);
2710 if (!refresh
&& argv
) {
2712 if (!format_argv(&view
->argv
, argv
, !view
->prev
))
2715 /* Put the current ref_* value to the view title ref
2716 * member. This is needed by the blob view. Most other
2717 * views sets it automatically after loading because the
2718 * first line is a commit line. */
2719 string_copy_rev(view
->ref
, view
->id
);
2722 if (view
->argv
&& view
->argv
[0] &&
2723 !io_run(&view
->io
, IO_RD
, view
->dir
, view
->argv
))
2727 setup_update(view
, view
->id
);
2733 update_view(struct view
*view
)
2735 char out_buffer
[BUFSIZ
* 2];
2737 /* Clear the view and redraw everything since the tree sorting
2738 * might have rearranged things. */
2739 bool redraw
= view
->lines
== 0;
2740 bool can_read
= TRUE
;
2745 if (!io_can_read(view
->pipe
, FALSE
)) {
2746 if (view
->lines
== 0 && view_is_displayed(view
)) {
2747 time_t secs
= time(NULL
) - view
->start_time
;
2749 if (secs
> 1 && secs
> view
->update_secs
) {
2750 if (view
->update_secs
== 0)
2752 update_view_title(view
);
2753 view
->update_secs
= secs
;
2759 for (; (line
= io_get(view
->pipe
, '\n', can_read
)); can_read
= FALSE
) {
2760 if (opt_iconv_in
!= ICONV_NONE
) {
2761 ICONV_CONST
char *inbuf
= line
;
2762 size_t inlen
= strlen(line
) + 1;
2764 char *outbuf
= out_buffer
;
2765 size_t outlen
= sizeof(out_buffer
);
2769 ret
= iconv(opt_iconv_in
, &inbuf
, &inlen
, &outbuf
, &outlen
);
2770 if (ret
!= (size_t) -1)
2774 if (!view
->ops
->read(view
, line
)) {
2775 report("Allocation failure");
2776 end_update(view
, TRUE
);
2782 unsigned long lines
= view
->lines
;
2785 for (digits
= 0; lines
; digits
++)
2788 /* Keep the displayed view in sync with line number scaling. */
2789 if (digits
!= view
->digits
) {
2790 view
->digits
= digits
;
2791 if (opt_line_number
|| view
->type
== VIEW_BLAME
)
2796 if (io_error(view
->pipe
)) {
2797 report("Failed to read: %s", io_strerror(view
->pipe
));
2798 end_update(view
, TRUE
);
2800 } else if (io_eof(view
->pipe
)) {
2801 if (view_is_displayed(view
))
2803 end_update(view
, FALSE
);
2806 if (restore_view_position(view
))
2809 if (!view_is_displayed(view
))
2813 redraw_view_from(view
, 0);
2815 redraw_view_dirty(view
);
2817 /* Update the title _after_ the redraw so that if the redraw picks up a
2818 * commit reference in view->ref it'll be available here. */
2819 update_view_title(view
);
2823 DEFINE_ALLOCATOR(realloc_lines
, struct line
, 256)
2825 static struct line
*
2826 add_line_data(struct view
*view
, void *data
, enum line_type type
)
2830 if (!realloc_lines(&view
->line
, view
->lines
, 1))
2833 line
= &view
->line
[view
->lines
++];
2834 memset(line
, 0, sizeof(*line
));
2842 static struct line
*
2843 add_line_text(struct view
*view
, const char *text
, enum line_type type
)
2845 char *data
= text
? strdup(text
) : NULL
;
2847 return data
? add_line_data(view
, data
, type
) : NULL
;
2850 static struct line
*
2851 add_line_format(struct view
*view
, enum line_type type
, const char *fmt
, ...)
2853 char buf
[SIZEOF_STR
];
2856 FORMAT_BUFFER(buf
, sizeof(buf
), fmt
, retval
);
2857 return retval
>= 0 ? add_line_text(view
, buf
, type
) : NULL
;
2865 load_view(struct view
*view
, enum open_flags flags
)
2868 end_update(view
, TRUE
);
2869 if (view
->ops
->private_size
) {
2871 view
->private = calloc(1, view
->ops
->private_size
);
2873 memset(view
->private, 0, view
->ops
->private_size
);
2875 if (!view
->ops
->open(view
, flags
)) {
2876 report("Failed to load %s view", view
->name
);
2879 restore_view_position(view
);
2881 if (view
->pipe
&& view
->lines
== 0) {
2882 /* Clear the old view and let the incremental updating refill
2885 view
->p_restore
= flags
& (OPEN_RELOAD
| OPEN_REFRESH
);
2887 } else if (view_is_displayed(view
)) {
2893 #define refresh_view(view) load_view(view, OPEN_REFRESH)
2894 #define reload_view(view) load_view(view, OPEN_RELOAD)
2897 split_view(struct view
*prev
, struct view
*view
)
2901 view
->parent
= prev
;
2904 if (prev
->lineno
- prev
->offset
>= prev
->height
) {
2905 /* Take the title line into account. */
2906 int lines
= prev
->lineno
- prev
->offset
- prev
->height
+ 1;
2908 /* Scroll the view that was split if the current line is
2909 * outside the new limited view. */
2910 do_scroll_view(prev
, lines
);
2913 if (view
!= prev
&& view_is_displayed(prev
)) {
2914 /* "Blur" the previous view. */
2915 update_view_title(prev
);
2920 open_view(struct view
*prev
, enum request request
, enum open_flags flags
)
2922 bool split
= !!(flags
& OPEN_SPLIT
);
2923 bool reload
= !!(flags
& (OPEN_RELOAD
| OPEN_PREPARED
));
2924 struct view
*view
= VIEW(request
);
2925 int nviews
= displayed_views();
2927 assert(flags
^ OPEN_REFRESH
);
2929 if (view
== prev
&& nviews
== 1 && !reload
) {
2930 report("Already in %s view", view
->name
);
2934 if (view
->git_dir
&& !opt_git_dir
[0]) {
2935 report("The %s view is disabled in pager view", view
->name
);
2940 split_view(prev
, view
);
2942 maximize_view(view
, FALSE
);
2945 /* No prev signals that this is the first loaded view. */
2946 if (prev
&& view
!= prev
) {
2950 load_view(view
, flags
);
2954 open_argv(struct view
*prev
, struct view
*view
, const char *argv
[], const char *dir
, enum open_flags flags
)
2956 enum request request
= view
- views
+ REQ_OFFSET
+ 1;
2959 end_update(view
, TRUE
);
2962 if (!argv_copy(&view
->argv
, argv
)) {
2963 report("Failed to open %s view: %s", view
->name
, io_strerror(&view
->io
));
2965 open_view(prev
, request
, flags
| OPEN_PREPARED
);
2970 open_external_viewer(const char *argv
[], const char *dir
)
2972 def_prog_mode(); /* save current tty modes */
2973 endwin(); /* restore original tty modes */
2974 io_run_fg(argv
, dir
);
2975 fprintf(stderr
, "Press Enter to continue");
2978 redraw_display(TRUE
);
2982 open_mergetool(const char *file
)
2984 const char *mergetool_argv
[] = { "git", "mergetool", file
, NULL
};
2986 open_external_viewer(mergetool_argv
, opt_cdup
);
2990 open_editor(const char *file
)
2992 const char *editor_argv
[SIZEOF_ARG
+ 1] = { "vi", file
, NULL
};
2993 char editor_cmd
[SIZEOF_STR
];
2997 editor
= getenv("GIT_EDITOR");
2998 if (!editor
&& *opt_editor
)
2999 editor
= opt_editor
;
3001 editor
= getenv("VISUAL");
3003 editor
= getenv("EDITOR");
3007 string_ncopy(editor_cmd
, editor
, strlen(editor
));
3008 if (!argv_from_string_no_quotes(editor_argv
, &argc
, editor_cmd
)) {
3009 report("Failed to read editor command");
3013 editor_argv
[argc
] = file
;
3014 open_external_viewer(editor_argv
, opt_cdup
);
3018 open_run_request(enum request request
)
3020 struct run_request
*req
= get_run_request(request
);
3021 const char **argv
= NULL
;
3024 report("Unknown run request");
3028 if (format_argv(&argv
, req
->argv
, FALSE
))
3029 open_external_viewer(argv
, NULL
);
3036 * User request switch noodle
3040 view_driver(struct view
*view
, enum request request
)
3044 if (request
== REQ_NONE
)
3047 if (request
> REQ_NONE
) {
3048 open_run_request(request
);
3049 view_request(view
, REQ_REFRESH
);
3053 request
= view_request(view
, request
);
3054 if (request
== REQ_NONE
)
3060 case REQ_MOVE_PAGE_UP
:
3061 case REQ_MOVE_PAGE_DOWN
:
3062 case REQ_MOVE_FIRST_LINE
:
3063 case REQ_MOVE_LAST_LINE
:
3064 move_view(view
, request
);
3067 case REQ_SCROLL_FIRST_COL
:
3068 case REQ_SCROLL_LEFT
:
3069 case REQ_SCROLL_RIGHT
:
3070 case REQ_SCROLL_LINE_DOWN
:
3071 case REQ_SCROLL_LINE_UP
:
3072 case REQ_SCROLL_PAGE_DOWN
:
3073 case REQ_SCROLL_PAGE_UP
:
3074 scroll_view(view
, request
);
3077 case REQ_VIEW_BLAME
:
3079 report("No file chosen, press %s to open tree view",
3080 get_key(view
->keymap
, REQ_VIEW_TREE
));
3083 open_view(view
, request
, OPEN_DEFAULT
);
3088 report("No file chosen, press %s to open tree view",
3089 get_key(view
->keymap
, REQ_VIEW_TREE
));
3092 open_view(view
, request
, OPEN_DEFAULT
);
3095 case REQ_VIEW_PAGER
:
3097 if (!io_open(&VIEW(REQ_VIEW_PAGER
)->io
, ""))
3098 die("Failed to open stdin");
3099 open_view(view
, request
, OPEN_PREPARED
);
3103 if (!VIEW(REQ_VIEW_PAGER
)->pipe
&& !VIEW(REQ_VIEW_PAGER
)->lines
) {
3104 report("No pager content, press %s to run command from prompt",
3105 get_key(view
->keymap
, REQ_PROMPT
));
3108 open_view(view
, request
, OPEN_DEFAULT
);
3111 case REQ_VIEW_STAGE
:
3112 if (!VIEW(REQ_VIEW_STAGE
)->lines
) {
3113 report("No stage content, press %s to open the status view and choose file",
3114 get_key(view
->keymap
, REQ_VIEW_STATUS
));
3117 open_view(view
, request
, OPEN_DEFAULT
);
3120 case REQ_VIEW_STATUS
:
3121 if (opt_is_inside_work_tree
== FALSE
) {
3122 report("The status view requires a working tree");
3125 open_view(view
, request
, OPEN_DEFAULT
);
3133 case REQ_VIEW_BRANCH
:
3134 open_view(view
, request
, OPEN_DEFAULT
);
3139 request
= request
== REQ_NEXT
? REQ_MOVE_DOWN
: REQ_MOVE_UP
;
3144 view
= view
->parent
;
3145 line
= view
->lineno
;
3146 move_view(view
, request
);
3147 if (view_is_displayed(view
))
3148 update_view_title(view
);
3149 if (line
!= view
->lineno
)
3150 view_request(view
, REQ_ENTER
);
3152 move_view(view
, request
);
3158 int nviews
= displayed_views();
3159 int next_view
= (current_view
+ 1) % nviews
;
3161 if (next_view
== current_view
) {
3162 report("Only one view is displayed");
3166 current_view
= next_view
;
3167 /* Blur out the title of the previous view. */
3168 update_view_title(view
);
3173 report("Refreshing is not yet supported for the %s view", view
->name
);
3177 if (displayed_views() == 2)
3178 maximize_view(view
, TRUE
);
3182 case REQ_TOGGLE_LINENO
:
3183 case REQ_TOGGLE_DATE
:
3184 case REQ_TOGGLE_AUTHOR
:
3185 case REQ_TOGGLE_FILENAME
:
3186 case REQ_TOGGLE_GRAPHIC
:
3187 case REQ_TOGGLE_REV_GRAPH
:
3188 case REQ_TOGGLE_REFS
:
3189 toggle_option(request
);
3192 case REQ_TOGGLE_SORT_FIELD
:
3193 case REQ_TOGGLE_SORT_ORDER
:
3194 report("Sorting is not yet supported for the %s view", view
->name
);
3197 case REQ_DIFF_CONTEXT_UP
:
3198 case REQ_DIFF_CONTEXT_DOWN
:
3199 report("Changing the diff context is not yet supported for the %s view", view
->name
);
3203 case REQ_SEARCH_BACK
:
3204 search_view(view
, request
);
3209 find_next(view
, request
);
3212 case REQ_STOP_LOADING
:
3213 foreach_view(view
, i
) {
3215 report("Stopped loading the %s view", view
->name
),
3216 end_update(view
, TRUE
);
3220 case REQ_SHOW_VERSION
:
3221 report("tig-%s (built %s)", TIG_VERSION
, __DATE__
);
3224 case REQ_SCREEN_REDRAW
:
3225 redraw_display(TRUE
);
3229 report("Nothing to edit");
3233 report("Nothing to enter");
3236 case REQ_VIEW_CLOSE
:
3237 /* XXX: Mark closed views by letting view->prev point to the
3238 * view itself. Parents to closed view should never be
3240 if (view
->prev
&& view
->prev
!= view
) {
3241 maximize_view(view
->prev
, TRUE
);
3250 report("Unknown key, press %s for help",
3251 get_key(view
->keymap
, REQ_VIEW_HELP
));
3260 * View backend utilities
3270 const enum sort_field
*fields
;
3271 size_t size
, current
;
3275 #define SORT_STATE(fields) { fields, ARRAY_SIZE(fields), 0 }
3276 #define get_sort_field(state) ((state).fields[(state).current])
3277 #define sort_order(state, result) ((state).reverse ? -(result) : (result))
3280 sort_view(struct view
*view
, enum request request
, struct sort_state
*state
,
3281 int (*compare
)(const void *, const void *))
3284 case REQ_TOGGLE_SORT_FIELD
:
3285 state
->current
= (state
->current
+ 1) % state
->size
;
3288 case REQ_TOGGLE_SORT_ORDER
:
3289 state
->reverse
= !state
->reverse
;
3292 die("Not a sort request");
3295 qsort(view
->line
, view
->lines
, sizeof(*view
->line
), compare
);
3300 update_diff_context(enum request request
)
3302 int diff_context
= opt_diff_context
;
3305 case REQ_DIFF_CONTEXT_UP
:
3306 opt_diff_context
+= 1;
3307 update_diff_context_arg(opt_diff_context
);
3310 case REQ_DIFF_CONTEXT_DOWN
:
3311 if (opt_diff_context
== 0) {
3312 report("Diff context cannot be less than zero");
3315 opt_diff_context
-= 1;
3316 update_diff_context_arg(opt_diff_context
);
3320 die("Not a diff context request");
3323 return diff_context
!= opt_diff_context
;
3326 DEFINE_ALLOCATOR(realloc_authors
, const char *, 256)
3328 /* Small author cache to reduce memory consumption. It uses binary
3329 * search to lookup or find place to position new entries. No entries
3330 * are ever freed. */
3332 get_author(const char *name
)
3334 static const char **authors
;
3335 static size_t authors_size
;
3336 int from
= 0, to
= authors_size
- 1;
3338 while (from
<= to
) {
3339 size_t pos
= (to
+ from
) / 2;
3340 int cmp
= strcmp(name
, authors
[pos
]);
3343 return authors
[pos
];
3351 if (!realloc_authors(&authors
, authors_size
, 1))
3353 name
= strdup(name
);
3357 memmove(authors
+ from
+ 1, authors
+ from
, (authors_size
- from
) * sizeof(*authors
));
3358 authors
[from
] = name
;
3365 parse_timesec(struct time
*time
, const char *sec
)
3367 time
->sec
= (time_t) atol(sec
);
3371 parse_timezone(struct time
*time
, const char *zone
)
3375 tz
= ('0' - zone
[1]) * 60 * 60 * 10;
3376 tz
+= ('0' - zone
[2]) * 60 * 60;
3377 tz
+= ('0' - zone
[3]) * 60 * 10;
3378 tz
+= ('0' - zone
[4]) * 60;
3387 /* Parse author lines where the name may be empty:
3388 * author <email@address.tld> 1138474660 +0100
3391 parse_author_line(char *ident
, const char **author
, struct time
*time
)
3393 char *nameend
= strchr(ident
, '<');
3394 char *emailend
= strchr(ident
, '>');
3396 if (nameend
&& emailend
)
3397 *nameend
= *emailend
= 0;
3398 ident
= chomp_string(ident
);
3401 ident
= chomp_string(nameend
+ 1);
3406 *author
= get_author(ident
);
3408 /* Parse epoch and timezone */
3409 if (emailend
&& emailend
[1] == ' ') {
3410 char *secs
= emailend
+ 2;
3411 char *zone
= strchr(secs
, ' ');
3413 parse_timesec(time
, secs
);
3415 if (zone
&& strlen(zone
) == STRING_SIZE(" +0700"))
3416 parse_timezone(time
, zone
+ 1);
3420 static struct line
*
3421 find_prev_line_by_type(struct view
*view
, struct line
*line
, enum line_type type
)
3423 for (; view
->line
< line
; line
--)
3424 if (line
->type
== type
)
3434 struct blame_commit
{
3435 char id
[SIZEOF_REV
]; /* SHA1 ID. */
3436 char title
[128]; /* First line of the commit message. */
3437 const char *author
; /* Author of the commit. */
3438 struct time time
; /* Date from the author ident. */
3439 char filename
[128]; /* Name of file. */
3440 char parent_id
[SIZEOF_REV
]; /* Parent/previous SHA1 ID. */
3441 char parent_filename
[128]; /* Parent/previous name of file. */
3444 struct blame_header
{
3445 char id
[SIZEOF_REV
]; /* SHA1 ID. */
3452 parse_number(const char **posref
, size_t *number
, size_t min
, size_t max
)
3454 const char *pos
= *posref
;
3457 pos
= strchr(pos
+ 1, ' ');
3458 if (!pos
|| !isdigit(pos
[1]))
3460 *number
= atoi(pos
+ 1);
3461 if (*number
< min
|| *number
> max
)
3469 parse_blame_header(struct blame_header
*header
, const char *text
, size_t max_lineno
)
3471 const char *pos
= text
+ SIZEOF_REV
- 2;
3473 if (strlen(text
) <= SIZEOF_REV
|| pos
[1] != ' ')
3476 string_ncopy(header
->id
, text
, SIZEOF_REV
);
3478 if (!parse_number(&pos
, &header
->orig_lineno
, 1, 9999999) ||
3479 !parse_number(&pos
, &header
->lineno
, 1, max_lineno
) ||
3480 !parse_number(&pos
, &header
->group
, 1, max_lineno
- header
->lineno
+ 1))
3487 match_blame_header(const char *name
, char **line
)
3489 size_t namelen
= strlen(name
);
3490 bool matched
= !strncmp(name
, *line
, namelen
);
3499 parse_blame_info(struct blame_commit
*commit
, char *line
)
3501 if (match_blame_header("author ", &line
)) {
3502 commit
->author
= get_author(line
);
3504 } else if (match_blame_header("author-time ", &line
)) {
3505 parse_timesec(&commit
->time
, line
);
3507 } else if (match_blame_header("author-tz ", &line
)) {
3508 parse_timezone(&commit
->time
, line
);
3510 } else if (match_blame_header("summary ", &line
)) {
3511 string_ncopy(commit
->title
, line
, strlen(line
));
3513 } else if (match_blame_header("previous ", &line
)) {
3514 if (strlen(line
) <= SIZEOF_REV
)
3516 string_copy_rev(commit
->parent_id
, line
);
3518 string_ncopy(commit
->parent_filename
, line
, strlen(line
));
3520 } else if (match_blame_header("filename ", &line
)) {
3521 string_ncopy(commit
->filename
, line
, strlen(line
));
3533 pager_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
3535 if (opt_line_number
&& draw_lineno(view
, lineno
))
3538 draw_text(view
, line
->type
, line
->data
);
3543 add_describe_ref(char *buf
, size_t *bufpos
, const char *commit_id
, const char *sep
)
3545 const char *describe_argv
[] = { "git", "describe", commit_id
, NULL
};
3546 char ref
[SIZEOF_STR
];
3548 if (!io_run_buf(describe_argv
, ref
, sizeof(ref
)) || !*ref
)
3551 /* This is the only fatal call, since it can "corrupt" the buffer. */
3552 if (!string_nformat(buf
, SIZEOF_STR
, bufpos
, "%s%s", sep
, ref
))
3559 add_pager_refs(struct view
*view
, struct line
*line
)
3561 char buf
[SIZEOF_STR
];
3562 char *commit_id
= (char *)line
->data
+ STRING_SIZE("commit ");
3563 struct ref_list
*list
;
3564 size_t bufpos
= 0, i
;
3565 const char *sep
= "Refs: ";
3566 bool is_tag
= FALSE
;
3568 assert(line
->type
== LINE_COMMIT
);
3570 list
= get_ref_list(commit_id
);
3572 if (view
->type
== VIEW_DIFF
)
3573 goto try_add_describe_ref
;
3577 for (i
= 0; i
< list
->size
; i
++) {
3578 struct ref
*ref
= list
->refs
[i
];
3579 const char *fmt
= ref
->tag
? "%s[%s]" :
3580 ref
->remote
? "%s<%s>" : "%s%s";
3582 if (!string_format_from(buf
, &bufpos
, fmt
, sep
, ref
->name
))
3589 if (!is_tag
&& view
->type
== VIEW_DIFF
) {
3590 try_add_describe_ref
:
3591 /* Add <tag>-g<commit_id> "fake" reference. */
3592 if (!add_describe_ref(buf
, &bufpos
, commit_id
, sep
))
3599 add_line_text(view
, buf
, LINE_PP_REFS
);
3603 pager_read(struct view
*view
, char *data
)
3610 line
= add_line_text(view
, data
, get_line_type(data
));
3614 if (line
->type
== LINE_COMMIT
&&
3615 (view
->type
== VIEW_DIFF
||
3616 view
->type
== VIEW_LOG
))
3617 add_pager_refs(view
, line
);
3623 pager_request(struct view
*view
, enum request request
, struct line
*line
)
3627 if (request
!= REQ_ENTER
)
3630 if (line
->type
== LINE_COMMIT
&&
3631 (view
->type
== VIEW_LOG
||
3632 view
->type
== VIEW_PAGER
)) {
3633 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
);
3637 /* Always scroll the view even if it was split. That way
3638 * you can use Enter to scroll through the log view and
3639 * split open each commit diff. */
3640 scroll_view(view
, REQ_SCROLL_LINE_DOWN
);
3642 /* FIXME: A minor workaround. Scrolling the view will call report("")
3643 * but if we are scrolling a non-current view this won't properly
3644 * update the view title. */
3646 update_view_title(view
);
3652 pager_grep(struct view
*view
, struct line
*line
)
3654 const char *text
[] = { line
->data
, NULL
};
3656 return grep_text(view
, text
);
3660 pager_select(struct view
*view
, struct line
*line
)
3662 if (line
->type
== LINE_COMMIT
) {
3663 char *text
= (char *)line
->data
+ STRING_SIZE("commit ");
3665 if (view
->type
!= VIEW_PAGER
)
3666 string_copy_rev(view
->ref
, text
);
3667 string_copy_rev(ref_commit
, text
);
3672 pager_open(struct view
*view
, enum open_flags flags
)
3674 return begin_update(view
, NULL
, NULL
, flags
);
3677 static struct view_ops pager_ops
= {
3689 log_open(struct view
*view
, enum open_flags flags
)
3691 static const char *log_argv
[] = {
3692 "git", "log", "--no-color", "--cc", "--stat", "-n100", "%(head)", NULL
3695 return begin_update(view
, NULL
, log_argv
, flags
);
3699 log_request(struct view
*view
, enum request request
, struct line
*line
)
3707 return pager_request(view
, request
, line
);
3711 static struct view_ops log_ops
= {
3723 bool reading_diff_stat
;
3727 diff_open(struct view
*view
, enum open_flags flags
)
3729 static const char *diff_argv
[] = {
3730 "git", "show", "--pretty=fuller", "--no-color", "--root",
3731 "--patch-with-stat", "--find-copies-harder", "-C",
3732 opt_notes_arg
, opt_diff_context_arg
, "%(diffargs)",
3733 "%(commit)", "--", "%(fileargs)", NULL
3736 return begin_update(view
, NULL
, diff_argv
, flags
);
3740 diff_common_read(struct view
*view
, char *data
, struct diff_state
*state
)
3742 if (state
->reading_diff_stat
) {
3743 size_t len
= strlen(data
);
3744 char *pipe
= strchr(data
, '|');
3745 bool has_histogram
= data
[len
- 1] == '-' || data
[len
- 1] == '+';
3746 bool has_bin_diff
= pipe
&& strstr(pipe
, "Bin") && strstr(pipe
, "->");
3748 if (pipe
&& (has_histogram
|| has_bin_diff
)) {
3749 return add_line_text(view
, data
, LINE_DIFF_STAT
) != NULL
;
3751 state
->reading_diff_stat
= FALSE
;
3754 } else if (!strcmp(data
, "---")) {
3755 state
->reading_diff_stat
= TRUE
;
3758 return pager_read(view
, data
);
3762 diff_common_enter(struct view
*view
, enum request request
, struct line
*line
)
3764 if (line
->type
== LINE_DIFF_STAT
) {
3765 int file_number
= 0;
3767 while (line
>= view
->line
&& line
->type
== LINE_DIFF_STAT
) {
3772 while (line
< view
->line
+ view
->lines
) {
3773 if (line
->type
== LINE_DIFF_HEADER
) {
3774 if (file_number
== 1) {
3783 select_view_line(view
, line
- view
->line
);
3788 return pager_request(view
, request
, line
);
3793 diff_common_draw_part(struct view
*view
, enum line_type
*type
, char **text
, char c
, enum line_type next_type
)
3795 char *sep
= strchr(*text
, c
);
3799 draw_text(view
, *type
, *text
);
3809 diff_common_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
3811 char *text
= line
->data
;
3812 enum line_type type
= line
->type
;
3814 if (opt_line_number
&& draw_lineno(view
, lineno
))
3817 if (type
== LINE_DIFF_STAT
) {
3818 diff_common_draw_part(view
, &type
, &text
, '|', LINE_DEFAULT
);
3819 if (diff_common_draw_part(view
, &type
, &text
, 'B', LINE_DEFAULT
)) {
3820 /* Handle binary diffstat: Bin <deleted> -> <added> bytes */
3821 diff_common_draw_part(view
, &type
, &text
, ' ', LINE_DIFF_DEL
);
3822 diff_common_draw_part(view
, &type
, &text
, '-', LINE_DEFAULT
);
3823 diff_common_draw_part(view
, &type
, &text
, ' ', LINE_DIFF_ADD
);
3824 diff_common_draw_part(view
, &type
, &text
, 'b', LINE_DEFAULT
);
3827 diff_common_draw_part(view
, &type
, &text
, '+', LINE_DIFF_ADD
);
3828 diff_common_draw_part(view
, &type
, &text
, '-', LINE_DIFF_DEL
);
3832 draw_text(view
, type
, text
);
3837 diff_read(struct view
*view
, char *data
)
3839 struct diff_state
*state
= view
->private;
3842 /* Fall back to retry if no diff will be shown. */
3843 if (view
->lines
== 0 && opt_file_argv
) {
3844 int pos
= argv_size(view
->argv
)
3845 - argv_size(opt_file_argv
) - 1;
3847 if (pos
> 0 && !strcmp(view
->argv
[pos
], "--")) {
3848 for (; view
->argv
[pos
]; pos
++) {
3849 free((void *) view
->argv
[pos
]);
3850 view
->argv
[pos
] = NULL
;
3854 io_done(view
->pipe
);
3855 if (io_run(&view
->io
, IO_RD
, view
->dir
, view
->argv
))
3862 return diff_common_read(view
, data
, state
);
3866 diff_blame_line(const char *ref
, const char *file
, unsigned long lineno
,
3867 struct blame_header
*header
, struct blame_commit
*commit
)
3869 char line_arg
[SIZEOF_STR
];
3870 const char *blame_argv
[] = {
3871 "git", "blame", "-p", line_arg
, ref
, "--", file
, NULL
3877 if (!string_format(line_arg
, "-L%d,+1", lineno
))
3880 if (!io_run(&io
, IO_RD
, opt_cdup
, blame_argv
))
3883 while ((buf
= io_get(&io
, '\n', TRUE
))) {
3885 if (!parse_blame_header(header
, buf
, 9999999))
3889 } else if (parse_blame_info(commit
, buf
)) {
3903 parse_chunk_lineno(int *lineno
, const char *chunk
, int marker
)
3905 return prefixcmp(chunk
, "@@ -") ||
3906 !(chunk
= strchr(chunk
, marker
)) ||
3907 parse_int(lineno
, chunk
+ 1, 0, 9999999) != OPT_OK
;
3911 diff_trace_origin(struct view
*view
, struct line
*line
)
3913 struct line
*diff
= find_prev_line_by_type(view
, line
, LINE_DIFF_HEADER
);
3914 struct line
*chunk
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
);
3915 const char *chunk_data
;
3916 int chunk_marker
= line
->type
== LINE_DIFF_DEL
? '-' : '+';
3918 const char *file
= NULL
;
3919 char ref
[SIZEOF_REF
];
3920 struct blame_header header
;
3921 struct blame_commit commit
;
3923 if (!diff
|| !chunk
|| chunk
== line
) {
3924 report("The line to trace must be inside a diff chunk");
3928 for (; diff
< line
&& !file
; diff
++) {
3929 const char *data
= diff
->data
;
3931 if (!prefixcmp(data
, "--- a/")) {
3932 file
= data
+ STRING_SIZE("--- a/");
3937 if (diff
== line
|| !file
) {
3938 report("Failed to read the file name");
3942 chunk_data
= chunk
->data
;
3944 if (parse_chunk_lineno(&lineno
, chunk_data
, chunk_marker
)) {
3945 report("Failed to read the line number");
3950 report("This is the origin of the line");
3954 for (chunk
+= 1; chunk
< line
; chunk
++) {
3955 if (chunk
->type
== LINE_DIFF_ADD
) {
3956 lineno
+= chunk_marker
== '+';
3957 } else if (chunk
->type
== LINE_DIFF_DEL
) {
3958 lineno
+= chunk_marker
== '-';
3964 if (chunk_marker
== '+')
3965 string_copy(ref
, view
->vid
);
3967 string_format(ref
, "%s^", view
->vid
);
3969 if (!diff_blame_line(ref
, file
, lineno
, &header
, &commit
)) {
3970 report("Failed to read blame data");
3974 string_ncopy(opt_file
, commit
.filename
, strlen(commit
.filename
));
3975 string_copy(opt_ref
, header
.id
);
3976 opt_goto_line
= header
.orig_lineno
- 1;
3978 return REQ_VIEW_BLAME
;
3982 diff_request(struct view
*view
, enum request request
, struct line
*line
)
3985 case REQ_VIEW_BLAME
:
3986 return diff_trace_origin(view
, line
);
3988 case REQ_DIFF_CONTEXT_UP
:
3989 case REQ_DIFF_CONTEXT_DOWN
:
3990 if (!update_diff_context(request
))
3996 return diff_common_enter(view
, request
, line
);
3999 return pager_request(view
, request
, line
);
4004 diff_select(struct view
*view
, struct line
*line
)
4006 if (line
->type
== LINE_DIFF_STAT
) {
4007 const char *key
= get_key(KEYMAP_DIFF
, REQ_ENTER
);
4009 string_format(view
->ref
, "Press '%s' to jump to file diff", key
);
4011 string_ncopy(view
->ref
, view
->id
, strlen(view
->id
));
4012 return pager_select(view
, line
);
4016 static struct view_ops diff_ops
= {
4018 sizeof(struct diff_state
),
4031 static bool help_keymap_hidden
[ARRAY_SIZE(keymap_map
)];
4034 help_open_keymap_title(struct view
*view
, enum keymap keymap
)
4038 line
= add_line_format(view
, LINE_HELP_KEYMAP
, "[%c] %s bindings",
4039 help_keymap_hidden
[keymap
] ? '+' : '-',
4040 enum_name(keymap_map
[keymap
]));
4042 line
->other
= keymap
;
4044 return help_keymap_hidden
[keymap
];
4048 help_open_keymap(struct view
*view
, enum keymap keymap
)
4050 const char *group
= NULL
;
4051 char buf
[SIZEOF_STR
];
4053 bool add_title
= TRUE
;
4056 for (i
= 0; i
< ARRAY_SIZE(req_info
); i
++) {
4057 const char *key
= NULL
;
4059 if (req_info
[i
].request
== REQ_NONE
)
4062 if (!req_info
[i
].request
) {
4063 group
= req_info
[i
].help
;
4067 key
= get_keys(keymap
, req_info
[i
].request
, TRUE
);
4071 if (add_title
&& help_open_keymap_title(view
, keymap
))
4076 add_line_text(view
, group
, LINE_HELP_GROUP
);
4080 add_line_format(view
, LINE_DEFAULT
, " %-25s %-20s %s", key
,
4081 enum_name(req_info
[i
]), req_info
[i
].help
);
4084 group
= "External commands:";
4086 for (i
= 0; i
< run_requests
; i
++) {
4087 struct run_request
*req
= get_run_request(REQ_NONE
+ i
+ 1);
4091 if (!req
|| req
->keymap
!= keymap
)
4094 key
= get_key_name(req
->key
);
4096 key
= "(no key defined)";
4098 if (add_title
&& help_open_keymap_title(view
, keymap
))
4101 add_line_text(view
, group
, LINE_HELP_GROUP
);
4105 for (bufpos
= 0, argc
= 0; req
->argv
[argc
]; argc
++)
4106 if (!string_format_from(buf
, &bufpos
, "%s%s",
4107 argc
? " " : "", req
->argv
[argc
]))
4110 add_line_format(view
, LINE_DEFAULT
, " %-25s `%s`", key
, buf
);
4115 help_open(struct view
*view
, enum open_flags flags
)
4120 view
->p_restore
= TRUE
;
4121 add_line_text(view
, "Quick reference for tig keybindings:", LINE_DEFAULT
);
4122 add_line_text(view
, "", LINE_DEFAULT
);
4124 for (keymap
= 0; keymap
< ARRAY_SIZE(keymap_map
); keymap
++)
4125 help_open_keymap(view
, keymap
);
4131 help_request(struct view
*view
, enum request request
, struct line
*line
)
4135 if (line
->type
== LINE_HELP_KEYMAP
) {
4136 help_keymap_hidden
[line
->other
] =
4137 !help_keymap_hidden
[line
->other
];
4143 return pager_request(view
, request
, line
);
4147 static struct view_ops help_ops
= {
4163 struct tree_stack_entry
{
4164 struct tree_stack_entry
*prev
; /* Entry below this in the stack */
4165 unsigned long lineno
; /* Line number to restore */
4166 char *name
; /* Position of name in opt_path */
4169 /* The top of the path stack. */
4170 static struct tree_stack_entry
*tree_stack
= NULL
;
4171 unsigned long tree_lineno
= 0;
4174 pop_tree_stack_entry(void)
4176 struct tree_stack_entry
*entry
= tree_stack
;
4178 tree_lineno
= entry
->lineno
;
4180 tree_stack
= entry
->prev
;
4185 push_tree_stack_entry(const char *name
, unsigned long lineno
)
4187 struct tree_stack_entry
*entry
= calloc(1, sizeof(*entry
));
4188 size_t pathlen
= strlen(opt_path
);
4193 entry
->prev
= tree_stack
;
4194 entry
->name
= opt_path
+ pathlen
;
4197 if (!string_format_from(opt_path
, &pathlen
, "%s/", name
)) {
4198 pop_tree_stack_entry();
4202 /* Move the current line to the first tree entry. */
4204 entry
->lineno
= lineno
;
4207 /* Parse output from git-ls-tree(1):
4209 * 100644 blob f931e1d229c3e185caad4449bf5b66ed72462657 tig.c
4212 #define SIZEOF_TREE_ATTR \
4213 STRING_SIZE("100644 blob f931e1d229c3e185caad4449bf5b66ed72462657\t")
4215 #define SIZEOF_TREE_MODE \
4216 STRING_SIZE("100644 ")
4218 #define TREE_ID_OFFSET \
4219 STRING_SIZE("100644 blob ")
4222 char id
[SIZEOF_REV
];
4224 struct time time
; /* Date from the author ident. */
4225 const char *author
; /* Author of the commit. */
4230 const char *author_name
;
4231 struct time author_time
;
4236 tree_path(const struct line
*line
)
4238 return ((struct tree_entry
*) line
->data
)->name
;
4242 tree_compare_entry(const struct line
*line1
, const struct line
*line2
)
4244 if (line1
->type
!= line2
->type
)
4245 return line1
->type
== LINE_TREE_DIR
? -1 : 1;
4246 return strcmp(tree_path(line1
), tree_path(line2
));
4249 static const enum sort_field tree_sort_fields
[] = {
4250 ORDERBY_NAME
, ORDERBY_DATE
, ORDERBY_AUTHOR
4252 static struct sort_state tree_sort_state
= SORT_STATE(tree_sort_fields
);
4255 tree_compare(const void *l1
, const void *l2
)
4257 const struct line
*line1
= (const struct line
*) l1
;
4258 const struct line
*line2
= (const struct line
*) l2
;
4259 const struct tree_entry
*entry1
= ((const struct line
*) l1
)->data
;
4260 const struct tree_entry
*entry2
= ((const struct line
*) l2
)->data
;
4262 if (line1
->type
== LINE_TREE_HEAD
)
4264 if (line2
->type
== LINE_TREE_HEAD
)
4267 switch (get_sort_field(tree_sort_state
)) {
4269 return sort_order(tree_sort_state
, timecmp(&entry1
->time
, &entry2
->time
));
4271 case ORDERBY_AUTHOR
:
4272 return sort_order(tree_sort_state
, strcmp_null(entry1
->author
, entry2
->author
));
4276 return sort_order(tree_sort_state
, tree_compare_entry(line1
, line2
));
4281 static struct line
*
4282 tree_entry(struct view
*view
, enum line_type type
, const char *path
,
4283 const char *mode
, const char *id
)
4285 struct tree_entry
*entry
= calloc(1, sizeof(*entry
) + strlen(path
));
4286 struct line
*line
= entry
? add_line_data(view
, entry
, type
) : NULL
;
4288 if (!entry
|| !line
) {
4293 strncpy(entry
->name
, path
, strlen(path
));
4295 entry
->mode
= strtoul(mode
, NULL
, 8);
4297 string_copy_rev(entry
->id
, id
);
4303 tree_read_date(struct view
*view
, char *text
, struct tree_state
*state
)
4305 if (!text
&& state
->read_date
) {
4306 state
->read_date
= FALSE
;
4310 /* Find next entry to process */
4311 const char *log_file
[] = {
4312 "git", "log", "--no-color", "--pretty=raw",
4313 "--cc", "--raw", view
->id
, "--", "%(directory)", NULL
4317 tree_entry(view
, LINE_TREE_HEAD
, opt_path
, NULL
, NULL
);
4318 report("Tree is empty");
4322 if (!begin_update(view
, opt_cdup
, log_file
, OPEN_EXTRA
)) {
4323 report("Failed to load tree data");
4327 state
->read_date
= TRUE
;
4330 } else if (*text
== 'a' && get_line_type(text
) == LINE_AUTHOR
) {
4331 parse_author_line(text
+ STRING_SIZE("author "),
4332 &state
->author_name
, &state
->author_time
);
4334 } else if (*text
== ':') {
4336 size_t annotated
= 1;
4339 pos
= strchr(text
, '\t');
4343 if (*opt_path
&& !strncmp(text
, opt_path
, strlen(opt_path
)))
4344 text
+= strlen(opt_path
);
4345 pos
= strchr(text
, '/');
4349 for (i
= 1; i
< view
->lines
; i
++) {
4350 struct line
*line
= &view
->line
[i
];
4351 struct tree_entry
*entry
= line
->data
;
4353 annotated
+= !!entry
->author
;
4354 if (entry
->author
|| strcmp(entry
->name
, text
))
4357 entry
->author
= state
->author_name
;
4358 entry
->time
= state
->author_time
;
4363 if (annotated
== view
->lines
)
4364 io_kill(view
->pipe
);
4370 tree_read(struct view
*view
, char *text
)
4372 struct tree_state
*state
= view
->private;
4373 struct tree_entry
*data
;
4374 struct line
*entry
, *line
;
4375 enum line_type type
;
4376 size_t textlen
= text
? strlen(text
) : 0;
4377 char *path
= text
+ SIZEOF_TREE_ATTR
;
4379 if (state
->read_date
|| !text
)
4380 return tree_read_date(view
, text
, state
);
4382 if (textlen
<= SIZEOF_TREE_ATTR
)
4384 if (view
->lines
== 0 &&
4385 !tree_entry(view
, LINE_TREE_HEAD
, opt_path
, NULL
, NULL
))
4388 /* Strip the path part ... */
4390 size_t pathlen
= textlen
- SIZEOF_TREE_ATTR
;
4391 size_t striplen
= strlen(opt_path
);
4393 if (pathlen
> striplen
)
4394 memmove(path
, path
+ striplen
,
4395 pathlen
- striplen
+ 1);
4397 /* Insert "link" to parent directory. */
4398 if (view
->lines
== 1 &&
4399 !tree_entry(view
, LINE_TREE_DIR
, "..", "040000", view
->ref
))
4403 type
= text
[SIZEOF_TREE_MODE
] == 't' ? LINE_TREE_DIR
: LINE_TREE_FILE
;
4404 entry
= tree_entry(view
, type
, path
, text
, text
+ TREE_ID_OFFSET
);
4409 /* Skip "Directory ..." and ".." line. */
4410 for (line
= &view
->line
[1 + !!*opt_path
]; line
< entry
; line
++) {
4411 if (tree_compare_entry(line
, entry
) <= 0)
4414 memmove(line
+ 1, line
, (entry
- line
) * sizeof(*entry
));
4418 for (; line
<= entry
; line
++)
4419 line
->dirty
= line
->cleareol
= 1;
4423 if (tree_lineno
> view
->lineno
) {
4424 view
->lineno
= tree_lineno
;
4432 tree_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
4434 struct tree_entry
*entry
= line
->data
;
4436 if (line
->type
== LINE_TREE_HEAD
) {
4437 if (draw_text(view
, line
->type
, "Directory path /"))
4440 if (draw_mode(view
, entry
->mode
))
4443 if (draw_author(view
, entry
->author
))
4446 if (draw_date(view
, &entry
->time
))
4450 draw_text(view
, line
->type
, entry
->name
);
4455 open_blob_editor(const char *id
)
4457 const char *blob_argv
[] = { "git", "cat-file", "blob", id
, NULL
};
4458 char file
[SIZEOF_STR
] = "/tmp/tigblob.XXXXXX";
4459 int fd
= mkstemp(file
);
4462 report("Failed to create temporary file");
4463 else if (!io_run_append(blob_argv
, fd
))
4464 report("Failed to save blob data to file");
4472 tree_request(struct view
*view
, enum request request
, struct line
*line
)
4474 enum open_flags flags
;
4475 struct tree_entry
*entry
= line
->data
;
4478 case REQ_VIEW_BLAME
:
4479 if (line
->type
!= LINE_TREE_FILE
) {
4480 report("Blame only supported for files");
4484 string_copy(opt_ref
, view
->vid
);
4488 if (line
->type
!= LINE_TREE_FILE
) {
4489 report("Edit only supported for files");
4490 } else if (!is_head_commit(view
->vid
)) {
4491 open_blob_editor(entry
->id
);
4493 open_editor(opt_file
);
4497 case REQ_TOGGLE_SORT_FIELD
:
4498 case REQ_TOGGLE_SORT_ORDER
:
4499 sort_view(view
, request
, &tree_sort_state
, tree_compare
);
4504 /* quit view if at top of tree */
4505 return REQ_VIEW_CLOSE
;
4508 line
= &view
->line
[1];
4518 /* Cleanup the stack if the tree view is at a different tree. */
4519 while (!*opt_path
&& tree_stack
)
4520 pop_tree_stack_entry();
4522 switch (line
->type
) {
4524 /* Depending on whether it is a subdirectory or parent link
4525 * mangle the path buffer. */
4526 if (line
== &view
->line
[1] && *opt_path
) {
4527 pop_tree_stack_entry();
4530 const char *basename
= tree_path(line
);
4532 push_tree_stack_entry(basename
, view
->lineno
);
4535 /* Trees and subtrees share the same ID, so they are not not
4536 * unique like blobs. */
4537 flags
= OPEN_RELOAD
;
4538 request
= REQ_VIEW_TREE
;
4541 case LINE_TREE_FILE
:
4542 flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
4543 request
= REQ_VIEW_BLOB
;
4550 open_view(view
, request
, flags
);
4551 if (request
== REQ_VIEW_TREE
)
4552 view
->lineno
= tree_lineno
;
4558 tree_grep(struct view
*view
, struct line
*line
)
4560 struct tree_entry
*entry
= line
->data
;
4561 const char *text
[] = {
4563 mkauthor(entry
->author
, opt_author_cols
, opt_author
),
4564 mkdate(&entry
->time
, opt_date
),
4568 return grep_text(view
, text
);
4572 tree_select(struct view
*view
, struct line
*line
)
4574 struct tree_entry
*entry
= line
->data
;
4576 if (line
->type
== LINE_TREE_FILE
) {
4577 string_copy_rev(ref_blob
, entry
->id
);
4578 string_format(opt_file
, "%s%s", opt_path
, tree_path(line
));
4580 } else if (line
->type
!= LINE_TREE_DIR
) {
4584 string_copy_rev(view
->ref
, entry
->id
);
4588 tree_open(struct view
*view
, enum open_flags flags
)
4590 static const char *tree_argv
[] = {
4591 "git", "ls-tree", "%(commit)", "%(directory)", NULL
4594 if (view
->lines
== 0 && opt_prefix
[0]) {
4595 char *pos
= opt_prefix
;
4597 while (pos
&& *pos
) {
4598 char *end
= strchr(pos
, '/');
4602 push_tree_stack_entry(pos
, 0);
4610 } else if (strcmp(view
->vid
, view
->id
)) {
4614 return begin_update(view
, opt_cdup
, tree_argv
, flags
);
4617 static struct view_ops tree_ops
= {
4619 sizeof(struct tree_state
),
4629 blob_open(struct view
*view
, enum open_flags flags
)
4631 static const char *blob_argv
[] = {
4632 "git", "cat-file", "blob", "%(blob)", NULL
4635 return begin_update(view
, NULL
, blob_argv
, flags
);
4639 blob_read(struct view
*view
, char *line
)
4643 return add_line_text(view
, line
, LINE_DEFAULT
) != NULL
;
4647 blob_request(struct view
*view
, enum request request
, struct line
*line
)
4651 open_blob_editor(view
->vid
);
4654 return pager_request(view
, request
, line
);
4658 static struct view_ops blob_ops
= {
4672 * Loading the blame view is a two phase job:
4674 * 1. File content is read either using opt_file from the
4675 * filesystem or using git-cat-file.
4676 * 2. Then blame information is incrementally added by
4677 * reading output from git-blame.
4681 struct blame_commit
*commit
;
4682 unsigned long lineno
;
4686 struct blame_state
{
4687 struct blame_commit
*commit
;
4690 bool auto_filename_display
;
4694 blame_detect_filename_display(struct view
*view
)
4696 bool show_filenames
= FALSE
;
4697 const char *filename
= NULL
;
4700 if (opt_blame_argv
) {
4701 for (i
= 0; opt_blame_argv
[i
]; i
++) {
4702 if (prefixcmp(opt_blame_argv
[i
], "-C"))
4705 show_filenames
= TRUE
;
4709 for (i
= 0; i
< view
->lines
; i
++) {
4710 struct blame
*blame
= view
->line
[i
].data
;
4712 if (blame
->commit
&& blame
->commit
->id
[0]) {
4714 filename
= blame
->commit
->filename
;
4715 else if (strcmp(filename
, blame
->commit
->filename
))
4716 show_filenames
= TRUE
;
4720 return show_filenames
;
4724 blame_open(struct view
*view
, enum open_flags flags
)
4726 const char *file_argv
[] = { opt_cdup
, opt_file
, NULL
};
4727 char path
[SIZEOF_STR
];
4730 if (!view
->prev
&& *opt_prefix
) {
4731 string_copy(path
, opt_file
);
4732 if (!string_format(opt_file
, "%s%s", opt_prefix
, path
))
4736 if (*opt_ref
|| !begin_update(view
, opt_cdup
, file_argv
, flags
)) {
4737 const char *blame_cat_file_argv
[] = {
4738 "git", "cat-file", "blob", "%(ref):%(file)", NULL
4741 if (!begin_update(view
, opt_cdup
, blame_cat_file_argv
, flags
))
4745 /* First pass: remove multiple references to the same commit. */
4746 for (i
= 0; i
< view
->lines
; i
++) {
4747 struct blame
*blame
= view
->line
[i
].data
;
4749 if (blame
->commit
&& blame
->commit
->id
[0])
4750 blame
->commit
->id
[0] = 0;
4752 blame
->commit
= NULL
;
4755 /* Second pass: free existing references. */
4756 for (i
= 0; i
< view
->lines
; i
++) {
4757 struct blame
*blame
= view
->line
[i
].data
;
4760 free(blame
->commit
);
4763 string_format(view
->vid
, "%s", opt_file
);
4764 string_format(view
->ref
, "%s ...", opt_file
);
4769 static struct blame_commit
*
4770 get_blame_commit(struct view
*view
, const char *id
)
4774 for (i
= 0; i
< view
->lines
; i
++) {
4775 struct blame
*blame
= view
->line
[i
].data
;
4780 if (!strncmp(blame
->commit
->id
, id
, SIZEOF_REV
- 1))
4781 return blame
->commit
;
4785 struct blame_commit
*commit
= calloc(1, sizeof(*commit
));
4788 string_ncopy(commit
->id
, id
, SIZEOF_REV
);
4793 static struct blame_commit
*
4794 read_blame_commit(struct view
*view
, const char *text
, struct blame_state
*state
)
4796 struct blame_header header
;
4797 struct blame_commit
*commit
;
4798 struct blame
*blame
;
4800 if (!parse_blame_header(&header
, text
, view
->lines
))
4803 commit
= get_blame_commit(view
, text
);
4807 state
->blamed
+= header
.group
;
4808 while (header
.group
--) {
4809 struct line
*line
= &view
->line
[header
.lineno
+ header
.group
- 1];
4812 blame
->commit
= commit
;
4813 blame
->lineno
= header
.orig_lineno
+ header
.group
- 1;
4821 blame_read_file(struct view
*view
, const char *line
, struct blame_state
*state
)
4824 const char *blame_argv
[] = {
4825 "git", "blame", "%(blameargs)", "--incremental",
4826 *opt_ref
? opt_ref
: "--incremental", "--", opt_file
, NULL
4829 if (view
->lines
== 0 && !view
->prev
)
4830 die("No blame exist for %s", view
->vid
);
4832 if (view
->lines
== 0 || !begin_update(view
, opt_cdup
, blame_argv
, OPEN_EXTRA
)) {
4833 report("Failed to load blame data");
4837 if (opt_goto_line
> 0) {
4838 select_view_line(view
, opt_goto_line
);
4842 state
->done_reading
= TRUE
;
4846 size_t linelen
= strlen(line
);
4847 struct blame
*blame
= malloc(sizeof(*blame
) + linelen
);
4852 blame
->commit
= NULL
;
4853 strncpy(blame
->text
, line
, linelen
);
4854 blame
->text
[linelen
] = 0;
4855 return add_line_data(view
, blame
, LINE_BLAME_ID
) != NULL
;
4860 blame_read(struct view
*view
, char *line
)
4862 struct blame_state
*state
= view
->private;
4864 if (!state
->done_reading
)
4865 return blame_read_file(view
, line
, state
);
4868 state
->auto_filename_display
= blame_detect_filename_display(view
);
4869 string_format(view
->ref
, "%s", view
->vid
);
4870 if (view_is_displayed(view
)) {
4871 update_view_title(view
);
4872 redraw_view_from(view
, 0);
4877 if (!state
->commit
) {
4878 state
->commit
= read_blame_commit(view
, line
, state
);
4879 string_format(view
->ref
, "%s %2d%%", view
->vid
,
4880 view
->lines
? state
->blamed
* 100 / view
->lines
: 0);
4882 } else if (parse_blame_info(state
->commit
, line
)) {
4883 state
->commit
= NULL
;
4890 blame_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
4892 struct blame_state
*state
= view
->private;
4893 struct blame
*blame
= line
->data
;
4894 struct time
*time
= NULL
;
4895 const char *id
= NULL
, *author
= NULL
, *filename
= NULL
;
4896 enum line_type id_type
= LINE_BLAME_ID
;
4897 static const enum line_type blame_colors
[] = {
4907 #define BLAME_COLOR(i) \
4908 (blame_colors[(i) % ARRAY_SIZE(blame_colors)])
4910 if (blame
->commit
&& *blame
->commit
->filename
) {
4911 id
= blame
->commit
->id
;
4912 author
= blame
->commit
->author
;
4913 filename
= blame
->commit
->filename
;
4914 time
= &blame
->commit
->time
;
4915 id_type
= BLAME_COLOR((long) blame
->commit
);
4918 if (draw_date(view
, time
))
4921 if (draw_author(view
, author
))
4924 if (draw_filename(view
, filename
, state
->auto_filename_display
))
4927 if (draw_field(view
, id_type
, id
, ID_COLS
, FALSE
))
4930 if (draw_lineno(view
, lineno
))
4933 draw_text(view
, LINE_DEFAULT
, blame
->text
);
4938 check_blame_commit(struct blame
*blame
, bool check_null_id
)
4941 report("Commit data not loaded yet");
4942 else if (check_null_id
&& !strcmp(blame
->commit
->id
, NULL_ID
))
4943 report("No commit exist for the selected line");
4950 setup_blame_parent_line(struct view
*view
, struct blame
*blame
)
4952 char from
[SIZEOF_REF
+ SIZEOF_STR
];
4953 char to
[SIZEOF_REF
+ SIZEOF_STR
];
4954 const char *diff_tree_argv
[] = {
4955 "git", "diff", "--no-textconv", "--no-extdiff", "--no-color",
4956 "-U0", from
, to
, "--", NULL
4959 int parent_lineno
= -1;
4960 int blamed_lineno
= -1;
4963 if (!string_format(from
, "%s:%s", opt_ref
, opt_file
) ||
4964 !string_format(to
, "%s:%s", blame
->commit
->id
, blame
->commit
->filename
) ||
4965 !io_run(&io
, IO_RD
, NULL
, diff_tree_argv
))
4968 while ((line
= io_get(&io
, '\n', TRUE
))) {
4970 char *pos
= strchr(line
, '+');
4972 parent_lineno
= atoi(line
+ 4);
4974 blamed_lineno
= atoi(pos
+ 1);
4976 } else if (*line
== '+' && parent_lineno
!= -1) {
4977 if (blame
->lineno
== blamed_lineno
- 1 &&
4978 !strcmp(blame
->text
, line
+ 1)) {
4979 view
->lineno
= parent_lineno
? parent_lineno
- 1 : 0;
4990 blame_request(struct view
*view
, enum request request
, struct line
*line
)
4992 enum open_flags flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
4993 struct blame
*blame
= line
->data
;
4996 case REQ_VIEW_BLAME
:
4997 if (check_blame_commit(blame
, TRUE
)) {
4998 string_copy(opt_ref
, blame
->commit
->id
);
4999 string_copy(opt_file
, blame
->commit
->filename
);
5001 view
->lineno
= blame
->lineno
;
5007 if (!check_blame_commit(blame
, TRUE
))
5009 if (!*blame
->commit
->parent_id
) {
5010 report("The selected commit has no parents");
5012 string_copy_rev(opt_ref
, blame
->commit
->parent_id
);
5013 string_copy(opt_file
, blame
->commit
->parent_filename
);
5014 setup_blame_parent_line(view
, blame
);
5015 opt_goto_line
= blame
->lineno
;
5021 if (!check_blame_commit(blame
, FALSE
))
5024 if (view_is_displayed(VIEW(REQ_VIEW_DIFF
)) &&
5025 !strcmp(blame
->commit
->id
, VIEW(REQ_VIEW_DIFF
)->ref
))
5028 if (!strcmp(blame
->commit
->id
, NULL_ID
)) {
5029 struct view
*diff
= VIEW(REQ_VIEW_DIFF
);
5030 const char *diff_index_argv
[] = {
5031 "git", "diff-index", "--root", "--patch-with-stat",
5032 "-C", "-M", opt_diff_context_arg
,
5033 "HEAD", "--", view
->vid
, NULL
5036 if (!*blame
->commit
->parent_id
) {
5037 diff_index_argv
[1] = "diff";
5038 diff_index_argv
[2] = "--no-color";
5039 diff_index_argv
[7] = "--";
5040 diff_index_argv
[8] = "/dev/null";
5043 open_argv(view
, diff
, diff_index_argv
, NULL
, flags
);
5045 string_copy_rev(diff
->ref
, NULL_ID
);
5047 open_view(view
, REQ_VIEW_DIFF
, flags
);
5059 blame_grep(struct view
*view
, struct line
*line
)
5061 struct blame
*blame
= line
->data
;
5062 struct blame_commit
*commit
= blame
->commit
;
5063 const char *text
[] = {
5065 commit
? commit
->title
: "",
5066 commit
? commit
->id
: "",
5067 commit
&& opt_author
? commit
->author
: "",
5068 commit
? mkdate(&commit
->time
, opt_date
) : "",
5072 return grep_text(view
, text
);
5076 blame_select(struct view
*view
, struct line
*line
)
5078 struct blame
*blame
= line
->data
;
5079 struct blame_commit
*commit
= blame
->commit
;
5084 if (!strcmp(commit
->id
, NULL_ID
))
5085 string_ncopy(ref_commit
, "HEAD", 4);
5087 string_copy_rev(ref_commit
, commit
->id
);
5090 static struct view_ops blame_ops
= {
5092 sizeof(struct blame_state
),
5106 const char *author
; /* Author of the last commit. */
5107 struct time time
; /* Date of the last activity. */
5108 const struct ref
*ref
; /* Name and commit ID information. */
5111 static const struct ref branch_all
;
5113 static const enum sort_field branch_sort_fields
[] = {
5114 ORDERBY_NAME
, ORDERBY_DATE
, ORDERBY_AUTHOR
5116 static struct sort_state branch_sort_state
= SORT_STATE(branch_sort_fields
);
5118 struct branch_state
{
5119 char id
[SIZEOF_REV
];
5123 branch_compare(const void *l1
, const void *l2
)
5125 const struct branch
*branch1
= ((const struct line
*) l1
)->data
;
5126 const struct branch
*branch2
= ((const struct line
*) l2
)->data
;
5128 if (branch1
->ref
== &branch_all
)
5130 else if (branch2
->ref
== &branch_all
)
5133 switch (get_sort_field(branch_sort_state
)) {
5135 return sort_order(branch_sort_state
, timecmp(&branch1
->time
, &branch2
->time
));
5137 case ORDERBY_AUTHOR
:
5138 return sort_order(branch_sort_state
, strcmp(branch1
->author
, branch2
->author
));
5142 return sort_order(branch_sort_state
, strcmp(branch1
->ref
->name
, branch2
->ref
->name
));
5147 branch_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
5149 struct branch
*branch
= line
->data
;
5150 enum line_type type
= branch
->ref
== &branch_all
? LINE_DEFAULT
: get_line_type_from_ref(branch
->ref
);
5152 if (draw_date(view
, &branch
->time
))
5155 if (draw_author(view
, branch
->author
))
5158 draw_text(view
, type
, branch
->ref
== &branch_all
? "All branches" : branch
->ref
->name
);
5163 branch_request(struct view
*view
, enum request request
, struct line
*line
)
5165 struct branch
*branch
= line
->data
;
5173 case REQ_TOGGLE_SORT_FIELD
:
5174 case REQ_TOGGLE_SORT_ORDER
:
5175 sort_view(view
, request
, &branch_sort_state
, branch_compare
);
5180 const struct ref
*ref
= branch
->ref
;
5181 const char *all_branches_argv
[] = {
5182 "git", "log", "--no-color", "--pretty=raw", "--parents",
5184 ref
== &branch_all
? "--all" : ref
->name
, NULL
5186 struct view
*main_view
= VIEW(REQ_VIEW_MAIN
);
5188 open_argv(view
, main_view
, all_branches_argv
, NULL
, OPEN_SPLIT
);
5191 case REQ_JUMP_COMMIT
:
5195 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
5196 struct branch
*branch
= view
->line
[lineno
].data
;
5198 if (!strncasecmp(branch
->ref
->id
, opt_search
, strlen(opt_search
))) {
5199 select_view_line(view
, lineno
);
5211 branch_read(struct view
*view
, char *line
)
5213 struct branch_state
*state
= view
->private;
5214 struct branch
*reference
;
5220 switch (get_line_type(line
)) {
5222 string_copy_rev(state
->id
, line
+ STRING_SIZE("commit "));
5226 for (i
= 0, reference
= NULL
; i
< view
->lines
; i
++) {
5227 struct branch
*branch
= view
->line
[i
].data
;
5229 if (strcmp(branch
->ref
->id
, state
->id
))
5232 view
->line
[i
].dirty
= TRUE
;
5234 branch
->author
= reference
->author
;
5235 branch
->time
= reference
->time
;
5239 parse_author_line(line
+ STRING_SIZE("author "),
5240 &branch
->author
, &branch
->time
);
5252 branch_open_visitor(void *data
, const struct ref
*ref
)
5254 struct view
*view
= data
;
5255 struct branch
*branch
;
5257 if (ref
->tag
|| ref
->ltag
)
5260 branch
= calloc(1, sizeof(*branch
));
5265 return !!add_line_data(view
, branch
, LINE_DEFAULT
);
5269 branch_open(struct view
*view
, enum open_flags flags
)
5271 const char *branch_log
[] = {
5272 "git", "log", "--no-color", "--pretty=raw",
5273 "--simplify-by-decoration", "--all", NULL
5276 if (!begin_update(view
, NULL
, branch_log
, flags
)) {
5277 report("Failed to load branch data");
5281 branch_open_visitor(view
, &branch_all
);
5282 foreach_ref(branch_open_visitor
, view
);
5283 view
->p_restore
= TRUE
;
5289 branch_grep(struct view
*view
, struct line
*line
)
5291 struct branch
*branch
= line
->data
;
5292 const char *text
[] = {
5294 mkauthor(branch
->author
, opt_author_cols
, opt_author
),
5298 return grep_text(view
, text
);
5302 branch_select(struct view
*view
, struct line
*line
)
5304 struct branch
*branch
= line
->data
;
5306 string_copy_rev(view
->ref
, branch
->ref
->id
);
5307 string_copy_rev(ref_commit
, branch
->ref
->id
);
5308 string_copy_rev(ref_head
, branch
->ref
->id
);
5309 string_copy_rev(ref_branch
, branch
->ref
->name
);
5312 static struct view_ops branch_ops
= {
5314 sizeof(struct branch_state
),
5331 char rev
[SIZEOF_REV
];
5332 char name
[SIZEOF_STR
];
5336 char rev
[SIZEOF_REV
];
5337 char name
[SIZEOF_STR
];
5341 static char status_onbranch
[SIZEOF_STR
];
5342 static struct status stage_status
;
5343 static enum line_type stage_line_type
;
5345 DEFINE_ALLOCATOR(realloc_ints
, int, 32)
5347 /* This should work even for the "On branch" line. */
5349 status_has_none(struct view
*view
, struct line
*line
)
5351 return line
< view
->line
+ view
->lines
&& !line
[1].data
;
5354 /* Get fields from the diff line:
5355 * :100644 100644 06a5d6ae9eca55be2e0e585a152e6b1336f2b20e 0000000000000000000000000000000000000000 M
5358 status_get_diff(struct status
*file
, const char *buf
, size_t bufsize
)
5360 const char *old_mode
= buf
+ 1;
5361 const char *new_mode
= buf
+ 8;
5362 const char *old_rev
= buf
+ 15;
5363 const char *new_rev
= buf
+ 56;
5364 const char *status
= buf
+ 97;
5367 old_mode
[-1] != ':' ||
5368 new_mode
[-1] != ' ' ||
5369 old_rev
[-1] != ' ' ||
5370 new_rev
[-1] != ' ' ||
5374 file
->status
= *status
;
5376 string_copy_rev(file
->old
.rev
, old_rev
);
5377 string_copy_rev(file
->new.rev
, new_rev
);
5379 file
->old
.mode
= strtoul(old_mode
, NULL
, 8);
5380 file
->new.mode
= strtoul(new_mode
, NULL
, 8);
5382 file
->old
.name
[0] = file
->new.name
[0] = 0;
5388 status_run(struct view
*view
, const char *argv
[], char status
, enum line_type type
)
5390 struct status
*unmerged
= NULL
;
5394 if (!io_run(&io
, IO_RD
, opt_cdup
, argv
))
5397 add_line_data(view
, NULL
, type
);
5399 while ((buf
= io_get(&io
, 0, TRUE
))) {
5400 struct status
*file
= unmerged
;
5403 file
= calloc(1, sizeof(*file
));
5404 if (!file
|| !add_line_data(view
, file
, type
))
5408 /* Parse diff info part. */
5410 file
->status
= status
;
5412 string_copy(file
->old
.rev
, NULL_ID
);
5414 } else if (!file
->status
|| file
== unmerged
) {
5415 if (!status_get_diff(file
, buf
, strlen(buf
)))
5418 buf
= io_get(&io
, 0, TRUE
);
5422 /* Collapse all modified entries that follow an
5423 * associated unmerged entry. */
5424 if (unmerged
== file
) {
5425 unmerged
->status
= 'U';
5427 } else if (file
->status
== 'U') {
5432 /* Grab the old name for rename/copy. */
5433 if (!*file
->old
.name
&&
5434 (file
->status
== 'R' || file
->status
== 'C')) {
5435 string_ncopy(file
->old
.name
, buf
, strlen(buf
));
5437 buf
= io_get(&io
, 0, TRUE
);
5442 /* git-ls-files just delivers a NUL separated list of
5443 * file names similar to the second half of the
5444 * git-diff-* output. */
5445 string_ncopy(file
->new.name
, buf
, strlen(buf
));
5446 if (!*file
->old
.name
)
5447 string_copy(file
->old
.name
, file
->new.name
);
5451 if (io_error(&io
)) {
5457 if (!view
->line
[view
->lines
- 1].data
)
5458 add_line_data(view
, NULL
, LINE_STAT_NONE
);
5464 /* Don't show unmerged entries in the staged section. */
5465 static const char *status_diff_index_argv
[] = {
5466 "git", "diff-index", "-z", "--diff-filter=ACDMRTXB",
5467 "--cached", "-M", "HEAD", NULL
5470 static const char *status_diff_files_argv
[] = {
5471 "git", "diff-files", "-z", NULL
5474 static const char *status_list_other_argv
[] = {
5475 "git", "ls-files", "-z", "--others", "--exclude-standard", opt_prefix
, NULL
, NULL
,
5478 static const char *status_list_no_head_argv
[] = {
5479 "git", "ls-files", "-z", "--cached", "--exclude-standard", NULL
5482 static const char *update_index_argv
[] = {
5483 "git", "update-index", "-q", "--unmerged", "--refresh", NULL
5486 /* Restore the previous line number to stay in the context or select a
5487 * line with something that can be updated. */
5489 status_restore(struct view
*view
)
5491 if (view
->p_lineno
>= view
->lines
)
5492 view
->p_lineno
= view
->lines
- 1;
5493 while (view
->p_lineno
< view
->lines
&& !view
->line
[view
->p_lineno
].data
)
5495 while (view
->p_lineno
> 0 && !view
->line
[view
->p_lineno
].data
)
5498 /* If the above fails, always skip the "On branch" line. */
5499 if (view
->p_lineno
< view
->lines
)
5500 view
->lineno
= view
->p_lineno
;
5504 if (view
->lineno
< view
->offset
)
5505 view
->offset
= view
->lineno
;
5506 else if (view
->offset
+ view
->height
<= view
->lineno
)
5507 view
->offset
= view
->lineno
- view
->height
+ 1;
5509 view
->p_restore
= FALSE
;
5513 status_update_onbranch(void)
5515 static const char *paths
[][2] = {
5516 { "rebase-apply/rebasing", "Rebasing" },
5517 { "rebase-apply/applying", "Applying mailbox" },
5518 { "rebase-apply/", "Rebasing mailbox" },
5519 { "rebase-merge/interactive", "Interactive rebase" },
5520 { "rebase-merge/", "Rebase merge" },
5521 { "MERGE_HEAD", "Merging" },
5522 { "BISECT_LOG", "Bisecting" },
5523 { "HEAD", "On branch" },
5525 char buf
[SIZEOF_STR
];
5529 if (is_initial_commit()) {
5530 string_copy(status_onbranch
, "Initial commit");
5534 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
5535 char *head
= opt_head
;
5537 if (!string_format(buf
, "%s/%s", opt_git_dir
, paths
[i
][0]) ||
5538 lstat(buf
, &stat
) < 0)
5544 if (io_open(&io
, "%s/rebase-merge/head-name", opt_git_dir
) &&
5545 io_read_buf(&io
, buf
, sizeof(buf
))) {
5547 if (!prefixcmp(head
, "refs/heads/"))
5548 head
+= STRING_SIZE("refs/heads/");
5552 if (!string_format(status_onbranch
, "%s %s", paths
[i
][1], head
))
5553 string_copy(status_onbranch
, opt_head
);
5557 string_copy(status_onbranch
, "Not currently on any branch");
5560 /* First parse staged info using git-diff-index(1), then parse unstaged
5561 * info using git-diff-files(1), and finally untracked files using
5562 * git-ls-files(1). */
5564 status_open(struct view
*view
, enum open_flags flags
)
5568 add_line_data(view
, NULL
, LINE_STAT_HEAD
);
5569 status_update_onbranch();
5571 io_run_bg(update_index_argv
);
5573 if (is_initial_commit()) {
5574 if (!status_run(view
, status_list_no_head_argv
, 'A', LINE_STAT_STAGED
))
5576 } else if (!status_run(view
, status_diff_index_argv
, 0, LINE_STAT_STAGED
)) {
5580 if (!opt_untracked_dirs_content
)
5581 status_list_other_argv
[ARRAY_SIZE(status_list_other_argv
) - 2] = "--directory";
5583 if (!status_run(view
, status_diff_files_argv
, 0, LINE_STAT_UNSTAGED
) ||
5584 !status_run(view
, status_list_other_argv
, '?', LINE_STAT_UNTRACKED
))
5587 /* Restore the exact position or use the specialized restore
5589 if (!view
->p_restore
)
5590 status_restore(view
);
5595 status_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
5597 struct status
*status
= line
->data
;
5598 enum line_type type
;
5602 switch (line
->type
) {
5603 case LINE_STAT_STAGED
:
5604 type
= LINE_STAT_SECTION
;
5605 text
= "Changes to be committed:";
5608 case LINE_STAT_UNSTAGED
:
5609 type
= LINE_STAT_SECTION
;
5610 text
= "Changed but not updated:";
5613 case LINE_STAT_UNTRACKED
:
5614 type
= LINE_STAT_SECTION
;
5615 text
= "Untracked files:";
5618 case LINE_STAT_NONE
:
5619 type
= LINE_DEFAULT
;
5620 text
= " (no files)";
5623 case LINE_STAT_HEAD
:
5624 type
= LINE_STAT_HEAD
;
5625 text
= status_onbranch
;
5632 static char buf
[] = { '?', ' ', ' ', ' ', 0 };
5634 buf
[0] = status
->status
;
5635 if (draw_text(view
, line
->type
, buf
))
5637 type
= LINE_DEFAULT
;
5638 text
= status
->new.name
;
5641 draw_text(view
, type
, text
);
5646 status_enter(struct view
*view
, struct line
*line
)
5648 struct status
*status
= line
->data
;
5649 enum open_flags flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
5651 if (line
->type
== LINE_STAT_NONE
||
5652 (!status
&& line
[1].type
== LINE_STAT_NONE
)) {
5653 report("No file to diff");
5657 switch (line
->type
) {
5658 case LINE_STAT_STAGED
:
5659 case LINE_STAT_UNSTAGED
:
5662 case LINE_STAT_UNTRACKED
:
5664 report("No file to show");
5668 if (!suffixcmp(status
->new.name
, -1, "/")) {
5669 report("Cannot display a directory");
5674 case LINE_STAT_HEAD
:
5678 die("line type %d not handled in switch", line
->type
);
5682 stage_status
= *status
;
5684 memset(&stage_status
, 0, sizeof(stage_status
));
5687 stage_line_type
= line
->type
;
5689 open_view(view
, REQ_VIEW_STAGE
, flags
);
5694 status_exists(struct view
*view
, struct status
*status
, enum line_type type
)
5696 unsigned long lineno
;
5698 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
5699 struct line
*line
= &view
->line
[lineno
];
5700 struct status
*pos
= line
->data
;
5702 if (line
->type
!= type
)
5704 if (!pos
&& (!status
|| !status
->status
) && line
[1].data
) {
5705 select_view_line(view
, lineno
);
5708 if (pos
&& !strcmp(status
->new.name
, pos
->new.name
)) {
5709 select_view_line(view
, lineno
);
5719 status_update_prepare(struct io
*io
, enum line_type type
)
5721 const char *staged_argv
[] = {
5722 "git", "update-index", "-z", "--index-info", NULL
5724 const char *others_argv
[] = {
5725 "git", "update-index", "-z", "--add", "--remove", "--stdin", NULL
5729 case LINE_STAT_STAGED
:
5730 return io_run(io
, IO_WR
, opt_cdup
, staged_argv
);
5732 case LINE_STAT_UNSTAGED
:
5733 case LINE_STAT_UNTRACKED
:
5734 return io_run(io
, IO_WR
, opt_cdup
, others_argv
);
5737 die("line type %d not handled in switch", type
);
5743 status_update_write(struct io
*io
, struct status
*status
, enum line_type type
)
5746 case LINE_STAT_STAGED
:
5747 return io_printf(io
, "%06o %s\t%s%c", status
->old
.mode
,
5748 status
->old
.rev
, status
->old
.name
, 0);
5750 case LINE_STAT_UNSTAGED
:
5751 case LINE_STAT_UNTRACKED
:
5752 return io_printf(io
, "%s%c", status
->new.name
, 0);
5755 die("line type %d not handled in switch", type
);
5761 status_update_file(struct status
*status
, enum line_type type
)
5766 if (!status_update_prepare(&io
, type
))
5769 result
= status_update_write(&io
, status
, type
);
5770 return io_done(&io
) && result
;
5774 status_update_files(struct view
*view
, struct line
*line
)
5776 char buf
[sizeof(view
->ref
)];
5779 struct line
*pos
= view
->line
+ view
->lines
;
5782 int cursor_y
= -1, cursor_x
= -1;
5784 if (!status_update_prepare(&io
, line
->type
))
5787 for (pos
= line
; pos
< view
->line
+ view
->lines
&& pos
->data
; pos
++)
5790 string_copy(buf
, view
->ref
);
5791 getsyx(cursor_y
, cursor_x
);
5792 for (file
= 0, done
= 5; result
&& file
< files
; line
++, file
++) {
5793 int almost_done
= file
* 100 / files
;
5795 if (almost_done
> done
) {
5797 string_format(view
->ref
, "updating file %u of %u (%d%% done)",
5799 update_view_title(view
);
5800 setsyx(cursor_y
, cursor_x
);
5803 result
= status_update_write(&io
, line
->data
, line
->type
);
5805 string_copy(view
->ref
, buf
);
5807 return io_done(&io
) && result
;
5811 status_update(struct view
*view
)
5813 struct line
*line
= &view
->line
[view
->lineno
];
5815 assert(view
->lines
);
5818 /* This should work even for the "On branch" line. */
5819 if (line
< view
->line
+ view
->lines
&& !line
[1].data
) {
5820 report("Nothing to update");
5824 if (!status_update_files(view
, line
+ 1)) {
5825 report("Failed to update file status");
5829 } else if (!status_update_file(line
->data
, line
->type
)) {
5830 report("Failed to update file status");
5838 status_revert(struct status
*status
, enum line_type type
, bool has_none
)
5840 if (!status
|| type
!= LINE_STAT_UNSTAGED
) {
5841 if (type
== LINE_STAT_STAGED
) {
5842 report("Cannot revert changes to staged files");
5843 } else if (type
== LINE_STAT_UNTRACKED
) {
5844 report("Cannot revert changes to untracked files");
5845 } else if (has_none
) {
5846 report("Nothing to revert");
5848 report("Cannot revert changes to multiple files");
5851 } else if (prompt_yesno("Are you sure you want to revert changes?")) {
5852 char mode
[10] = "100644";
5853 const char *reset_argv
[] = {
5854 "git", "update-index", "--cacheinfo", mode
,
5855 status
->old
.rev
, status
->old
.name
, NULL
5857 const char *checkout_argv
[] = {
5858 "git", "checkout", "--", status
->old
.name
, NULL
5861 if (status
->status
== 'U') {
5862 string_format(mode
, "%5o", status
->old
.mode
);
5864 if (status
->old
.mode
== 0 && status
->new.mode
== 0) {
5865 reset_argv
[2] = "--force-remove";
5866 reset_argv
[3] = status
->old
.name
;
5867 reset_argv
[4] = NULL
;
5870 if (!io_run_fg(reset_argv
, opt_cdup
))
5872 if (status
->old
.mode
== 0 && status
->new.mode
== 0)
5876 return io_run_fg(checkout_argv
, opt_cdup
);
5883 status_request(struct view
*view
, enum request request
, struct line
*line
)
5885 struct status
*status
= line
->data
;
5888 case REQ_STATUS_UPDATE
:
5889 if (!status_update(view
))
5893 case REQ_STATUS_REVERT
:
5894 if (!status_revert(status
, line
->type
, status_has_none(view
, line
)))
5898 case REQ_STATUS_MERGE
:
5899 if (!status
|| status
->status
!= 'U') {
5900 report("Merging only possible for files with unmerged status ('U').");
5903 open_mergetool(status
->new.name
);
5909 if (status
->status
== 'D') {
5910 report("File has been deleted.");
5914 open_editor(status
->new.name
);
5917 case REQ_VIEW_BLAME
:
5923 /* After returning the status view has been split to
5924 * show the stage view. No further reloading is
5926 return status_enter(view
, line
);
5929 /* Simply reload the view. */
5942 status_select(struct view
*view
, struct line
*line
)
5944 struct status
*status
= line
->data
;
5945 char file
[SIZEOF_STR
] = "all files";
5949 if (status
&& !string_format(file
, "'%s'", status
->new.name
))
5952 if (!status
&& line
[1].type
== LINE_STAT_NONE
)
5955 switch (line
->type
) {
5956 case LINE_STAT_STAGED
:
5957 text
= "Press %s to unstage %s for commit";
5960 case LINE_STAT_UNSTAGED
:
5961 text
= "Press %s to stage %s for commit";
5964 case LINE_STAT_UNTRACKED
:
5965 text
= "Press %s to stage %s for addition";
5968 case LINE_STAT_HEAD
:
5969 case LINE_STAT_NONE
:
5970 text
= "Nothing to update";
5974 die("line type %d not handled in switch", line
->type
);
5977 if (status
&& status
->status
== 'U') {
5978 text
= "Press %s to resolve conflict in %s";
5979 key
= get_key(KEYMAP_STATUS
, REQ_STATUS_MERGE
);
5982 key
= get_key(KEYMAP_STATUS
, REQ_STATUS_UPDATE
);
5985 string_format(view
->ref
, text
, key
, file
);
5987 string_copy(opt_file
, status
->new.name
);
5991 status_grep(struct view
*view
, struct line
*line
)
5993 struct status
*status
= line
->data
;
5996 const char buf
[2] = { status
->status
, 0 };
5997 const char *text
[] = { status
->new.name
, buf
, NULL
};
5999 return grep_text(view
, text
);
6005 static struct view_ops status_ops
= {
6017 struct stage_state
{
6018 struct diff_state diff
;
6024 stage_diff_write(struct io
*io
, struct line
*line
, struct line
*end
)
6026 while (line
< end
) {
6027 if (!io_write(io
, line
->data
, strlen(line
->data
)) ||
6028 !io_write(io
, "\n", 1))
6031 if (line
->type
== LINE_DIFF_CHUNK
||
6032 line
->type
== LINE_DIFF_HEADER
)
6040 stage_apply_chunk(struct view
*view
, struct line
*chunk
, struct line
*line
, bool revert
)
6042 const char *apply_argv
[SIZEOF_ARG
] = {
6043 "git", "apply", "--whitespace=nowarn", NULL
6045 struct line
*diff_hdr
;
6049 diff_hdr
= find_prev_line_by_type(view
, chunk
, LINE_DIFF_HEADER
);
6054 apply_argv
[argc
++] = "--cached";
6056 apply_argv
[argc
++] = "--unidiff-zero";
6057 if (revert
|| stage_line_type
== LINE_STAT_STAGED
)
6058 apply_argv
[argc
++] = "-R";
6059 apply_argv
[argc
++] = "-";
6060 apply_argv
[argc
++] = NULL
;
6061 if (!io_run(&io
, IO_WR
, opt_cdup
, apply_argv
))
6066 struct line
*context
= chunk
+ 1;
6067 const char *markers
[] = {
6068 line
->type
== LINE_DIFF_DEL
? "" : ",0",
6069 line
->type
== LINE_DIFF_DEL
? ",0" : "",
6072 parse_chunk_lineno(&lineno
, chunk
->data
, line
->type
== LINE_DIFF_DEL
? '+' : '-');
6074 while (context
< line
) {
6075 if (context
->type
== LINE_DIFF_CHUNK
|| context
->type
== LINE_DIFF_HEADER
) {
6077 } else if (context
->type
!= LINE_DIFF_DEL
&& context
->type
!= LINE_DIFF_ADD
) {
6083 if (!stage_diff_write(&io
, diff_hdr
, chunk
) ||
6084 !io_printf(&io
, "@@ -%d%s +%d%s @@\n",
6085 lineno
, markers
[0], lineno
, markers
[1]) ||
6086 !stage_diff_write(&io
, line
, line
+ 1)) {
6090 if (!stage_diff_write(&io
, diff_hdr
, chunk
) ||
6091 !stage_diff_write(&io
, chunk
, view
->line
+ view
->lines
))
6096 io_run_bg(update_index_argv
);
6098 return chunk
? TRUE
: FALSE
;
6102 stage_update(struct view
*view
, struct line
*line
, bool single
)
6104 struct line
*chunk
= NULL
;
6106 if (!is_initial_commit() && stage_line_type
!= LINE_STAT_UNTRACKED
)
6107 chunk
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
);
6110 if (!stage_apply_chunk(view
, chunk
, single
? line
: NULL
, FALSE
)) {
6111 report("Failed to apply chunk");
6115 } else if (!stage_status
.status
) {
6116 view
= view
->parent
;
6118 for (line
= view
->line
; line
< view
->line
+ view
->lines
; line
++)
6119 if (line
->type
== stage_line_type
)
6122 if (!status_update_files(view
, line
+ 1)) {
6123 report("Failed to update files");
6127 } else if (!status_update_file(&stage_status
, stage_line_type
)) {
6128 report("Failed to update file");
6136 stage_revert(struct view
*view
, struct line
*line
)
6138 struct line
*chunk
= NULL
;
6140 if (!is_initial_commit() && stage_line_type
== LINE_STAT_UNSTAGED
)
6141 chunk
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
);
6144 if (!prompt_yesno("Are you sure you want to revert changes?"))
6147 if (!stage_apply_chunk(view
, chunk
, NULL
, TRUE
)) {
6148 report("Failed to revert chunk");
6154 return status_revert(stage_status
.status
? &stage_status
: NULL
,
6155 stage_line_type
, FALSE
);
6161 stage_next(struct view
*view
, struct line
*line
)
6163 struct stage_state
*state
= view
->private;
6166 if (!state
->chunks
) {
6167 for (line
= view
->line
; line
< view
->line
+ view
->lines
; line
++) {
6168 if (line
->type
!= LINE_DIFF_CHUNK
)
6171 if (!realloc_ints(&state
->chunk
, state
->chunks
, 1)) {
6172 report("Allocation failure");
6176 state
->chunk
[state
->chunks
++] = line
- view
->line
;
6180 for (i
= 0; i
< state
->chunks
; i
++) {
6181 if (state
->chunk
[i
] > view
->lineno
) {
6182 do_scroll_view(view
, state
->chunk
[i
] - view
->lineno
);
6183 report("Chunk %d of %d", i
+ 1, state
->chunks
);
6188 report("No next chunk found");
6192 stage_request(struct view
*view
, enum request request
, struct line
*line
)
6195 case REQ_STATUS_UPDATE
:
6196 if (!stage_update(view
, line
, FALSE
))
6200 case REQ_STATUS_REVERT
:
6201 if (!stage_revert(view
, line
))
6205 case REQ_STAGE_UPDATE_LINE
:
6206 if (line
->type
!= LINE_DIFF_DEL
&& line
->type
!= LINE_DIFF_ADD
) {
6207 report("Please select a change to stage");
6210 if (!stage_update(view
, line
, TRUE
))
6214 case REQ_STAGE_NEXT
:
6215 if (stage_line_type
== LINE_STAT_UNTRACKED
) {
6216 report("File is untracked; press %s to add",
6217 get_key(KEYMAP_STAGE
, REQ_STATUS_UPDATE
));
6220 stage_next(view
, line
);
6224 if (!stage_status
.new.name
[0])
6226 if (stage_status
.status
== 'D') {
6227 report("File has been deleted.");
6231 open_editor(stage_status
.new.name
);
6235 /* Reload everything ... */
6238 case REQ_VIEW_BLAME
:
6239 if (stage_status
.new.name
[0]) {
6240 string_copy(opt_file
, stage_status
.new.name
);
6246 return diff_common_enter(view
, request
, line
);
6248 case REQ_DIFF_CONTEXT_UP
:
6249 case REQ_DIFF_CONTEXT_DOWN
:
6250 if (!update_diff_context(request
))
6258 refresh_view(view
->parent
);
6260 /* Check whether the staged entry still exists, and close the
6261 * stage view if it doesn't. */
6262 if (!status_exists(view
->parent
, &stage_status
, stage_line_type
)) {
6263 status_restore(view
->parent
);
6264 return REQ_VIEW_CLOSE
;
6273 stage_open(struct view
*view
, enum open_flags flags
)
6275 static const char *no_head_diff_argv
[] = {
6276 "git", "diff", "--no-color", "--patch-with-stat",
6277 opt_diff_context_arg
,
6278 "--", "/dev/null", stage_status
.new.name
, NULL
6280 static const char *index_show_argv
[] = {
6281 "git", "diff-index", "--root", "--patch-with-stat", "-C", "-M",
6282 "--cached", opt_diff_context_arg
, "HEAD", "--",
6283 stage_status
.old
.name
, stage_status
.new.name
, NULL
6285 static const char *files_show_argv
[] = {
6286 "git", "diff-files", "--root", "--patch-with-stat",
6287 "-C", "-M", opt_diff_context_arg
, "--",
6288 stage_status
.old
.name
, stage_status
.new.name
, NULL
6290 /* Diffs for unmerged entries are empty when passing the new
6291 * path, so leave out the new path. */
6292 static const char *files_unmerged_argv
[] = {
6293 "git", "diff-files", "--root", "--patch-with-stat",
6294 "-C", "-M", opt_diff_context_arg
, "--",
6295 stage_status
.old
.name
, NULL
6297 static const char *file_argv
[] = { opt_cdup
, stage_status
.new.name
, NULL
};
6298 const char **argv
= NULL
;
6301 switch (stage_line_type
) {
6302 case LINE_STAT_STAGED
:
6303 if (is_initial_commit()) {
6304 argv
= no_head_diff_argv
;
6306 argv
= index_show_argv
;
6308 if (stage_status
.status
)
6309 info
= "Staged changes to %s";
6311 info
= "Staged changes";
6314 case LINE_STAT_UNSTAGED
:
6315 if (stage_status
.status
!= 'U')
6316 argv
= files_show_argv
;
6318 argv
= files_unmerged_argv
;
6319 if (stage_status
.status
)
6320 info
= "Unstaged changes to %s";
6322 info
= "Unstaged changes";
6325 case LINE_STAT_UNTRACKED
:
6326 info
= "Untracked file %s";
6330 case LINE_STAT_HEAD
:
6332 die("line type %d not handled in switch", stage_line_type
);
6335 string_format(view
->ref
, info
, stage_status
.new.name
);
6337 view
->dir
= opt_cdup
;
6338 return argv_copy(&view
->argv
, argv
)
6339 && begin_update(view
, NULL
, NULL
, flags
);
6343 stage_read(struct view
*view
, char *data
)
6345 struct stage_state
*state
= view
->private;
6347 if (data
&& diff_common_read(view
, data
, &state
->diff
))
6350 return pager_read(view
, data
);
6353 static struct view_ops stage_ops
= {
6355 sizeof(struct stage_state
),
6369 static const enum line_type graph_colors
[] = {
6379 static enum line_type
get_graph_color(struct graph_symbol
*symbol
)
6382 return LINE_GRAPH_COMMIT
;
6383 assert(symbol
->color
< ARRAY_SIZE(graph_colors
));
6384 return graph_colors
[symbol
->color
];
6388 draw_graph_utf8(struct view
*view
, struct graph_symbol
*symbol
, enum line_type color
, bool first
)
6390 const char *chars
= graph_symbol_to_utf8(symbol
);
6392 return draw_text(view
, color
, chars
+ !!first
);
6396 draw_graph_ascii(struct view
*view
, struct graph_symbol
*symbol
, enum line_type color
, bool first
)
6398 const char *chars
= graph_symbol_to_ascii(symbol
);
6400 return draw_text(view
, color
, chars
+ !!first
);
6404 draw_graph_chtype(struct view
*view
, struct graph_symbol
*symbol
, enum line_type color
, bool first
)
6406 const chtype
*chars
= graph_symbol_to_chtype(symbol
);
6408 return draw_graphic(view
, color
, chars
+ !!first
, 2 - !!first
, FALSE
);
6411 typedef bool (*draw_graph_fn
)(struct view
*, struct graph_symbol
*, enum line_type
, bool);
6413 static bool draw_graph(struct view
*view
, struct graph_canvas
*canvas
)
6415 static const draw_graph_fn fns
[] = {
6420 draw_graph_fn fn
= fns
[opt_line_graphics
];
6423 for (i
= 0; i
< canvas
->size
; i
++) {
6424 struct graph_symbol
*symbol
= &canvas
->symbols
[i
];
6425 enum line_type color
= get_graph_color(symbol
);
6427 if (fn(view
, symbol
, color
, i
== 0))
6431 return draw_text(view
, LINE_MAIN_REVGRAPH
, " ");
6439 char id
[SIZEOF_REV
]; /* SHA1 ID. */
6440 char title
[128]; /* First line of the commit message. */
6441 const char *author
; /* Author of the commit. */
6442 struct time time
; /* Date from the author ident. */
6443 struct ref_list
*refs
; /* Repository references. */
6444 struct graph_canvas graph
; /* Ancestry chain graphics. */
6448 main_open(struct view
*view
, enum open_flags flags
)
6450 static const char *main_argv
[] = {
6451 "git", "log", "--no-color", "--pretty=raw", "--parents",
6452 "--topo-order", "%(diffargs)", "%(revargs)",
6453 "--", "%(fileargs)", NULL
6456 return begin_update(view
, NULL
, main_argv
, flags
);
6460 main_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
6462 struct commit
*commit
= line
->data
;
6464 if (!commit
->author
)
6467 if (opt_line_number
&& draw_lineno(view
, lineno
))
6470 if (draw_date(view
, &commit
->time
))
6473 if (draw_author(view
, commit
->author
))
6476 if (opt_rev_graph
&& draw_graph(view
, &commit
->graph
))
6479 if (draw_refs(view
, commit
->refs
))
6482 draw_text(view
, LINE_DEFAULT
, commit
->title
);
6486 /* Reads git log --pretty=raw output and parses it into the commit struct. */
6488 main_read(struct view
*view
, char *line
)
6490 struct graph
*graph
= view
->private;
6491 enum line_type type
;
6492 struct commit
*commit
;
6495 if (!view
->lines
&& !view
->prev
)
6496 die("No revisions match the given arguments.");
6497 if (view
->lines
> 0) {
6498 commit
= view
->line
[view
->lines
- 1].data
;
6499 view
->line
[view
->lines
- 1].dirty
= 1;
6500 if (!commit
->author
) {
6510 type
= get_line_type(line
);
6511 if (type
== LINE_COMMIT
) {
6514 commit
= calloc(1, sizeof(struct commit
));
6518 line
+= STRING_SIZE("commit ");
6519 is_boundary
= *line
== '-';
6523 string_copy_rev(commit
->id
, line
);
6524 commit
->refs
= get_ref_list(commit
->id
);
6525 add_line_data(view
, commit
, LINE_MAIN_COMMIT
);
6526 graph_add_commit(graph
, &commit
->graph
, commit
->id
, line
, is_boundary
);
6532 commit
= view
->line
[view
->lines
- 1].data
;
6536 if (!graph
->has_parents
)
6537 graph_add_parent(graph
, line
+ STRING_SIZE("parent "));
6541 parse_author_line(line
+ STRING_SIZE("author "),
6542 &commit
->author
, &commit
->time
);
6543 graph_render_parents(graph
);
6547 /* Fill in the commit title if it has not already been set. */
6548 if (commit
->title
[0])
6551 /* Require titles to start with a non-space character at the
6552 * offset used by git log. */
6553 if (strncmp(line
, " ", 4))
6556 /* Well, if the title starts with a whitespace character,
6557 * try to be forgiving. Otherwise we end up with no title. */
6558 while (isspace(*line
))
6562 /* FIXME: More graceful handling of titles; append "..." to
6563 * shortened titles, etc. */
6565 string_expand(commit
->title
, sizeof(commit
->title
), line
, 1);
6566 view
->line
[view
->lines
- 1].dirty
= 1;
6573 main_request(struct view
*view
, enum request request
, struct line
*line
)
6575 enum open_flags flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
6579 if (view_is_displayed(view
) && display
[0] != view
)
6580 maximize_view(view
, TRUE
);
6581 open_view(view
, REQ_VIEW_DIFF
, flags
);
6588 case REQ_JUMP_COMMIT
:
6592 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
6593 struct commit
*commit
= view
->line
[lineno
].data
;
6595 if (!strncasecmp(commit
->id
, opt_search
, strlen(opt_search
))) {
6596 select_view_line(view
, lineno
);
6602 report("Unable to find commit '%s'", opt_search
);
6613 grep_refs(struct ref_list
*list
, regex_t
*regex
)
6618 if (!opt_show_refs
|| !list
)
6621 for (i
= 0; i
< list
->size
; i
++) {
6622 if (regexec(regex
, list
->refs
[i
]->name
, 1, &pmatch
, 0) != REG_NOMATCH
)
6630 main_grep(struct view
*view
, struct line
*line
)
6632 struct commit
*commit
= line
->data
;
6633 const char *text
[] = {
6635 mkauthor(commit
->author
, opt_author_cols
, opt_author
),
6636 mkdate(&commit
->time
, opt_date
),
6640 return grep_text(view
, text
) || grep_refs(commit
->refs
, view
->regex
);
6644 main_select(struct view
*view
, struct line
*line
)
6646 struct commit
*commit
= line
->data
;
6648 string_copy_rev(view
->ref
, commit
->id
);
6649 string_copy_rev(ref_commit
, view
->ref
);
6652 static struct view_ops main_ops
= {
6654 sizeof(struct graph
),
6668 /* Whether or not the curses interface has been initialized. */
6669 static bool cursed
= FALSE
;
6671 /* Terminal hacks and workarounds. */
6672 static bool use_scroll_redrawwin
;
6673 static bool use_scroll_status_wclear
;
6675 /* The status window is used for polling keystrokes. */
6676 static WINDOW
*status_win
;
6678 /* Reading from the prompt? */
6679 static bool input_mode
= FALSE
;
6681 static bool status_empty
= FALSE
;
6683 /* Update status and title window. */
6685 report(const char *msg
, ...)
6687 struct view
*view
= display
[current_view
];
6693 char buf
[SIZEOF_STR
];
6696 FORMAT_BUFFER(buf
, sizeof(buf
), msg
, retval
);
6697 if (retval
>= sizeof(buf
)) {
6698 buf
[sizeof(buf
) - 1] = 0;
6699 buf
[sizeof(buf
) - 2] = '.';
6700 buf
[sizeof(buf
) - 3] = '.';
6701 buf
[sizeof(buf
) - 4] = '.';
6706 if (!status_empty
|| *msg
) {
6709 va_start(args
, msg
);
6711 wmove(status_win
, 0, 0);
6712 if (view
->has_scrolled
&& use_scroll_status_wclear
)
6715 vwprintw(status_win
, msg
, args
);
6716 status_empty
= FALSE
;
6718 status_empty
= TRUE
;
6720 wclrtoeol(status_win
);
6721 wnoutrefresh(status_win
);
6726 update_view_title(view
);
6735 /* Initialize the curses library */
6736 if (isatty(STDIN_FILENO
)) {
6737 cursed
= !!initscr();
6740 /* Leave stdin and stdout alone when acting as a pager. */
6741 opt_tty
= fopen("/dev/tty", "r+");
6743 die("Failed to open /dev/tty");
6744 cursed
= !!newterm(NULL
, opt_tty
, opt_tty
);
6748 die("Failed to initialize curses");
6750 nonl(); /* Disable conversion and detect newlines from input. */
6751 cbreak(); /* Take input chars one at a time, no wait for \n */
6752 noecho(); /* Don't echo input */
6753 leaveok(stdscr
, FALSE
);
6758 getmaxyx(stdscr
, y
, x
);
6759 status_win
= newwin(1, x
, y
- 1, 0);
6761 die("Failed to create status window");
6763 /* Enable keyboard mapping */
6764 keypad(status_win
, TRUE
);
6765 wbkgdset(status_win
, get_line_attr(LINE_STATUS
));
6767 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
6768 set_tabsize(opt_tab_size
);
6770 TABSIZE
= opt_tab_size
;
6773 term
= getenv("XTERM_VERSION") ? NULL
: getenv("COLORTERM");
6774 if (term
&& !strcmp(term
, "gnome-terminal")) {
6775 /* In the gnome-terminal-emulator, the message from
6776 * scrolling up one line when impossible followed by
6777 * scrolling down one line causes corruption of the
6778 * status line. This is fixed by calling wclear. */
6779 use_scroll_status_wclear
= TRUE
;
6780 use_scroll_redrawwin
= FALSE
;
6782 } else if (term
&& !strcmp(term
, "xrvt-xpm")) {
6783 /* No problems with full optimizations in xrvt-(unicode)
6785 use_scroll_status_wclear
= use_scroll_redrawwin
= FALSE
;
6788 /* When scrolling in (u)xterm the last line in the
6789 * scrolling direction will update slowly. */
6790 use_scroll_redrawwin
= TRUE
;
6791 use_scroll_status_wclear
= FALSE
;
6796 get_input(int prompt_position
)
6799 int i
, key
, cursor_y
, cursor_x
;
6801 if (prompt_position
)
6805 bool loading
= FALSE
;
6807 foreach_view (view
, i
) {
6809 if (view_is_displayed(view
) && view
->has_scrolled
&&
6810 use_scroll_redrawwin
)
6811 redrawwin(view
->win
);
6812 view
->has_scrolled
= FALSE
;
6817 /* Update the cursor position. */
6818 if (prompt_position
) {
6819 getbegyx(status_win
, cursor_y
, cursor_x
);
6820 cursor_x
= prompt_position
;
6822 view
= display
[current_view
];
6823 getbegyx(view
->win
, cursor_y
, cursor_x
);
6824 cursor_x
= view
->width
- 1;
6825 cursor_y
+= view
->lineno
- view
->offset
;
6827 setsyx(cursor_y
, cursor_x
);
6829 /* Refresh, accept single keystroke of input */
6831 nodelay(status_win
, loading
);
6832 key
= wgetch(status_win
);
6834 /* wgetch() with nodelay() enabled returns ERR when
6835 * there's no input. */
6838 } else if (key
== KEY_RESIZE
) {
6841 getmaxyx(stdscr
, height
, width
);
6843 wresize(status_win
, 1, width
);
6844 mvwin(status_win
, height
- 1, 0);
6845 wnoutrefresh(status_win
);
6847 redraw_display(TRUE
);
6851 if (key
== erasechar())
6852 key
= KEY_BACKSPACE
;
6859 prompt_input(const char *prompt
, input_handler handler
, void *data
)
6861 enum input_status status
= INPUT_OK
;
6862 static char buf
[SIZEOF_STR
];
6867 while (status
== INPUT_OK
|| status
== INPUT_SKIP
) {
6870 mvwprintw(status_win
, 0, 0, "%s%.*s", prompt
, pos
, buf
);
6871 wclrtoeol(status_win
);
6873 key
= get_input(pos
+ 1);
6878 status
= pos
? INPUT_STOP
: INPUT_CANCEL
;
6885 status
= INPUT_CANCEL
;
6889 status
= INPUT_CANCEL
;
6893 if (pos
>= sizeof(buf
)) {
6894 report("Input string too long");
6898 status
= handler(data
, buf
, key
);
6899 if (status
== INPUT_OK
)
6900 buf
[pos
++] = (char) key
;
6904 /* Clear the status window */
6905 status_empty
= FALSE
;
6908 if (status
== INPUT_CANCEL
)
6916 static enum input_status
6917 prompt_yesno_handler(void *data
, char *buf
, int c
)
6919 if (c
== 'y' || c
== 'Y')
6921 if (c
== 'n' || c
== 'N')
6922 return INPUT_CANCEL
;
6927 prompt_yesno(const char *prompt
)
6929 char prompt2
[SIZEOF_STR
];
6931 if (!string_format(prompt2
, "%s [Yy/Nn]", prompt
))
6934 return !!prompt_input(prompt2
, prompt_yesno_handler
, NULL
);
6937 static enum input_status
6938 read_prompt_handler(void *data
, char *buf
, int c
)
6940 return isprint(c
) ? INPUT_OK
: INPUT_SKIP
;
6944 read_prompt(const char *prompt
)
6946 return prompt_input(prompt
, read_prompt_handler
, NULL
);
6949 static bool prompt_menu(const char *prompt
, const struct menu_item
*items
, int *selected
)
6951 enum input_status status
= INPUT_OK
;
6954 while (items
[size
].text
)
6957 while (status
== INPUT_OK
) {
6958 const struct menu_item
*item
= &items
[*selected
];
6962 mvwprintw(status_win
, 0, 0, "%s (%d of %d) ",
6963 prompt
, *selected
+ 1, size
);
6965 wprintw(status_win
, "[%c] ", (char) item
->hotkey
);
6966 wprintw(status_win
, "%s", item
->text
);
6967 wclrtoeol(status_win
);
6969 key
= get_input(COLS
- 1);
6974 status
= INPUT_STOP
;
6979 *selected
= *selected
- 1;
6981 *selected
= size
- 1;
6986 *selected
= (*selected
+ 1) % size
;
6990 status
= INPUT_CANCEL
;
6994 for (i
= 0; items
[i
].text
; i
++)
6995 if (items
[i
].hotkey
== key
) {
6997 status
= INPUT_STOP
;
7003 /* Clear the status window */
7004 status_empty
= FALSE
;
7007 return status
!= INPUT_CANCEL
;
7011 * Repository properties
7014 static struct ref
**refs
= NULL
;
7015 static size_t refs_size
= 0;
7016 static struct ref
*refs_head
= NULL
;
7018 static struct ref_list
**ref_lists
= NULL
;
7019 static size_t ref_lists_size
= 0;
7021 DEFINE_ALLOCATOR(realloc_refs
, struct ref
*, 256)
7022 DEFINE_ALLOCATOR(realloc_refs_list
, struct ref
*, 8)
7023 DEFINE_ALLOCATOR(realloc_ref_lists
, struct ref_list
*, 8)
7026 compare_refs(const void *ref1_
, const void *ref2_
)
7028 const struct ref
*ref1
= *(const struct ref
**)ref1_
;
7029 const struct ref
*ref2
= *(const struct ref
**)ref2_
;
7031 if (ref1
->tag
!= ref2
->tag
)
7032 return ref2
->tag
- ref1
->tag
;
7033 if (ref1
->ltag
!= ref2
->ltag
)
7034 return ref2
->ltag
- ref1
->ltag
;
7035 if (ref1
->head
!= ref2
->head
)
7036 return ref2
->head
- ref1
->head
;
7037 if (ref1
->tracked
!= ref2
->tracked
)
7038 return ref2
->tracked
- ref1
->tracked
;
7039 if (ref1
->replace
!= ref2
->replace
)
7040 return ref2
->replace
- ref1
->replace
;
7041 /* Order remotes last. */
7042 if (ref1
->remote
!= ref2
->remote
)
7043 return ref1
->remote
- ref2
->remote
;
7044 return strcmp(ref1
->name
, ref2
->name
);
7048 foreach_ref(bool (*visitor
)(void *data
, const struct ref
*ref
), void *data
)
7052 for (i
= 0; i
< refs_size
; i
++)
7053 if (!visitor(data
, refs
[i
]))
7063 static struct ref_list
*
7064 get_ref_list(const char *id
)
7066 struct ref_list
*list
;
7069 for (i
= 0; i
< ref_lists_size
; i
++)
7070 if (!strcmp(id
, ref_lists
[i
]->id
))
7071 return ref_lists
[i
];
7073 if (!realloc_ref_lists(&ref_lists
, ref_lists_size
, 1))
7075 list
= calloc(1, sizeof(*list
));
7079 for (i
= 0; i
< refs_size
; i
++) {
7080 if (!strcmp(id
, refs
[i
]->id
) &&
7081 realloc_refs_list(&list
->refs
, list
->size
, 1))
7082 list
->refs
[list
->size
++] = refs
[i
];
7090 qsort(list
->refs
, list
->size
, sizeof(*list
->refs
), compare_refs
);
7091 ref_lists
[ref_lists_size
++] = list
;
7096 read_ref(char *id
, size_t idlen
, char *name
, size_t namelen
, void *data
)
7098 struct ref
*ref
= NULL
;
7101 bool remote
= FALSE
;
7102 bool replace
= FALSE
;
7103 bool tracked
= FALSE
;
7105 int from
= 0, to
= refs_size
- 1;
7107 if (!prefixcmp(name
, "refs/tags/")) {
7108 if (!suffixcmp(name
, namelen
, "^{}")) {
7116 namelen
-= STRING_SIZE("refs/tags/");
7117 name
+= STRING_SIZE("refs/tags/");
7119 } else if (!prefixcmp(name
, "refs/remotes/")) {
7121 namelen
-= STRING_SIZE("refs/remotes/");
7122 name
+= STRING_SIZE("refs/remotes/");
7123 tracked
= !strcmp(opt_remote
, name
);
7125 } else if (!prefixcmp(name
, "refs/replace/")) {
7127 id
= name
+ strlen("refs/replace/");
7128 idlen
= namelen
- strlen("refs/replace/");
7130 namelen
= strlen(name
);
7132 } else if (!prefixcmp(name
, "refs/heads/")) {
7133 namelen
-= STRING_SIZE("refs/heads/");
7134 name
+= STRING_SIZE("refs/heads/");
7135 if (strlen(opt_head
) == namelen
7136 && !strncmp(opt_head
, name
, namelen
))
7139 } else if (!strcmp(name
, "HEAD")) {
7142 namelen
= strlen(opt_head
);
7147 /* If we are reloading or it's an annotated tag, replace the
7148 * previous SHA1 with the resolved commit id; relies on the fact
7149 * git-ls-remote lists the commit id of an annotated tag right
7150 * before the commit id it points to. */
7151 while ((from
<= to
) && !replace
) {
7152 size_t pos
= (to
+ from
) / 2;
7153 int cmp
= strcmp(name
, refs
[pos
]->name
);
7167 if (!realloc_refs(&refs
, refs_size
, 1))
7169 ref
= calloc(1, sizeof(*ref
) + namelen
);
7172 memmove(refs
+ from
+ 1, refs
+ from
,
7173 (refs_size
- from
) * sizeof(*refs
));
7175 strncpy(ref
->name
, name
, namelen
);
7182 ref
->remote
= remote
;
7183 ref
->replace
= replace
;
7184 ref
->tracked
= tracked
;
7185 string_copy_rev(ref
->id
, id
);
7195 const char *head_argv
[] = {
7196 "git", "symbolic-ref", "HEAD", NULL
7198 static const char *ls_remote_argv
[SIZEOF_ARG
] = {
7199 "git", "ls-remote", opt_git_dir
, NULL
7201 static bool init
= FALSE
;
7205 if (!argv_from_env(ls_remote_argv
, "TIG_LS_REMOTE"))
7206 die("TIG_LS_REMOTE contains too many arguments");
7213 if (io_run_buf(head_argv
, opt_head
, sizeof(opt_head
)) &&
7214 !prefixcmp(opt_head
, "refs/heads/")) {
7215 char *offset
= opt_head
+ STRING_SIZE("refs/heads/");
7217 memmove(opt_head
, offset
, strlen(offset
) + 1);
7221 for (i
= 0; i
< refs_size
; i
++)
7224 if (io_run_load(ls_remote_argv
, "\t", read_ref
, NULL
) == ERR
)
7227 /* Update the ref lists to reflect changes. */
7228 for (i
= 0; i
< ref_lists_size
; i
++) {
7229 struct ref_list
*list
= ref_lists
[i
];
7232 for (old
= new = 0; old
< list
->size
; old
++)
7233 if (!strcmp(list
->id
, list
->refs
[old
]->id
))
7234 list
->refs
[new++] = list
->refs
[old
];
7238 qsort(refs
, refs_size
, sizeof(*refs
), compare_refs
);
7244 set_remote_branch(const char *name
, const char *value
, size_t valuelen
)
7246 if (!strcmp(name
, ".remote")) {
7247 string_ncopy(opt_remote
, value
, valuelen
);
7249 } else if (*opt_remote
&& !strcmp(name
, ".merge")) {
7250 size_t from
= strlen(opt_remote
);
7252 if (!prefixcmp(value
, "refs/heads/"))
7253 value
+= STRING_SIZE("refs/heads/");
7255 if (!string_format_from(opt_remote
, &from
, "/%s", value
))
7261 set_repo_config_option(char *name
, char *value
, enum option_code (*cmd
)(int, const char **))
7263 const char *argv
[SIZEOF_ARG
] = { name
, "=" };
7264 int argc
= 1 + (cmd
== option_set_command
);
7265 enum option_code error
;
7267 if (!argv_from_string(argv
, &argc
, value
))
7268 error
= OPT_ERR_TOO_MANY_OPTION_ARGUMENTS
;
7270 error
= cmd(argc
, argv
);
7272 if (error
!= OPT_OK
)
7273 warn("Option 'tig.%s': %s", name
, option_errors
[error
]);
7277 set_environment_variable(const char *name
, const char *value
)
7279 size_t len
= strlen(name
) + 1 + strlen(value
) + 1;
7280 char *env
= malloc(len
);
7283 string_nformat(env
, len
, NULL
, "%s=%s", name
, value
) &&
7291 set_work_tree(const char *value
)
7293 char cwd
[SIZEOF_STR
];
7295 if (!getcwd(cwd
, sizeof(cwd
)))
7296 die("Failed to get cwd path: %s", strerror(errno
));
7297 if (chdir(opt_git_dir
) < 0)
7298 die("Failed to chdir(%s): %s", strerror(errno
));
7299 if (!getcwd(opt_git_dir
, sizeof(opt_git_dir
)))
7300 die("Failed to get git path: %s", strerror(errno
));
7302 die("Failed to chdir(%s): %s", cwd
, strerror(errno
));
7303 if (chdir(value
) < 0)
7304 die("Failed to chdir(%s): %s", value
, strerror(errno
));
7305 if (!getcwd(cwd
, sizeof(cwd
)))
7306 die("Failed to get cwd path: %s", strerror(errno
));
7307 if (!set_environment_variable("GIT_WORK_TREE", cwd
))
7308 die("Failed to set GIT_WORK_TREE to '%s'", cwd
);
7309 if (!set_environment_variable("GIT_DIR", opt_git_dir
))
7310 die("Failed to set GIT_DIR to '%s'", opt_git_dir
);
7311 opt_is_inside_work_tree
= TRUE
;
7315 read_repo_config_option(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
7317 if (!strcmp(name
, "i18n.commitencoding"))
7318 string_ncopy(opt_encoding
, value
, valuelen
);
7320 else if (!strcmp(name
, "core.editor"))
7321 string_ncopy(opt_editor
, value
, valuelen
);
7323 else if (!strcmp(name
, "core.worktree"))
7324 set_work_tree(value
);
7326 else if (!prefixcmp(name
, "tig.color."))
7327 set_repo_config_option(name
+ 10, value
, option_color_command
);
7329 else if (!prefixcmp(name
, "tig.bind."))
7330 set_repo_config_option(name
+ 9, value
, option_bind_command
);
7332 else if (!prefixcmp(name
, "tig."))
7333 set_repo_config_option(name
+ 4, value
, option_set_command
);
7335 else if (*opt_head
&& !prefixcmp(name
, "branch.") &&
7336 !strncmp(name
+ 7, opt_head
, strlen(opt_head
)))
7337 set_remote_branch(name
+ 7 + strlen(opt_head
), value
, valuelen
);
7343 load_git_config(void)
7345 const char *config_list_argv
[] = { "git", "config", "--list", NULL
};
7347 return io_run_load(config_list_argv
, "=", read_repo_config_option
, NULL
);
7351 read_repo_info(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
7353 if (!opt_git_dir
[0]) {
7354 string_ncopy(opt_git_dir
, name
, namelen
);
7356 } else if (opt_is_inside_work_tree
== -1) {
7357 /* This can be 3 different values depending on the
7358 * version of git being used. If git-rev-parse does not
7359 * understand --is-inside-work-tree it will simply echo
7360 * the option else either "true" or "false" is printed.
7361 * Default to true for the unknown case. */
7362 opt_is_inside_work_tree
= strcmp(name
, "false") ? TRUE
: FALSE
;
7364 } else if (*name
== '.') {
7365 string_ncopy(opt_cdup
, name
, namelen
);
7368 string_ncopy(opt_prefix
, name
, namelen
);
7375 load_repo_info(void)
7377 const char *rev_parse_argv
[] = {
7378 "git", "rev-parse", "--git-dir", "--is-inside-work-tree",
7379 "--show-cdup", "--show-prefix", NULL
7382 return io_run_load(rev_parse_argv
, "=", read_repo_info
, NULL
);
7390 static const char usage
[] =
7391 "tig " TIG_VERSION
" (" __DATE__
")\n"
7393 "Usage: tig [options] [revs] [--] [paths]\n"
7394 " or: tig show [options] [revs] [--] [paths]\n"
7395 " or: tig blame [options] [rev] [--] path\n"
7397 " or: tig < [git command output]\n"
7400 " +<number> Select line <number> in the first view\n"
7401 " -v, --version Show version and exit\n"
7402 " -h, --help Show help message and exit";
7404 static void __NORETURN
7407 /* XXX: Restore tty modes and let the OS cleanup the rest! */
7413 static void __NORETURN
7414 die(const char *err
, ...)
7420 va_start(args
, err
);
7421 fputs("tig: ", stderr
);
7422 vfprintf(stderr
, err
, args
);
7423 fputs("\n", stderr
);
7430 warn(const char *msg
, ...)
7434 va_start(args
, msg
);
7435 fputs("tig warning: ", stderr
);
7436 vfprintf(stderr
, msg
, args
);
7437 fputs("\n", stderr
);
7442 read_filter_args(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
7444 const char ***filter_args
= data
;
7446 return argv_append(filter_args
, name
) ? OK
: ERR
;
7450 filter_rev_parse(const char ***args
, const char *arg1
, const char *arg2
, const char *argv
[])
7452 const char *rev_parse_argv
[SIZEOF_ARG
] = { "git", "rev-parse", arg1
, arg2
};
7453 const char **all_argv
= NULL
;
7455 if (!argv_append_array(&all_argv
, rev_parse_argv
) ||
7456 !argv_append_array(&all_argv
, argv
) ||
7457 !io_run_load(all_argv
, "\n", read_filter_args
, args
) == ERR
)
7458 die("Failed to split arguments");
7459 argv_free(all_argv
);
7464 filter_options(const char *argv
[], bool blame
)
7466 filter_rev_parse(&opt_file_argv
, "--no-revs", "--no-flags", argv
);
7469 filter_rev_parse(&opt_blame_argv
, "--no-revs", "--flags", argv
);
7471 filter_rev_parse(&opt_diff_argv
, "--no-revs", "--flags", argv
);
7473 filter_rev_parse(&opt_rev_argv
, "--symbolic", "--revs-only", argv
);
7477 parse_options(int argc
, const char *argv
[])
7479 enum request request
= REQ_VIEW_MAIN
;
7480 const char *subcommand
;
7481 bool seen_dashdash
= FALSE
;
7482 const char **filter_argv
= NULL
;
7485 if (!isatty(STDIN_FILENO
))
7486 return REQ_VIEW_PAGER
;
7489 return REQ_VIEW_MAIN
;
7491 subcommand
= argv
[1];
7492 if (!strcmp(subcommand
, "status")) {
7494 warn("ignoring arguments after `%s'", subcommand
);
7495 return REQ_VIEW_STATUS
;
7497 } else if (!strcmp(subcommand
, "blame")) {
7498 request
= REQ_VIEW_BLAME
;
7500 } else if (!strcmp(subcommand
, "show")) {
7501 request
= REQ_VIEW_DIFF
;
7507 for (i
= 1 + !!subcommand
; i
< argc
; i
++) {
7508 const char *opt
= argv
[i
];
7510 // stop parsing our options after -- and let rev-parse handle the rest
7511 if (!seen_dashdash
) {
7512 if (!strcmp(opt
, "--")) {
7513 seen_dashdash
= TRUE
;
7516 } else if (!strcmp(opt
, "-v") || !strcmp(opt
, "--version")) {
7517 printf("tig version %s\n", TIG_VERSION
);
7520 } else if (!strcmp(opt
, "-h") || !strcmp(opt
, "--help")) {
7521 printf("%s\n", usage
);
7524 } else if (strlen(opt
) >= 2 && *opt
== '+' && string_isnumber(opt
+ 1)) {
7525 opt_lineno
= atoi(opt
+ 1);
7531 if (!argv_append(&filter_argv
, opt
))
7532 die("command too long");
7536 filter_options(filter_argv
, request
== REQ_VIEW_BLAME
);
7538 /* Finish validating and setting up blame options */
7539 if (request
== REQ_VIEW_BLAME
) {
7540 if (!opt_file_argv
|| opt_file_argv
[1] || (opt_rev_argv
&& opt_rev_argv
[1]))
7541 die("invalid number of options to blame\n\n%s", usage
);
7544 string_ncopy(opt_ref
, opt_rev_argv
[0], strlen(opt_rev_argv
[0]));
7547 string_ncopy(opt_file
, opt_file_argv
[0], strlen(opt_file_argv
[0]));
7554 main(int argc
, const char *argv
[])
7556 const char *codeset
= ENCODING_UTF8
;
7557 enum request request
= parse_options(argc
, argv
);
7560 signal(SIGINT
, quit
);
7561 signal(SIGPIPE
, SIG_IGN
);
7563 if (setlocale(LC_ALL
, "")) {
7564 codeset
= nl_langinfo(CODESET
);
7567 if (load_repo_info() == ERR
)
7568 die("Failed to load repo info.");
7570 if (load_options() == ERR
)
7571 die("Failed to load user config.");
7573 if (load_git_config() == ERR
)
7574 die("Failed to load repo config.");
7576 /* Require a git repository unless when running in pager mode. */
7577 if (!opt_git_dir
[0] && request
!= REQ_VIEW_PAGER
)
7578 die("Not a git repository");
7580 if (*opt_encoding
&& strcmp(codeset
, ENCODING_UTF8
)) {
7581 opt_iconv_in
= iconv_open(ENCODING_UTF8
, opt_encoding
);
7582 if (opt_iconv_in
== ICONV_NONE
)
7583 die("Failed to initialize character set conversion");
7586 if (codeset
&& strcmp(codeset
, ENCODING_UTF8
)) {
7587 char translit
[SIZEOF_STR
];
7589 if (string_format(translit
, "%s%s", codeset
, ICONV_TRANSLIT
))
7590 opt_iconv_out
= iconv_open(translit
, ENCODING_UTF8
);
7592 opt_iconv_out
= iconv_open(codeset
, ENCODING_UTF8
);
7593 if (opt_iconv_out
== ICONV_NONE
)
7594 die("Failed to initialize character set conversion");
7597 if (load_refs() == ERR
)
7598 die("Failed to load refs.");
7602 while (view_driver(display
[current_view
], request
)) {
7603 int key
= get_input(0);
7605 view
= display
[current_view
];
7606 request
= get_keybinding(view
->keymap
, key
);
7608 /* Some low-level request handling. This keeps access to
7609 * status_win restricted. */
7612 report("Unknown key, press %s for help",
7613 get_key(view
->keymap
, REQ_VIEW_HELP
));
7617 char *cmd
= read_prompt(":");
7619 if (cmd
&& string_isnumber(cmd
)) {
7620 int lineno
= view
->lineno
+ 1;
7622 if (parse_int(&lineno
, cmd
, 1, view
->lines
+ 1) == OPT_OK
) {
7623 select_view_line(view
, lineno
- 1);
7626 report("Unable to parse '%s' as a line number", cmd
);
7628 } else if (cmd
&& iscommit(cmd
)) {
7629 string_ncopy(opt_search
, cmd
, strlen(cmd
));
7631 request
= view_request(view
, REQ_JUMP_COMMIT
);
7632 if (request
== REQ_JUMP_COMMIT
) {
7633 report("Jumping to commits is not supported by the '%s' view", view
->name
);
7637 struct view
*next
= VIEW(REQ_VIEW_PAGER
);
7638 const char *argv
[SIZEOF_ARG
] = { "git" };
7641 /* When running random commands, initially show the
7642 * command in the title. However, it maybe later be
7643 * overwritten if a commit line is selected. */
7644 string_ncopy(next
->ref
, cmd
, strlen(cmd
));
7646 if (!argv_from_string(argv
, &argc
, cmd
)) {
7647 report("Too many arguments");
7648 } else if (!format_argv(&next
->argv
, argv
, FALSE
)) {
7649 report("Argument formatting failed");
7652 open_view(view
, REQ_VIEW_PAGER
, OPEN_PREPARED
);
7660 case REQ_SEARCH_BACK
:
7662 const char *prompt
= request
== REQ_SEARCH
? "/" : "?";
7663 char *search
= read_prompt(prompt
);
7666 string_ncopy(opt_search
, search
, strlen(search
));
7667 else if (*opt_search
)
7668 request
= request
== REQ_SEARCH
?