Partly restore old refresh behavior
[tig.git] / src / blob.c
blob1600119528cdea5f92954c6963be24dbbccb90ec
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/refdb.h"
15 #include "tig/parse.h"
16 #include "tig/display.h"
17 #include "tig/draw.h"
18 #include "tig/log.h"
19 #include "tig/pager.h"
20 #include "tig/tree.h"
22 struct blob_state {
23 char commit[SIZEOF_REF];
24 const char *file;
27 static bool
28 blob_open(struct view *view, enum open_flags flags)
30 struct blob_state *state = view->private;
31 static const char *blob_argv[] = {
32 "git", "cat-file", "blob", "%(blob)", NULL
34 const char **argv = (flags & (OPEN_PREPARED | OPEN_REFRESH)) ? view->argv : blob_argv;
36 if (argv != blob_argv) {
37 state->file = get_path(view->env->file);
38 state->commit[0] = 0;
41 if (!state->file && !view->env->blob[0] && view->env->file[0]) {
42 const char *commit = view->env->commit[0] ? view->env->commit : "HEAD";
43 char blob_spec[SIZEOF_STR];
44 const char *rev_parse_argv[] = {
45 "git", "rev-parse", blob_spec, NULL
48 if (!string_format(blob_spec, "%s:%s", commit, view->env->file) ||
49 !io_run_buf(rev_parse_argv, view->env->blob, sizeof(view->env->blob))) {
50 report("Failed to resolve blob from file name");
51 return FALSE;
54 string_ncopy(state->commit, commit, strlen(commit));
57 if (!state->file && !view->env->blob[0]) {
58 report("No file chosen, press %s to open tree view",
59 get_view_key(view, REQ_VIEW_TREE));
60 return FALSE;
63 view->encoding = get_path_encoding(view->env->file, default_encoding);
64 string_copy(view->ref, view->env->file);
66 return begin_update(view, NULL, argv, flags);
69 static bool
70 blob_read(struct view *view, struct buffer *buf)
72 if (!buf) {
73 if (view->env->lineno > 0) {
74 select_view_line(view, view->env->lineno);
75 view->env->lineno = 0;
77 return TRUE;
80 return add_line_text(view, buf->data, LINE_DEFAULT) != NULL;
83 static enum request
84 blob_request(struct view *view, enum request request, struct line *line)
86 struct blob_state *state = view->private;
88 switch (request) {
89 case REQ_REFRESH:
90 if (!state->file) {
91 report("Cannot reload immutable blob");
92 } else {
93 string_ncopy(view->env->file, state->file, strlen(state->file));
94 refresh_view(view);
96 return REQ_NONE;
98 case REQ_VIEW_BLAME:
99 string_ncopy(view->env->ref, state->commit, strlen(state->commit));
100 view->env->lineno = line - view->line;
101 return request;
103 case REQ_EDIT:
104 if (state->file)
105 open_editor(state->file, (line - view->line) + 1);
106 else
107 open_blob_editor(view->vid, NULL, (line - view->line) + 1);
108 return REQ_NONE;
110 default:
111 return pager_request(view, request, line);
115 static struct view_ops blob_ops = {
116 "line",
117 argv_env.blob,
118 VIEW_NO_FLAGS | VIEW_REFRESH,
119 sizeof(struct blob_state),
120 blob_open,
121 blob_read,
122 view_column_draw,
123 blob_request,
124 view_column_grep,
125 pager_select,
126 NULL,
127 view_column_bit(LINE_NUMBER) | view_column_bit(TEXT),
128 pager_get_column_data,
131 DEFINE_VIEW(blob);
133 /* vim: set ts=8 sw=8 noexpandtab: */