2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
19 31120ada 2018-04-30 stsp #include <errno.h>
20 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
21 9f7d7167 2018-04-29 stsp #include <curses.h>
22 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <panel.h>
24 9f7d7167 2018-04-29 stsp #include <locale.h>
25 9f7d7167 2018-04-29 stsp #include <stdlib.h>
26 26ed57b2 2018-05-19 stsp #include <stdio.h>
27 9f7d7167 2018-04-29 stsp #include <getopt.h>
28 9f7d7167 2018-04-29 stsp #include <string.h>
29 9f7d7167 2018-04-29 stsp #include <err.h>
30 80ddbec8 2018-04-29 stsp #include <unistd.h>
31 26ed57b2 2018-05-19 stsp #include <util.h>
32 26ed57b2 2018-05-19 stsp #include <limits.h>
33 61e69b96 2018-05-20 stsp #include <wchar.h>
35 9f7d7167 2018-04-29 stsp #include "got_error.h"
36 80ddbec8 2018-04-29 stsp #include "got_object.h"
37 80ddbec8 2018-04-29 stsp #include "got_reference.h"
38 80ddbec8 2018-04-29 stsp #include "got_repository.h"
39 80ddbec8 2018-04-29 stsp #include "got_diff.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
41 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
42 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
45 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 9f7d7167 2018-04-29 stsp #ifndef nitems
49 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 9f7d7167 2018-04-29 stsp struct tog_cmd {
53 c2301be8 2018-04-30 stsp const char *name;
54 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
55 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
56 c2301be8 2018-04-30 stsp const char *descr;
59 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
60 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
61 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
62 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
64 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
65 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
66 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
68 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
69 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
70 9f7d7167 2018-04-29 stsp "show repository history" },
71 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
72 9f7d7167 2018-04-29 stsp "compare files and directories" },
73 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
74 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
77 fed328cc 2018-05-20 stsp static struct tog_view {
78 26ed57b2 2018-05-19 stsp WINDOW *window;
79 26ed57b2 2018-05-19 stsp PANEL *panel;
80 fed328cc 2018-05-20 stsp } tog_log_view, tog_diff_view;
82 cd0acaa7 2018-05-20 stsp static const struct got_error *
83 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
84 cd0acaa7 2018-05-20 stsp struct got_repository *);
85 cd0acaa7 2018-05-20 stsp static const struct got_error *
86 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
88 4ed7e80c 2018-05-20 stsp __dead static void
89 9f7d7167 2018-04-29 stsp usage_log(void)
92 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
93 9f7d7167 2018-04-29 stsp getprogname());
97 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
98 80ddbec8 2018-04-29 stsp static const struct got_error *
99 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
101 00dfcb92 2018-06-11 stsp char *vis = NULL;
102 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
104 963b370f 2018-05-20 stsp *ws = NULL;
105 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
106 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
107 00dfcb92 2018-06-11 stsp int vislen;
108 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
109 00dfcb92 2018-06-11 stsp return got_error_from_errno();
111 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
112 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
114 00dfcb92 2018-06-11 stsp return err;
115 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
116 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
117 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
122 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
123 a7f50699 2018-06-11 stsp if (*ws == NULL) {
124 a7f50699 2018-06-11 stsp err = got_error_from_errno();
128 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
129 963b370f 2018-05-20 stsp err = got_error_from_errno();
134 963b370f 2018-05-20 stsp *ws = NULL;
137 963b370f 2018-05-20 stsp return err;
140 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
141 963b370f 2018-05-20 stsp static const struct got_error *
142 963b370f 2018-05-20 stsp format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
144 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
145 963b370f 2018-05-20 stsp int cols = 0;
146 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
147 963b370f 2018-05-20 stsp size_t wlen;
150 963b370f 2018-05-20 stsp *wlinep = NULL;
152 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
154 963b370f 2018-05-20 stsp return err;
157 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
158 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
159 963b370f 2018-05-20 stsp switch (width) {
164 963b370f 2018-05-20 stsp cols += width;
167 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
168 963b370f 2018-05-20 stsp cols += TABSIZE;
171 963b370f 2018-05-20 stsp err = got_error_from_errno();
174 963b370f 2018-05-20 stsp if (cols <= COLS) {
176 963b370f 2018-05-20 stsp if (widthp)
177 963b370f 2018-05-20 stsp *widthp = cols;
180 963b370f 2018-05-20 stsp wline[i] = L'\0';
183 963b370f 2018-05-20 stsp free(wline);
185 963b370f 2018-05-20 stsp *wlinep = wline;
186 963b370f 2018-05-20 stsp return err;
189 963b370f 2018-05-20 stsp static const struct got_error *
190 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
192 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
193 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
194 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
195 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
196 bb737323 2018-05-20 stsp int author_width, logmsg_width;
197 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
198 80ddbec8 2018-04-29 stsp char *line = NULL;
199 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
200 bb737323 2018-05-20 stsp size_t id_len;
201 bb737323 2018-05-20 stsp int col, limit;
202 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
203 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
204 9c2eaf34 2018-05-20 stsp const int avail = COLS;
206 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
208 80ddbec8 2018-04-29 stsp return err;
209 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
210 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
211 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
212 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
214 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
215 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
217 bb737323 2018-05-20 stsp col = limit + 1;
218 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
219 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
222 9c2eaf34 2018-05-20 stsp if (col > avail)
225 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
226 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
227 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
230 6d9fbc00 2018-04-29 stsp author = author0;
231 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
232 6d9fbc00 2018-04-29 stsp if (smallerthan)
233 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
235 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
237 6d9fbc00 2018-04-29 stsp *at = '\0';
239 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
240 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
243 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
244 bb737323 2018-05-20 stsp col += author_width;
245 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
246 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
248 bb737323 2018-05-20 stsp author_width++;
250 9c2eaf34 2018-05-20 stsp if (col > avail)
253 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
254 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
255 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
258 bb737323 2018-05-20 stsp logmsg = logmsg0;
259 bb737323 2018-05-20 stsp while (*logmsg == '\n')
261 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
262 bb737323 2018-05-20 stsp if (newline)
263 bb737323 2018-05-20 stsp *newline = '\0';
264 bb737323 2018-05-20 stsp limit = avail - col;
265 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
268 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
269 bb737323 2018-05-20 stsp col += logmsg_width;
270 bb737323 2018-05-20 stsp while (col <= avail) {
271 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
275 80ddbec8 2018-04-29 stsp free(logmsg0);
276 bb737323 2018-05-20 stsp free(wlogmsg);
277 6d9fbc00 2018-04-29 stsp free(author0);
278 bb737323 2018-05-20 stsp free(wauthor);
279 80ddbec8 2018-04-29 stsp free(line);
280 6d9fbc00 2018-04-29 stsp free(id_str);
281 80ddbec8 2018-04-29 stsp return err;
284 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
285 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
286 80ddbec8 2018-04-29 stsp struct got_object_id *id;
287 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
289 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
291 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
292 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
293 899d86c2 2018-05-10 stsp struct got_object_id *id)
295 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
297 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
298 80ddbec8 2018-04-29 stsp if (entry == NULL)
299 899d86c2 2018-05-10 stsp return NULL;
301 899d86c2 2018-05-10 stsp entry->id = id;
302 99db9666 2018-05-07 stsp entry->commit = commit;
303 899d86c2 2018-05-10 stsp return entry;
306 99db9666 2018-05-07 stsp static void
307 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
309 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
311 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
312 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
313 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
314 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
315 99db9666 2018-05-07 stsp free(entry);
318 99db9666 2018-05-07 stsp static void
319 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
321 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
322 99db9666 2018-05-07 stsp pop_commit(commits);
325 c4972b91 2018-05-07 stsp static const struct got_error *
326 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
327 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
329 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
330 899d86c2 2018-05-10 stsp struct got_object_id *id;
331 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
333 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
335 c4972b91 2018-05-07 stsp return err;
337 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
338 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
339 9ba79e04 2018-06-11 stsp int nfetched;
341 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
342 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
343 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
344 9ba79e04 2018-06-11 stsp return err;
346 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
348 9ba79e04 2018-06-11 stsp return err;
351 9ba79e04 2018-06-11 stsp while (1) {
352 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
354 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
356 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
357 9ba79e04 2018-06-11 stsp err = NULL;
361 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
365 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
366 9ba79e04 2018-06-11 stsp if (entry == NULL) {
367 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
371 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
374 9ba79e04 2018-06-11 stsp return err;
377 99db9666 2018-05-07 stsp static const struct got_error *
378 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
379 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
380 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
382 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
383 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
385 9ba79e04 2018-06-11 stsp *pentry = NULL;
387 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
388 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
389 9ba79e04 2018-06-11 stsp int nfetched;
390 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
391 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
393 9ba79e04 2018-06-11 stsp return err;
396 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
397 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
399 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
400 9ba79e04 2018-06-11 stsp err = NULL;
401 9ba79e04 2018-06-11 stsp return err;
404 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
405 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
406 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
407 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
409 9ba79e04 2018-06-11 stsp return NULL;
412 899d86c2 2018-05-10 stsp static const struct got_error *
413 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
415 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
416 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
418 9ba79e04 2018-06-11 stsp *head_id = NULL;
420 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
422 99db9666 2018-05-07 stsp return err;
424 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
425 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
427 9ba79e04 2018-06-11 stsp *head_id = NULL;
428 99db9666 2018-05-07 stsp return err;
431 9ba79e04 2018-06-11 stsp return NULL;
434 0553a4e3 2018-04-30 stsp static const struct got_error *
435 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
436 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
438 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
439 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
440 0553a4e3 2018-04-30 stsp int ncommits = 0;
442 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
444 899d86c2 2018-05-10 stsp entry = first;
445 899d86c2 2018-05-10 stsp *last = first;
446 899d86c2 2018-05-10 stsp while (entry) {
447 899d86c2 2018-05-10 stsp if (ncommits == limit)
449 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
450 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
451 cd0acaa7 2018-05-20 stsp *selected = entry;
453 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
454 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
455 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
458 0553a4e3 2018-04-30 stsp ncommits++;
459 899d86c2 2018-05-10 stsp *last = entry;
460 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
463 80ddbec8 2018-04-29 stsp update_panels();
464 80ddbec8 2018-04-29 stsp doupdate();
466 80ddbec8 2018-04-29 stsp return err;
469 07b55e75 2018-05-10 stsp static void
470 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
471 07b55e75 2018-05-10 stsp struct commit_queue *commits)
473 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
474 07b55e75 2018-05-10 stsp int nscrolled = 0;
476 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
477 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
480 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
481 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
482 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
483 07b55e75 2018-05-10 stsp if (entry) {
484 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
485 07b55e75 2018-05-10 stsp nscrolled++;
490 aa075928 2018-05-10 stsp static const struct got_error *
491 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
492 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
493 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
494 9ba79e04 2018-06-11 stsp struct got_repository *repo)
496 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
497 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
498 aa075928 2018-05-10 stsp int nscrolled = 0;
501 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
502 aa075928 2018-05-10 stsp if (pentry == NULL) {
503 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
504 9ba79e04 2018-06-11 stsp commits, graph, repo);
505 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
508 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
510 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
511 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
513 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
514 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
516 dd0a52c1 2018-05-20 stsp return err;
520 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
522 4a7f7875 2018-05-10 stsp int nparents = 0;
524 4a7f7875 2018-05-10 stsp while (entry) {
525 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
526 4a7f7875 2018-05-10 stsp nparents++;
529 4a7f7875 2018-05-10 stsp return nparents;
532 cd0acaa7 2018-05-20 stsp static const struct got_error *
533 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
535 cd0acaa7 2018-05-20 stsp const struct got_error *err;
536 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
537 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
539 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
541 cd0acaa7 2018-05-20 stsp return err;
543 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
544 9ba79e04 2018-06-11 stsp if (parent_id) {
545 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
550 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
553 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
555 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
556 cd0acaa7 2018-05-20 stsp return err;
559 80ddbec8 2018-04-29 stsp static const struct got_error *
560 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
562 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
563 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
564 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
565 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
566 0553a4e3 2018-04-30 stsp struct commit_queue commits;
567 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
568 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
569 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
570 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
572 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
573 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
574 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
575 80ddbec8 2018-04-29 stsp return got_error_from_errno();
576 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
578 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
579 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
580 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
581 80ddbec8 2018-04-29 stsp return got_error_from_errno();
583 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
585 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
587 9ba79e04 2018-06-11 stsp return err;
589 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
591 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
595 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
596 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
600 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
605 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
606 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
607 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
608 9ba79e04 2018-06-11 stsp * updating the display.
610 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
611 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
614 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
615 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
616 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
617 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
621 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
622 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
626 899d86c2 2018-05-10 stsp while (!done) {
627 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
628 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
632 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
633 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
634 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
635 80ddbec8 2018-04-29 stsp switch (ch) {
637 31120ada 2018-04-30 stsp if (errno) {
638 31120ada 2018-04-30 stsp err = got_error_from_errno();
646 80ddbec8 2018-04-29 stsp case KEY_UP:
647 80ddbec8 2018-04-29 stsp if (selected > 0)
648 80ddbec8 2018-04-29 stsp selected--;
649 8ec9698d 2018-05-10 stsp if (selected > 0)
651 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
653 48531068 2018-05-10 stsp case KEY_PPAGE:
654 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
655 dfc1d240 2018-05-10 stsp first_displayed_entry) {
656 dfc1d240 2018-05-10 stsp selected = 0;
659 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
663 80ddbec8 2018-04-29 stsp case KEY_DOWN:
664 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
665 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
666 15c91275 2018-05-20 stsp selected < nparents - 1) {
667 15c91275 2018-05-20 stsp selected++;
670 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
671 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
676 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
677 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
678 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
682 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
684 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
685 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
686 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
687 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
688 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
690 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
691 31120ada 2018-04-30 stsp if (selected > LINES)
692 31120ada 2018-04-30 stsp selected = LINES - 1;
694 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
696 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
699 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
706 9ba79e04 2018-06-11 stsp free(head_id);
708 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
709 0553a4e3 2018-04-30 stsp free_commits(&commits);
710 80ddbec8 2018-04-29 stsp return err;
713 4ed7e80c 2018-05-20 stsp static const struct got_error *
714 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
716 80ddbec8 2018-04-29 stsp const struct got_error *error;
717 80ddbec8 2018-04-29 stsp struct got_repository *repo;
718 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
719 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
720 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
723 80ddbec8 2018-04-29 stsp #ifndef PROFILE
724 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
725 80ddbec8 2018-04-29 stsp err(1, "pledge");
728 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
729 80ddbec8 2018-04-29 stsp switch (ch) {
731 80ddbec8 2018-04-29 stsp start_commit = optarg;
735 80ddbec8 2018-04-29 stsp /* NOTREACHED */
739 80ddbec8 2018-04-29 stsp argc -= optind;
740 80ddbec8 2018-04-29 stsp argv += optind;
742 80ddbec8 2018-04-29 stsp if (argc == 0) {
743 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
744 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
745 80ddbec8 2018-04-29 stsp return got_error_from_errno();
746 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
747 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
748 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
749 80ddbec8 2018-04-29 stsp return got_error_from_errno();
751 80ddbec8 2018-04-29 stsp usage_log();
753 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
754 80ddbec8 2018-04-29 stsp free(repo_path);
755 80ddbec8 2018-04-29 stsp if (error != NULL)
756 80ddbec8 2018-04-29 stsp return error;
758 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
759 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
760 80ddbec8 2018-04-29 stsp if (error != NULL)
761 80ddbec8 2018-04-29 stsp return error;
763 80ddbec8 2018-04-29 stsp struct got_object *obj;
764 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
765 80ddbec8 2018-04-29 stsp if (error == NULL) {
766 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
767 899d86c2 2018-05-10 stsp if (start_id == NULL)
768 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
771 80ddbec8 2018-04-29 stsp if (error != NULL)
772 80ddbec8 2018-04-29 stsp return error;
773 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
774 899d86c2 2018-05-10 stsp free(start_id);
775 80ddbec8 2018-04-29 stsp got_repo_close(repo);
776 80ddbec8 2018-04-29 stsp return error;
779 4ed7e80c 2018-05-20 stsp __dead static void
780 9f7d7167 2018-04-29 stsp usage_diff(void)
783 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
784 9f7d7167 2018-04-29 stsp getprogname());
788 b304db33 2018-05-20 stsp static char *
789 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
791 b304db33 2018-05-20 stsp char *line;
792 b304db33 2018-05-20 stsp size_t linelen;
793 b304db33 2018-05-20 stsp size_t lineno;
794 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
796 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
798 b304db33 2018-05-20 stsp *len = linelen;
799 b304db33 2018-05-20 stsp return line;
802 4ed7e80c 2018-05-20 stsp static const struct got_error *
803 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
804 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
806 61e69b96 2018-05-20 stsp const struct got_error *err;
807 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
808 b304db33 2018-05-20 stsp char *line;
809 b304db33 2018-05-20 stsp size_t len;
810 61e69b96 2018-05-20 stsp wchar_t *wline;
814 9c2de6ef 2018-05-20 stsp werase(tog_diff_view.window);
817 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
818 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
819 26ed57b2 2018-05-19 stsp if (line == NULL) {
823 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
824 26ed57b2 2018-05-19 stsp free(line);
828 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
830 61e69b96 2018-05-20 stsp free(line);
831 61e69b96 2018-05-20 stsp return err;
833 61e69b96 2018-05-20 stsp waddwstr(tog_diff_view.window, wline);
834 e0b650dd 2018-05-20 stsp if (width < COLS)
835 e0b650dd 2018-05-20 stsp waddch(tog_diff_view.window, '\n');
836 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
837 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
838 26ed57b2 2018-05-19 stsp free(line);
840 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
842 26ed57b2 2018-05-19 stsp update_panels();
843 26ed57b2 2018-05-19 stsp doupdate();
845 26ed57b2 2018-05-19 stsp return NULL;
848 4ed7e80c 2018-05-20 stsp static const struct got_error *
849 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
850 26ed57b2 2018-05-19 stsp struct got_repository *repo)
852 26ed57b2 2018-05-19 stsp const struct got_error *err;
854 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
855 b304db33 2018-05-20 stsp int eof, i;
857 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
858 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
859 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
861 511a516b 2018-05-19 stsp f = got_opentemp();
862 26ed57b2 2018-05-19 stsp if (f == NULL)
863 26ed57b2 2018-05-19 stsp return got_error_from_errno();
865 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
866 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
867 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
869 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
870 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
872 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
873 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
876 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
881 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
882 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
883 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
884 26ed57b2 2018-05-19 stsp return got_error_from_errno();
885 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
887 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
888 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
889 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
890 26ed57b2 2018-05-19 stsp return got_error_from_errno();
892 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
894 26ed57b2 2018-05-19 stsp while (!done) {
895 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
896 26ed57b2 2018-05-19 stsp &eof, LINES);
899 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
900 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
901 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
902 26ed57b2 2018-05-19 stsp switch (ch) {
907 26ed57b2 2018-05-19 stsp case KEY_UP:
908 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
909 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
910 b304db33 2018-05-20 stsp first_displayed_line--;
912 b304db33 2018-05-20 stsp case KEY_PPAGE:
914 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
915 d56caa55 2018-05-20 stsp first_displayed_line > 1)
916 26ed57b2 2018-05-19 stsp first_displayed_line--;
919 26ed57b2 2018-05-19 stsp case KEY_DOWN:
920 925e6f23 2018-05-20 stsp case KEY_ENTER:
923 26ed57b2 2018-05-19 stsp first_displayed_line++;
925 b304db33 2018-05-20 stsp case KEY_NPAGE:
928 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
929 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
930 b304db33 2018-05-20 stsp first_displayed_line++;
931 b304db33 2018-05-20 stsp if (line == NULL)
940 26ed57b2 2018-05-19 stsp return err;
943 4ed7e80c 2018-05-20 stsp static const struct got_error *
944 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
946 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
947 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
948 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
949 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
950 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
953 26ed57b2 2018-05-19 stsp #ifndef PROFILE
954 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
955 26ed57b2 2018-05-19 stsp err(1, "pledge");
958 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
959 26ed57b2 2018-05-19 stsp switch (ch) {
962 26ed57b2 2018-05-19 stsp /* NOTREACHED */
966 26ed57b2 2018-05-19 stsp argc -= optind;
967 26ed57b2 2018-05-19 stsp argv += optind;
969 26ed57b2 2018-05-19 stsp if (argc == 0) {
970 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
971 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
972 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
973 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
974 26ed57b2 2018-05-19 stsp return got_error_from_errno();
975 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
976 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
977 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
978 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
979 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
980 26ed57b2 2018-05-19 stsp return got_error_from_errno();
981 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
982 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
984 26ed57b2 2018-05-19 stsp usage_diff();
986 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
987 26ed57b2 2018-05-19 stsp free(repo_path);
991 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
995 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
999 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1001 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1003 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1005 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1006 26ed57b2 2018-05-19 stsp return error;
1009 4ed7e80c 2018-05-20 stsp __dead static void
1010 9f7d7167 2018-04-29 stsp usage_blame(void)
1013 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1014 9f7d7167 2018-04-29 stsp getprogname());
1018 4ed7e80c 2018-05-20 stsp static const struct got_error *
1019 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1021 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
1024 5c5136c5 2018-05-20 stsp static void
1025 9f7d7167 2018-04-29 stsp init_curses(void)
1027 9f7d7167 2018-04-29 stsp initscr();
1031 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1032 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1033 1f475ad8 2018-05-10 stsp curs_set(0);
1036 4ed7e80c 2018-05-20 stsp __dead static void
1037 9f7d7167 2018-04-29 stsp usage(void)
1041 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1042 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1043 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1044 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1045 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1050 c2301be8 2018-04-30 stsp static char **
1051 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1053 c2301be8 2018-04-30 stsp char **argv;
1054 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1056 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1057 c2301be8 2018-04-30 stsp if (argv == NULL)
1058 c2301be8 2018-04-30 stsp err(1, "calloc");
1059 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1060 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1061 c2301be8 2018-04-30 stsp err(1, "calloc");
1062 c2301be8 2018-04-30 stsp if (arg1) {
1063 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1064 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1065 c2301be8 2018-04-30 stsp err(1, "calloc");
1068 c2301be8 2018-04-30 stsp return argv;
1072 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1074 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1075 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1076 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1077 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1079 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1081 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1082 9f7d7167 2018-04-29 stsp switch (ch) {
1084 9f7d7167 2018-04-29 stsp hflag = 1;
1088 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1092 9f7d7167 2018-04-29 stsp argc -= optind;
1093 9f7d7167 2018-04-29 stsp argv += optind;
1094 9f7d7167 2018-04-29 stsp optind = 0;
1095 c2301be8 2018-04-30 stsp optreset = 1;
1097 c2301be8 2018-04-30 stsp if (argc == 0) {
1098 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1099 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1100 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1105 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1106 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1107 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1108 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1109 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1110 9f7d7167 2018-04-29 stsp if (hflag)
1111 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1115 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1116 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1117 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1118 c2301be8 2018-04-30 stsp if (repo_path) {
1119 c2301be8 2018-04-30 stsp struct got_repository *repo;
1120 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1121 c2301be8 2018-04-30 stsp if (error == NULL)
1122 c2301be8 2018-04-30 stsp got_repo_close(repo);
1124 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1125 c2301be8 2018-04-30 stsp if (error) {
1126 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1127 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1128 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1129 ad7de8d9 2018-04-30 stsp free(repo_path);
1132 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1133 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1135 c2301be8 2018-04-30 stsp free(repo_path);
1139 5c5136c5 2018-05-20 stsp init_curses();
1141 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1142 9f7d7167 2018-04-29 stsp if (error)
1143 9f7d7167 2018-04-29 stsp goto done;
1146 c2301be8 2018-04-30 stsp free(cmd_argv);
1147 9f7d7167 2018-04-29 stsp if (error)
1148 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);