old-graph: use init_graph to setup the graph in the test-graph tool
[tig.git] / src / blob.c
blobecf7d168890afbfe6e1850b964cf322b6f07614d
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);
65 if (*view->env->file)
66 string_copy(view->ref, view->env->file);
67 else
68 string_copy_rev(view->ref, view->ops->id);
70 return begin_update(view, NULL, argv, flags);
73 static bool
74 blob_read(struct view *view, struct buffer *buf)
76 if (!buf) {
77 if (view->env->goto_lineno > 0) {
78 select_view_line(view, view->env->goto_lineno);
79 view->env->goto_lineno = 0;
81 return TRUE;
84 return add_line_text(view, buf->data, LINE_DEFAULT) != NULL;
87 static void
88 blob_select(struct view *view, struct line *line)
90 struct blob_state *state = view->private;
92 if (state->file)
93 string_format(view->env->file, "%s", state->file);
94 view->env->lineno = view->pos.lineno + 1;
97 static enum request
98 blob_request(struct view *view, enum request request, struct line *line)
100 struct blob_state *state = view->private;
102 switch (request) {
103 case REQ_REFRESH:
104 if (!state->file) {
105 report("Cannot reload immutable blob");
106 } else {
107 string_ncopy(view->env->file, state->file, strlen(state->file));
108 refresh_view(view);
110 return REQ_NONE;
112 case REQ_VIEW_BLAME:
113 string_ncopy(view->env->ref, state->commit, strlen(state->commit));
114 view->env->goto_lineno = line - view->line;
115 return request;
117 case REQ_EDIT:
118 if (state->file)
119 open_editor(state->file, (line - view->line) + 1);
120 else
121 open_blob_editor(view->vid, NULL, (line - view->line) + 1);
122 return REQ_NONE;
124 default:
125 return pager_request(view, request, line);
129 static struct view_ops blob_ops = {
130 "line",
131 argv_env.blob,
132 VIEW_NO_FLAGS | VIEW_REFRESH,
133 sizeof(struct blob_state),
134 blob_open,
135 blob_read,
136 view_column_draw,
137 blob_request,
138 view_column_grep,
139 blob_select,
140 NULL,
141 view_column_bit(LINE_NUMBER) | view_column_bit(TEXT),
142 pager_get_column_data,
145 DEFINE_VIEW(blob);
147 /* vim: set ts=8 sw=8 noexpandtab: */