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