-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblob.c
133 lines (113 loc) · 3.35 KB
/
blob.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "tig/refdb.h"
#include "tig/parse.h"
#include "tig/display.h"
#include "tig/draw.h"
#include "tig/log.h"
#include "tig/pager.h"
#include "tig/tree.h"
struct blob_state {
char commit[SIZEOF_REF];
const char *file;
};
static bool
blob_open(struct view *view, enum open_flags flags)
{
struct blob_state *state = view->private;
static const char *blob_argv[] = {
"git", "cat-file", "blob", "%(blob)", NULL
};
const char **argv = (flags & (OPEN_PREPARED | OPEN_REFRESH)) ? view->argv : blob_argv;
if (argv != blob_argv) {
state->file = get_path(view->env->file);
state->commit[0] = 0;
}
if (!state->file && !view->env->blob[0] && view->env->file[0]) {
const char *commit = view->env->commit[0] ? view->env->commit : "HEAD";
char blob_spec[SIZEOF_STR];
const char *rev_parse_argv[] = {
"git", "rev-parse", blob_spec, NULL
};
if (!string_format(blob_spec, "%s:%s", commit, view->env->file) ||
!io_run_buf(rev_parse_argv, view->env->blob, sizeof(view->env->blob))) {
report("Failed to resolve blob from file name");
return FALSE;
}
string_ncopy(state->commit, commit, strlen(commit));
}
if (!state->file && !view->env->blob[0]) {
report("No file chosen, press %s to open tree view",
get_view_key(view, REQ_VIEW_TREE));
return FALSE;
}
view->encoding = get_path_encoding(view->env->file, default_encoding);
string_copy(view->ref, view->env->file);
return begin_update(view, NULL, argv, flags);
}
static bool
blob_read(struct view *view, struct buffer *buf)
{
if (!buf) {
if (view->env->lineno > 0) {
select_view_line(view, view->env->lineno);
view->env->lineno = 0;
}
return TRUE;
}
return add_line_text(view, buf->data, LINE_DEFAULT) != NULL;
}
static enum request
blob_request(struct view *view, enum request request, struct line *line)
{
struct blob_state *state = view->private;
switch (request) {
case REQ_REFRESH:
if (!state->file) {
report("Cannot reload immutable blob");
} else {
string_ncopy(view->env->file, state->file, strlen(state->file));
refresh_view(view);
}
return REQ_NONE;
case REQ_VIEW_BLAME:
string_ncopy(view->env->ref, state->commit, strlen(state->commit));
view->env->lineno = line - view->line;
return request;
case REQ_EDIT:
if (state->file)
open_editor(state->file, (line - view->line) + 1);
else
open_blob_editor(view->vid, NULL, (line - view->line) + 1);
return REQ_NONE;
default:
return pager_request(view, request, line);
}
}
static struct view_ops blob_ops = {
"line",
argv_env.blob,
VIEW_NO_FLAGS | VIEW_REFRESH,
sizeof(struct blob_state),
blob_open,
blob_read,
view_column_draw,
blob_request,
view_column_grep,
pager_select,
NULL,
view_column_bit(LINE_NUMBER) | view_column_bit(TEXT),
pager_get_column_data,
};
DEFINE_VIEW(blob);
/* vim: set ts=8 sw=8 noexpandtab: */