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/graph.h"
18 #include "tig/display.h"
22 #include "tig/status.h"
30 DEFINE_ALLOCATOR(realloc_reflogs
, char *, 32)
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];
52 /* FIXME: More graceful handling of titles; append "..." to
53 * shortened titles, etc. */
54 string_expand(buf
, sizeof(buf
), title
, 1);
56 titlelen
= strlen(title
);
58 line
= add_line_alloc(view
, &commit
, type
, titlelen
, custom
);
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
);
73 main_flush_commit(struct view
*view
, struct commit
*commit
)
76 main_add_commit(view
, LINE_MAIN_COMMIT
, commit
, "", FALSE
);
80 main_has_changes(const char *argv
[])
84 if (!io_exec(&io
, IO_BG
, NULL
, opt_env
, argv
, -1))
87 return io
.status
== 1;
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
= {};
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
);
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
))
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");
144 main_check_argv(struct view
*view
, const char *argv
[])
146 struct main_state
*state
= view
->private;
147 bool with_reflog
= FALSE
;
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
))
157 if (rev_flags
.with_reflog
)
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
));
169 static const enum view_column_type main_columns
[] = {
170 VIEW_COLUMN_LINE_NUMBER
,
174 VIEW_COLUMN_COMMIT_TITLE
,
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
);
208 main_done(struct view
*view
)
210 struct main_state
*state
= view
->private;
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
]);
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
);
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
;
248 column_data
->id
= state
->reflog
[line
->lineno
- 1];
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
;
263 main_add_reflog(struct view
*view
, struct main_state
*state
, char *reflog
)
265 char *end
= strchr(reflog
, ' ');
272 if (!realloc_reflogs(&state
->reflog
, state
->reflogs
, 1)
273 || !(reflog
= strdup(reflog
)))
276 state
->reflog
[state
->reflogs
++] = reflog
;
277 id_width
= strlen(reflog
);
278 if (state
->reflog_width
< id_width
) {
279 state
->reflog_width
= id_width
;
281 view
->force_redraw
= TRUE
;
287 /* Reads git log --pretty=raw output and parses it into the commit struct. */
289 main_read(struct view
*view
, char *line
)
291 struct main_state
*state
= view
->private;
292 struct graph
*graph
= &state
->graph
;
294 struct commit
*commit
= &state
->current
;
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;
311 if (state
->with_graph
)
316 type
= get_line_type(line
);
317 if (type
== LINE_COMMIT
) {
321 state
->in_header
= TRUE
;
322 line
+= STRING_SIZE("commit ");
323 is_boundary
= *line
== '-';
324 while (*line
&& !isalnum(*line
))
327 if (!state
->added_changes_commits
&& opt_show_changes
&& repo
.is_inside_work_tree
)
328 main_add_changes_commits(view
, state
, line
);
330 main_flush_commit(view
, commit
);
332 main_register_commit(view
, &state
->current
, line
, is_boundary
);
334 author
= io_memchr(&view
->io
, line
, 0);
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
);
342 main_add_commit(view
, LINE_MAIN_COMMIT
, commit
, title
, FALSE
);
351 /* Empty line separates the commit header from the log itself. */
353 state
->in_header
= FALSE
;
357 if (!main_add_reflog(view
, state
, line
+ STRING_SIZE("Reflog: ")))
361 case LINE_PP_REFLOGMSG
:
362 line
+= STRING_SIZE("Reflog message: ");
363 string_ncopy(state
->reflogmsg
, line
, strlen(line
));
367 if (state
->with_graph
&& !graph
->has_parents
)
368 graph_add_parent(graph
, line
+ STRING_SIZE("parent "));
372 parse_author_line(line
+ STRING_SIZE("author "),
373 &commit
->author
, &commit
->time
);
374 if (state
->with_graph
)
375 graph_render_parents(graph
);
379 /* Fill in the commit title if it has not already been set. */
383 /* Skip lines in the commit header. */
384 if (state
->in_header
)
387 /* Require titles to start with a non-space character at the
388 * offset used by git log. */
389 if (strncmp(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
))
398 if (*state
->reflogmsg
)
399 line
= state
->reflogmsg
;
400 main_add_commit(view
, LINE_MAIN_COMMIT
, commit
, line
, FALSE
);
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
;
415 if (view_is_displayed(view
) && display
[0] != view
)
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
;
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
,
432 ignore_space_arg(), NULL
, NULL
)
434 const char *diff_unstaged_argv
[] = {
435 GIT_DIFF_UNSTAGED(encoding_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
);
446 open_diff_view(view
, flags
);
454 case REQ_JUMP_COMMIT
:
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
);
468 report("Unable to find commit '%s'", view
->env
->search
);
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
;
485 for (i
= 0; list
&& i
< list
->size
; i
++) {
486 struct ref
*ref
= list
->refs
[i
];
488 switch (get_line_type_from_ref(ref
)) {
491 /* Always prefer local branches. */
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
));
510 struct ref
*branch
= main_get_commit_branch(line
, commit
);
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
= {
522 VIEW_SEND_CHILD_ENTER
| VIEW_FILE_FILTER
| VIEW_LOG_LIKE
| VIEW_REFRESH
,
523 sizeof(struct main_state
),
531 main_get_column_data
,
536 /* vim: set ts=8 sw=8 noexpandtab: */