Blame


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