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