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/refdb.h"
17 #include "tig/options.h"
18 #include "tig/parse.h"
19 #include "tig/display.h"
20 #include "tig/prompt.h"
24 #include "tig/watch.h"
25 #include "tig/status.h"
26 #include "tig/stage.h"
32 static char status_onbranch
[SIZEOF_STR
];
34 /* This should work even for the "On branch" line. */
36 status_has_none(struct view
*view
, struct line
*line
)
38 return view_has_line(view
, line
) && !line
[1].data
;
41 /* Get fields from the diff line:
42 * :100644 100644 06a5d6ae9eca55be2e0e585a152e6b1336f2b20e 0000000000000000000000000000000000000000 M
45 status_get_diff(struct status
*file
, const char *buf
, size_t bufsize
)
47 const char *old_mode
= buf
+ 1;
48 const char *new_mode
= buf
+ 8;
49 const char *old_rev
= buf
+ 15;
50 const char *new_rev
= buf
+ 56;
51 const char *status
= buf
+ 97;
54 old_mode
[-1] != ':' ||
55 new_mode
[-1] != ' ' ||
61 file
->status
= *status
;
63 string_copy_rev(file
->old
.rev
, old_rev
);
64 string_copy_rev(file
->new.rev
, new_rev
);
66 file
->old
.mode
= strtoul(old_mode
, NULL
, 8);
67 file
->new.mode
= strtoul(new_mode
, NULL
, 8);
69 file
->old
.name
[0] = file
->new.name
[0] = 0;
75 status_run(struct view
*view
, const char *argv
[], char status
, enum line_type type
)
77 struct status
*unmerged
= NULL
;
81 if (!io_run(&io
, IO_RD
, repo
.cdup
, opt_env
, argv
))
84 add_line_nodata(view
, type
);
86 while (io_get(&io
, &buf
, 0, TRUE
)) {
88 struct status parsed
= {};
89 struct status
*file
= &parsed
;
91 /* Parse diff info part. */
93 file
->status
= status
;
95 string_copy(file
->old
.rev
, NULL_ID
);
98 if (!status_get_diff(&parsed
, buf
.data
, buf
.size
))
101 if (!io_get(&io
, &buf
, 0, TRUE
))
105 /* Grab the old name for rename/copy. */
106 if (!*file
->old
.name
&&
107 (file
->status
== 'R' || file
->status
== 'C')) {
108 string_ncopy(file
->old
.name
, buf
.data
, buf
.size
);
110 if (!io_get(&io
, &buf
, 0, TRUE
))
114 /* git-ls-files just delivers a NUL separated list of
115 * file names similar to the second half of the
116 * git-diff-* output. */
117 string_ncopy(file
->new.name
, buf
.data
, buf
.size
);
118 if (!*file
->old
.name
)
119 string_copy(file
->old
.name
, file
->new.name
);
121 /* Collapse all modified entries that follow an associated
123 if (unmerged
&& !strcmp(unmerged
->new.name
, file
->new.name
)) {
124 unmerged
->status
= 'U';
129 line
= add_line_alloc(view
, &file
, type
, 0, FALSE
);
133 view_column_info_update(view
, line
);
134 if (file
->status
== 'U')
144 if (!view
->line
[view
->lines
- 1].data
) {
145 add_line_nodata(view
, LINE_STAT_NONE
);
146 if (type
== LINE_STAT_STAGED
)
147 watch_apply(&view
->watch
, WATCH_INDEX_STAGED_NO
);
148 else if (type
== LINE_STAT_UNSTAGED
)
149 watch_apply(&view
->watch
, WATCH_INDEX_UNSTAGED_NO
);
151 if (type
== LINE_STAT_STAGED
)
152 watch_apply(&view
->watch
, WATCH_INDEX_STAGED_YES
);
153 else if (type
== LINE_STAT_UNSTAGED
)
154 watch_apply(&view
->watch
, WATCH_INDEX_UNSTAGED_YES
);
161 static const char *status_diff_index_argv
[] = { GIT_DIFF_STAGED_FILES("-z") };
162 static const char *status_diff_files_argv
[] = { GIT_DIFF_UNSTAGED_FILES("-z") };
164 static const char *status_list_other_argv
[] = {
165 "git", "ls-files", "-z", "--others", "--exclude-standard", repo
.prefix
, NULL
, NULL
, NULL
168 static const char *status_list_no_head_argv
[] = {
169 "git", "ls-files", "-z", "--cached", "--exclude-standard", NULL
172 /* Restore the previous line number to stay in the context or select a
173 * line with something that can be updated. */
175 status_restore(struct view
*view
)
177 if (!check_position(&view
->prev_pos
))
180 if (view
->prev_pos
.lineno
>= view
->lines
)
181 view
->prev_pos
.lineno
= view
->lines
- 1;
182 while (view
->prev_pos
.lineno
< view
->lines
&& !view
->line
[view
->prev_pos
.lineno
].data
)
183 view
->prev_pos
.lineno
++;
184 while (view
->prev_pos
.lineno
> 0 && !view
->line
[view
->prev_pos
.lineno
].data
)
185 view
->prev_pos
.lineno
--;
187 /* If the above fails, always skip the "On branch" line. */
188 if (view
->prev_pos
.lineno
< view
->lines
)
189 view
->pos
.lineno
= view
->prev_pos
.lineno
;
191 view
->pos
.lineno
= 1;
193 if (view
->prev_pos
.offset
> view
->pos
.lineno
)
194 view
->pos
.offset
= view
->pos
.lineno
;
195 else if (view
->prev_pos
.offset
< view
->lines
)
196 view
->pos
.offset
= view
->prev_pos
.offset
;
198 clear_position(&view
->prev_pos
);
202 status_update_onbranch(void)
204 static const char *paths
[][3] = {
205 { "rebase-apply/rebasing", "rebase-apply/head-name", "Rebasing" },
206 { "rebase-apply/applying", "rebase-apply/head-name", "Applying mailbox to" },
207 { "rebase-apply/", "rebase-apply/head-name", "Rebasing mailbox onto" },
208 { "rebase-merge/interactive", "rebase-merge/head-name", "Interactive rebase" },
209 { "rebase-merge/", "rebase-merge/head-name", "Rebase merge" },
210 { "MERGE_HEAD", NULL
, "Merging" },
211 { "BISECT_LOG", NULL
, "Bisecting" },
212 { "HEAD", NULL
, "On branch" },
214 char buf
[SIZEOF_STR
];
218 if (is_initial_commit()) {
219 string_copy(status_onbranch
, "Initial commit");
223 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
224 const char *prefix
= paths
[i
][2];
225 char *head
= repo
.head
;
227 if (!string_format(buf
, "%s/%s", repo
.git_dir
, paths
[i
][0]) ||
228 lstat(buf
, &stat
) < 0)
234 if (io_open(&io
, "%s/%s", repo
.git_dir
, paths
[i
][1]) &&
235 io_read_buf(&io
, buf
, sizeof(buf
))) {
237 if (!prefixcmp(head
, "refs/heads/"))
238 head
+= STRING_SIZE("refs/heads/");
242 if (!strcmp(head
, "HEAD") && !strcmp(paths
[i
][0], "HEAD") && *repo
.head_id
) {
243 prefix
= "On detached head";
247 if (!string_format(status_onbranch
, "%s %s", prefix
, head
))
248 string_copy(status_onbranch
, repo
.head
);
252 string_copy(status_onbranch
, "Not currently on any branch");
255 /* First parse staged info using git-diff-index(1), then parse unstaged
256 * info using git-diff-files(1), and finally untracked files using
257 * git-ls-files(1). */
259 status_open(struct view
*view
, enum open_flags flags
)
261 const char **staged_argv
= is_initial_commit() ?
262 status_list_no_head_argv
: status_diff_index_argv
;
263 char staged_status
= staged_argv
== status_list_no_head_argv
? 'A' : 0;
265 if (repo
.is_inside_work_tree
== FALSE
) {
266 report("The status view requires a working tree");
272 /* FIXME: Watch untracked files and on-branch info. */
273 watch_register(&view
->watch
, WATCH_INDEX
);
275 add_line_nodata(view
, LINE_HEADER
);
276 status_update_onbranch();
280 status_list_other_argv
[ARRAY_SIZE(status_list_other_argv
) - 3] =
281 opt_status_untracked_dirs
? NULL
: "--directory";
282 status_list_other_argv
[ARRAY_SIZE(status_list_other_argv
) - 2] =
283 opt_status_untracked_dirs
? NULL
: "--no-empty-directory";
285 if (!status_run(view
, staged_argv
, staged_status
, LINE_STAT_STAGED
) ||
286 !status_run(view
, status_diff_files_argv
, 0, LINE_STAT_UNSTAGED
) ||
287 !status_run(view
, status_list_other_argv
, '?', LINE_STAT_UNTRACKED
)) {
288 report("Failed to load status data");
292 /* Restore the exact position or use the specialized restore
294 status_restore(view
);
299 status_get_column_data(struct view
*view
, const struct line
*line
, struct view_column_data
*column_data
)
301 struct status
*status
= line
->data
;
304 static struct view_column group_column
;
308 column_data
->section
= &group_column
;
309 column_data
->section
->type
= VIEW_COLUMN_SECTION
;
311 switch (line
->type
) {
312 case LINE_STAT_STAGED
:
314 text
= "Changes to be committed:";
317 case LINE_STAT_UNSTAGED
:
319 text
= "Changed but not updated:";
322 case LINE_STAT_UNTRACKED
:
324 text
= "Untracked files:";
329 text
= " (no files)";
334 text
= status_onbranch
;
341 column_data
->section
->opt
.section
.text
= text
;
342 column_data
->section
->opt
.section
.type
= type
;
345 column_data
->status
= &status
->status
;
346 column_data
->file_name
= status
->new.name
;
352 status_enter(struct view
*view
, struct line
*line
)
354 struct status
*status
= line
->data
;
355 enum open_flags flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
357 if (line
->type
== LINE_STAT_NONE
||
358 (!status
&& line
[1].type
== LINE_STAT_NONE
)) {
359 report("No file to diff");
363 switch (line
->type
) {
364 case LINE_STAT_STAGED
:
365 case LINE_STAT_UNSTAGED
:
368 case LINE_STAT_UNTRACKED
:
370 report("No file to show");
374 if (!suffixcmp(status
->new.name
, -1, "/")) {
375 report("Cannot display a directory");
381 report("Nothing to enter");
385 open_stage_view(view
, status
, line
->type
, flags
);
390 status_exists(struct view
*view
, struct status
*status
, enum line_type type
)
392 unsigned long lineno
;
394 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
395 struct line
*line
= &view
->line
[lineno
];
396 struct status
*pos
= line
->data
;
398 if (line
->type
!= type
)
400 if ((!pos
&& (!status
|| !status
->status
) && line
[1].data
) ||
401 (pos
&& !strcmp(status
->new.name
, pos
->new.name
))) {
402 select_view_line(view
, lineno
);
403 status_restore(view
);
413 status_update_prepare(struct io
*io
, enum line_type type
)
415 const char *staged_argv
[] = {
416 "git", "update-index", "-z", "--index-info", NULL
418 const char *others_argv
[] = {
419 "git", "update-index", "-z", "--add", "--remove", "--stdin", NULL
423 case LINE_STAT_STAGED
:
424 return io_run(io
, IO_WR
, repo
.cdup
, opt_env
, staged_argv
);
426 case LINE_STAT_UNSTAGED
:
427 case LINE_STAT_UNTRACKED
:
428 return io_run(io
, IO_WR
, repo
.cdup
, opt_env
, others_argv
);
431 die("line type %d not handled in switch", type
);
437 status_update_write(struct io
*io
, struct status
*status
, enum line_type type
)
440 case LINE_STAT_STAGED
:
441 return io_printf(io
, "%06o %s\t%s%c", status
->old
.mode
,
442 status
->old
.rev
, status
->old
.name
, 0);
444 case LINE_STAT_UNSTAGED
:
445 case LINE_STAT_UNTRACKED
:
446 return io_printf(io
, "%s%c", status
->new.name
, 0);
449 die("line type %d not handled in switch", type
);
455 status_update_file(struct status
*status
, enum line_type type
)
457 const char *name
= status
->new.name
;
461 if (type
== LINE_STAT_UNTRACKED
&& !suffixcmp(name
, strlen(name
), "/")) {
462 const char *add_argv
[] = { "git", "add", "--", name
, NULL
};
464 return io_run_bg(add_argv
);
467 if (!status_update_prepare(&io
, type
))
470 result
= status_update_write(&io
, status
, type
);
471 return io_done(&io
) && result
;
475 status_update_files(struct view
*view
, struct line
*line
)
477 char buf
[sizeof(view
->ref
)];
483 int cursor_y
= -1, cursor_x
= -1;
485 if (!status_update_prepare(&io
, line
->type
))
488 for (pos
= line
; view_has_line(view
, pos
) && pos
->data
; pos
++)
491 string_copy(buf
, view
->ref
);
492 getsyx(cursor_y
, cursor_x
);
493 for (file
= 0, done
= 5; result
&& file
< files
; line
++, file
++) {
494 int almost_done
= file
* 100 / files
;
496 if (almost_done
> done
) {
498 string_format(view
->ref
, "updating file %u of %u (%d%% done)",
500 update_view_title(view
);
501 setsyx(cursor_y
, cursor_x
);
504 result
= status_update_write(&io
, line
->data
, line
->type
);
506 string_copy(view
->ref
, buf
);
508 return io_done(&io
) && result
;
512 status_update(struct view
*view
)
514 struct line
*line
= &view
->line
[view
->pos
.lineno
];
519 if (status_has_none(view
, line
)) {
520 report("Nothing to update");
524 if (!status_update_files(view
, line
+ 1)) {
525 report("Failed to update file status");
529 } else if (!status_update_file(line
->data
, line
->type
)) {
530 report("Failed to update file status");
538 status_revert(struct status
*status
, enum line_type type
, bool has_none
)
540 if (!status
|| type
!= LINE_STAT_UNSTAGED
) {
541 if (type
== LINE_STAT_STAGED
) {
542 report("Cannot revert changes to staged files");
543 } else if (type
== LINE_STAT_UNTRACKED
) {
544 report("Cannot revert changes to untracked files");
545 } else if (has_none
) {
546 report("Nothing to revert");
548 report("Cannot revert changes to multiple files");
551 } else if (prompt_yesno("Are you sure you want to revert changes?")) {
552 char mode
[10] = "100644";
553 const char *reset_argv
[] = {
554 "git", "update-index", "--cacheinfo", mode
,
555 status
->old
.rev
, status
->old
.name
, NULL
557 const char *checkout_argv
[] = {
558 "git", "checkout", "--", status
->old
.name
, NULL
561 if (status
->status
== 'U') {
562 string_format(mode
, "%5o", status
->old
.mode
);
564 if (status
->old
.mode
== 0 && status
->new.mode
== 0) {
565 reset_argv
[2] = "--force-remove";
566 reset_argv
[3] = status
->old
.name
;
567 reset_argv
[4] = NULL
;
570 if (!io_run_fg(reset_argv
, repo
.cdup
))
572 if (status
->old
.mode
== 0 && status
->new.mode
== 0)
576 return io_run_fg(checkout_argv
, repo
.cdup
);
583 open_mergetool(const char *file
)
585 const char *mergetool_argv
[] = { "git", "mergetool", file
, NULL
};
587 open_external_viewer(mergetool_argv
, repo
.cdup
, FALSE
, TRUE
, TRUE
, "");
591 status_request(struct view
*view
, enum request request
, struct line
*line
)
593 struct status
*status
= line
->data
;
596 case REQ_STATUS_UPDATE
:
597 if (!status_update(view
))
601 case REQ_STATUS_REVERT
:
602 if (!status_revert(status
, line
->type
, status_has_none(view
, line
)))
606 case REQ_STATUS_MERGE
:
607 if (!status
|| status
->status
!= 'U') {
608 report("Merging only possible for files with unmerged status ('U').");
611 open_mergetool(status
->new.name
);
617 if (status
->status
== 'D') {
618 report("File has been deleted.");
622 open_editor(status
->new.name
, 0);
626 if (line
->type
== LINE_STAT_UNTRACKED
|| !status
) {
627 report("Nothing to blame here");
631 view
->env
->ref
[0] = 0;
635 /* After returning the status view has been split to
636 * show the stage view. No further reloading is
638 return status_enter(view
, line
);
641 /* Load the current branch information and then the view. */
655 status_stage_info_(char *buf
, size_t bufsize
,
656 enum line_type type
, struct status
*status
)
658 const char *file
= status
? status
->new.name
: "";
662 case LINE_STAT_STAGED
:
663 if (status
&& status
->status
)
664 info
= "Staged changes to %s";
666 info
= "Staged changes";
669 case LINE_STAT_UNSTAGED
:
670 if (status
&& status
->status
)
671 info
= "Unstaged changes to %s";
673 info
= "Unstaged changes";
676 case LINE_STAT_UNTRACKED
:
677 info
= "Untracked file %s";
685 return string_nformat(buf
, bufsize
, NULL
, info
, file
);
689 status_select(struct view
*view
, struct line
*line
)
691 struct status
*status
= line
->data
;
692 char file
[SIZEOF_STR
] = "all files";
696 if (status
&& !string_format(file
, "'%s'", status
->new.name
))
699 if (!status
&& line
[1].type
== LINE_STAT_NONE
)
702 switch (line
->type
) {
703 case LINE_STAT_STAGED
:
704 text
= "Press %s to unstage %s for commit";
707 case LINE_STAT_UNSTAGED
:
708 text
= "Press %s to stage %s for commit";
711 case LINE_STAT_UNTRACKED
:
712 text
= "Press %s to stage %s for addition";
716 text
= "Nothing to update";
719 if (status
&& status
->status
== 'U') {
720 text
= "Press %s to resolve conflict in %s";
721 key
= get_view_key(view
, REQ_STATUS_MERGE
);
724 key
= get_view_key(view
, REQ_STATUS_UPDATE
);
727 string_format(view
->ref
, text
, key
, file
);
728 status_stage_info(view
->env
->status
, line
->type
, status
);
730 string_copy(view
->env
->file
, status
->new.name
);
733 static struct view_ops status_ops
= {
736 VIEW_CUSTOM_STATUS
| VIEW_SEND_CHILD_ENTER
| VIEW_STATUS_LIKE
| VIEW_REFRESH
,
745 view_column_bit(FILE_NAME
) | view_column_bit(LINE_NUMBER
) |
746 view_column_bit(STATUS
),
747 status_get_column_data
,
752 /* vim: set ts=8 sw=8 noexpandtab: */