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