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.
15 #include "tig/options.h"
16 #include "tig/parse.h"
17 #include "tig/watch.h"
18 #include "tig/graph.h"
19 #include "tig/display.h"
23 #include "tig/status.h"
24 #include "tig/stage.h"
32 DEFINE_ALLOCATOR(realloc_reflogs
, char *, 32)
35 main_register_commit(struct view
*view
, struct commit
*commit
, const char *ids
, bool is_boundary
)
37 struct main_state
*state
= view
->private;
39 string_copy_rev(commit
->id
, ids
);
40 if (state
->with_graph
)
41 graph_add_commit(state
->graph
, &commit
->graph
, commit
->id
, ids
, is_boundary
);
44 static struct commit
*
45 main_add_commit(struct view
*view
, enum line_type type
, struct commit
*template,
46 const char *title
, bool custom
)
48 struct main_state
*state
= view
->private;
50 struct commit
*commit
;
51 char buf
[SIZEOF_STR
/ 2];
54 /* FIXME: More graceful handling of titles; append "..." to
55 * shortened titles, etc. */
56 string_expand(buf
, sizeof(buf
), title
, 1);
58 titlelen
= strlen(title
);
60 line
= add_line_alloc(view
, &commit
, type
, titlelen
, custom
);
65 strncpy(commit
->title
, title
, titlelen
);
66 memset(template, 0, sizeof(*template));
67 state
->reflogmsg
[0] = 0;
69 view_column_info_update(view
, line
);
74 main_flush_commit(struct view
*view
, struct commit
*commit
)
77 main_add_commit(view
, LINE_MAIN_COMMIT
, commit
, "", FALSE
);
81 main_add_changes_commit(struct view
*view
, enum line_type type
, const char *parent
, const char *title
)
83 char ids
[SIZEOF_STR
] = NULL_ID
" ";
84 struct main_state
*state
= view
->private;
85 struct commit commit
= {};
93 string_copy_rev(ids
+ STRING_SIZE(NULL_ID
" "), parent
);
95 ids
[STRING_SIZE(NULL_ID
)] = 0;
97 if (!gettimeofday(&now
, &tz
)) {
98 commit
.time
.tz
= tz
.tz_minuteswest
* 60;
99 commit
.time
.sec
= now
.tv_sec
- commit
.time
.tz
;
102 commit
.author
= &unknown_ident
;
103 main_register_commit(view
, &commit
, ids
, FALSE
);
104 if (state
->with_graph
&& *parent
)
105 graph_render_parents(state
->graph
, &commit
.graph
);
107 if (!main_add_commit(view
, type
, &commit
, title
, TRUE
))
114 main_check_index(struct view
*view
, struct main_state
*state
)
116 struct index_diff diff
;
118 if (!index_diff(&diff
, FALSE
, FALSE
))
121 if (!diff
.unstaged
) {
122 watch_apply(&view
->watch
, WATCH_INDEX_UNSTAGED_NO
);
124 watch_apply(&view
->watch
, WATCH_INDEX_UNSTAGED_YES
);
125 state
->add_changes_unstaged
= TRUE
;
129 watch_apply(&view
->watch
, WATCH_INDEX_STAGED_NO
);
131 watch_apply(&view
->watch
, WATCH_INDEX_STAGED_YES
);
132 state
->add_changes_staged
= TRUE
;
139 main_add_changes(struct view
*view
, struct main_state
*state
, const char *parent
)
141 const char *staged_parent
= NULL_ID
;
142 const char *unstaged_parent
= parent
;
144 if (!state
->add_changes_unstaged
) {
145 unstaged_parent
= NULL
;
146 staged_parent
= parent
;
149 if (!state
->add_changes_staged
) {
150 staged_parent
= NULL
;
153 return main_add_changes_commit(view
, LINE_STAT_STAGED
, staged_parent
, "Staged changes")
154 && main_add_changes_commit(view
, LINE_STAT_UNSTAGED
, unstaged_parent
, "Unstaged changes");
158 main_check_argv(struct view
*view
, const char *argv
[])
160 struct main_state
*state
= view
->private;
161 bool with_reflog
= FALSE
;
164 for (i
= 0; argv
[i
]; i
++) {
165 const char *arg
= argv
[i
];
166 struct rev_flags rev_flags
= {};
168 if (!strcmp(arg
, "--graph")) {
169 struct view_column
*column
= get_view_column(view
, VIEW_COLUMN_COMMIT_TITLE
);
172 column
->opt
.commit_title
.graph
= TRUE
;
173 if (opt_commit_order
!= COMMIT_ORDER_REVERSE
)
174 state
->with_graph
= TRUE
;
180 if (!strcmp(arg
, "--first-parent"))
181 state
->first_parent
= TRUE
;
183 if (!argv_parse_rev_flag(arg
, &rev_flags
))
186 if (rev_flags
.with_reflog
)
188 if (!rev_flags
.with_graph
)
189 state
->with_graph
= FALSE
;
190 arg
+= rev_flags
.search_offset
;
191 if (*arg
&& !*view
->env
->search
)
192 string_ncopy(view
->env
->search
, arg
, strlen(arg
));
198 static enum graph_display
199 main_with_graph(struct view
*view
, enum open_flags flags
)
201 struct view_column
*column
= get_view_column(view
, VIEW_COLUMN_COMMIT_TITLE
);
203 return column
&& opt_commit_order
!= COMMIT_ORDER_REVERSE
&& !open_in_pager_mode(flags
)
204 ? column
->opt
.commit_title
.graph
: GRAPH_DISPLAY_NO
;
208 main_open(struct view
*view
, enum open_flags flags
)
210 enum graph_display graph_display
= main_with_graph(view
, flags
);
211 const char *pretty_custom_argv
[] = {
212 GIT_MAIN_LOG_CUSTOM(encoding_arg
, commit_order_arg_with_graph(graph_display
),
213 "%(cmdlineargs)", "%(revargs)", "%(fileargs)")
215 const char *pretty_raw_argv
[] = {
216 GIT_MAIN_LOG_RAW(encoding_arg
, commit_order_arg_with_graph(graph_display
),
217 "%(cmdlineargs)", "%(revargs)", "%(fileargs)")
219 struct main_state
*state
= view
->private;
220 const char **main_argv
= pretty_custom_argv
;
221 enum watch_trigger changes_triggers
= WATCH_NONE
;
223 if (opt_show_changes
&& repo
.is_inside_work_tree
)
224 changes_triggers
|= WATCH_INDEX
;
226 state
->with_graph
= graph_display
!= GRAPH_DISPLAY_NO
;
228 if (opt_rev_args
&& main_check_argv(view
, opt_rev_args
))
229 main_argv
= pretty_raw_argv
;
231 if (state
->with_graph
) {
232 state
->graph
= init_graph();
237 if (open_in_pager_mode(flags
)) {
238 changes_triggers
= WATCH_NONE
;
241 /* This calls reset_view() so must be before adding changes commits. */
242 if (!begin_update(view
, NULL
, main_argv
, flags
))
245 /* Register watch before changes commits are added to record the
247 if (view_can_refresh(view
))
248 watch_register(&view
->watch
, WATCH_HEAD
| WATCH_REFS
| changes_triggers
);
250 if (changes_triggers
)
251 main_check_index(view
, state
);
257 main_done(struct view
*view
)
259 struct main_state
*state
= view
->private;
262 for (i
= 0; i
< view
->lines
; i
++) {
263 struct commit
*commit
= view
->line
[i
].data
;
265 free(commit
->graph
.symbols
);
268 for (i
= 0; i
< state
->reflogs
; i
++)
269 free(state
->reflog
[i
]);
273 #define main_check_commit_refs(line) !((line)->no_commit_refs)
274 #define main_mark_no_commit_refs(line) (((struct line *) (line))->no_commit_refs = 1)
276 static inline struct ref_list
*
277 main_get_commit_refs(const struct line
*line
, struct commit
*commit
)
279 struct ref_list
*refs
= NULL
;
281 if (main_check_commit_refs(line
) && !(refs
= get_ref_list(commit
->id
)))
282 main_mark_no_commit_refs(line
);
288 main_get_column_data(struct view
*view
, const struct line
*line
, struct view_column_data
*column_data
)
290 struct main_state
*state
= view
->private;
291 struct commit
*commit
= line
->data
;
292 struct ref_list
*refs
= NULL
;
294 column_data
->author
= commit
->author
;
295 column_data
->date
= &commit
->time
;
296 column_data
->id
= commit
->id
;
298 column_data
->reflog
= state
->reflog
[line
->lineno
- 1];
300 column_data
->commit_title
= commit
->title
;
301 if (state
->with_graph
)
302 column_data
->graph
= &commit
->graph
;
304 if ((refs
= main_get_commit_refs(line
, commit
)))
305 column_data
->refs
= refs
;
311 main_add_reflog(struct view
*view
, struct main_state
*state
, char *reflog
)
313 char *end
= strchr(reflog
, ' ');
320 if (!realloc_reflogs(&state
->reflog
, state
->reflogs
, 1)
321 || !(reflog
= strdup(reflog
)))
324 state
->reflog
[state
->reflogs
++] = reflog
;
325 id_width
= strlen(reflog
);
326 if (state
->reflog_width
< id_width
) {
327 struct view_column
*column
= get_view_column(view
, VIEW_COLUMN_ID
);
329 state
->reflog_width
= id_width
;
330 if (column
&& column
->opt
.id
.display
)
331 view
->force_redraw
= TRUE
;
337 /* Reads git log --pretty=raw output and parses it into the commit struct. */
339 main_read(struct view
*view
, struct buffer
*buf
)
341 struct main_state
*state
= view
->private;
342 struct graph
*graph
= state
->graph
;
344 struct commit
*commit
= &state
->current
;
348 main_flush_commit(view
, commit
);
350 if (failed_to_load_initial_view(view
))
351 die("No revisions match the given arguments.");
352 if (view
->lines
> 0) {
353 struct commit
*last
= view
->line
[view
->lines
- 1].data
;
355 view
->line
[view
->lines
- 1].dirty
= 1;
370 type
= get_line_type(line
);
371 if (type
== LINE_COMMIT
) {
375 state
->in_header
= TRUE
;
376 line
+= STRING_SIZE("commit ");
377 is_boundary
= *line
== '-';
378 while (*line
&& !isalnum(*line
))
381 if (state
->add_changes_unstaged
|| state
->add_changes_staged
) {
382 main_add_changes(view
, state
, line
);
383 state
->add_changes_unstaged
= state
->add_changes_staged
= FALSE
;
386 main_flush_commit(view
, commit
);
388 author
= io_memchr(buf
, line
, 0);
390 if (state
->first_parent
) {
391 char *parent
= strchr(line
, ' ');
392 char *parent_end
= parent
? strchr(parent
+ 1, ' ') : NULL
;
397 io_trace("[parent] %s\n", line
);
400 main_register_commit(view
, &state
->current
, line
, is_boundary
);
403 char *title
= io_memchr(buf
, author
, 0);
405 parse_author_line(author
, &commit
->author
, &commit
->time
);
406 if (state
->with_graph
)
407 graph_render_parents(graph
, &commit
->graph
);
409 main_add_commit(view
, LINE_MAIN_COMMIT
, commit
, title
, FALSE
);
418 /* Empty line separates the commit header from the log itself. */
420 state
->in_header
= FALSE
;
424 if (!main_add_reflog(view
, state
, line
+ STRING_SIZE("Reflog: ")))
428 case LINE_PP_REFLOGMSG
:
429 line
+= STRING_SIZE("Reflog message: ");
430 string_ncopy(state
->reflogmsg
, line
, strlen(line
));
434 if (state
->with_graph
&& !graph
->has_parents
)
435 graph_add_parent(graph
, line
+ STRING_SIZE("parent "));
439 parse_author_line(line
+ STRING_SIZE("author "),
440 &commit
->author
, &commit
->time
);
441 if (state
->with_graph
)
442 graph_render_parents(graph
, &commit
->graph
);
446 /* Fill in the commit title if it has not already been set. */
450 /* Skip lines in the commit header. */
451 if (state
->in_header
)
454 /* Require titles to start with a non-space character at the
455 * offset used by git log. */
456 if (strncmp(line
, " ", 4))
459 /* Well, if the title starts with a whitespace character,
460 * try to be forgiving. Otherwise we end up with no title. */
461 while (isspace(*line
))
465 if (*state
->reflogmsg
)
466 line
= state
->reflogmsg
;
467 main_add_commit(view
, LINE_MAIN_COMMIT
, commit
, line
, FALSE
);
474 main_request(struct view
*view
, enum request request
, struct line
*line
)
476 enum open_flags flags
= (view_is_displayed(view
) && request
!= REQ_VIEW_DIFF
)
477 ? OPEN_SPLIT
: OPEN_DEFAULT
;
482 if (view_is_displayed(view
) && display
[0] != view
)
484 /* Do not pass navigation requests to the branch view
485 * when the main view is maximized. (GH #38) */
486 return request
== REQ_NEXT
? REQ_MOVE_DOWN
: REQ_MOVE_UP
;
490 if (view_is_displayed(view
) && display
[0] != view
)
491 maximize_view(view
, TRUE
);
493 if (line
->type
== LINE_STAT_UNSTAGED
494 || line
->type
== LINE_STAT_STAGED
)
495 open_stage_view(view
, NULL
, line
->type
, flags
);
497 open_diff_view(view
, flags
);
513 main_update_env(struct view
*view
, struct line
*line
, struct commit
*commit
)
515 struct ref_list
*list
= main_get_commit_refs(line
, commit
);
518 for (i
= 0; list
&& i
< list
->size
; i
++)
519 ref_update_env(view
->env
, list
->refs
[list
->size
- i
- 1], !i
);
523 main_select(struct view
*view
, struct line
*line
)
525 struct commit
*commit
= line
->data
;
527 if (line
->type
== LINE_STAT_STAGED
|| line
->type
== LINE_STAT_UNSTAGED
) {
528 string_ncopy(view
->ref
, commit
->title
, strlen(commit
->title
));
529 status_stage_info(view
->env
->status
, line
->type
, NULL
);
531 string_copy_rev(view
->ref
, commit
->id
);
532 main_update_env(view
, line
, commit
);
534 string_copy_rev(view
->env
->commit
, commit
->id
);
537 static struct view_ops main_ops
= {
540 VIEW_SEND_CHILD_ENTER
| VIEW_FILE_FILTER
| VIEW_LOG_LIKE
| VIEW_REFRESH
,
541 sizeof(struct main_state
),
549 view_column_bit(AUTHOR
) | view_column_bit(COMMIT_TITLE
) |
550 view_column_bit(DATE
) | view_column_bit(ID
) |
551 view_column_bit(LINE_NUMBER
),
552 main_get_column_data
,
557 /* vim: set ts=8 sw=8 noexpandtab: */