Blame


1 9f7d7167 2018-04-29 stsp /*
2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 80ddbec8 2018-04-29 stsp
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
37 9f7d7167 2018-04-29 stsp
38 9f7d7167 2018-04-29 stsp #include "got_error.h"
39 80ddbec8 2018-04-29 stsp #include "got_object.h"
40 80ddbec8 2018-04-29 stsp #include "got_reference.h"
41 80ddbec8 2018-04-29 stsp #include "got_repository.h"
42 80ddbec8 2018-04-29 stsp #include "got_diff.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
45 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
46 a70480e0 2018-06-23 stsp #include "got_blame.h"
47 9f7d7167 2018-04-29 stsp
48 881b2d3e 2018-04-30 stsp #ifndef MIN
49 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 881b2d3e 2018-04-30 stsp #endif
51 881b2d3e 2018-04-30 stsp
52 9f7d7167 2018-04-29 stsp #ifndef nitems
53 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 9f7d7167 2018-04-29 stsp #endif
55 9f7d7167 2018-04-29 stsp
56 9f7d7167 2018-04-29 stsp struct tog_cmd {
57 c2301be8 2018-04-30 stsp const char *name;
58 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
59 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
60 c2301be8 2018-04-30 stsp const char *descr;
61 9f7d7167 2018-04-29 stsp };
62 9f7d7167 2018-04-29 stsp
63 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
64 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
66 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
67 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
68 9f7d7167 2018-04-29 stsp
69 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
71 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
72 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
73 9f7d7167 2018-04-29 stsp
74 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
75 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
76 9f7d7167 2018-04-29 stsp "show repository history" },
77 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
78 9f7d7167 2018-04-29 stsp "compare files and directories" },
79 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
80 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
81 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
82 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
83 9f7d7167 2018-04-29 stsp };
84 9f7d7167 2018-04-29 stsp
85 d6b05b5b 2018-08-04 stsp enum tog_view_type {
86 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
87 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
88 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
89 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
90 d6b05b5b 2018-08-04 stsp };
91 d6b05b5b 2018-08-04 stsp
92 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
93 bcbd79e2 2018-08-19 stsp struct got_object_id *id;
94 ad80ab7b 2018-08-04 stsp FILE *f;
95 ad80ab7b 2018-08-04 stsp int first_displayed_line;
96 ad80ab7b 2018-08-04 stsp int last_displayed_line;
97 e5a0f69f 2018-08-18 stsp int eof;
98 ad80ab7b 2018-08-04 stsp };
99 ad80ab7b 2018-08-04 stsp
100 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
101 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
102 ba4f502b 2018-08-04 stsp struct got_object_id *id;
103 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
104 ba4f502b 2018-08-04 stsp };
105 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 ba4f502b 2018-08-04 stsp struct commit_queue {
107 ba4f502b 2018-08-04 stsp int ncommits;
108 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
109 b01e7d3b 2018-08-04 stsp };
110 b01e7d3b 2018-08-04 stsp
111 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
112 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
113 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
114 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
115 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
116 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
117 b01e7d3b 2018-08-04 stsp int selected;
118 b01e7d3b 2018-08-04 stsp char *in_repo_path;
119 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
120 ba4f502b 2018-08-04 stsp };
121 ba4f502b 2018-08-04 stsp
122 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
123 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
124 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
125 e9424729 2018-08-04 stsp int nlines;
126 e9424729 2018-08-04 stsp
127 e9424729 2018-08-04 stsp struct tog_view *view;
128 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
129 e9424729 2018-08-04 stsp FILE *f;
130 e9424729 2018-08-04 stsp const char *path;
131 e9424729 2018-08-04 stsp int *first_displayed_line;
132 e9424729 2018-08-04 stsp int *last_displayed_line;
133 e9424729 2018-08-04 stsp int *selected_line;
134 e9424729 2018-08-04 stsp int *quit;
135 e5a0f69f 2018-08-18 stsp int *eof;
136 e9424729 2018-08-04 stsp };
137 e9424729 2018-08-04 stsp
138 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
139 e9424729 2018-08-04 stsp const char *path;
140 e9424729 2018-08-04 stsp struct got_repository *repo;
141 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
142 e9424729 2018-08-04 stsp int *complete;
143 e9424729 2018-08-04 stsp };
144 e9424729 2018-08-04 stsp
145 e9424729 2018-08-04 stsp struct tog_blame {
146 e9424729 2018-08-04 stsp FILE *f;
147 e9424729 2018-08-04 stsp size_t filesize;
148 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
149 e9424729 2018-08-04 stsp size_t nlines;
150 e9424729 2018-08-04 stsp pthread_t thread;
151 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
152 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
153 e9424729 2018-08-04 stsp const char *path;
154 e9424729 2018-08-04 stsp };
155 e9424729 2018-08-04 stsp
156 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
157 7cbe629d 2018-08-04 stsp int first_displayed_line;
158 7cbe629d 2018-08-04 stsp int last_displayed_line;
159 7cbe629d 2018-08-04 stsp int selected_line;
160 7cbe629d 2018-08-04 stsp int blame_complete;
161 e5a0f69f 2018-08-18 stsp int eof;
162 e5a0f69f 2018-08-18 stsp int done;
163 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
164 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
165 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
166 e5a0f69f 2018-08-18 stsp char *path;
167 7cbe629d 2018-08-04 stsp struct got_repository *repo;
168 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
169 e9424729 2018-08-04 stsp struct tog_blame blame;
170 ad80ab7b 2018-08-04 stsp };
171 ad80ab7b 2018-08-04 stsp
172 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
173 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
174 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
175 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
176 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
177 ad80ab7b 2018-08-04 stsp int selected;
178 ad80ab7b 2018-08-04 stsp };
179 ad80ab7b 2018-08-04 stsp
180 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
181 ad80ab7b 2018-08-04 stsp
182 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
183 ad80ab7b 2018-08-04 stsp char *tree_label;
184 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
185 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
186 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
187 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
188 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
189 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
190 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
191 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
192 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
193 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
194 7cbe629d 2018-08-04 stsp };
195 7cbe629d 2018-08-04 stsp
196 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
197 cc3c9aac 2018-08-01 stsp struct tog_view {
198 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
199 26ed57b2 2018-05-19 stsp WINDOW *window;
200 26ed57b2 2018-05-19 stsp PANEL *panel;
201 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
202 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
203 317b96e7 2018-09-02 stsp int focussed;
204 6d0fee91 2018-08-01 stsp struct tog_view *parent;
205 bcbd79e2 2018-08-19 stsp struct tog_view *child;
206 5dc9f4bc 2018-08-04 stsp
207 5dc9f4bc 2018-08-04 stsp /* type-specific state */
208 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
209 5dc9f4bc 2018-08-04 stsp union {
210 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
211 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
212 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
213 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
214 5dc9f4bc 2018-08-04 stsp } state;
215 e5a0f69f 2018-08-18 stsp
216 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
217 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
218 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
219 bcbd79e2 2018-08-19 stsp const struct got_error *(*set_child)(struct tog_view *,
220 bcbd79e2 2018-08-19 stsp struct tog_view *);
221 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
222 cc3c9aac 2018-08-01 stsp };
223 cd0acaa7 2018-05-20 stsp
224 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
225 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
226 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
227 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
228 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
229 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
230 e5a0f69f 2018-08-18 stsp
231 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
232 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
233 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
234 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
235 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
236 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
237 bcbd79e2 2018-08-19 stsp static const struct got_error* set_child_log_view(struct tog_view *,
238 bcbd79e2 2018-08-19 stsp struct tog_view *);
239 e5a0f69f 2018-08-18 stsp
240 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
241 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
242 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
243 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
244 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
245 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
246 e5a0f69f 2018-08-18 stsp
247 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
248 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
250 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
251 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
252 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
253 26ed57b2 2018-05-19 stsp
254 e5a0f69f 2018-08-18 stsp static const struct got_error *
255 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
256 ea5e7bb5 2018-08-01 stsp {
257 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
258 e5a0f69f 2018-08-18 stsp
259 bcbd79e2 2018-08-19 stsp if (view->child)
260 bcbd79e2 2018-08-19 stsp view->child->parent = NULL;
261 bcbd79e2 2018-08-19 stsp if (view->parent)
262 bcbd79e2 2018-08-19 stsp view->parent->child = NULL;
263 e5a0f69f 2018-08-18 stsp if (view->close)
264 e5a0f69f 2018-08-18 stsp err = view->close(view);
265 ea5e7bb5 2018-08-01 stsp if (view->panel)
266 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
267 ea5e7bb5 2018-08-01 stsp if (view->window)
268 ea5e7bb5 2018-08-01 stsp delwin(view->window);
269 ea5e7bb5 2018-08-01 stsp free(view);
270 e5a0f69f 2018-08-18 stsp return err;
271 ea5e7bb5 2018-08-01 stsp }
272 ea5e7bb5 2018-08-01 stsp
273 ea5e7bb5 2018-08-01 stsp static struct tog_view *
274 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
275 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
276 ea5e7bb5 2018-08-01 stsp {
277 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
278 ea5e7bb5 2018-08-01 stsp
279 ea5e7bb5 2018-08-01 stsp if (view == NULL)
280 ea5e7bb5 2018-08-01 stsp return NULL;
281 ea5e7bb5 2018-08-01 stsp
282 dd04dd3f 2018-08-19 stsp if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 dd04dd3f 2018-08-19 stsp begin_x = parent->ncols - 80;
284 dd04dd3f 2018-08-19 stsp
285 6d0fee91 2018-08-01 stsp view->parent = parent;
286 bcbd79e2 2018-08-19 stsp if (parent)
287 bcbd79e2 2018-08-19 stsp parent->child = view;
288 d6b05b5b 2018-08-04 stsp view->type = type;
289 f7d12f7e 2018-08-01 stsp view->lines = LINES;
290 f7d12f7e 2018-08-01 stsp view->cols = COLS;
291 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
292 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
293 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
294 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
295 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
296 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
297 96a765a8 2018-08-04 stsp view_close(view);
298 ea5e7bb5 2018-08-01 stsp return NULL;
299 ea5e7bb5 2018-08-01 stsp }
300 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
301 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
302 96a765a8 2018-08-04 stsp view_close(view);
303 ea5e7bb5 2018-08-01 stsp return NULL;
304 ea5e7bb5 2018-08-01 stsp }
305 ea5e7bb5 2018-08-01 stsp
306 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
307 ea5e7bb5 2018-08-01 stsp return view;
308 f7d12f7e 2018-08-01 stsp }
309 f7d12f7e 2018-08-01 stsp
310 4d8c2215 2018-08-19 stsp static const struct got_error *
311 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
312 cdf1ee82 2018-08-01 stsp {
313 e5a0f69f 2018-08-18 stsp const struct got_error *err;
314 e5a0f69f 2018-08-18 stsp
315 bcbd79e2 2018-08-19 stsp if (view->parent) {
316 bcbd79e2 2018-08-19 stsp err = view->parent->show(view->parent);
317 bcbd79e2 2018-08-19 stsp if (err)
318 bcbd79e2 2018-08-19 stsp return err;
319 bcbd79e2 2018-08-19 stsp show_panel(view->parent->panel);
320 bcbd79e2 2018-08-19 stsp }
321 bcbd79e2 2018-08-19 stsp
322 e5a0f69f 2018-08-18 stsp err = view->show(view);
323 e5a0f69f 2018-08-18 stsp if (err)
324 e5a0f69f 2018-08-18 stsp return err;
325 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
326 bcbd79e2 2018-08-19 stsp
327 bcbd79e2 2018-08-19 stsp if (view->child) {
328 bcbd79e2 2018-08-19 stsp err = view->child->show(view->child);
329 bcbd79e2 2018-08-19 stsp if (err)
330 bcbd79e2 2018-08-19 stsp return err;
331 bcbd79e2 2018-08-19 stsp show_panel(view->child->panel);
332 bcbd79e2 2018-08-19 stsp }
333 bcbd79e2 2018-08-19 stsp
334 cdf1ee82 2018-08-01 stsp update_panels();
335 e5a0f69f 2018-08-18 stsp doupdate();
336 e5a0f69f 2018-08-18 stsp
337 e5a0f69f 2018-08-18 stsp return err;
338 cdf1ee82 2018-08-01 stsp }
339 cdf1ee82 2018-08-01 stsp
340 4d8c2215 2018-08-19 stsp static const struct got_error *
341 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
342 f7d12f7e 2018-08-01 stsp {
343 f7d12f7e 2018-08-01 stsp int nlines, ncols;
344 f7d12f7e 2018-08-01 stsp
345 6d0fee91 2018-08-01 stsp while (view) {
346 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
347 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
348 6d0fee91 2018-08-01 stsp else
349 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
350 f7d12f7e 2018-08-01 stsp
351 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
352 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
353 6d0fee91 2018-08-01 stsp else
354 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
355 f7d12f7e 2018-08-01 stsp
356 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
357 6d0fee91 2018-08-01 stsp return got_error_from_errno();
358 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
359 f7d12f7e 2018-08-01 stsp
360 6d0fee91 2018-08-01 stsp view->nlines = nlines;
361 6d0fee91 2018-08-01 stsp view->ncols = ncols;
362 6d0fee91 2018-08-01 stsp view->lines = LINES;
363 6d0fee91 2018-08-01 stsp view->cols = COLS;
364 6d0fee91 2018-08-01 stsp
365 6d0fee91 2018-08-01 stsp view = view->parent;
366 6d0fee91 2018-08-01 stsp }
367 6d0fee91 2018-08-01 stsp
368 f7d12f7e 2018-08-01 stsp return NULL;
369 e5a0f69f 2018-08-18 stsp }
370 e5a0f69f 2018-08-18 stsp
371 e5a0f69f 2018-08-18 stsp static const struct got_error *
372 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
373 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
374 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
375 e5a0f69f 2018-08-18 stsp {
376 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
377 31607d6c 2018-08-18 stsp struct tog_view *next, *prev;
378 e5a0f69f 2018-08-18 stsp int ch;
379 e5a0f69f 2018-08-18 stsp
380 e5a0f69f 2018-08-18 stsp *new = NULL;
381 e5a0f69f 2018-08-18 stsp *dead = NULL;
382 e5a0f69f 2018-08-18 stsp
383 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
384 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
385 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
386 e5a0f69f 2018-08-18 stsp switch (ch) {
387 e5a0f69f 2018-08-18 stsp case ERR:
388 48fcc512 2018-08-18 stsp break;
389 48fcc512 2018-08-18 stsp case '\t':
390 48fcc512 2018-08-18 stsp next = TAILQ_NEXT(view, entry);
391 48fcc512 2018-08-18 stsp if (next)
392 48fcc512 2018-08-18 stsp *focus = next;
393 48fcc512 2018-08-18 stsp else
394 48fcc512 2018-08-18 stsp *focus = TAILQ_FIRST(views);
395 317b96e7 2018-09-02 stsp view->focussed = 0;
396 317b96e7 2018-09-02 stsp (*focus)->focussed = 1;
397 e5a0f69f 2018-08-18 stsp break;
398 31607d6c 2018-08-18 stsp case KEY_BACKSPACE:
399 31607d6c 2018-08-18 stsp prev = TAILQ_PREV(view, tog_view_list_head, entry);
400 31607d6c 2018-08-18 stsp if (prev)
401 31607d6c 2018-08-18 stsp *focus = prev;
402 31607d6c 2018-08-18 stsp else
403 31607d6c 2018-08-18 stsp *focus = TAILQ_LAST(views, tog_view_list_head);
404 31607d6c 2018-08-18 stsp break;
405 e5a0f69f 2018-08-18 stsp case 'q':
406 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
407 e5a0f69f 2018-08-18 stsp *dead = view;
408 e4197bf9 2018-08-18 stsp break;
409 e4197bf9 2018-08-18 stsp case 'Q':
410 e4197bf9 2018-08-18 stsp *done = 1;
411 e5a0f69f 2018-08-18 stsp break;
412 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
413 e5a0f69f 2018-08-18 stsp err = view_resize(view);
414 e5a0f69f 2018-08-18 stsp if (err)
415 e5a0f69f 2018-08-18 stsp return err;
416 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
417 e5a0f69f 2018-08-18 stsp break;
418 e5a0f69f 2018-08-18 stsp default:
419 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
420 e5a0f69f 2018-08-18 stsp break;
421 e5a0f69f 2018-08-18 stsp }
422 e5a0f69f 2018-08-18 stsp
423 e5a0f69f 2018-08-18 stsp return err;
424 e5a0f69f 2018-08-18 stsp }
425 e5a0f69f 2018-08-18 stsp
426 e5a0f69f 2018-08-18 stsp static const struct got_error *
427 bcbd79e2 2018-08-19 stsp view_set_child(struct tog_view *view, struct tog_view *child)
428 bcbd79e2 2018-08-19 stsp {
429 bcbd79e2 2018-08-19 stsp const struct got_error *err;
430 bcbd79e2 2018-08-19 stsp
431 bcbd79e2 2018-08-19 stsp if (view->set_child) {
432 bcbd79e2 2018-08-19 stsp err = view->set_child(view, child);
433 bcbd79e2 2018-08-19 stsp if (err)
434 bcbd79e2 2018-08-19 stsp return err;
435 bcbd79e2 2018-08-19 stsp }
436 bcbd79e2 2018-08-19 stsp
437 bcbd79e2 2018-08-19 stsp view->child = child;
438 bcbd79e2 2018-08-19 stsp return NULL;
439 1a57306a 2018-09-02 stsp }
440 1a57306a 2018-09-02 stsp
441 1a57306a 2018-09-02 stsp void
442 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
443 1a57306a 2018-09-02 stsp {
444 1a57306a 2018-09-02 stsp if (view->child == NULL)
445 1a57306a 2018-09-02 stsp return;
446 1a57306a 2018-09-02 stsp
447 1a57306a 2018-09-02 stsp mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
448 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
449 bcbd79e2 2018-08-19 stsp }
450 bcbd79e2 2018-08-19 stsp
451 bcbd79e2 2018-08-19 stsp static const struct got_error *
452 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
453 e5a0f69f 2018-08-18 stsp {
454 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
455 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
456 e5a0f69f 2018-08-18 stsp struct tog_view *new_view, *dead_view;
457 e4197bf9 2018-08-18 stsp int done = 0;
458 e5a0f69f 2018-08-18 stsp
459 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
460 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
461 e5a0f69f 2018-08-18 stsp
462 317b96e7 2018-09-02 stsp view->focussed = 1;
463 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
464 e5a0f69f 2018-08-18 stsp err = view_show(view);
465 e5a0f69f 2018-08-18 stsp if (err)
466 e5a0f69f 2018-08-18 stsp break;
467 e4197bf9 2018-08-18 stsp err = view_input(&new_view, &dead_view, &view, &done,
468 e4197bf9 2018-08-18 stsp view, &views);
469 e5a0f69f 2018-08-18 stsp if (err)
470 e5a0f69f 2018-08-18 stsp break;
471 e5a0f69f 2018-08-18 stsp if (dead_view) {
472 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, dead_view, entry);
473 f8405c92 2018-09-02 stsp TAILQ_FOREACH(view, &views, entry) {
474 f8405c92 2018-09-02 stsp if (view->parent == dead_view)
475 f8405c92 2018-09-02 stsp view->parent = NULL;
476 f8405c92 2018-09-02 stsp }
477 e5a0f69f 2018-08-18 stsp if (dead_view->parent)
478 e5a0f69f 2018-08-18 stsp view = dead_view->parent;
479 e5a0f69f 2018-08-18 stsp else
480 e5a0f69f 2018-08-18 stsp view = TAILQ_LAST(&views, tog_view_list_head);
481 bcbd79e2 2018-08-19 stsp if (dead_view->child) {
482 bcbd79e2 2018-08-19 stsp TAILQ_REMOVE(&views, dead_view->child, entry);
483 bcbd79e2 2018-08-19 stsp err = view_close(dead_view->child);
484 bcbd79e2 2018-08-19 stsp if (err)
485 bcbd79e2 2018-08-19 stsp goto done;
486 bcbd79e2 2018-08-19 stsp }
487 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
488 e5a0f69f 2018-08-18 stsp if (err)
489 e5a0f69f 2018-08-18 stsp goto done;
490 e5a0f69f 2018-08-18 stsp }
491 bcbd79e2 2018-08-19 stsp if (new_view) {
492 bcbd79e2 2018-08-19 stsp /* TODO: de-duplicate! */
493 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
494 bcbd79e2 2018-08-19 stsp if (new_view->parent) {
495 bcbd79e2 2018-08-19 stsp err = view_set_child(new_view->parent, new_view);
496 bcbd79e2 2018-08-19 stsp if (err)
497 bcbd79e2 2018-08-19 stsp goto done;
498 317b96e7 2018-09-02 stsp new_view->parent->focussed = 0;
499 bcbd79e2 2018-08-19 stsp }
500 bcbd79e2 2018-08-19 stsp view = new_view;
501 317b96e7 2018-09-02 stsp view->focussed = 1;
502 bcbd79e2 2018-08-19 stsp }
503 e5a0f69f 2018-08-18 stsp }
504 e5a0f69f 2018-08-18 stsp done:
505 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
506 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
507 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
508 e5a0f69f 2018-08-18 stsp view_close(view);
509 e5a0f69f 2018-08-18 stsp }
510 e5a0f69f 2018-08-18 stsp return err;
511 ea5e7bb5 2018-08-01 stsp }
512 ea5e7bb5 2018-08-01 stsp
513 4ed7e80c 2018-05-20 stsp __dead static void
514 9f7d7167 2018-04-29 stsp usage_log(void)
515 9f7d7167 2018-04-29 stsp {
516 80ddbec8 2018-04-29 stsp endwin();
517 c70c5802 2018-08-01 stsp fprintf(stderr,
518 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
519 9f7d7167 2018-04-29 stsp getprogname());
520 9f7d7167 2018-04-29 stsp exit(1);
521 80ddbec8 2018-04-29 stsp }
522 80ddbec8 2018-04-29 stsp
523 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
524 80ddbec8 2018-04-29 stsp static const struct got_error *
525 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
526 963b370f 2018-05-20 stsp {
527 00dfcb92 2018-06-11 stsp char *vis = NULL;
528 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
529 963b370f 2018-05-20 stsp
530 963b370f 2018-05-20 stsp *ws = NULL;
531 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
532 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
533 00dfcb92 2018-06-11 stsp int vislen;
534 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
535 00dfcb92 2018-06-11 stsp return got_error_from_errno();
536 00dfcb92 2018-06-11 stsp
537 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
538 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
539 00dfcb92 2018-06-11 stsp if (err)
540 00dfcb92 2018-06-11 stsp return err;
541 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
542 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
543 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
544 a7f50699 2018-06-11 stsp goto done;
545 a7f50699 2018-06-11 stsp }
546 00dfcb92 2018-06-11 stsp }
547 963b370f 2018-05-20 stsp
548 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
549 a7f50699 2018-06-11 stsp if (*ws == NULL) {
550 a7f50699 2018-06-11 stsp err = got_error_from_errno();
551 a7f50699 2018-06-11 stsp goto done;
552 a7f50699 2018-06-11 stsp }
553 963b370f 2018-05-20 stsp
554 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
555 963b370f 2018-05-20 stsp err = got_error_from_errno();
556 a7f50699 2018-06-11 stsp done:
557 00dfcb92 2018-06-11 stsp free(vis);
558 963b370f 2018-05-20 stsp if (err) {
559 963b370f 2018-05-20 stsp free(*ws);
560 963b370f 2018-05-20 stsp *ws = NULL;
561 963b370f 2018-05-20 stsp *wlen = 0;
562 963b370f 2018-05-20 stsp }
563 963b370f 2018-05-20 stsp return err;
564 963b370f 2018-05-20 stsp }
565 963b370f 2018-05-20 stsp
566 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
567 963b370f 2018-05-20 stsp static const struct got_error *
568 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
569 963b370f 2018-05-20 stsp {
570 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
571 963b370f 2018-05-20 stsp int cols = 0;
572 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
573 963b370f 2018-05-20 stsp size_t wlen;
574 963b370f 2018-05-20 stsp int i;
575 963b370f 2018-05-20 stsp
576 963b370f 2018-05-20 stsp *wlinep = NULL;
577 b700b5d6 2018-07-10 stsp *widthp = 0;
578 963b370f 2018-05-20 stsp
579 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
580 963b370f 2018-05-20 stsp if (err)
581 963b370f 2018-05-20 stsp return err;
582 963b370f 2018-05-20 stsp
583 963b370f 2018-05-20 stsp i = 0;
584 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
585 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
586 963b370f 2018-05-20 stsp switch (width) {
587 963b370f 2018-05-20 stsp case 0:
588 b700b5d6 2018-07-10 stsp i++;
589 963b370f 2018-05-20 stsp break;
590 963b370f 2018-05-20 stsp case 1:
591 963b370f 2018-05-20 stsp case 2:
592 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
593 b700b5d6 2018-07-10 stsp cols += width;
594 b700b5d6 2018-07-10 stsp i++;
595 b700b5d6 2018-07-10 stsp }
596 963b370f 2018-05-20 stsp break;
597 963b370f 2018-05-20 stsp case -1:
598 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
599 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
600 b700b5d6 2018-07-10 stsp i++;
601 963b370f 2018-05-20 stsp break;
602 963b370f 2018-05-20 stsp default:
603 963b370f 2018-05-20 stsp err = got_error_from_errno();
604 963b370f 2018-05-20 stsp goto done;
605 963b370f 2018-05-20 stsp }
606 963b370f 2018-05-20 stsp }
607 963b370f 2018-05-20 stsp wline[i] = L'\0';
608 b700b5d6 2018-07-10 stsp if (widthp)
609 b700b5d6 2018-07-10 stsp *widthp = cols;
610 963b370f 2018-05-20 stsp done:
611 963b370f 2018-05-20 stsp if (err)
612 963b370f 2018-05-20 stsp free(wline);
613 963b370f 2018-05-20 stsp else
614 963b370f 2018-05-20 stsp *wlinep = wline;
615 963b370f 2018-05-20 stsp return err;
616 963b370f 2018-05-20 stsp }
617 963b370f 2018-05-20 stsp
618 963b370f 2018-05-20 stsp static const struct got_error *
619 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
620 2814baeb 2018-08-01 stsp struct got_object_id *id)
621 80ddbec8 2018-04-29 stsp {
622 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
623 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
624 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
625 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
626 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
627 bb737323 2018-05-20 stsp int author_width, logmsg_width;
628 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
629 80ddbec8 2018-04-29 stsp char *line = NULL;
630 bb737323 2018-05-20 stsp int col, limit;
631 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
632 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
633 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
634 80ddbec8 2018-04-29 stsp
635 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
636 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
637 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
638 b39d25c7 2018-07-10 stsp
639 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
640 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
641 b39d25c7 2018-07-10 stsp else
642 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
643 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
644 b39d25c7 2018-07-10 stsp col = limit + 1;
645 b39d25c7 2018-07-10 stsp if (col > avail)
646 b39d25c7 2018-07-10 stsp goto done;
647 b39d25c7 2018-07-10 stsp
648 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
649 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
650 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
651 80ddbec8 2018-04-29 stsp goto done;
652 80ddbec8 2018-04-29 stsp }
653 6d9fbc00 2018-04-29 stsp author = author0;
654 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
655 6d9fbc00 2018-04-29 stsp if (smallerthan)
656 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
657 6d9fbc00 2018-04-29 stsp else {
658 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
659 6d9fbc00 2018-04-29 stsp if (at)
660 6d9fbc00 2018-04-29 stsp *at = '\0';
661 6d9fbc00 2018-04-29 stsp }
662 b39d25c7 2018-07-10 stsp limit = avail - col;
663 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
664 bb737323 2018-05-20 stsp if (err)
665 bb737323 2018-05-20 stsp goto done;
666 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
667 bb737323 2018-05-20 stsp col += author_width;
668 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
669 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
670 bb737323 2018-05-20 stsp col++;
671 bb737323 2018-05-20 stsp author_width++;
672 bb737323 2018-05-20 stsp }
673 9c2eaf34 2018-05-20 stsp if (col > avail)
674 9c2eaf34 2018-05-20 stsp goto done;
675 80ddbec8 2018-04-29 stsp
676 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
677 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
678 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
679 6d9fbc00 2018-04-29 stsp goto done;
680 6d9fbc00 2018-04-29 stsp }
681 bb737323 2018-05-20 stsp logmsg = logmsg0;
682 bb737323 2018-05-20 stsp while (*logmsg == '\n')
683 bb737323 2018-05-20 stsp logmsg++;
684 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
685 bb737323 2018-05-20 stsp if (newline)
686 bb737323 2018-05-20 stsp *newline = '\0';
687 bb737323 2018-05-20 stsp limit = avail - col;
688 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
689 bb737323 2018-05-20 stsp if (err)
690 bb737323 2018-05-20 stsp goto done;
691 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
692 bb737323 2018-05-20 stsp col += logmsg_width;
693 bb737323 2018-05-20 stsp while (col <= avail) {
694 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
695 bb737323 2018-05-20 stsp col++;
696 881b2d3e 2018-04-30 stsp }
697 80ddbec8 2018-04-29 stsp done:
698 80ddbec8 2018-04-29 stsp free(logmsg0);
699 bb737323 2018-05-20 stsp free(wlogmsg);
700 6d9fbc00 2018-04-29 stsp free(author0);
701 bb737323 2018-05-20 stsp free(wauthor);
702 80ddbec8 2018-04-29 stsp free(line);
703 80ddbec8 2018-04-29 stsp return err;
704 80ddbec8 2018-04-29 stsp }
705 26ed57b2 2018-05-19 stsp
706 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
707 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
708 899d86c2 2018-05-10 stsp struct got_object_id *id)
709 80ddbec8 2018-04-29 stsp {
710 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
711 80ddbec8 2018-04-29 stsp
712 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
713 80ddbec8 2018-04-29 stsp if (entry == NULL)
714 899d86c2 2018-05-10 stsp return NULL;
715 99db9666 2018-05-07 stsp
716 899d86c2 2018-05-10 stsp entry->id = id;
717 99db9666 2018-05-07 stsp entry->commit = commit;
718 899d86c2 2018-05-10 stsp return entry;
719 99db9666 2018-05-07 stsp }
720 80ddbec8 2018-04-29 stsp
721 99db9666 2018-05-07 stsp static void
722 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
723 99db9666 2018-05-07 stsp {
724 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
725 99db9666 2018-05-07 stsp
726 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
727 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
728 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
729 ecb28ae0 2018-07-16 stsp commits->ncommits--;
730 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
731 99db9666 2018-05-07 stsp free(entry);
732 99db9666 2018-05-07 stsp }
733 99db9666 2018-05-07 stsp
734 99db9666 2018-05-07 stsp static void
735 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
736 99db9666 2018-05-07 stsp {
737 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
738 99db9666 2018-05-07 stsp pop_commit(commits);
739 c4972b91 2018-05-07 stsp }
740 c4972b91 2018-05-07 stsp
741 c4972b91 2018-05-07 stsp static const struct got_error *
742 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
743 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
744 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
745 c4972b91 2018-05-07 stsp {
746 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
747 899d86c2 2018-05-10 stsp struct got_object_id *id;
748 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
749 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
750 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
751 c4972b91 2018-05-07 stsp
752 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
753 c4972b91 2018-05-07 stsp if (err)
754 c4972b91 2018-05-07 stsp return err;
755 c4972b91 2018-05-07 stsp
756 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
757 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
758 9ba79e04 2018-06-11 stsp int nfetched;
759 899d86c2 2018-05-10 stsp
760 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
761 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
762 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
763 9ba79e04 2018-06-11 stsp return err;
764 9ba79e04 2018-06-11 stsp
765 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
766 9ba79e04 2018-06-11 stsp if (err)
767 9ba79e04 2018-06-11 stsp return err;
768 c4972b91 2018-05-07 stsp }
769 c4972b91 2018-05-07 stsp
770 9ba79e04 2018-06-11 stsp while (1) {
771 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
772 899d86c2 2018-05-10 stsp
773 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
774 9ba79e04 2018-06-11 stsp if (err) {
775 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
776 ecb28ae0 2018-07-16 stsp break;
777 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
778 9ba79e04 2018-06-11 stsp err = NULL;
779 ecb28ae0 2018-07-16 stsp break;
780 ecb28ae0 2018-07-16 stsp }
781 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
782 ecb28ae0 2018-07-16 stsp graph, 1, repo);
783 ecb28ae0 2018-07-16 stsp if (err)
784 ecb28ae0 2018-07-16 stsp return err;
785 ecb28ae0 2018-07-16 stsp continue;
786 9ba79e04 2018-06-11 stsp }
787 ecb28ae0 2018-07-16 stsp if (id == NULL)
788 ecb28ae0 2018-07-16 stsp break;
789 899d86c2 2018-05-10 stsp
790 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
791 9ba79e04 2018-06-11 stsp if (err)
792 9ba79e04 2018-06-11 stsp break;
793 899d86c2 2018-05-10 stsp
794 c8f60bff 2018-07-23 stsp if (!is_root_path) {
795 ecb28ae0 2018-07-16 stsp struct got_object *obj;
796 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
797 ecb28ae0 2018-07-16 stsp int changed = 0;
798 ecb28ae0 2018-07-16 stsp
799 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
800 ecb28ae0 2018-07-16 stsp if (err) {
801 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
802 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
803 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
804 ecb28ae0 2018-07-16 stsp /* History stops here. */
805 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
806 ecb28ae0 2018-07-16 stsp }
807 ecb28ae0 2018-07-16 stsp break;
808 ecb28ae0 2018-07-16 stsp }
809 ecb28ae0 2018-07-16 stsp found_obj = 1;
810 ecb28ae0 2018-07-16 stsp
811 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
812 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
813 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
814 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
815 ecb28ae0 2018-07-16 stsp pid->id, path);
816 ecb28ae0 2018-07-16 stsp if (err) {
817 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
818 ecb28ae0 2018-07-16 stsp got_object_close(obj);
819 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
820 ecb28ae0 2018-07-16 stsp break;
821 ecb28ae0 2018-07-16 stsp }
822 ecb28ae0 2018-07-16 stsp err = NULL;
823 ecb28ae0 2018-07-16 stsp changed = 1;
824 ecb28ae0 2018-07-16 stsp } else {
825 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
826 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
827 c2f16475 2018-07-23 stsp if (id == NULL) {
828 c2f16475 2018-07-23 stsp err = got_error_from_errno();
829 c8f60bff 2018-07-23 stsp got_object_close(obj);
830 c8f60bff 2018-07-23 stsp got_object_close(pobj);
831 c2f16475 2018-07-23 stsp break;
832 c2f16475 2018-07-23 stsp }
833 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
834 c2f16475 2018-07-23 stsp if (pid == NULL) {
835 c2f16475 2018-07-23 stsp err = got_error_from_errno();
836 c2f16475 2018-07-23 stsp free(id);
837 c8f60bff 2018-07-23 stsp got_object_close(obj);
838 c8f60bff 2018-07-23 stsp got_object_close(pobj);
839 c2f16475 2018-07-23 stsp break;
840 c2f16475 2018-07-23 stsp }
841 c2f16475 2018-07-23 stsp changed =
842 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
843 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
844 c2f16475 2018-07-23 stsp free(id);
845 c2f16475 2018-07-23 stsp free(pid);
846 ecb28ae0 2018-07-16 stsp }
847 ecb28ae0 2018-07-16 stsp }
848 c8f60bff 2018-07-23 stsp got_object_close(obj);
849 ecb28ae0 2018-07-16 stsp if (!changed) {
850 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
851 ecb28ae0 2018-07-16 stsp continue;
852 ecb28ae0 2018-07-16 stsp }
853 ecb28ae0 2018-07-16 stsp }
854 ecb28ae0 2018-07-16 stsp
855 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
856 9ba79e04 2018-06-11 stsp if (entry == NULL) {
857 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
858 9ba79e04 2018-06-11 stsp break;
859 9ba79e04 2018-06-11 stsp }
860 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
861 ecb28ae0 2018-07-16 stsp nqueued++;
862 ecb28ae0 2018-07-16 stsp commits->ncommits++;
863 899d86c2 2018-05-10 stsp }
864 899d86c2 2018-05-10 stsp
865 9ba79e04 2018-06-11 stsp return err;
866 99db9666 2018-05-07 stsp }
867 99db9666 2018-05-07 stsp
868 99db9666 2018-05-07 stsp static const struct got_error *
869 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
870 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
871 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
872 ecb28ae0 2018-07-16 stsp const char *path)
873 899d86c2 2018-05-10 stsp {
874 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
875 899d86c2 2018-05-10 stsp
876 9ba79e04 2018-06-11 stsp *pentry = NULL;
877 899d86c2 2018-05-10 stsp
878 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
879 ecb28ae0 2018-07-16 stsp if (err)
880 9ba79e04 2018-06-11 stsp return err;
881 899d86c2 2018-05-10 stsp
882 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
883 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
884 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
885 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
886 899d86c2 2018-05-10 stsp
887 9ba79e04 2018-06-11 stsp return NULL;
888 899d86c2 2018-05-10 stsp }
889 899d86c2 2018-05-10 stsp
890 899d86c2 2018-05-10 stsp static const struct got_error *
891 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
892 99db9666 2018-05-07 stsp {
893 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
894 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
895 99db9666 2018-05-07 stsp
896 9ba79e04 2018-06-11 stsp *head_id = NULL;
897 899d86c2 2018-05-10 stsp
898 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
899 99db9666 2018-05-07 stsp if (err)
900 99db9666 2018-05-07 stsp return err;
901 99db9666 2018-05-07 stsp
902 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
903 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
904 9ba79e04 2018-06-11 stsp if (err) {
905 9ba79e04 2018-06-11 stsp *head_id = NULL;
906 99db9666 2018-05-07 stsp return err;
907 0553a4e3 2018-04-30 stsp }
908 80ddbec8 2018-04-29 stsp
909 9ba79e04 2018-06-11 stsp return NULL;
910 0553a4e3 2018-04-30 stsp }
911 0553a4e3 2018-04-30 stsp
912 0553a4e3 2018-04-30 stsp static const struct got_error *
913 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
914 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
915 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
916 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
917 a7f40148 2018-07-18 stsp const char *path)
918 0553a4e3 2018-04-30 stsp {
919 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
920 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
921 ecb28ae0 2018-07-16 stsp int ncommits, width;
922 867c6645 2018-07-10 stsp char *id_str, *header;
923 ecb28ae0 2018-07-16 stsp wchar_t *wline;
924 0553a4e3 2018-04-30 stsp
925 e0d42f60 2018-07-22 stsp entry = first;
926 e0d42f60 2018-07-22 stsp ncommits = 0;
927 e0d42f60 2018-07-22 stsp while (entry) {
928 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
929 e0d42f60 2018-07-22 stsp *selected = entry;
930 e0d42f60 2018-07-22 stsp break;
931 e0d42f60 2018-07-22 stsp }
932 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
933 e0d42f60 2018-07-22 stsp ncommits++;
934 e0d42f60 2018-07-22 stsp }
935 e0d42f60 2018-07-22 stsp
936 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
937 867c6645 2018-07-10 stsp if (err)
938 867c6645 2018-07-10 stsp return err;
939 867c6645 2018-07-10 stsp
940 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
941 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
942 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
943 ecb28ae0 2018-07-16 stsp free(id_str);
944 ecb28ae0 2018-07-16 stsp return err;
945 ecb28ae0 2018-07-16 stsp }
946 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
947 867c6645 2018-07-10 stsp err = got_error_from_errno();
948 867c6645 2018-07-10 stsp free(id_str);
949 867c6645 2018-07-10 stsp return err;
950 867c6645 2018-07-10 stsp }
951 ecb28ae0 2018-07-16 stsp free(id_str);
952 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
953 ecb28ae0 2018-07-16 stsp if (err) {
954 ecb28ae0 2018-07-16 stsp free(header);
955 ecb28ae0 2018-07-16 stsp return err;
956 ecb28ae0 2018-07-16 stsp }
957 ecb28ae0 2018-07-16 stsp free(header);
958 867c6645 2018-07-10 stsp
959 2814baeb 2018-08-01 stsp werase(view->window);
960 867c6645 2018-07-10 stsp
961 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
962 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
963 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
964 ecb28ae0 2018-07-16 stsp free(wline);
965 ecb28ae0 2018-07-16 stsp if (limit <= 1)
966 ecb28ae0 2018-07-16 stsp return NULL;
967 0553a4e3 2018-04-30 stsp
968 899d86c2 2018-05-10 stsp entry = first;
969 899d86c2 2018-05-10 stsp *last = first;
970 867c6645 2018-07-10 stsp ncommits = 0;
971 899d86c2 2018-05-10 stsp while (entry) {
972 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
973 899d86c2 2018-05-10 stsp break;
974 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
975 2814baeb 2018-08-01 stsp wstandout(view->window);
976 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
977 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
978 2814baeb 2018-08-01 stsp wstandend(view->window);
979 0553a4e3 2018-04-30 stsp if (err)
980 0553a4e3 2018-04-30 stsp break;
981 0553a4e3 2018-04-30 stsp ncommits++;
982 899d86c2 2018-05-10 stsp *last = entry;
983 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
984 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
985 a7f40148 2018-07-18 stsp 0, repo, path);
986 a7f40148 2018-07-18 stsp if (err) {
987 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
988 a7f40148 2018-07-18 stsp return err;
989 a7f40148 2018-07-18 stsp err = NULL;
990 a7f40148 2018-07-18 stsp }
991 a7f40148 2018-07-18 stsp }
992 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
993 80ddbec8 2018-04-29 stsp }
994 80ddbec8 2018-04-29 stsp
995 1a57306a 2018-09-02 stsp view_vborder(view);
996 0553a4e3 2018-04-30 stsp
997 80ddbec8 2018-04-29 stsp return err;
998 9f7d7167 2018-04-29 stsp }
999 07b55e75 2018-05-10 stsp
1000 07b55e75 2018-05-10 stsp static void
1001 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1002 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1003 07b55e75 2018-05-10 stsp {
1004 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1005 07b55e75 2018-05-10 stsp int nscrolled = 0;
1006 07b55e75 2018-05-10 stsp
1007 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1008 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1009 07b55e75 2018-05-10 stsp return;
1010 9f7d7167 2018-04-29 stsp
1011 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1012 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1013 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1014 07b55e75 2018-05-10 stsp if (entry) {
1015 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1016 07b55e75 2018-05-10 stsp nscrolled++;
1017 07b55e75 2018-05-10 stsp }
1018 07b55e75 2018-05-10 stsp }
1019 aa075928 2018-05-10 stsp }
1020 aa075928 2018-05-10 stsp
1021 aa075928 2018-05-10 stsp static const struct got_error *
1022 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1023 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
1024 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
1025 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
1026 aa075928 2018-05-10 stsp {
1027 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1028 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1029 aa075928 2018-05-10 stsp int nscrolled = 0;
1030 aa075928 2018-05-10 stsp
1031 aa075928 2018-05-10 stsp do {
1032 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
1033 aa075928 2018-05-10 stsp if (pentry == NULL) {
1034 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
1035 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
1036 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
1037 aa075928 2018-05-10 stsp break;
1038 aa075928 2018-05-10 stsp }
1039 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
1040 aa075928 2018-05-10 stsp
1041 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1042 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1043 dd0a52c1 2018-05-20 stsp break;
1044 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1045 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1046 aa075928 2018-05-10 stsp
1047 dd0a52c1 2018-05-20 stsp return err;
1048 07b55e75 2018-05-10 stsp }
1049 4a7f7875 2018-05-10 stsp
1050 cd0acaa7 2018-05-20 stsp static const struct got_error *
1051 e5a0f69f 2018-08-18 stsp show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1052 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1053 cd0acaa7 2018-05-20 stsp {
1054 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1055 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1056 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1057 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1058 cd0acaa7 2018-05-20 stsp
1059 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
1060 cd0acaa7 2018-05-20 stsp if (err)
1061 cd0acaa7 2018-05-20 stsp return err;
1062 cd0acaa7 2018-05-20 stsp
1063 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1064 9ba79e04 2018-06-11 stsp if (parent_id) {
1065 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1066 cd0acaa7 2018-05-20 stsp if (err)
1067 cd0acaa7 2018-05-20 stsp goto done;
1068 cd0acaa7 2018-05-20 stsp }
1069 cd0acaa7 2018-05-20 stsp
1070 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1071 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1072 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1073 ea5e7bb5 2018-08-01 stsp goto done;
1074 ea5e7bb5 2018-08-01 stsp }
1075 ea5e7bb5 2018-08-01 stsp
1076 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1077 e5a0f69f 2018-08-18 stsp if (err == NULL)
1078 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1079 cd0acaa7 2018-05-20 stsp done:
1080 cd0acaa7 2018-05-20 stsp if (obj1)
1081 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1082 cd0acaa7 2018-05-20 stsp if (obj2)
1083 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1084 cd0acaa7 2018-05-20 stsp return err;
1085 4a7f7875 2018-05-10 stsp }
1086 4a7f7875 2018-05-10 stsp
1087 80ddbec8 2018-04-29 stsp static const struct got_error *
1088 e5a0f69f 2018-08-18 stsp browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1089 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1090 9343a5fb 2018-06-23 stsp {
1091 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1092 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1093 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1094 9343a5fb 2018-06-23 stsp
1095 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1096 9343a5fb 2018-06-23 stsp if (err)
1097 9343a5fb 2018-06-23 stsp return err;
1098 9343a5fb 2018-06-23 stsp
1099 e5a0f69f 2018-08-18 stsp tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1100 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1101 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1102 e5a0f69f 2018-08-18 stsp
1103 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1104 ad80ab7b 2018-08-04 stsp if (err)
1105 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1106 e5a0f69f 2018-08-18 stsp else
1107 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1108 9343a5fb 2018-06-23 stsp return err;
1109 bcbd79e2 2018-08-19 stsp }
1110 bcbd79e2 2018-08-19 stsp
1111 bcbd79e2 2018-08-19 stsp static const struct got_error *
1112 bcbd79e2 2018-08-19 stsp set_child_log_view(struct tog_view *view, struct tog_view *child)
1113 bcbd79e2 2018-08-19 stsp {
1114 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *s = &view->state.log;
1115 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1116 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *commit, *child_entry = NULL;
1117 bcbd79e2 2018-08-19 stsp int selected_idx = 0;
1118 bcbd79e2 2018-08-19 stsp
1119 bcbd79e2 2018-08-19 stsp if (child->type != TOG_VIEW_DIFF)
1120 bcbd79e2 2018-08-19 stsp return NULL;
1121 bcbd79e2 2018-08-19 stsp ds = &child->state.diff;
1122 bcbd79e2 2018-08-19 stsp
1123 bcbd79e2 2018-08-19 stsp TAILQ_FOREACH(commit, &s->commits.head, entry) {
1124 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, ds->id) == 0) {
1125 bcbd79e2 2018-08-19 stsp child_entry = commit;
1126 bcbd79e2 2018-08-19 stsp break;
1127 bcbd79e2 2018-08-19 stsp }
1128 bcbd79e2 2018-08-19 stsp }
1129 bcbd79e2 2018-08-19 stsp if (child_entry == NULL)
1130 bcbd79e2 2018-08-19 stsp return NULL;
1131 bcbd79e2 2018-08-19 stsp
1132 bcbd79e2 2018-08-19 stsp commit = s->first_displayed_entry;
1133 bcbd79e2 2018-08-19 stsp while (commit) {
1134 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1135 bcbd79e2 2018-08-19 stsp s->selected_entry = child_entry;
1136 bcbd79e2 2018-08-19 stsp s->selected = selected_idx;
1137 bcbd79e2 2018-08-19 stsp break;
1138 bcbd79e2 2018-08-19 stsp }
1139 bcbd79e2 2018-08-19 stsp if (commit == s->last_displayed_entry)
1140 bcbd79e2 2018-08-19 stsp break;
1141 bcbd79e2 2018-08-19 stsp selected_idx++;
1142 bcbd79e2 2018-08-19 stsp commit = TAILQ_NEXT(commit, entry);
1143 bcbd79e2 2018-08-19 stsp }
1144 bcbd79e2 2018-08-19 stsp
1145 bcbd79e2 2018-08-19 stsp return show_log_view(view);
1146 9343a5fb 2018-06-23 stsp }
1147 9343a5fb 2018-06-23 stsp
1148 9343a5fb 2018-06-23 stsp static const struct got_error *
1149 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1150 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1151 80ddbec8 2018-04-29 stsp {
1152 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1153 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
1154 ba4f502b 2018-08-04 stsp int nfetched;
1155 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1156 80ddbec8 2018-04-29 stsp
1157 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1158 ecb28ae0 2018-07-16 stsp if (err != NULL)
1159 ecb28ae0 2018-07-16 stsp goto done;
1160 ecb28ae0 2018-07-16 stsp
1161 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
1162 9ba79e04 2018-06-11 stsp if (err)
1163 9ba79e04 2018-06-11 stsp return err;
1164 9ba79e04 2018-06-11 stsp
1165 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
1166 fb2756b9 2018-08-04 stsp err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1167 9ba79e04 2018-06-11 stsp if (err)
1168 9ba79e04 2018-06-11 stsp goto done;
1169 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
1170 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1171 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1172 9ba79e04 2018-06-11 stsp
1173 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
1174 fb2756b9 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1175 fb2756b9 2018-08-04 stsp start_id, repo);
1176 9ba79e04 2018-06-11 stsp if (err)
1177 9ba79e04 2018-06-11 stsp goto done;
1178 9ba79e04 2018-06-11 stsp
1179 9ba79e04 2018-06-11 stsp /*
1180 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1181 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1182 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1183 9ba79e04 2018-06-11 stsp * updating the display.
1184 9ba79e04 2018-06-11 stsp */
1185 fb2756b9 2018-08-04 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1186 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1187 55198a88 2018-07-16 stsp if (err) {
1188 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1189 55198a88 2018-07-16 stsp goto done;
1190 55198a88 2018-07-16 stsp err = NULL;
1191 2814baeb 2018-08-01 stsp }
1192 2814baeb 2018-08-01 stsp
1193 1bfa490b 2018-08-18 stsp s->first_displayed_entry =
1194 1bfa490b 2018-08-18 stsp TAILQ_FIRST(&s->commits.head);
1195 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1196 fb2756b9 2018-08-04 stsp s->repo = repo;
1197 e5a0f69f 2018-08-18 stsp
1198 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1199 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1200 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1201 bcbd79e2 2018-08-19 stsp view->set_child = set_child_log_view;
1202 ba4f502b 2018-08-04 stsp done:
1203 ba4f502b 2018-08-04 stsp free(head_id);
1204 ba4f502b 2018-08-04 stsp return err;
1205 ba4f502b 2018-08-04 stsp }
1206 ba4f502b 2018-08-04 stsp
1207 e5a0f69f 2018-08-18 stsp static const struct got_error *
1208 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1209 ba4f502b 2018-08-04 stsp {
1210 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1211 4d7951a5 2018-08-04 stsp
1212 fb2756b9 2018-08-04 stsp if (s->graph)
1213 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1214 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1215 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1216 e5a0f69f 2018-08-18 stsp return NULL;
1217 ba4f502b 2018-08-04 stsp }
1218 ba4f502b 2018-08-04 stsp
1219 ba4f502b 2018-08-04 stsp static const struct got_error *
1220 bcbd79e2 2018-08-19 stsp update_diff_child_view(struct tog_view *parent,
1221 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *selected_entry, struct got_repository *repo)
1222 bcbd79e2 2018-08-19 stsp {
1223 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1224 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1225 bcbd79e2 2018-08-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1226 bcbd79e2 2018-08-19 stsp struct got_object_qid *parent_id;
1227 bcbd79e2 2018-08-19 stsp struct tog_view *child_view = parent->child;
1228 bcbd79e2 2018-08-19 stsp
1229 bcbd79e2 2018-08-19 stsp if (child_view == NULL)
1230 bcbd79e2 2018-08-19 stsp return NULL;
1231 bcbd79e2 2018-08-19 stsp if (child_view->type != TOG_VIEW_DIFF)
1232 bcbd79e2 2018-08-19 stsp return NULL;
1233 bcbd79e2 2018-08-19 stsp ds = &child_view->state.diff;
1234 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(ds->id, selected_entry->id) == 0)
1235 bcbd79e2 2018-08-19 stsp return NULL;
1236 bcbd79e2 2018-08-19 stsp
1237 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj2, repo, selected_entry->id);
1238 bcbd79e2 2018-08-19 stsp if (err)
1239 bcbd79e2 2018-08-19 stsp return err;
1240 bcbd79e2 2018-08-19 stsp
1241 bcbd79e2 2018-08-19 stsp parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1242 bcbd79e2 2018-08-19 stsp if (parent_id) {
1243 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj1, repo, parent_id->id);
1244 bcbd79e2 2018-08-19 stsp if (err)
1245 bcbd79e2 2018-08-19 stsp goto done;
1246 bcbd79e2 2018-08-19 stsp }
1247 bcbd79e2 2018-08-19 stsp
1248 bcbd79e2 2018-08-19 stsp err = close_diff_view(child_view);
1249 bcbd79e2 2018-08-19 stsp if (err)
1250 bcbd79e2 2018-08-19 stsp goto done;
1251 bcbd79e2 2018-08-19 stsp
1252 bcbd79e2 2018-08-19 stsp err = open_diff_view(child_view, obj1, obj2, repo);
1253 bcbd79e2 2018-08-19 stsp if (err)
1254 bcbd79e2 2018-08-19 stsp goto done;
1255 bcbd79e2 2018-08-19 stsp done:
1256 bcbd79e2 2018-08-19 stsp if (obj1)
1257 bcbd79e2 2018-08-19 stsp got_object_close(obj1);
1258 bcbd79e2 2018-08-19 stsp if (obj2)
1259 bcbd79e2 2018-08-19 stsp got_object_close(obj2);
1260 bcbd79e2 2018-08-19 stsp return err;
1261 bcbd79e2 2018-08-19 stsp }
1262 bcbd79e2 2018-08-19 stsp
1263 bcbd79e2 2018-08-19 stsp static const struct got_error *
1264 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1265 ba4f502b 2018-08-04 stsp {
1266 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1267 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1268 ba4f502b 2018-08-04 stsp
1269 bcbd79e2 2018-08-19 stsp err = draw_commits(view, &s->last_displayed_entry,
1270 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1271 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1272 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1273 bcbd79e2 2018-08-19 stsp if (err)
1274 bcbd79e2 2018-08-19 stsp return err;
1275 bcbd79e2 2018-08-19 stsp
1276 bcbd79e2 2018-08-19 stsp return update_diff_child_view(view, s->selected_entry, s->repo);
1277 e5a0f69f 2018-08-18 stsp }
1278 04cc582a 2018-08-01 stsp
1279 e5a0f69f 2018-08-18 stsp static const struct got_error *
1280 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1281 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1282 e5a0f69f 2018-08-18 stsp {
1283 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1284 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1285 80ddbec8 2018-04-29 stsp
1286 e5a0f69f 2018-08-18 stsp switch (ch) {
1287 e5a0f69f 2018-08-18 stsp case 'k':
1288 e5a0f69f 2018-08-18 stsp case KEY_UP:
1289 bcbd79e2 2018-08-19 stsp case '[':
1290 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1291 e5a0f69f 2018-08-18 stsp s->selected--;
1292 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1293 31120ada 2018-04-30 stsp break;
1294 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1295 e5a0f69f 2018-08-18 stsp &s->commits);
1296 e5a0f69f 2018-08-18 stsp break;
1297 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1298 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1299 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1300 e5a0f69f 2018-08-18 stsp s->selected = 0;
1301 80ddbec8 2018-04-29 stsp break;
1302 e5a0f69f 2018-08-18 stsp }
1303 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1304 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1305 e5a0f69f 2018-08-18 stsp break;
1306 e5a0f69f 2018-08-18 stsp case 'j':
1307 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1308 bcbd79e2 2018-08-19 stsp case ']':
1309 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1310 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1311 e5a0f69f 2018-08-18 stsp s->selected++;
1312 80ddbec8 2018-04-29 stsp break;
1313 e5a0f69f 2018-08-18 stsp }
1314 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1315 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, &s->commits,
1316 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1317 e5a0f69f 2018-08-18 stsp if (err) {
1318 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1319 e5a0f69f 2018-08-18 stsp break;
1320 4d7951a5 2018-08-04 stsp err = NULL;
1321 ecb28ae0 2018-07-16 stsp }
1322 e5a0f69f 2018-08-18 stsp break;
1323 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1324 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1325 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1326 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1327 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
1328 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1329 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1330 e5a0f69f 2018-08-18 stsp if (err == NULL)
1331 cd0acaa7 2018-05-20 stsp break;
1332 e5a0f69f 2018-08-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1333 9343a5fb 2018-06-23 stsp break;
1334 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1335 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1336 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1337 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1338 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1339 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1340 e5a0f69f 2018-08-18 stsp }
1341 e5a0f69f 2018-08-18 stsp err = NULL;
1342 e5a0f69f 2018-08-18 stsp break;
1343 80ddbec8 2018-04-29 stsp }
1344 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1345 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1346 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1347 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1348 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1349 e5a0f69f 2018-08-18 stsp break;
1350 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1351 e5a0f69f 2018-08-18 stsp case '\r':
1352 e5a0f69f 2018-08-18 stsp err = show_commit(new_view, view, s->selected_entry,
1353 e5a0f69f 2018-08-18 stsp s->repo);
1354 e5a0f69f 2018-08-18 stsp break;
1355 e5a0f69f 2018-08-18 stsp case 't':
1356 e5a0f69f 2018-08-18 stsp err = browse_commit(new_view, view, s->selected_entry,
1357 e5a0f69f 2018-08-18 stsp s->repo);
1358 e5a0f69f 2018-08-18 stsp break;
1359 e5a0f69f 2018-08-18 stsp default:
1360 e5a0f69f 2018-08-18 stsp break;
1361 899d86c2 2018-05-10 stsp }
1362 e5a0f69f 2018-08-18 stsp
1363 80ddbec8 2018-04-29 stsp return err;
1364 80ddbec8 2018-04-29 stsp }
1365 80ddbec8 2018-04-29 stsp
1366 4ed7e80c 2018-05-20 stsp static const struct got_error *
1367 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1368 9f7d7167 2018-04-29 stsp {
1369 80ddbec8 2018-04-29 stsp const struct got_error *error;
1370 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1371 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1372 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1373 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1374 80ddbec8 2018-04-29 stsp int ch;
1375 04cc582a 2018-08-01 stsp struct tog_view *view;
1376 80ddbec8 2018-04-29 stsp
1377 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1378 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1379 80ddbec8 2018-04-29 stsp err(1, "pledge");
1380 80ddbec8 2018-04-29 stsp #endif
1381 80ddbec8 2018-04-29 stsp
1382 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1383 80ddbec8 2018-04-29 stsp switch (ch) {
1384 80ddbec8 2018-04-29 stsp case 'c':
1385 80ddbec8 2018-04-29 stsp start_commit = optarg;
1386 80ddbec8 2018-04-29 stsp break;
1387 ecb28ae0 2018-07-16 stsp case 'r':
1388 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1389 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1390 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1391 ecb28ae0 2018-07-16 stsp break;
1392 80ddbec8 2018-04-29 stsp default:
1393 80ddbec8 2018-04-29 stsp usage();
1394 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1395 80ddbec8 2018-04-29 stsp }
1396 80ddbec8 2018-04-29 stsp }
1397 80ddbec8 2018-04-29 stsp
1398 80ddbec8 2018-04-29 stsp argc -= optind;
1399 80ddbec8 2018-04-29 stsp argv += optind;
1400 80ddbec8 2018-04-29 stsp
1401 ecb28ae0 2018-07-16 stsp if (argc == 0)
1402 ecb28ae0 2018-07-16 stsp path = strdup("");
1403 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1404 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1405 ecb28ae0 2018-07-16 stsp else
1406 80ddbec8 2018-04-29 stsp usage_log();
1407 ecb28ae0 2018-07-16 stsp if (path == NULL)
1408 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1409 80ddbec8 2018-04-29 stsp
1410 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1411 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1412 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1413 ecb28ae0 2018-07-16 stsp goto done;
1414 ecb28ae0 2018-07-16 stsp }
1415 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1416 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1417 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1418 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1419 ecb28ae0 2018-07-16 stsp goto done;
1420 ecb28ae0 2018-07-16 stsp }
1421 ecb28ae0 2018-07-16 stsp }
1422 ecb28ae0 2018-07-16 stsp
1423 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1424 80ddbec8 2018-04-29 stsp if (error != NULL)
1425 ecb28ae0 2018-07-16 stsp goto done;
1426 80ddbec8 2018-04-29 stsp
1427 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1428 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1429 80ddbec8 2018-04-29 stsp if (error != NULL)
1430 ecb28ae0 2018-07-16 stsp goto done;
1431 80ddbec8 2018-04-29 stsp } else {
1432 80ddbec8 2018-04-29 stsp struct got_object *obj;
1433 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1434 80ddbec8 2018-04-29 stsp if (error == NULL) {
1435 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1436 899d86c2 2018-05-10 stsp if (start_id == NULL)
1437 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1438 ecb28ae0 2018-07-16 stsp goto done;
1439 80ddbec8 2018-04-29 stsp }
1440 80ddbec8 2018-04-29 stsp }
1441 80ddbec8 2018-04-29 stsp if (error != NULL)
1442 ecb28ae0 2018-07-16 stsp goto done;
1443 ecb28ae0 2018-07-16 stsp
1444 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1445 04cc582a 2018-08-01 stsp if (view == NULL) {
1446 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1447 04cc582a 2018-08-01 stsp goto done;
1448 04cc582a 2018-08-01 stsp }
1449 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1450 ba4f502b 2018-08-04 stsp if (error)
1451 ba4f502b 2018-08-04 stsp goto done;
1452 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1453 ecb28ae0 2018-07-16 stsp done:
1454 ecb28ae0 2018-07-16 stsp free(repo_path);
1455 ecb28ae0 2018-07-16 stsp free(cwd);
1456 ecb28ae0 2018-07-16 stsp free(path);
1457 899d86c2 2018-05-10 stsp free(start_id);
1458 ecb28ae0 2018-07-16 stsp if (repo)
1459 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1460 80ddbec8 2018-04-29 stsp return error;
1461 9f7d7167 2018-04-29 stsp }
1462 9f7d7167 2018-04-29 stsp
1463 4ed7e80c 2018-05-20 stsp __dead static void
1464 9f7d7167 2018-04-29 stsp usage_diff(void)
1465 9f7d7167 2018-04-29 stsp {
1466 80ddbec8 2018-04-29 stsp endwin();
1467 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1468 9f7d7167 2018-04-29 stsp getprogname());
1469 9f7d7167 2018-04-29 stsp exit(1);
1470 b304db33 2018-05-20 stsp }
1471 b304db33 2018-05-20 stsp
1472 b304db33 2018-05-20 stsp static char *
1473 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1474 b304db33 2018-05-20 stsp {
1475 b304db33 2018-05-20 stsp char *line;
1476 b304db33 2018-05-20 stsp size_t linelen;
1477 b304db33 2018-05-20 stsp size_t lineno;
1478 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1479 b304db33 2018-05-20 stsp
1480 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1481 b304db33 2018-05-20 stsp if (len)
1482 b304db33 2018-05-20 stsp *len = linelen;
1483 b304db33 2018-05-20 stsp return line;
1484 26ed57b2 2018-05-19 stsp }
1485 26ed57b2 2018-05-19 stsp
1486 4ed7e80c 2018-05-20 stsp static const struct got_error *
1487 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1488 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1489 26ed57b2 2018-05-19 stsp {
1490 61e69b96 2018-05-20 stsp const struct got_error *err;
1491 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1492 b304db33 2018-05-20 stsp char *line;
1493 b304db33 2018-05-20 stsp size_t len;
1494 61e69b96 2018-05-20 stsp wchar_t *wline;
1495 e0b650dd 2018-05-20 stsp int width;
1496 26ed57b2 2018-05-19 stsp
1497 26ed57b2 2018-05-19 stsp rewind(f);
1498 f7d12f7e 2018-08-01 stsp werase(view->window);
1499 26ed57b2 2018-05-19 stsp
1500 26ed57b2 2018-05-19 stsp *eof = 0;
1501 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1502 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1503 26ed57b2 2018-05-19 stsp if (line == NULL) {
1504 26ed57b2 2018-05-19 stsp *eof = 1;
1505 26ed57b2 2018-05-19 stsp break;
1506 26ed57b2 2018-05-19 stsp }
1507 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1508 26ed57b2 2018-05-19 stsp free(line);
1509 26ed57b2 2018-05-19 stsp continue;
1510 26ed57b2 2018-05-19 stsp }
1511 26ed57b2 2018-05-19 stsp
1512 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1513 61e69b96 2018-05-20 stsp if (err) {
1514 61e69b96 2018-05-20 stsp free(line);
1515 2550e4c3 2018-07-13 stsp free(wline);
1516 61e69b96 2018-05-20 stsp return err;
1517 61e69b96 2018-05-20 stsp }
1518 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1519 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1520 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1521 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1522 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1523 26ed57b2 2018-05-19 stsp free(line);
1524 2550e4c3 2018-07-13 stsp free(wline);
1525 2550e4c3 2018-07-13 stsp wline = NULL;
1526 26ed57b2 2018-05-19 stsp }
1527 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1528 26ed57b2 2018-05-19 stsp
1529 1a57306a 2018-09-02 stsp view_vborder(view);
1530 26ed57b2 2018-05-19 stsp
1531 26ed57b2 2018-05-19 stsp return NULL;
1532 9f7d7167 2018-04-29 stsp }
1533 9f7d7167 2018-04-29 stsp
1534 4ed7e80c 2018-05-20 stsp static const struct got_error *
1535 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1536 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1537 26ed57b2 2018-05-19 stsp {
1538 26ed57b2 2018-05-19 stsp const struct got_error *err;
1539 26ed57b2 2018-05-19 stsp FILE *f;
1540 26ed57b2 2018-05-19 stsp
1541 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1542 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1543 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1544 26ed57b2 2018-05-19 stsp
1545 511a516b 2018-05-19 stsp f = got_opentemp();
1546 26ed57b2 2018-05-19 stsp if (f == NULL)
1547 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1548 26ed57b2 2018-05-19 stsp
1549 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1550 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1551 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1552 26ed57b2 2018-05-19 stsp break;
1553 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1554 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1555 26ed57b2 2018-05-19 stsp break;
1556 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1557 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1558 26ed57b2 2018-05-19 stsp break;
1559 26ed57b2 2018-05-19 stsp default:
1560 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1561 26ed57b2 2018-05-19 stsp }
1562 26ed57b2 2018-05-19 stsp
1563 26ed57b2 2018-05-19 stsp fflush(f);
1564 5dc9f4bc 2018-08-04 stsp
1565 bcbd79e2 2018-08-19 stsp view->state.diff.id = got_object_get_id(obj2);
1566 bcbd79e2 2018-08-19 stsp if (view->state.diff.id == NULL)
1567 bcbd79e2 2018-08-19 stsp return got_error_from_errno();
1568 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1569 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1570 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1571 5dc9f4bc 2018-08-04 stsp
1572 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1573 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1574 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1575 e5a0f69f 2018-08-18 stsp
1576 5dc9f4bc 2018-08-04 stsp return NULL;
1577 5dc9f4bc 2018-08-04 stsp }
1578 5dc9f4bc 2018-08-04 stsp
1579 e5a0f69f 2018-08-18 stsp static const struct got_error *
1580 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1581 5dc9f4bc 2018-08-04 stsp {
1582 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1583 e5a0f69f 2018-08-18 stsp
1584 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1585 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1586 bcbd79e2 2018-08-19 stsp free(view->state.diff.id);
1587 e5a0f69f 2018-08-18 stsp return err;
1588 5dc9f4bc 2018-08-04 stsp }
1589 5dc9f4bc 2018-08-04 stsp
1590 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1591 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1592 5dc9f4bc 2018-08-04 stsp {
1593 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1594 26ed57b2 2018-05-19 stsp
1595 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1596 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
1597 e5a0f69f 2018-08-18 stsp }
1598 26ed57b2 2018-05-19 stsp
1599 e5a0f69f 2018-08-18 stsp static const struct got_error *
1600 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1601 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1602 e5a0f69f 2018-08-18 stsp {
1603 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1604 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1605 e5a0f69f 2018-08-18 stsp int i;
1606 e5a0f69f 2018-08-18 stsp
1607 e5a0f69f 2018-08-18 stsp switch (ch) {
1608 e5a0f69f 2018-08-18 stsp case 'k':
1609 e5a0f69f 2018-08-18 stsp case KEY_UP:
1610 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1611 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1612 26ed57b2 2018-05-19 stsp break;
1613 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1614 e5a0f69f 2018-08-18 stsp i = 0;
1615 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1616 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1617 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1618 e5a0f69f 2018-08-18 stsp break;
1619 e5a0f69f 2018-08-18 stsp case 'j':
1620 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1621 e5a0f69f 2018-08-18 stsp if (!s->eof)
1622 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1623 e5a0f69f 2018-08-18 stsp break;
1624 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1625 e5a0f69f 2018-08-18 stsp case ' ':
1626 e5a0f69f 2018-08-18 stsp i = 0;
1627 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1628 e5a0f69f 2018-08-18 stsp char *line;
1629 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1630 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1631 e5a0f69f 2018-08-18 stsp if (line == NULL)
1632 e5a0f69f 2018-08-18 stsp break;
1633 bcbd79e2 2018-08-19 stsp }
1634 bcbd79e2 2018-08-19 stsp break;
1635 bcbd79e2 2018-08-19 stsp case '[':
1636 bcbd79e2 2018-08-19 stsp case ']': {
1637 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *ls;
1638 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *entry;
1639 bcbd79e2 2018-08-19 stsp struct tog_view *diff_view;
1640 bcbd79e2 2018-08-19 stsp
1641 bcbd79e2 2018-08-19 stsp if (view->parent == NULL)
1642 bcbd79e2 2018-08-19 stsp break;
1643 bcbd79e2 2018-08-19 stsp if (view->parent->type != TOG_VIEW_LOG)
1644 bcbd79e2 2018-08-19 stsp break;
1645 bcbd79e2 2018-08-19 stsp ls = &view->parent->state.log;
1646 bcbd79e2 2018-08-19 stsp
1647 bcbd79e2 2018-08-19 stsp if (ch == '[') {
1648 bcbd79e2 2018-08-19 stsp entry = TAILQ_PREV(ls->selected_entry,
1649 bcbd79e2 2018-08-19 stsp commit_queue_head, entry);
1650 bcbd79e2 2018-08-19 stsp } else {
1651 bcbd79e2 2018-08-19 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
1652 bcbd79e2 2018-08-19 stsp if (entry == NULL) {
1653 bcbd79e2 2018-08-19 stsp err = fetch_next_commit(&entry,
1654 bcbd79e2 2018-08-19 stsp ls->selected_entry,
1655 bcbd79e2 2018-08-19 stsp &ls->commits, ls->graph,
1656 bcbd79e2 2018-08-19 stsp ls->repo, ls->in_repo_path);
1657 bcbd79e2 2018-08-19 stsp if (err)
1658 bcbd79e2 2018-08-19 stsp break;
1659 bcbd79e2 2018-08-19 stsp }
1660 e5a0f69f 2018-08-18 stsp }
1661 bcbd79e2 2018-08-19 stsp if (entry == NULL)
1662 bcbd79e2 2018-08-19 stsp break;
1663 bcbd79e2 2018-08-19 stsp err = show_commit(&diff_view, view->parent,
1664 bcbd79e2 2018-08-19 stsp entry, ls->repo);
1665 bcbd79e2 2018-08-19 stsp if (err)
1666 bcbd79e2 2018-08-19 stsp break;
1667 bcbd79e2 2018-08-19 stsp *new_view = diff_view;
1668 bcbd79e2 2018-08-19 stsp *dead_view = view;
1669 e5a0f69f 2018-08-18 stsp break;
1670 bcbd79e2 2018-08-19 stsp }
1671 e5a0f69f 2018-08-18 stsp default:
1672 e5a0f69f 2018-08-18 stsp break;
1673 26ed57b2 2018-05-19 stsp }
1674 e5a0f69f 2018-08-18 stsp
1675 bcbd79e2 2018-08-19 stsp return err;
1676 26ed57b2 2018-05-19 stsp }
1677 26ed57b2 2018-05-19 stsp
1678 4ed7e80c 2018-05-20 stsp static const struct got_error *
1679 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1680 9f7d7167 2018-04-29 stsp {
1681 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1682 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1683 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1684 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1685 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1686 26ed57b2 2018-05-19 stsp int ch;
1687 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1688 26ed57b2 2018-05-19 stsp
1689 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1690 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1691 26ed57b2 2018-05-19 stsp err(1, "pledge");
1692 26ed57b2 2018-05-19 stsp #endif
1693 26ed57b2 2018-05-19 stsp
1694 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1695 26ed57b2 2018-05-19 stsp switch (ch) {
1696 26ed57b2 2018-05-19 stsp default:
1697 26ed57b2 2018-05-19 stsp usage();
1698 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1699 26ed57b2 2018-05-19 stsp }
1700 26ed57b2 2018-05-19 stsp }
1701 26ed57b2 2018-05-19 stsp
1702 26ed57b2 2018-05-19 stsp argc -= optind;
1703 26ed57b2 2018-05-19 stsp argv += optind;
1704 26ed57b2 2018-05-19 stsp
1705 26ed57b2 2018-05-19 stsp if (argc == 0) {
1706 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1707 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1708 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1709 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1710 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1711 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1712 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1713 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1714 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1715 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1716 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1717 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1718 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1719 26ed57b2 2018-05-19 stsp } else
1720 26ed57b2 2018-05-19 stsp usage_diff();
1721 26ed57b2 2018-05-19 stsp
1722 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1723 26ed57b2 2018-05-19 stsp free(repo_path);
1724 26ed57b2 2018-05-19 stsp if (error)
1725 26ed57b2 2018-05-19 stsp goto done;
1726 26ed57b2 2018-05-19 stsp
1727 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1728 26ed57b2 2018-05-19 stsp if (error)
1729 26ed57b2 2018-05-19 stsp goto done;
1730 26ed57b2 2018-05-19 stsp
1731 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1732 26ed57b2 2018-05-19 stsp if (error)
1733 26ed57b2 2018-05-19 stsp goto done;
1734 26ed57b2 2018-05-19 stsp
1735 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1736 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1737 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1738 ea5e7bb5 2018-08-01 stsp goto done;
1739 ea5e7bb5 2018-08-01 stsp }
1740 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1741 5dc9f4bc 2018-08-04 stsp if (error)
1742 5dc9f4bc 2018-08-04 stsp goto done;
1743 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1744 26ed57b2 2018-05-19 stsp done:
1745 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1746 26ed57b2 2018-05-19 stsp if (obj1)
1747 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1748 26ed57b2 2018-05-19 stsp if (obj2)
1749 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1750 26ed57b2 2018-05-19 stsp return error;
1751 9f7d7167 2018-04-29 stsp }
1752 9f7d7167 2018-04-29 stsp
1753 4ed7e80c 2018-05-20 stsp __dead static void
1754 9f7d7167 2018-04-29 stsp usage_blame(void)
1755 9f7d7167 2018-04-29 stsp {
1756 80ddbec8 2018-04-29 stsp endwin();
1757 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1758 9f7d7167 2018-04-29 stsp getprogname());
1759 9f7d7167 2018-04-29 stsp exit(1);
1760 9f7d7167 2018-04-29 stsp }
1761 84451b3e 2018-07-10 stsp
1762 84451b3e 2018-07-10 stsp struct tog_blame_line {
1763 84451b3e 2018-07-10 stsp int annotated;
1764 84451b3e 2018-07-10 stsp struct got_object_id *id;
1765 84451b3e 2018-07-10 stsp };
1766 9f7d7167 2018-04-29 stsp
1767 4ed7e80c 2018-05-20 stsp static const struct got_error *
1768 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1769 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1770 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1771 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1772 84451b3e 2018-07-10 stsp {
1773 84451b3e 2018-07-10 stsp const struct got_error *err;
1774 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1775 84451b3e 2018-07-10 stsp char *line;
1776 84451b3e 2018-07-10 stsp size_t len;
1777 84451b3e 2018-07-10 stsp wchar_t *wline;
1778 b700b5d6 2018-07-10 stsp int width, wlimit;
1779 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1780 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1781 ab089a2a 2018-07-12 stsp char *id_str;
1782 ab089a2a 2018-07-12 stsp
1783 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1784 ab089a2a 2018-07-12 stsp if (err)
1785 ab089a2a 2018-07-12 stsp return err;
1786 84451b3e 2018-07-10 stsp
1787 84451b3e 2018-07-10 stsp rewind(f);
1788 f7d12f7e 2018-08-01 stsp werase(view->window);
1789 84451b3e 2018-07-10 stsp
1790 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1791 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1792 ab089a2a 2018-07-12 stsp free(id_str);
1793 ab089a2a 2018-07-12 stsp return err;
1794 ab089a2a 2018-07-12 stsp }
1795 ab089a2a 2018-07-12 stsp
1796 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1797 ab089a2a 2018-07-12 stsp free(line);
1798 2550e4c3 2018-07-13 stsp line = NULL;
1799 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1800 2550e4c3 2018-07-13 stsp free(wline);
1801 2550e4c3 2018-07-13 stsp wline = NULL;
1802 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1803 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1804 ab089a2a 2018-07-12 stsp
1805 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1806 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1807 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1808 ab089a2a 2018-07-12 stsp free(id_str);
1809 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1810 ab089a2a 2018-07-12 stsp }
1811 ab089a2a 2018-07-12 stsp free(id_str);
1812 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1813 3f60a8ef 2018-07-10 stsp free(line);
1814 2550e4c3 2018-07-13 stsp line = NULL;
1815 3f60a8ef 2018-07-10 stsp if (err)
1816 3f60a8ef 2018-07-10 stsp return err;
1817 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1818 2550e4c3 2018-07-13 stsp free(wline);
1819 2550e4c3 2018-07-13 stsp wline = NULL;
1820 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1821 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1822 3f60a8ef 2018-07-10 stsp
1823 84451b3e 2018-07-10 stsp *eof = 0;
1824 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1825 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1826 84451b3e 2018-07-10 stsp if (line == NULL) {
1827 84451b3e 2018-07-10 stsp *eof = 1;
1828 84451b3e 2018-07-10 stsp break;
1829 84451b3e 2018-07-10 stsp }
1830 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1831 84451b3e 2018-07-10 stsp free(line);
1832 84451b3e 2018-07-10 stsp continue;
1833 84451b3e 2018-07-10 stsp }
1834 84451b3e 2018-07-10 stsp
1835 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1836 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1837 84451b3e 2018-07-10 stsp if (err) {
1838 84451b3e 2018-07-10 stsp free(line);
1839 84451b3e 2018-07-10 stsp return err;
1840 84451b3e 2018-07-10 stsp }
1841 84451b3e 2018-07-10 stsp
1842 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1843 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1844 b700b5d6 2018-07-10 stsp
1845 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1846 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1847 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1848 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1849 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1850 84451b3e 2018-07-10 stsp char *id_str;
1851 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1852 84451b3e 2018-07-10 stsp if (err) {
1853 84451b3e 2018-07-10 stsp free(line);
1854 2550e4c3 2018-07-13 stsp free(wline);
1855 84451b3e 2018-07-10 stsp return err;
1856 84451b3e 2018-07-10 stsp }
1857 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1858 84451b3e 2018-07-10 stsp free(id_str);
1859 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1860 ee41ec32 2018-07-10 stsp } else {
1861 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1862 ee41ec32 2018-07-10 stsp prev_id = NULL;
1863 ee41ec32 2018-07-10 stsp }
1864 84451b3e 2018-07-10 stsp
1865 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1866 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1867 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1868 b700b5d6 2018-07-10 stsp width++;
1869 b700b5d6 2018-07-10 stsp }
1870 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1871 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1872 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1873 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1874 84451b3e 2018-07-10 stsp free(line);
1875 2550e4c3 2018-07-13 stsp free(wline);
1876 2550e4c3 2018-07-13 stsp wline = NULL;
1877 84451b3e 2018-07-10 stsp }
1878 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1879 84451b3e 2018-07-10 stsp
1880 1a57306a 2018-09-02 stsp view_vborder(view);
1881 84451b3e 2018-07-10 stsp
1882 84451b3e 2018-07-10 stsp return NULL;
1883 84451b3e 2018-07-10 stsp }
1884 84451b3e 2018-07-10 stsp
1885 84451b3e 2018-07-10 stsp static const struct got_error *
1886 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1887 84451b3e 2018-07-10 stsp {
1888 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1889 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1890 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1891 84451b3e 2018-07-10 stsp
1892 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1893 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1894 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1895 84451b3e 2018-07-10 stsp
1896 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1897 84451b3e 2018-07-10 stsp return got_error_from_errno();
1898 84451b3e 2018-07-10 stsp
1899 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1900 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1901 d68a0a7d 2018-07-10 stsp goto done;
1902 d68a0a7d 2018-07-10 stsp }
1903 d68a0a7d 2018-07-10 stsp
1904 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1905 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1906 d68a0a7d 2018-07-10 stsp
1907 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1908 d68a0a7d 2018-07-10 stsp if (line->annotated)
1909 d68a0a7d 2018-07-10 stsp goto done;
1910 d68a0a7d 2018-07-10 stsp
1911 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1912 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1913 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1914 84451b3e 2018-07-10 stsp goto done;
1915 84451b3e 2018-07-10 stsp }
1916 84451b3e 2018-07-10 stsp line->annotated = 1;
1917 84451b3e 2018-07-10 stsp
1918 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1919 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1920 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1921 84451b3e 2018-07-10 stsp done:
1922 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1923 84451b3e 2018-07-10 stsp return got_error_from_errno();
1924 84451b3e 2018-07-10 stsp return err;
1925 84451b3e 2018-07-10 stsp }
1926 84451b3e 2018-07-10 stsp
1927 84451b3e 2018-07-10 stsp static void *
1928 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1929 84451b3e 2018-07-10 stsp {
1930 18430de3 2018-07-10 stsp const struct got_error *err;
1931 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1932 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1933 18430de3 2018-07-10 stsp
1934 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1935 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1936 18430de3 2018-07-10 stsp
1937 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1938 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1939 18430de3 2018-07-10 stsp
1940 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1941 c9beca56 2018-07-22 stsp ta->repo = NULL;
1942 c9beca56 2018-07-22 stsp *ta->complete = 1;
1943 c9beca56 2018-07-22 stsp if (!err)
1944 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1945 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1946 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
1947 f7d12f7e 2018-08-01 stsp a->view->nlines);
1948 18430de3 2018-07-10 stsp
1949 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1950 18430de3 2018-07-10 stsp err = got_error_from_errno();
1951 18430de3 2018-07-10 stsp
1952 18430de3 2018-07-10 stsp return (void *)err;
1953 84451b3e 2018-07-10 stsp }
1954 84451b3e 2018-07-10 stsp
1955 245d91c1 2018-07-12 stsp static struct got_object_id *
1956 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1957 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1958 245d91c1 2018-07-12 stsp {
1959 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1960 b880a918 2018-07-10 stsp
1961 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1962 245d91c1 2018-07-12 stsp if (!line->annotated)
1963 245d91c1 2018-07-12 stsp return NULL;
1964 245d91c1 2018-07-12 stsp
1965 245d91c1 2018-07-12 stsp return line->id;
1966 245d91c1 2018-07-12 stsp }
1967 245d91c1 2018-07-12 stsp
1968 84451b3e 2018-07-10 stsp static const struct got_error *
1969 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1970 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1971 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1972 b880a918 2018-07-10 stsp {
1973 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1974 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1975 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1976 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1977 b880a918 2018-07-10 stsp
1978 b880a918 2018-07-10 stsp *pobj = NULL;
1979 b880a918 2018-07-10 stsp *obj = NULL;
1980 b880a918 2018-07-10 stsp
1981 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1982 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1983 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1984 b880a918 2018-07-10 stsp return NULL;
1985 b880a918 2018-07-10 stsp
1986 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1987 b880a918 2018-07-10 stsp if (err)
1988 b880a918 2018-07-10 stsp goto done;
1989 b880a918 2018-07-10 stsp
1990 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1991 b880a918 2018-07-10 stsp if (err)
1992 b880a918 2018-07-10 stsp goto done;
1993 b880a918 2018-07-10 stsp
1994 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1995 b880a918 2018-07-10 stsp if (pid) {
1996 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1997 b880a918 2018-07-10 stsp if (err)
1998 b880a918 2018-07-10 stsp goto done;
1999 b880a918 2018-07-10 stsp }
2000 b880a918 2018-07-10 stsp done:
2001 b880a918 2018-07-10 stsp if (commit)
2002 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2003 b880a918 2018-07-10 stsp return err;
2004 b880a918 2018-07-10 stsp }
2005 245d91c1 2018-07-12 stsp
2006 b880a918 2018-07-10 stsp static const struct got_error *
2007 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2008 a70480e0 2018-06-23 stsp {
2009 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2010 245d91c1 2018-07-12 stsp int i;
2011 245d91c1 2018-07-12 stsp
2012 245d91c1 2018-07-12 stsp if (blame->thread) {
2013 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2014 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2015 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2016 245d91c1 2018-07-12 stsp err = NULL;
2017 245d91c1 2018-07-12 stsp blame->thread = NULL;
2018 245d91c1 2018-07-12 stsp }
2019 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2020 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2021 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2022 245d91c1 2018-07-12 stsp }
2023 245d91c1 2018-07-12 stsp if (blame->f) {
2024 245d91c1 2018-07-12 stsp fclose(blame->f);
2025 245d91c1 2018-07-12 stsp blame->f = NULL;
2026 245d91c1 2018-07-12 stsp }
2027 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2028 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2029 245d91c1 2018-07-12 stsp free(blame->lines);
2030 245d91c1 2018-07-12 stsp blame->lines = NULL;
2031 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2032 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2033 245d91c1 2018-07-12 stsp
2034 245d91c1 2018-07-12 stsp return err;
2035 245d91c1 2018-07-12 stsp }
2036 245d91c1 2018-07-12 stsp
2037 245d91c1 2018-07-12 stsp static const struct got_error *
2038 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2039 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2040 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2041 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2042 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2043 245d91c1 2018-07-12 stsp struct got_repository *repo)
2044 245d91c1 2018-07-12 stsp {
2045 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2046 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2047 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2048 245d91c1 2018-07-12 stsp struct got_object *obj;
2049 a70480e0 2018-06-23 stsp
2050 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
2051 84451b3e 2018-07-10 stsp if (err)
2052 84451b3e 2018-07-10 stsp goto done;
2053 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2054 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2055 84451b3e 2018-07-10 stsp goto done;
2056 84451b3e 2018-07-10 stsp }
2057 a70480e0 2018-06-23 stsp
2058 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2059 a70480e0 2018-06-23 stsp if (err)
2060 a70480e0 2018-06-23 stsp goto done;
2061 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2062 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2063 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2064 84451b3e 2018-07-10 stsp goto done;
2065 84451b3e 2018-07-10 stsp }
2066 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2067 245d91c1 2018-07-12 stsp blame->f, blob);
2068 84451b3e 2018-07-10 stsp if (err)
2069 84451b3e 2018-07-10 stsp goto done;
2070 a70480e0 2018-06-23 stsp
2071 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2072 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2073 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2074 84451b3e 2018-07-10 stsp goto done;
2075 84451b3e 2018-07-10 stsp }
2076 a70480e0 2018-06-23 stsp
2077 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2078 bd24772e 2018-07-11 stsp if (err)
2079 bd24772e 2018-07-11 stsp goto done;
2080 bd24772e 2018-07-11 stsp
2081 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2082 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2083 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2084 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2085 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2086 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2087 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2088 245d91c1 2018-07-12 stsp goto done;
2089 245d91c1 2018-07-12 stsp }
2090 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2091 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2092 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2093 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2094 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2095 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2096 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2097 245d91c1 2018-07-12 stsp
2098 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2099 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2100 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2101 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2102 245d91c1 2018-07-12 stsp *blame_complete = 0;
2103 245d91c1 2018-07-12 stsp
2104 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2105 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2106 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2107 245d91c1 2018-07-12 stsp goto done;
2108 245d91c1 2018-07-12 stsp }
2109 245d91c1 2018-07-12 stsp
2110 245d91c1 2018-07-12 stsp done:
2111 245d91c1 2018-07-12 stsp if (blob)
2112 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2113 245d91c1 2018-07-12 stsp if (obj)
2114 245d91c1 2018-07-12 stsp got_object_close(obj);
2115 245d91c1 2018-07-12 stsp if (err)
2116 245d91c1 2018-07-12 stsp stop_blame(blame);
2117 245d91c1 2018-07-12 stsp return err;
2118 245d91c1 2018-07-12 stsp }
2119 245d91c1 2018-07-12 stsp
2120 245d91c1 2018-07-12 stsp static const struct got_error *
2121 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2122 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2123 245d91c1 2018-07-12 stsp {
2124 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2125 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2126 dbc6a6b6 2018-07-12 stsp
2127 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2128 245d91c1 2018-07-12 stsp
2129 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2130 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2131 245d91c1 2018-07-12 stsp
2132 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2133 dbc6a6b6 2018-07-12 stsp if (err)
2134 7cbe629d 2018-08-04 stsp return err;
2135 245d91c1 2018-07-12 stsp
2136 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2137 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2138 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2139 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2140 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2141 fb2756b9 2018-08-04 stsp s->path = path;
2142 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2143 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2144 fb2756b9 2018-08-04 stsp s->repo = repo;
2145 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2146 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2147 7cbe629d 2018-08-04 stsp
2148 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2149 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2150 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2151 e5a0f69f 2018-08-18 stsp
2152 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2153 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2154 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2155 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2156 7cbe629d 2018-08-04 stsp }
2157 7cbe629d 2018-08-04 stsp
2158 e5a0f69f 2018-08-18 stsp static const struct got_error *
2159 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2160 7cbe629d 2018-08-04 stsp {
2161 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2162 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2163 7cbe629d 2018-08-04 stsp
2164 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2165 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2166 e5a0f69f 2018-08-18 stsp
2167 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2168 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2169 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2170 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2171 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2172 7cbe629d 2018-08-04 stsp }
2173 e5a0f69f 2018-08-18 stsp
2174 e5a0f69f 2018-08-18 stsp free(s->path);
2175 e5a0f69f 2018-08-18 stsp
2176 e5a0f69f 2018-08-18 stsp return err;
2177 7cbe629d 2018-08-04 stsp }
2178 7cbe629d 2018-08-04 stsp
2179 7cbe629d 2018-08-04 stsp static const struct got_error *
2180 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2181 7cbe629d 2018-08-04 stsp {
2182 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2183 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2184 e5a0f69f 2018-08-18 stsp
2185 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2186 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2187 e5a0f69f 2018-08-18 stsp
2188 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2189 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2190 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2191 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2192 e5a0f69f 2018-08-18 stsp
2193 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2194 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2195 e5a0f69f 2018-08-18 stsp
2196 e5a0f69f 2018-08-18 stsp return err;
2197 e5a0f69f 2018-08-18 stsp }
2198 e5a0f69f 2018-08-18 stsp
2199 e5a0f69f 2018-08-18 stsp static const struct got_error *
2200 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2201 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2202 e5a0f69f 2018-08-18 stsp {
2203 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2204 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2205 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2206 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2207 7cbe629d 2018-08-04 stsp
2208 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2209 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2210 e5a0f69f 2018-08-18 stsp goto done;
2211 e5a0f69f 2018-08-18 stsp }
2212 a70480e0 2018-06-23 stsp
2213 e5a0f69f 2018-08-18 stsp switch (ch) {
2214 e5a0f69f 2018-08-18 stsp case 'q':
2215 e5a0f69f 2018-08-18 stsp s->done = 1;
2216 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2217 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2218 e5a0f69f 2018-08-18 stsp goto done;
2219 e5a0f69f 2018-08-18 stsp }
2220 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2221 e5a0f69f 2018-08-18 stsp case 'k':
2222 e5a0f69f 2018-08-18 stsp case KEY_UP:
2223 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2224 e5a0f69f 2018-08-18 stsp s->selected_line--;
2225 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2226 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2227 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2228 a70480e0 2018-06-23 stsp break;
2229 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2230 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2231 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2232 a70480e0 2018-06-23 stsp break;
2233 e5a0f69f 2018-08-18 stsp }
2234 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2235 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2236 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2237 e5a0f69f 2018-08-18 stsp else
2238 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2239 e5a0f69f 2018-08-18 stsp break;
2240 e5a0f69f 2018-08-18 stsp case 'j':
2241 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2242 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2243 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2244 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2245 e5a0f69f 2018-08-18 stsp s->selected_line++;
2246 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2247 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2248 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2249 e5a0f69f 2018-08-18 stsp break;
2250 e5a0f69f 2018-08-18 stsp case 'b':
2251 e5a0f69f 2018-08-18 stsp case 'p': {
2252 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2253 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2254 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2255 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2256 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2257 a70480e0 2018-06-23 stsp break;
2258 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2259 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2260 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2261 e5a0f69f 2018-08-18 stsp if (err)
2262 a70480e0 2018-06-23 stsp break;
2263 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2264 b700b5d6 2018-07-10 stsp break;
2265 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2266 dbc6a6b6 2018-07-12 stsp break;
2267 e5a0f69f 2018-08-18 stsp s->done = 1;
2268 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2269 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2270 e5a0f69f 2018-08-18 stsp goto done;
2271 dbc6a6b6 2018-07-12 stsp }
2272 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2273 e5a0f69f 2018-08-18 stsp s->done = 0;
2274 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2275 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2276 e5a0f69f 2018-08-18 stsp goto done;
2277 e5a0f69f 2018-08-18 stsp }
2278 e5a0f69f 2018-08-18 stsp if (thread_err)
2279 245d91c1 2018-07-12 stsp break;
2280 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2281 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2282 e5a0f69f 2018-08-18 stsp obj = NULL;
2283 e5a0f69f 2018-08-18 stsp if (pobj) {
2284 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2285 e5a0f69f 2018-08-18 stsp pobj = NULL;
2286 245d91c1 2018-07-12 stsp }
2287 e5a0f69f 2018-08-18 stsp if (id == NULL) {
2288 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2289 a70480e0 2018-06-23 stsp break;
2290 e5a0f69f 2018-08-18 stsp }
2291 e5a0f69f 2018-08-18 stsp err = got_object_qid_alloc(
2292 e5a0f69f 2018-08-18 stsp &s->blamed_commit, id);
2293 e5a0f69f 2018-08-18 stsp free(id);
2294 e5a0f69f 2018-08-18 stsp if (err)
2295 e5a0f69f 2018-08-18 stsp goto done;
2296 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2297 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2298 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2299 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2300 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2301 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2302 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2303 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2304 e5a0f69f 2018-08-18 stsp if (err)
2305 a70480e0 2018-06-23 stsp break;
2306 e5a0f69f 2018-08-18 stsp break;
2307 84451b3e 2018-07-10 stsp }
2308 e5a0f69f 2018-08-18 stsp case 'B': {
2309 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2310 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2311 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2312 e5a0f69f 2018-08-18 stsp break;
2313 e5a0f69f 2018-08-18 stsp s->done = 1;
2314 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2315 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2316 e5a0f69f 2018-08-18 stsp goto done;
2317 e5a0f69f 2018-08-18 stsp }
2318 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2319 e5a0f69f 2018-08-18 stsp s->done = 0;
2320 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2321 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2322 e5a0f69f 2018-08-18 stsp goto done;
2323 e5a0f69f 2018-08-18 stsp }
2324 e5a0f69f 2018-08-18 stsp if (thread_err)
2325 e5a0f69f 2018-08-18 stsp break;
2326 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2327 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2328 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2329 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2330 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2331 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2332 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2333 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2334 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2335 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2336 e5a0f69f 2018-08-18 stsp if (err)
2337 e5a0f69f 2018-08-18 stsp break;
2338 245d91c1 2018-07-12 stsp break;
2339 e5a0f69f 2018-08-18 stsp }
2340 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2341 e5a0f69f 2018-08-18 stsp case '\r':
2342 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2343 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2344 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2345 e5a0f69f 2018-08-18 stsp if (err)
2346 e5a0f69f 2018-08-18 stsp break;
2347 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2348 e5a0f69f 2018-08-18 stsp break;
2349 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, view,
2350 e5a0f69f 2018-08-18 stsp TOG_VIEW_DIFF);
2351 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2352 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2353 e5a0f69f 2018-08-18 stsp break;
2354 e5a0f69f 2018-08-18 stsp }
2355 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2356 e5a0f69f 2018-08-18 stsp s->repo);
2357 e5a0f69f 2018-08-18 stsp if (err) {
2358 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2359 e5a0f69f 2018-08-18 stsp break;
2360 e5a0f69f 2018-08-18 stsp }
2361 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2362 e5a0f69f 2018-08-18 stsp if (pobj) {
2363 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2364 e5a0f69f 2018-08-18 stsp pobj = NULL;
2365 e5a0f69f 2018-08-18 stsp }
2366 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2367 e5a0f69f 2018-08-18 stsp obj = NULL;
2368 e5a0f69f 2018-08-18 stsp if (err)
2369 e5a0f69f 2018-08-18 stsp break;
2370 e5a0f69f 2018-08-18 stsp break;
2371 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2372 e5a0f69f 2018-08-18 stsp case ' ':
2373 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2374 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2375 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2376 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2377 e5a0f69f 2018-08-18 stsp break;
2378 e5a0f69f 2018-08-18 stsp }
2379 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2380 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2381 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2382 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2383 e5a0f69f 2018-08-18 stsp else
2384 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2385 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2386 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2387 e5a0f69f 2018-08-18 stsp break;
2388 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2389 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2390 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2391 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2392 e5a0f69f 2018-08-18 stsp }
2393 e5a0f69f 2018-08-18 stsp break;
2394 e5a0f69f 2018-08-18 stsp default:
2395 e5a0f69f 2018-08-18 stsp break;
2396 a70480e0 2018-06-23 stsp }
2397 e5a0f69f 2018-08-18 stsp
2398 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2399 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2400 a70480e0 2018-06-23 stsp done:
2401 b880a918 2018-07-10 stsp if (pobj)
2402 b880a918 2018-07-10 stsp got_object_close(pobj);
2403 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2404 a70480e0 2018-06-23 stsp }
2405 a70480e0 2018-06-23 stsp
2406 a70480e0 2018-06-23 stsp static const struct got_error *
2407 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2408 9f7d7167 2018-04-29 stsp {
2409 a70480e0 2018-06-23 stsp const struct got_error *error;
2410 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2411 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2412 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2413 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2414 a70480e0 2018-06-23 stsp int ch;
2415 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2416 a70480e0 2018-06-23 stsp
2417 a70480e0 2018-06-23 stsp #ifndef PROFILE
2418 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2419 a70480e0 2018-06-23 stsp err(1, "pledge");
2420 a70480e0 2018-06-23 stsp #endif
2421 a70480e0 2018-06-23 stsp
2422 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2423 a70480e0 2018-06-23 stsp switch (ch) {
2424 a70480e0 2018-06-23 stsp case 'c':
2425 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2426 a70480e0 2018-06-23 stsp break;
2427 69069811 2018-08-02 stsp case 'r':
2428 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2429 69069811 2018-08-02 stsp if (repo_path == NULL)
2430 69069811 2018-08-02 stsp err(1, "-r option");
2431 69069811 2018-08-02 stsp break;
2432 a70480e0 2018-06-23 stsp default:
2433 a70480e0 2018-06-23 stsp usage();
2434 a70480e0 2018-06-23 stsp /* NOTREACHED */
2435 a70480e0 2018-06-23 stsp }
2436 a70480e0 2018-06-23 stsp }
2437 a70480e0 2018-06-23 stsp
2438 a70480e0 2018-06-23 stsp argc -= optind;
2439 a70480e0 2018-06-23 stsp argv += optind;
2440 a70480e0 2018-06-23 stsp
2441 69069811 2018-08-02 stsp if (argc == 1)
2442 69069811 2018-08-02 stsp path = argv[0];
2443 69069811 2018-08-02 stsp else
2444 a70480e0 2018-06-23 stsp usage_blame();
2445 69069811 2018-08-02 stsp
2446 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2447 69069811 2018-08-02 stsp if (cwd == NULL) {
2448 69069811 2018-08-02 stsp error = got_error_from_errno();
2449 69069811 2018-08-02 stsp goto done;
2450 69069811 2018-08-02 stsp }
2451 69069811 2018-08-02 stsp if (repo_path == NULL) {
2452 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2453 69069811 2018-08-02 stsp if (repo_path == NULL) {
2454 69069811 2018-08-02 stsp error = got_error_from_errno();
2455 69069811 2018-08-02 stsp goto done;
2456 69069811 2018-08-02 stsp }
2457 69069811 2018-08-02 stsp }
2458 69069811 2018-08-02 stsp
2459 69069811 2018-08-02 stsp
2460 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2461 a70480e0 2018-06-23 stsp if (error != NULL)
2462 66b4983c 2018-06-23 stsp return error;
2463 69069811 2018-08-02 stsp
2464 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2465 69069811 2018-08-02 stsp if (error != NULL)
2466 69069811 2018-08-02 stsp goto done;
2467 a70480e0 2018-06-23 stsp
2468 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2469 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2470 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2471 a70480e0 2018-06-23 stsp if (error != NULL)
2472 66b4983c 2018-06-23 stsp goto done;
2473 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2474 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2475 a70480e0 2018-06-23 stsp } else {
2476 a70480e0 2018-06-23 stsp struct got_object *obj;
2477 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2478 a70480e0 2018-06-23 stsp if (error != NULL)
2479 66b4983c 2018-06-23 stsp goto done;
2480 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
2481 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2482 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2483 a19e88aa 2018-06-23 stsp got_object_close(obj);
2484 a70480e0 2018-06-23 stsp }
2485 a19e88aa 2018-06-23 stsp if (error != NULL)
2486 a19e88aa 2018-06-23 stsp goto done;
2487 a70480e0 2018-06-23 stsp
2488 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2489 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2490 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2491 e1cd8fed 2018-08-01 stsp goto done;
2492 e1cd8fed 2018-08-01 stsp }
2493 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2494 7cbe629d 2018-08-04 stsp if (error)
2495 7cbe629d 2018-08-04 stsp goto done;
2496 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2497 a70480e0 2018-06-23 stsp done:
2498 69069811 2018-08-02 stsp free(repo_path);
2499 69069811 2018-08-02 stsp free(cwd);
2500 a70480e0 2018-06-23 stsp free(commit_id);
2501 a70480e0 2018-06-23 stsp if (repo)
2502 a70480e0 2018-06-23 stsp got_repo_close(repo);
2503 a70480e0 2018-06-23 stsp return error;
2504 ffd1d5e5 2018-06-23 stsp }
2505 ffd1d5e5 2018-06-23 stsp
2506 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2507 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2508 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2509 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2510 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2511 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2512 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2513 ffd1d5e5 2018-06-23 stsp {
2514 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2515 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2516 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2517 ffd1d5e5 2018-06-23 stsp int width, n;
2518 ffd1d5e5 2018-06-23 stsp
2519 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2520 ffd1d5e5 2018-06-23 stsp
2521 f7d12f7e 2018-08-01 stsp werase(view->window);
2522 ffd1d5e5 2018-06-23 stsp
2523 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2524 ffd1d5e5 2018-06-23 stsp return NULL;
2525 ffd1d5e5 2018-06-23 stsp
2526 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2527 ffd1d5e5 2018-06-23 stsp if (err)
2528 ffd1d5e5 2018-06-23 stsp return err;
2529 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2530 2550e4c3 2018-07-13 stsp free(wline);
2531 2550e4c3 2018-07-13 stsp wline = NULL;
2532 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2533 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2534 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2535 ffd1d5e5 2018-06-23 stsp return NULL;
2536 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2537 ce52c690 2018-06-23 stsp if (err)
2538 ce52c690 2018-06-23 stsp return err;
2539 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2540 2550e4c3 2018-07-13 stsp free(wline);
2541 2550e4c3 2018-07-13 stsp wline = NULL;
2542 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2543 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2544 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2545 ffd1d5e5 2018-06-23 stsp return NULL;
2546 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2547 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2548 a1eca9bb 2018-06-23 stsp return NULL;
2549 ffd1d5e5 2018-06-23 stsp
2550 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2551 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2552 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2553 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2554 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2555 ffd1d5e5 2018-06-23 stsp }
2556 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2557 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2558 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2559 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2560 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2561 ffd1d5e5 2018-06-23 stsp return NULL;
2562 ffd1d5e5 2018-06-23 stsp n = 1;
2563 ffd1d5e5 2018-06-23 stsp } else {
2564 ffd1d5e5 2018-06-23 stsp n = 0;
2565 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2566 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2567 ffd1d5e5 2018-06-23 stsp }
2568 ffd1d5e5 2018-06-23 stsp
2569 ffd1d5e5 2018-06-23 stsp while (te) {
2570 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2571 1d13200f 2018-07-12 stsp
2572 1d13200f 2018-07-12 stsp if (show_ids) {
2573 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2574 1d13200f 2018-07-12 stsp if (err)
2575 1d13200f 2018-07-12 stsp return got_error_from_errno();
2576 1d13200f 2018-07-12 stsp }
2577 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2578 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2579 1d13200f 2018-07-12 stsp free(id_str);
2580 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2581 1d13200f 2018-07-12 stsp }
2582 1d13200f 2018-07-12 stsp free(id_str);
2583 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2584 ffd1d5e5 2018-06-23 stsp if (err) {
2585 ffd1d5e5 2018-06-23 stsp free(line);
2586 ffd1d5e5 2018-06-23 stsp break;
2587 ffd1d5e5 2018-06-23 stsp }
2588 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2589 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2590 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2591 ffd1d5e5 2018-06-23 stsp }
2592 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2593 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2594 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2595 ffd1d5e5 2018-06-23 stsp if (n == selected)
2596 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2597 ffd1d5e5 2018-06-23 stsp free(line);
2598 2550e4c3 2018-07-13 stsp free(wline);
2599 2550e4c3 2018-07-13 stsp wline = NULL;
2600 ffd1d5e5 2018-06-23 stsp n++;
2601 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2602 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2603 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2604 ffd1d5e5 2018-06-23 stsp break;
2605 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2606 ffd1d5e5 2018-06-23 stsp }
2607 ffd1d5e5 2018-06-23 stsp
2608 1a57306a 2018-09-02 stsp view_vborder(view);
2609 ffd1d5e5 2018-06-23 stsp return err;
2610 ffd1d5e5 2018-06-23 stsp }
2611 ffd1d5e5 2018-06-23 stsp
2612 ffd1d5e5 2018-06-23 stsp static void
2613 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2614 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2615 ffd1d5e5 2018-06-23 stsp {
2616 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2617 ffd1d5e5 2018-06-23 stsp int i;
2618 ffd1d5e5 2018-06-23 stsp
2619 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2620 ffd1d5e5 2018-06-23 stsp return;
2621 ffd1d5e5 2018-06-23 stsp
2622 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2623 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2624 ffd1d5e5 2018-06-23 stsp if (!isroot)
2625 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2626 ffd1d5e5 2018-06-23 stsp return;
2627 ffd1d5e5 2018-06-23 stsp }
2628 ffd1d5e5 2018-06-23 stsp
2629 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2630 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2631 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2632 ffd1d5e5 2018-06-23 stsp prev = te;
2633 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2634 ffd1d5e5 2018-06-23 stsp }
2635 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2636 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2637 ffd1d5e5 2018-06-23 stsp }
2638 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2639 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2640 ffd1d5e5 2018-06-23 stsp }
2641 ffd1d5e5 2018-06-23 stsp
2642 ffd1d5e5 2018-06-23 stsp static void
2643 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2644 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2645 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2646 ffd1d5e5 2018-06-23 stsp {
2647 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2648 ffd1d5e5 2018-06-23 stsp int n = 0;
2649 ffd1d5e5 2018-06-23 stsp
2650 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2651 ffd1d5e5 2018-06-23 stsp return;
2652 ffd1d5e5 2018-06-23 stsp
2653 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2654 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2655 ffd1d5e5 2018-06-23 stsp else
2656 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2657 ffd1d5e5 2018-06-23 stsp while (next) {
2658 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2659 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2660 ffd1d5e5 2018-06-23 stsp break;
2661 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2662 ffd1d5e5 2018-06-23 stsp }
2663 ffd1d5e5 2018-06-23 stsp }
2664 ffd1d5e5 2018-06-23 stsp
2665 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2666 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2667 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2668 ffd1d5e5 2018-06-23 stsp {
2669 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2670 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2671 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2672 ffd1d5e5 2018-06-23 stsp
2673 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2674 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2675 ce52c690 2018-06-23 stsp if (te)
2676 ce52c690 2018-06-23 stsp len += strlen(te->name);
2677 ce52c690 2018-06-23 stsp
2678 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2679 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2680 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2681 ffd1d5e5 2018-06-23 stsp
2682 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2683 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2684 d9765a41 2018-06-23 stsp while (pt) {
2685 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2686 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2687 cb2ebc8a 2018-06-23 stsp goto done;
2688 cb2ebc8a 2018-06-23 stsp }
2689 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2690 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2691 cb2ebc8a 2018-06-23 stsp goto done;
2692 cb2ebc8a 2018-06-23 stsp }
2693 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2694 ffd1d5e5 2018-06-23 stsp }
2695 ce52c690 2018-06-23 stsp if (te) {
2696 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2697 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2698 ce52c690 2018-06-23 stsp goto done;
2699 ce52c690 2018-06-23 stsp }
2700 cb2ebc8a 2018-06-23 stsp }
2701 ce52c690 2018-06-23 stsp done:
2702 ce52c690 2018-06-23 stsp if (err) {
2703 ce52c690 2018-06-23 stsp free(*path);
2704 ce52c690 2018-06-23 stsp *path = NULL;
2705 ce52c690 2018-06-23 stsp }
2706 ce52c690 2018-06-23 stsp return err;
2707 ce52c690 2018-06-23 stsp }
2708 ce52c690 2018-06-23 stsp
2709 ce52c690 2018-06-23 stsp static const struct got_error *
2710 e5a0f69f 2018-08-18 stsp blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2711 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2712 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2713 ce52c690 2018-06-23 stsp {
2714 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2715 ce52c690 2018-06-23 stsp char *path;
2716 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2717 69efd4c4 2018-07-18 stsp
2718 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2719 ce52c690 2018-06-23 stsp if (err)
2720 ce52c690 2018-06-23 stsp return err;
2721 ffd1d5e5 2018-06-23 stsp
2722 e5a0f69f 2018-08-18 stsp blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2723 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2724 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2725 cdf1ee82 2018-08-01 stsp
2726 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2727 e5a0f69f 2018-08-18 stsp if (err) {
2728 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2729 e5a0f69f 2018-08-18 stsp free(path);
2730 e5a0f69f 2018-08-18 stsp } else
2731 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2732 69efd4c4 2018-07-18 stsp return err;
2733 69efd4c4 2018-07-18 stsp }
2734 69efd4c4 2018-07-18 stsp
2735 69efd4c4 2018-07-18 stsp static const struct got_error *
2736 e5a0f69f 2018-08-18 stsp log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2737 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2738 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2739 69efd4c4 2018-07-18 stsp {
2740 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2741 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2742 69efd4c4 2018-07-18 stsp char *path;
2743 69efd4c4 2018-07-18 stsp
2744 e5a0f69f 2018-08-18 stsp log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2745 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2746 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2747 e5a0f69f 2018-08-18 stsp
2748 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2749 69efd4c4 2018-07-18 stsp if (err)
2750 69efd4c4 2018-07-18 stsp return err;
2751 69efd4c4 2018-07-18 stsp
2752 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2753 ba4f502b 2018-08-04 stsp if (err)
2754 e5a0f69f 2018-08-18 stsp view_close(log_view);
2755 e5a0f69f 2018-08-18 stsp else
2756 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2757 cb2ebc8a 2018-06-23 stsp free(path);
2758 cb2ebc8a 2018-06-23 stsp return err;
2759 ffd1d5e5 2018-06-23 stsp }
2760 ffd1d5e5 2018-06-23 stsp
2761 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2762 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2763 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2764 ffd1d5e5 2018-06-23 stsp {
2765 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2766 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2767 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2768 ffd1d5e5 2018-06-23 stsp
2769 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2770 ffd1d5e5 2018-06-23 stsp
2771 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2772 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2773 ffd1d5e5 2018-06-23 stsp goto done;
2774 ffd1d5e5 2018-06-23 stsp
2775 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2776 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2777 ffd1d5e5 2018-06-23 stsp goto done;
2778 ffd1d5e5 2018-06-23 stsp }
2779 ffd1d5e5 2018-06-23 stsp
2780 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2781 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2782 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2783 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2784 fb2756b9 2018-08-04 stsp s->repo = repo;
2785 e5a0f69f 2018-08-18 stsp
2786 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2787 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2788 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2789 ad80ab7b 2018-08-04 stsp done:
2790 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2791 ad80ab7b 2018-08-04 stsp if (err)
2792 fb2756b9 2018-08-04 stsp free(s->tree_label);
2793 ad80ab7b 2018-08-04 stsp return err;
2794 ad80ab7b 2018-08-04 stsp }
2795 ad80ab7b 2018-08-04 stsp
2796 e5a0f69f 2018-08-18 stsp static const struct got_error *
2797 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2798 ad80ab7b 2018-08-04 stsp {
2799 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2800 ad80ab7b 2018-08-04 stsp
2801 fb2756b9 2018-08-04 stsp free(s->tree_label);
2802 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2803 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2804 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2805 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2806 ad80ab7b 2018-08-04 stsp free(parent);
2807 ad80ab7b 2018-08-04 stsp
2808 ad80ab7b 2018-08-04 stsp }
2809 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2810 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2811 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2812 e5a0f69f 2018-08-18 stsp
2813 e5a0f69f 2018-08-18 stsp return NULL;
2814 ad80ab7b 2018-08-04 stsp }
2815 ad80ab7b 2018-08-04 stsp
2816 ad80ab7b 2018-08-04 stsp static const struct got_error *
2817 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2818 ad80ab7b 2018-08-04 stsp {
2819 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2820 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2821 e5a0f69f 2018-08-18 stsp char *parent_path;
2822 ad80ab7b 2018-08-04 stsp
2823 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2824 e5a0f69f 2018-08-18 stsp if (err)
2825 e5a0f69f 2018-08-18 stsp return err;
2826 ffd1d5e5 2018-06-23 stsp
2827 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2828 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2829 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2830 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2831 e5a0f69f 2018-08-18 stsp free(parent_path);
2832 e5a0f69f 2018-08-18 stsp return err;
2833 e5a0f69f 2018-08-18 stsp }
2834 ce52c690 2018-06-23 stsp
2835 e5a0f69f 2018-08-18 stsp static const struct got_error *
2836 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2837 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2838 e5a0f69f 2018-08-18 stsp {
2839 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2840 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2841 ffd1d5e5 2018-06-23 stsp
2842 e5a0f69f 2018-08-18 stsp switch (ch) {
2843 e5a0f69f 2018-08-18 stsp case 'i':
2844 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2845 ffd1d5e5 2018-06-23 stsp break;
2846 e5a0f69f 2018-08-18 stsp case 'l':
2847 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2848 e5a0f69f 2018-08-18 stsp err = log_tree_entry(new_view, view,
2849 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2850 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2851 e5a0f69f 2018-08-18 stsp }
2852 e5a0f69f 2018-08-18 stsp break;
2853 e5a0f69f 2018-08-18 stsp case 'k':
2854 e5a0f69f 2018-08-18 stsp case KEY_UP:
2855 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2856 e5a0f69f 2018-08-18 stsp s->selected--;
2857 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2858 ffd1d5e5 2018-06-23 stsp break;
2859 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2860 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2861 e5a0f69f 2018-08-18 stsp break;
2862 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2863 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2864 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2865 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2866 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2867 e5a0f69f 2018-08-18 stsp s->selected = 0;
2868 69efd4c4 2018-07-18 stsp break;
2869 e5a0f69f 2018-08-18 stsp }
2870 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2871 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2872 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2873 e5a0f69f 2018-08-18 stsp break;
2874 e5a0f69f 2018-08-18 stsp case 'j':
2875 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2876 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2877 e5a0f69f 2018-08-18 stsp s->selected++;
2878 1d13200f 2018-07-12 stsp break;
2879 e5a0f69f 2018-08-18 stsp }
2880 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2881 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2882 e5a0f69f 2018-08-18 stsp break;
2883 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2884 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2885 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2886 e5a0f69f 2018-08-18 stsp s->entries);
2887 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2888 e5a0f69f 2018-08-18 stsp entry))
2889 ffd1d5e5 2018-06-23 stsp break;
2890 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2891 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2892 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2893 e5a0f69f 2018-08-18 stsp break;
2894 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2895 e5a0f69f 2018-08-18 stsp case '\r':
2896 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2897 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2898 31607d6c 2018-08-18 stsp case KEY_LEFT:
2899 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2900 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2901 ffd1d5e5 2018-06-23 stsp break;
2902 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2903 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2904 e5a0f69f 2018-08-18 stsp entry);
2905 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2906 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2907 e5a0f69f 2018-08-18 stsp s->entries =
2908 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2909 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
2910 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
2911 e5a0f69f 2018-08-18 stsp s->selected_entry =
2912 e5a0f69f 2018-08-18 stsp parent->selected_entry;
2913 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
2914 e5a0f69f 2018-08-18 stsp free(parent);
2915 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2916 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2917 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
2918 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
2919 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
2920 e5a0f69f 2018-08-18 stsp if (err)
2921 ffd1d5e5 2018-06-23 stsp break;
2922 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
2923 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
2924 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2925 e5a0f69f 2018-08-18 stsp break;
2926 ffd1d5e5 2018-06-23 stsp }
2927 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
2928 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
2929 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
2930 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
2931 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
2932 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2933 e5a0f69f 2018-08-18 stsp s->tree = child;
2934 e5a0f69f 2018-08-18 stsp s->entries =
2935 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2936 e5a0f69f 2018-08-18 stsp s->selected = 0;
2937 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2938 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
2939 e5a0f69f 2018-08-18 stsp err = blame_tree_entry(new_view, view,
2940 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2941 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2942 e5a0f69f 2018-08-18 stsp if (err)
2943 ffd1d5e5 2018-06-23 stsp break;
2944 e5a0f69f 2018-08-18 stsp }
2945 e5a0f69f 2018-08-18 stsp break;
2946 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2947 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
2948 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2949 e5a0f69f 2018-08-18 stsp break;
2950 e5a0f69f 2018-08-18 stsp default:
2951 e5a0f69f 2018-08-18 stsp break;
2952 ffd1d5e5 2018-06-23 stsp }
2953 e5a0f69f 2018-08-18 stsp
2954 ffd1d5e5 2018-06-23 stsp return err;
2955 9f7d7167 2018-04-29 stsp }
2956 9f7d7167 2018-04-29 stsp
2957 ffd1d5e5 2018-06-23 stsp __dead static void
2958 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2959 ffd1d5e5 2018-06-23 stsp {
2960 ffd1d5e5 2018-06-23 stsp endwin();
2961 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2962 ffd1d5e5 2018-06-23 stsp getprogname());
2963 ffd1d5e5 2018-06-23 stsp exit(1);
2964 ffd1d5e5 2018-06-23 stsp }
2965 ffd1d5e5 2018-06-23 stsp
2966 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2967 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2968 ffd1d5e5 2018-06-23 stsp {
2969 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2970 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2971 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2972 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2973 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2974 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2975 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2976 ffd1d5e5 2018-06-23 stsp int ch;
2977 5221c383 2018-08-01 stsp struct tog_view *view;
2978 ffd1d5e5 2018-06-23 stsp
2979 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2980 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2981 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2982 ffd1d5e5 2018-06-23 stsp #endif
2983 ffd1d5e5 2018-06-23 stsp
2984 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2985 ffd1d5e5 2018-06-23 stsp switch (ch) {
2986 ffd1d5e5 2018-06-23 stsp case 'c':
2987 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2988 ffd1d5e5 2018-06-23 stsp break;
2989 ffd1d5e5 2018-06-23 stsp default:
2990 ffd1d5e5 2018-06-23 stsp usage();
2991 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2992 ffd1d5e5 2018-06-23 stsp }
2993 ffd1d5e5 2018-06-23 stsp }
2994 ffd1d5e5 2018-06-23 stsp
2995 ffd1d5e5 2018-06-23 stsp argc -= optind;
2996 ffd1d5e5 2018-06-23 stsp argv += optind;
2997 ffd1d5e5 2018-06-23 stsp
2998 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2999 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3000 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3001 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3002 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3003 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3004 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3005 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3006 ffd1d5e5 2018-06-23 stsp } else
3007 ffd1d5e5 2018-06-23 stsp usage_log();
3008 ffd1d5e5 2018-06-23 stsp
3009 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3010 ffd1d5e5 2018-06-23 stsp free(repo_path);
3011 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3012 ffd1d5e5 2018-06-23 stsp return error;
3013 ffd1d5e5 2018-06-23 stsp
3014 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3015 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3016 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3017 ffd1d5e5 2018-06-23 stsp goto done;
3018 ffd1d5e5 2018-06-23 stsp } else {
3019 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3020 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3021 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3022 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
3023 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3024 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3025 ffd1d5e5 2018-06-23 stsp }
3026 ffd1d5e5 2018-06-23 stsp }
3027 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3028 ffd1d5e5 2018-06-23 stsp goto done;
3029 ffd1d5e5 2018-06-23 stsp
3030 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3031 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3032 ffd1d5e5 2018-06-23 stsp goto done;
3033 ffd1d5e5 2018-06-23 stsp
3034 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3035 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3036 ffd1d5e5 2018-06-23 stsp goto done;
3037 ffd1d5e5 2018-06-23 stsp
3038 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3039 5221c383 2018-08-01 stsp if (view == NULL) {
3040 5221c383 2018-08-01 stsp error = got_error_from_errno();
3041 5221c383 2018-08-01 stsp goto done;
3042 5221c383 2018-08-01 stsp }
3043 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3044 ad80ab7b 2018-08-04 stsp if (error)
3045 ad80ab7b 2018-08-04 stsp goto done;
3046 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3047 ffd1d5e5 2018-06-23 stsp done:
3048 ffd1d5e5 2018-06-23 stsp free(commit_id);
3049 ffd1d5e5 2018-06-23 stsp if (commit)
3050 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3051 ffd1d5e5 2018-06-23 stsp if (tree)
3052 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3053 ffd1d5e5 2018-06-23 stsp if (repo)
3054 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3055 ffd1d5e5 2018-06-23 stsp return error;
3056 ffd1d5e5 2018-06-23 stsp }
3057 5c5136c5 2018-05-20 stsp static void
3058 9f7d7167 2018-04-29 stsp init_curses(void)
3059 9f7d7167 2018-04-29 stsp {
3060 9f7d7167 2018-04-29 stsp initscr();
3061 9f7d7167 2018-04-29 stsp cbreak();
3062 9f7d7167 2018-04-29 stsp noecho();
3063 9f7d7167 2018-04-29 stsp nonl();
3064 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3065 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3066 1f475ad8 2018-05-10 stsp curs_set(0);
3067 9f7d7167 2018-04-29 stsp }
3068 9f7d7167 2018-04-29 stsp
3069 4ed7e80c 2018-05-20 stsp __dead static void
3070 9f7d7167 2018-04-29 stsp usage(void)
3071 9f7d7167 2018-04-29 stsp {
3072 9f7d7167 2018-04-29 stsp int i;
3073 9f7d7167 2018-04-29 stsp
3074 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3075 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3076 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3077 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3078 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3079 9f7d7167 2018-04-29 stsp }
3080 9f7d7167 2018-04-29 stsp exit(1);
3081 9f7d7167 2018-04-29 stsp }
3082 9f7d7167 2018-04-29 stsp
3083 c2301be8 2018-04-30 stsp static char **
3084 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3085 c2301be8 2018-04-30 stsp {
3086 c2301be8 2018-04-30 stsp char **argv;
3087 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3088 c2301be8 2018-04-30 stsp
3089 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3090 c2301be8 2018-04-30 stsp if (argv == NULL)
3091 c2301be8 2018-04-30 stsp err(1, "calloc");
3092 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3093 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3094 c2301be8 2018-04-30 stsp err(1, "calloc");
3095 c2301be8 2018-04-30 stsp if (arg1) {
3096 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3097 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3098 c2301be8 2018-04-30 stsp err(1, "calloc");
3099 c2301be8 2018-04-30 stsp }
3100 c2301be8 2018-04-30 stsp
3101 c2301be8 2018-04-30 stsp return argv;
3102 c2301be8 2018-04-30 stsp }
3103 c2301be8 2018-04-30 stsp
3104 9f7d7167 2018-04-29 stsp int
3105 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3106 9f7d7167 2018-04-29 stsp {
3107 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3108 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3109 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3110 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3111 9f7d7167 2018-04-29 stsp
3112 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3113 9f7d7167 2018-04-29 stsp
3114 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3115 9f7d7167 2018-04-29 stsp switch (ch) {
3116 9f7d7167 2018-04-29 stsp case 'h':
3117 9f7d7167 2018-04-29 stsp hflag = 1;
3118 9f7d7167 2018-04-29 stsp break;
3119 9f7d7167 2018-04-29 stsp default:
3120 9f7d7167 2018-04-29 stsp usage();
3121 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3122 9f7d7167 2018-04-29 stsp }
3123 9f7d7167 2018-04-29 stsp }
3124 9f7d7167 2018-04-29 stsp
3125 9f7d7167 2018-04-29 stsp argc -= optind;
3126 9f7d7167 2018-04-29 stsp argv += optind;
3127 9f7d7167 2018-04-29 stsp optind = 0;
3128 c2301be8 2018-04-30 stsp optreset = 1;
3129 9f7d7167 2018-04-29 stsp
3130 c2301be8 2018-04-30 stsp if (argc == 0) {
3131 f29d3e89 2018-06-23 stsp if (hflag)
3132 f29d3e89 2018-06-23 stsp usage();
3133 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3134 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3135 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3136 c2301be8 2018-04-30 stsp argc = 1;
3137 c2301be8 2018-04-30 stsp } else {
3138 9f7d7167 2018-04-29 stsp int i;
3139 9f7d7167 2018-04-29 stsp
3140 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3141 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3142 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3143 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3144 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3145 9f7d7167 2018-04-29 stsp if (hflag)
3146 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3147 9f7d7167 2018-04-29 stsp break;
3148 9f7d7167 2018-04-29 stsp }
3149 9f7d7167 2018-04-29 stsp }
3150 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3151 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3152 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3153 c2301be8 2018-04-30 stsp if (repo_path) {
3154 c2301be8 2018-04-30 stsp struct got_repository *repo;
3155 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3156 c2301be8 2018-04-30 stsp if (error == NULL)
3157 c2301be8 2018-04-30 stsp got_repo_close(repo);
3158 c2301be8 2018-04-30 stsp } else
3159 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3160 c2301be8 2018-04-30 stsp if (error) {
3161 f29d3e89 2018-06-23 stsp if (hflag) {
3162 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3163 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3164 f29d3e89 2018-06-23 stsp argv[0]);
3165 f29d3e89 2018-06-23 stsp usage();
3166 f29d3e89 2018-06-23 stsp }
3167 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3168 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3169 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3170 ad7de8d9 2018-04-30 stsp free(repo_path);
3171 c2301be8 2018-04-30 stsp return 1;
3172 c2301be8 2018-04-30 stsp }
3173 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3174 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3175 c2301be8 2018-04-30 stsp argc = 2;
3176 c2301be8 2018-04-30 stsp free(repo_path);
3177 9f7d7167 2018-04-29 stsp }
3178 9f7d7167 2018-04-29 stsp }
3179 9f7d7167 2018-04-29 stsp
3180 5c5136c5 2018-05-20 stsp init_curses();
3181 9f7d7167 2018-04-29 stsp
3182 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3183 9f7d7167 2018-04-29 stsp if (error)
3184 9f7d7167 2018-04-29 stsp goto done;
3185 9f7d7167 2018-04-29 stsp done:
3186 9f7d7167 2018-04-29 stsp endwin();
3187 c2301be8 2018-04-30 stsp free(cmd_argv);
3188 9f7d7167 2018-04-29 stsp if (error)
3189 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3190 9f7d7167 2018-04-29 stsp return 0;
3191 9f7d7167 2018-04-29 stsp }