view: replace line user_flags with no_commit_refs and commit_title flags
[tig.git] / src / main.c
blob9cd14778e69bcf13ba9e1a284bd3b3c51f2de5a2
1 /* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
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.
14 #include "tig/repo.h"
15 #include "tig/options.h"
16 #include "tig/parse.h"
17 #include "tig/graph.h"
18 #include "tig/display.h"
19 #include "tig/view.h"
20 #include "tig/draw.h"
21 #include "tig/git.h"
22 #include "tig/status.h"
23 #include "tig/main.h"
24 #include "tig/diff.h"
27 * Main view backend
30 DEFINE_ALLOCATOR(realloc_reflogs, char *, 32)
32 static void
33 main_register_commit(struct view *view, struct commit *commit, const char *ids, bool is_boundary)
35 struct main_state *state = view->private;
37 string_copy_rev(commit->id, ids);
38 if (state->with_graph)
39 graph_add_commit(&state->graph, &commit->graph, commit->id, ids, is_boundary);
42 static struct commit *
43 main_add_commit(struct view *view, enum line_type type, struct commit *template,
44 const char *title, bool custom)
46 struct main_state *state = view->private;
47 size_t titlelen = strlen(title);
48 struct commit *commit;
49 char buf[SIZEOF_STR / 2];
50 struct line *line;
52 /* FIXME: More graceful handling of titles; append "..." to
53 * shortened titles, etc. */
54 string_expand(buf, sizeof(buf), title, 1);
55 title = buf;
56 titlelen = strlen(title);
58 line = add_line_alloc(view, &commit, type, titlelen, custom);
59 if (!line)
60 return NULL;
62 *commit = *template;
63 strncpy(commit->title, title, titlelen);
64 state->graph.canvas = &commit->graph;
65 memset(template, 0, sizeof(*template));
66 state->reflogmsg[0] = 0;
68 view_column_info_update(view, line);
69 return commit;
72 static inline void
73 main_flush_commit(struct view *view, struct commit *commit)
75 if (*commit->id)
76 main_add_commit(view, LINE_MAIN_COMMIT, commit, "", FALSE);
79 static bool
80 main_has_changes(const char *argv[])
82 struct io io;
84 if (!io_exec(&io, IO_BG, NULL, opt_env, argv, -1))
85 return FALSE;
86 io_done(&io);
87 return io.status == 1;
90 static void
91 main_add_changes_commit(struct view *view, enum line_type type, const char *parent, const char *title)
93 char ids[SIZEOF_STR] = NULL_ID " ";
94 struct main_state *state = view->private;
95 struct commit commit = {};
96 struct timeval now;
97 struct timezone tz;
99 if (!parent)
100 return;
102 string_copy_rev(ids + STRING_SIZE(NULL_ID " "), parent);
104 if (!gettimeofday(&now, &tz)) {
105 commit.time.tz = tz.tz_minuteswest * 60;
106 commit.time.sec = now.tv_sec - commit.time.tz;
109 commit.author = &unknown_ident;
110 main_register_commit(view, &commit, ids, FALSE);
111 if (main_add_commit(view, type, &commit, title, TRUE) && state->with_graph)
112 graph_render_parents(&state->graph);
115 static void
116 main_add_changes_commits(struct view *view, struct main_state *state, const char *parent)
118 const char *staged_argv[] = { GIT_DIFF_STAGED_FILES("--quiet") };
119 const char *unstaged_argv[] = { GIT_DIFF_UNSTAGED_FILES("--quiet") };
120 const char *staged_parent = NULL_ID;
121 const char *unstaged_parent = parent;
123 if (!is_head_commit(parent))
124 return;
126 state->added_changes_commits = TRUE;
128 io_run_bg(update_index_argv);
130 if (!main_has_changes(unstaged_argv)) {
131 unstaged_parent = NULL;
132 staged_parent = parent;
135 if (!main_has_changes(staged_argv)) {
136 staged_parent = NULL;
139 main_add_changes_commit(view, LINE_STAT_STAGED, staged_parent, "Staged changes");
140 main_add_changes_commit(view, LINE_STAT_UNSTAGED, unstaged_parent, "Unstaged changes");
143 static bool
144 main_check_argv(struct view *view, const char *argv[])
146 struct main_state *state = view->private;
147 bool with_reflog = FALSE;
148 int i;
150 for (i = 0; argv[i]; i++) {
151 const char *arg = argv[i];
152 struct rev_flags rev_flags = {};
154 if (!argv_parse_rev_flag(arg, &rev_flags))
155 continue;
157 if (rev_flags.with_reflog)
158 with_reflog = TRUE;
159 if (!rev_flags.with_graph)
160 state->with_graph = FALSE;
161 arg += rev_flags.search_offset;
162 if (*arg && !*view->env->search)
163 string_ncopy(view->env->search, arg, strlen(arg));
166 return with_reflog;
169 static const enum view_column_type main_columns[] = {
170 VIEW_COLUMN_LINE_NUMBER,
171 VIEW_COLUMN_ID,
172 VIEW_COLUMN_DATE,
173 VIEW_COLUMN_AUTHOR,
174 VIEW_COLUMN_COMMIT_TITLE,
177 static bool
178 main_open(struct view *view, enum open_flags flags)
180 const char *pretty_custom_argv[] = {
181 GIT_MAIN_LOG_CUSTOM(encoding_arg, commit_order_arg(), "%(cmdlineargs)", "%(revargs)", "%(fileargs)")
183 const char *pretty_raw_argv[] = {
184 GIT_MAIN_LOG_RAW(encoding_arg, commit_order_arg(), "%(cmdlineargs)", "%(revargs)", "%(fileargs)")
186 struct main_state *state = view->private;
187 const char **main_argv = pretty_custom_argv;
188 struct view_column *column;
190 view_column_init(view, main_columns, ARRAY_SIZE(main_columns));
192 column = get_view_column(view, VIEW_COLUMN_COMMIT_TITLE);
193 state->with_graph = column && column->opt.commit_title.graph &&
194 opt_commit_order != COMMIT_ORDER_REVERSE;
196 if (opt_rev_argv && main_check_argv(view, opt_rev_argv))
197 main_argv = pretty_raw_argv;
199 if (open_in_pager_mode(flags)) {
200 state->added_changes_commits = TRUE;
201 state->with_graph = FALSE;
204 return begin_update(view, NULL, main_argv, flags);
207 void
208 main_done(struct view *view)
210 struct main_state *state = view->private;
211 int i;
213 for (i = 0; i < view->lines; i++) {
214 struct commit *commit = view->line[i].data;
216 free(commit->graph.symbols);
219 for (i = 0; i < state->reflogs; i++)
220 free(state->reflog[i]);
221 free(state->reflog);
224 #define main_check_commit_refs(line) !((line)->no_commit_refs)
225 #define main_mark_no_commit_refs(line) (((struct line *) (line))->no_commit_refs = 1)
227 static inline struct ref_list *
228 main_get_commit_refs(const struct line *line, struct commit *commit)
230 struct ref_list *refs = NULL;
232 if (main_check_commit_refs(line) && !(refs = get_ref_list(commit->id)))
233 main_mark_no_commit_refs(line);
235 return refs;
238 bool
239 main_get_column_data(struct view *view, const struct line *line, struct view_column_data *column_data)
241 struct main_state *state = view->private;
242 struct commit *commit = line->data;
243 struct ref_list *refs = NULL;
245 column_data->author = commit->author;
246 column_data->date = &commit->time;
247 if (state->reflogs)
248 column_data->id = state->reflog[line->lineno - 1];
249 else
250 column_data->id = commit->id;
252 column_data->commit_title = commit->title;
253 if (state->with_graph)
254 column_data->graph = &commit->graph;
256 if ((refs = main_get_commit_refs(line, commit)))
257 column_data->refs = refs;
259 return TRUE;
262 static bool
263 main_add_reflog(struct view *view, struct main_state *state, char *reflog)
265 char *end = strchr(reflog, ' ');
266 int id_width;
268 if (!end)
269 return FALSE;
270 *end = 0;
272 if (!realloc_reflogs(&state->reflog, state->reflogs, 1)
273 || !(reflog = strdup(reflog)))
274 return FALSE;
276 state->reflog[state->reflogs++] = reflog;
277 id_width = strlen(reflog);
278 if (state->reflog_width < id_width) {
279 state->reflog_width = id_width;
280 if (opt_show_id)
281 view->force_redraw = TRUE;
284 return TRUE;
287 /* Reads git log --pretty=raw output and parses it into the commit struct. */
288 bool
289 main_read(struct view *view, char *line)
291 struct main_state *state = view->private;
292 struct graph *graph = &state->graph;
293 enum line_type type;
294 struct commit *commit = &state->current;
296 if (!line) {
297 main_flush_commit(view, commit);
299 if (failed_to_load_initial_view(view))
300 die("No revisions match the given arguments.");
301 if (view->lines > 0) {
302 struct commit *last = view->line[view->lines - 1].data;
304 view->line[view->lines - 1].dirty = 1;
305 if (!last->author) {
306 view->lines--;
307 free(last);
311 if (state->with_graph)
312 done_graph(graph);
313 return TRUE;
316 type = get_line_type(line);
317 if (type == LINE_COMMIT) {
318 bool is_boundary;
319 char *author;
321 state->in_header = TRUE;
322 line += STRING_SIZE("commit ");
323 is_boundary = *line == '-';
324 while (*line && !isalnum(*line))
325 line++;
327 if (!state->added_changes_commits && opt_show_changes && repo.is_inside_work_tree)
328 main_add_changes_commits(view, state, line);
329 else
330 main_flush_commit(view, commit);
332 main_register_commit(view, &state->current, line, is_boundary);
334 author = io_memchr(&view->io, line, 0);
335 if (author) {
336 char *title = io_memchr(&view->io, author, 0);
338 parse_author_line(author, &commit->author, &commit->time);
339 if (state->with_graph)
340 graph_render_parents(graph);
341 if (title)
342 main_add_commit(view, LINE_MAIN_COMMIT, commit, title, FALSE);
345 return TRUE;
348 if (!*commit->id)
349 return TRUE;
351 /* Empty line separates the commit header from the log itself. */
352 if (*line == '\0')
353 state->in_header = FALSE;
355 switch (type) {
356 case LINE_PP_REFLOG:
357 if (!main_add_reflog(view, state, line + STRING_SIZE("Reflog: ")))
358 return FALSE;
359 break;
361 case LINE_PP_REFLOGMSG:
362 line += STRING_SIZE("Reflog message: ");
363 string_ncopy(state->reflogmsg, line, strlen(line));
364 break;
366 case LINE_PARENT:
367 if (state->with_graph && !graph->has_parents)
368 graph_add_parent(graph, line + STRING_SIZE("parent "));
369 break;
371 case LINE_AUTHOR:
372 parse_author_line(line + STRING_SIZE("author "),
373 &commit->author, &commit->time);
374 if (state->with_graph)
375 graph_render_parents(graph);
376 break;
378 default:
379 /* Fill in the commit title if it has not already been set. */
380 if (*commit->title)
381 break;
383 /* Skip lines in the commit header. */
384 if (state->in_header)
385 break;
387 /* Require titles to start with a non-space character at the
388 * offset used by git log. */
389 if (strncmp(line, " ", 4))
390 break;
391 line += 4;
392 /* Well, if the title starts with a whitespace character,
393 * try to be forgiving. Otherwise we end up with no title. */
394 while (isspace(*line))
395 line++;
396 if (*line == '\0')
397 break;
398 if (*state->reflogmsg)
399 line = state->reflogmsg;
400 main_add_commit(view, LINE_MAIN_COMMIT, commit, line, FALSE);
403 return TRUE;
406 enum request
407 main_request(struct view *view, enum request request, struct line *line)
409 enum open_flags flags = (view_is_displayed(view) && request != REQ_VIEW_DIFF)
410 ? OPEN_SPLIT : OPEN_DEFAULT;
412 switch (request) {
413 case REQ_NEXT:
414 case REQ_PREVIOUS:
415 if (view_is_displayed(view) && display[0] != view)
416 return request;
417 /* Do not pass navigation requests to the branch view
418 * when the main view is maximized. (GH #38) */
419 return request == REQ_NEXT ? REQ_MOVE_DOWN : REQ_MOVE_UP;
421 case REQ_VIEW_DIFF:
422 case REQ_ENTER:
423 if (view_is_displayed(view) && display[0] != view)
424 maximize_view(view, TRUE);
426 if (line->type == LINE_STAT_UNSTAGED
427 || line->type == LINE_STAT_STAGED) {
428 struct view *diff = &diff_view;
429 const char *diff_staged_argv[] = {
430 GIT_DIFF_STAGED(encoding_arg,
431 diff_context_arg(),
432 ignore_space_arg(), NULL, NULL)
434 const char *diff_unstaged_argv[] = {
435 GIT_DIFF_UNSTAGED(encoding_arg,
436 diff_context_arg(),
437 ignore_space_arg(), NULL, NULL)
439 const char **diff_argv = line->type == LINE_STAT_STAGED
440 ? diff_staged_argv : diff_unstaged_argv;
442 open_argv(view, diff, diff_argv, NULL, flags);
443 break;
446 open_diff_view(view, flags);
447 break;
449 case REQ_REFRESH:
450 load_refs(TRUE);
451 refresh_view(view);
452 break;
454 case REQ_JUMP_COMMIT:
456 int lineno;
458 for (lineno = 0; lineno < view->lines; lineno++) {
459 struct commit *commit = view->line[lineno].data;
461 if (!strncasecmp(commit->id, view->env->search, strlen(view->env->search))) {
462 select_view_line(view, lineno);
463 report_clear();
464 return REQ_NONE;
468 report("Unable to find commit '%s'", view->env->search);
469 break;
471 default:
472 return request;
475 return REQ_NONE;
478 static struct ref *
479 main_get_commit_branch(struct line *line, struct commit *commit)
481 struct ref_list *list = main_get_commit_refs(line, commit);
482 struct ref *branch = NULL;
483 size_t i;
485 for (i = 0; list && i < list->size; i++) {
486 struct ref *ref = list->refs[i];
488 switch (get_line_type_from_ref(ref)) {
489 case LINE_MAIN_HEAD:
490 case LINE_MAIN_REF:
491 /* Always prefer local branches. */
492 return ref;
494 default:
495 branch = ref;
499 return branch;
502 void
503 main_select(struct view *view, struct line *line)
505 struct commit *commit = line->data;
507 if (line->type == LINE_STAT_STAGED || line->type == LINE_STAT_UNSTAGED) {
508 string_ncopy(view->ref, commit->title, strlen(commit->title));
509 } else {
510 struct ref *branch = main_get_commit_branch(line, commit);
512 if (branch)
513 string_copy_rev(view->env->branch, branch->name);
514 string_copy_rev(view->ref, commit->id);
516 string_copy_rev(view->env->commit, commit->id);
519 static struct view_ops main_ops = {
520 "commit",
521 argv_env.head,
522 VIEW_SEND_CHILD_ENTER | VIEW_FILE_FILTER | VIEW_LOG_LIKE | VIEW_REFRESH,
523 sizeof(struct main_state),
524 main_open,
525 main_read,
526 view_column_draw,
527 main_request,
528 view_column_grep,
529 main_select,
530 main_done,
531 main_get_column_data,
534 DEFINE_VIEW(main);
536 /* vim: set ts=8 sw=8 noexpandtab: */