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