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"
19 #include "tig/pager.h"
23 char commit
[SIZEOF_REF
];
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
);
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");
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
));
63 view
->encoding
= get_path_encoding(view
->env
->file
, default_encoding
);
66 string_copy(view
->ref
, view
->env
->file
);
68 string_copy_rev(view
->ref
, view
->ops
->id
);
70 return begin_update(view
, NULL
, argv
, flags
);
74 blob_read(struct view
*view
, struct buffer
*buf
)
77 if (view
->env
->goto_lineno
> 0) {
78 select_view_line(view
, view
->env
->goto_lineno
);
79 view
->env
->goto_lineno
= 0;
84 return add_line_text(view
, buf
->data
, LINE_DEFAULT
) != NULL
;
88 blob_select(struct view
*view
, struct line
*line
)
90 struct blob_state
*state
= view
->private;
93 string_format(view
->env
->file
, "%s", state
->file
);
94 view
->env
->lineno
= view
->pos
.lineno
+ 1;
98 blob_request(struct view
*view
, enum request request
, struct line
*line
)
100 struct blob_state
*state
= view
->private;
105 report("Cannot reload immutable blob");
107 string_ncopy(view
->env
->file
, state
->file
, strlen(state
->file
));
113 string_ncopy(view
->env
->ref
, state
->commit
, strlen(state
->commit
));
114 view
->env
->goto_lineno
= line
- view
->line
;
119 open_editor(state
->file
, (line
- view
->line
) + 1);
121 open_blob_editor(view
->vid
, NULL
, (line
- view
->line
) + 1);
125 return pager_request(view
, request
, line
);
129 static struct view_ops blob_ops
= {
132 VIEW_NO_FLAGS
| VIEW_REFRESH
,
133 sizeof(struct blob_state
),
141 view_column_bit(LINE_NUMBER
) | view_column_bit(TEXT
),
142 pager_get_column_data
,
147 /* vim: set ts=8 sw=8 noexpandtab: */