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