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