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