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"
44 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 9f7d7167 2018-04-29 stsp #ifndef nitems
48 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 9f7d7167 2018-04-29 stsp struct tog_cmd {
52 c2301be8 2018-04-30 stsp const char *name;
53 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
54 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
55 c2301be8 2018-04-30 stsp const char *descr;
58 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
59 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
60 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
61 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
63 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
64 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
65 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
67 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
68 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
69 9f7d7167 2018-04-29 stsp "show repository history" },
70 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
71 9f7d7167 2018-04-29 stsp "compare files and directories" },
72 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
73 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
76 fed328cc 2018-05-20 stsp static struct tog_view {
77 26ed57b2 2018-05-19 stsp WINDOW *window;
78 26ed57b2 2018-05-19 stsp PANEL *panel;
79 fed328cc 2018-05-20 stsp } tog_log_view, tog_diff_view;
81 cd0acaa7 2018-05-20 stsp static const struct got_error *
82 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
83 cd0acaa7 2018-05-20 stsp struct got_repository *);
84 cd0acaa7 2018-05-20 stsp static const struct got_error *
85 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
87 4ed7e80c 2018-05-20 stsp __dead static void
88 9f7d7167 2018-04-29 stsp usage_log(void)
91 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
92 9f7d7167 2018-04-29 stsp getprogname());
96 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
97 80ddbec8 2018-04-29 stsp static const struct got_error *
98 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
100 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
102 963b370f 2018-05-20 stsp *ws = NULL;
103 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
104 963b370f 2018-05-20 stsp if (*wlen == (size_t)-1)
105 963b370f 2018-05-20 stsp return got_error_from_errno();
107 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
108 963b370f 2018-05-20 stsp if (*ws == NULL)
109 963b370f 2018-05-20 stsp return got_error_from_errno();
111 963b370f 2018-05-20 stsp if (mbstowcs(*ws, s, *wlen) != *wlen)
112 963b370f 2018-05-20 stsp err = got_error_from_errno();
116 963b370f 2018-05-20 stsp *ws = NULL;
119 963b370f 2018-05-20 stsp return err;
122 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
123 963b370f 2018-05-20 stsp static const struct got_error *
124 963b370f 2018-05-20 stsp format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
126 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
127 963b370f 2018-05-20 stsp int cols = 0;
128 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
129 963b370f 2018-05-20 stsp size_t wlen;
132 963b370f 2018-05-20 stsp *wlinep = NULL;
134 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
136 963b370f 2018-05-20 stsp return err;
139 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
140 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
141 963b370f 2018-05-20 stsp switch (width) {
146 963b370f 2018-05-20 stsp cols += width;
149 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
150 963b370f 2018-05-20 stsp cols += TABSIZE;
153 963b370f 2018-05-20 stsp err = got_error_from_errno();
156 963b370f 2018-05-20 stsp if (cols <= COLS) {
158 963b370f 2018-05-20 stsp if (widthp)
159 963b370f 2018-05-20 stsp *widthp = cols;
162 963b370f 2018-05-20 stsp wline[i] = L'\0';
165 963b370f 2018-05-20 stsp free(wline);
167 963b370f 2018-05-20 stsp *wlinep = wline;
168 963b370f 2018-05-20 stsp return err;
171 963b370f 2018-05-20 stsp static const struct got_error *
172 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
174 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
175 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
176 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
177 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
178 bb737323 2018-05-20 stsp int author_width, logmsg_width;
179 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
180 80ddbec8 2018-04-29 stsp char *line = NULL;
181 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
182 bb737323 2018-05-20 stsp size_t id_len;
183 bb737323 2018-05-20 stsp int col, limit;
184 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
185 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
186 9c2eaf34 2018-05-20 stsp const int avail = COLS;
188 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
190 80ddbec8 2018-04-29 stsp return err;
191 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
192 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
193 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
194 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
196 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
197 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
199 bb737323 2018-05-20 stsp col = limit + 1;
200 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
201 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
204 9c2eaf34 2018-05-20 stsp if (col > avail)
207 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
208 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
209 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
212 6d9fbc00 2018-04-29 stsp author = author0;
213 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
214 6d9fbc00 2018-04-29 stsp if (smallerthan)
215 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
217 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
219 6d9fbc00 2018-04-29 stsp *at = '\0';
221 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
222 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
225 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
226 bb737323 2018-05-20 stsp col += author_width;
227 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
228 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
230 bb737323 2018-05-20 stsp author_width++;
232 9c2eaf34 2018-05-20 stsp if (col > avail)
235 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
236 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
237 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
240 bb737323 2018-05-20 stsp logmsg = logmsg0;
241 bb737323 2018-05-20 stsp while (*logmsg == '\n')
243 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
244 bb737323 2018-05-20 stsp if (newline)
245 bb737323 2018-05-20 stsp *newline = '\0';
246 bb737323 2018-05-20 stsp limit = avail - col;
247 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
250 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
251 bb737323 2018-05-20 stsp col += logmsg_width;
252 bb737323 2018-05-20 stsp while (col <= avail) {
253 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
257 80ddbec8 2018-04-29 stsp free(logmsg0);
258 bb737323 2018-05-20 stsp free(wlogmsg);
259 6d9fbc00 2018-04-29 stsp free(author0);
260 bb737323 2018-05-20 stsp free(wauthor);
261 80ddbec8 2018-04-29 stsp free(line);
262 6d9fbc00 2018-04-29 stsp free(id_str);
263 80ddbec8 2018-04-29 stsp return err;
266 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
267 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
268 80ddbec8 2018-04-29 stsp struct got_object_id *id;
269 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
271 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
273 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
274 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
275 899d86c2 2018-05-10 stsp struct got_object_id *id)
277 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
279 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
280 80ddbec8 2018-04-29 stsp if (entry == NULL)
281 899d86c2 2018-05-10 stsp return NULL;
283 899d86c2 2018-05-10 stsp entry->id = id;
284 99db9666 2018-05-07 stsp entry->commit = commit;
285 899d86c2 2018-05-10 stsp return entry;
288 99db9666 2018-05-07 stsp static void
289 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
291 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
293 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
294 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
295 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
296 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
297 99db9666 2018-05-07 stsp free(entry);
300 99db9666 2018-05-07 stsp static void
301 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
303 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
304 99db9666 2018-05-07 stsp pop_commit(commits);
307 c4972b91 2018-05-07 stsp static const struct got_error *
308 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
309 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
311 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
312 899d86c2 2018-05-10 stsp struct got_object_id *id;
313 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
315 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
317 c4972b91 2018-05-07 stsp return err;
319 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
320 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
321 9ba79e04 2018-06-11 stsp int nfetched;
323 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
324 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
325 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
326 9ba79e04 2018-06-11 stsp return err;
328 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
330 9ba79e04 2018-06-11 stsp return err;
333 9ba79e04 2018-06-11 stsp while (1) {
334 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
336 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
338 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
339 9ba79e04 2018-06-11 stsp err = NULL;
343 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
347 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
348 9ba79e04 2018-06-11 stsp if (entry == NULL) {
349 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
353 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
356 9ba79e04 2018-06-11 stsp return err;
359 99db9666 2018-05-07 stsp static const struct got_error *
360 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
361 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
362 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
364 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
365 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
367 9ba79e04 2018-06-11 stsp *pentry = NULL;
369 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
370 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
371 9ba79e04 2018-06-11 stsp int nfetched;
372 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
373 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
375 9ba79e04 2018-06-11 stsp return err;
378 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
379 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
381 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
382 9ba79e04 2018-06-11 stsp err = NULL;
383 9ba79e04 2018-06-11 stsp return err;
386 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
387 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
388 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
389 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
391 9ba79e04 2018-06-11 stsp return NULL;
394 899d86c2 2018-05-10 stsp static const struct got_error *
395 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
397 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
398 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
400 9ba79e04 2018-06-11 stsp *head_id = NULL;
402 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
404 99db9666 2018-05-07 stsp return err;
406 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
407 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
409 9ba79e04 2018-06-11 stsp *head_id = NULL;
410 99db9666 2018-05-07 stsp return err;
413 9ba79e04 2018-06-11 stsp return NULL;
416 0553a4e3 2018-04-30 stsp static const struct got_error *
417 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
418 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
420 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
421 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
422 0553a4e3 2018-04-30 stsp int ncommits = 0;
424 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
426 899d86c2 2018-05-10 stsp entry = first;
427 899d86c2 2018-05-10 stsp *last = first;
428 899d86c2 2018-05-10 stsp while (entry) {
429 899d86c2 2018-05-10 stsp if (ncommits == limit)
431 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
432 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
433 cd0acaa7 2018-05-20 stsp *selected = entry;
435 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
436 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
437 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
440 0553a4e3 2018-04-30 stsp ncommits++;
441 899d86c2 2018-05-10 stsp *last = entry;
442 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
445 80ddbec8 2018-04-29 stsp update_panels();
446 80ddbec8 2018-04-29 stsp doupdate();
448 80ddbec8 2018-04-29 stsp return err;
451 07b55e75 2018-05-10 stsp static void
452 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
453 07b55e75 2018-05-10 stsp struct commit_queue *commits)
455 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
456 07b55e75 2018-05-10 stsp int nscrolled = 0;
458 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
459 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
462 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
463 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
464 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
465 07b55e75 2018-05-10 stsp if (entry) {
466 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
467 07b55e75 2018-05-10 stsp nscrolled++;
472 aa075928 2018-05-10 stsp static const struct got_error *
473 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
474 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
475 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
476 9ba79e04 2018-06-11 stsp struct got_repository *repo)
478 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
479 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
480 aa075928 2018-05-10 stsp int nscrolled = 0;
483 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
484 aa075928 2018-05-10 stsp if (pentry == NULL) {
485 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
486 9ba79e04 2018-06-11 stsp commits, graph, repo);
487 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
490 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
492 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
493 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
495 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
496 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
498 dd0a52c1 2018-05-20 stsp return err;
502 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
504 4a7f7875 2018-05-10 stsp int nparents = 0;
506 4a7f7875 2018-05-10 stsp while (entry) {
507 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
508 4a7f7875 2018-05-10 stsp nparents++;
511 4a7f7875 2018-05-10 stsp return nparents;
514 cd0acaa7 2018-05-20 stsp static const struct got_error *
515 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
517 cd0acaa7 2018-05-20 stsp const struct got_error *err;
518 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
519 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
521 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
523 cd0acaa7 2018-05-20 stsp return err;
525 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
526 9ba79e04 2018-06-11 stsp if (parent_id) {
527 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
532 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
535 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
537 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
538 cd0acaa7 2018-05-20 stsp return err;
541 80ddbec8 2018-04-29 stsp static const struct got_error *
542 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
544 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
545 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
546 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
547 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
548 0553a4e3 2018-04-30 stsp struct commit_queue commits;
549 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
550 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
551 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
552 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
554 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
555 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
556 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
557 80ddbec8 2018-04-29 stsp return got_error_from_errno();
558 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
560 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
561 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
562 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
563 80ddbec8 2018-04-29 stsp return got_error_from_errno();
565 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
567 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
569 9ba79e04 2018-06-11 stsp return err;
571 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
573 9ba79e04 2018-06-11 stsp err = got_commit_graph_open(&graph, head_id, repo);
577 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
578 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
582 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
587 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
588 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
589 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
590 9ba79e04 2018-06-11 stsp * updating the display.
592 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
593 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
596 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
597 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
598 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
599 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
603 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
604 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
608 899d86c2 2018-05-10 stsp while (!done) {
609 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
610 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
614 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
615 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
616 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
617 80ddbec8 2018-04-29 stsp switch (ch) {
619 31120ada 2018-04-30 stsp if (errno) {
620 31120ada 2018-04-30 stsp err = got_error_from_errno();
628 80ddbec8 2018-04-29 stsp case KEY_UP:
629 80ddbec8 2018-04-29 stsp if (selected > 0)
630 80ddbec8 2018-04-29 stsp selected--;
631 8ec9698d 2018-05-10 stsp if (selected > 0)
633 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
635 48531068 2018-05-10 stsp case KEY_PPAGE:
636 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
637 dfc1d240 2018-05-10 stsp first_displayed_entry) {
638 dfc1d240 2018-05-10 stsp selected = 0;
641 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
645 80ddbec8 2018-04-29 stsp case KEY_DOWN:
646 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
647 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
648 15c91275 2018-05-20 stsp selected < nparents - 1) {
649 15c91275 2018-05-20 stsp selected++;
652 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
653 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
658 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
659 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
660 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
664 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
666 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
667 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
668 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
669 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
670 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
672 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
673 31120ada 2018-04-30 stsp if (selected > LINES)
674 31120ada 2018-04-30 stsp selected = LINES - 1;
676 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
678 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
681 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
688 9ba79e04 2018-06-11 stsp free(head_id);
690 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
691 0553a4e3 2018-04-30 stsp free_commits(&commits);
692 80ddbec8 2018-04-29 stsp return err;
695 4ed7e80c 2018-05-20 stsp static const struct got_error *
696 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
698 80ddbec8 2018-04-29 stsp const struct got_error *error;
699 80ddbec8 2018-04-29 stsp struct got_repository *repo;
700 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
701 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
702 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
705 80ddbec8 2018-04-29 stsp #ifndef PROFILE
706 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
707 80ddbec8 2018-04-29 stsp err(1, "pledge");
710 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
711 80ddbec8 2018-04-29 stsp switch (ch) {
713 80ddbec8 2018-04-29 stsp start_commit = optarg;
717 80ddbec8 2018-04-29 stsp /* NOTREACHED */
721 80ddbec8 2018-04-29 stsp argc -= optind;
722 80ddbec8 2018-04-29 stsp argv += optind;
724 80ddbec8 2018-04-29 stsp if (argc == 0) {
725 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
726 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
727 80ddbec8 2018-04-29 stsp return got_error_from_errno();
728 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
729 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
730 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
731 80ddbec8 2018-04-29 stsp return got_error_from_errno();
733 80ddbec8 2018-04-29 stsp usage_log();
735 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
736 80ddbec8 2018-04-29 stsp free(repo_path);
737 80ddbec8 2018-04-29 stsp if (error != NULL)
738 80ddbec8 2018-04-29 stsp return error;
740 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
741 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
742 80ddbec8 2018-04-29 stsp if (error != NULL)
743 80ddbec8 2018-04-29 stsp return error;
745 80ddbec8 2018-04-29 stsp struct got_object *obj;
746 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
747 80ddbec8 2018-04-29 stsp if (error == NULL) {
748 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
749 899d86c2 2018-05-10 stsp if (start_id == NULL)
750 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
753 80ddbec8 2018-04-29 stsp if (error != NULL)
754 80ddbec8 2018-04-29 stsp return error;
755 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
756 899d86c2 2018-05-10 stsp free(start_id);
757 80ddbec8 2018-04-29 stsp got_repo_close(repo);
758 80ddbec8 2018-04-29 stsp return error;
761 4ed7e80c 2018-05-20 stsp __dead static void
762 9f7d7167 2018-04-29 stsp usage_diff(void)
765 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
766 9f7d7167 2018-04-29 stsp getprogname());
770 b304db33 2018-05-20 stsp static char *
771 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
773 b304db33 2018-05-20 stsp char *line;
774 b304db33 2018-05-20 stsp size_t linelen;
775 b304db33 2018-05-20 stsp size_t lineno;
776 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
778 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
780 b304db33 2018-05-20 stsp *len = linelen;
781 b304db33 2018-05-20 stsp return line;
784 4ed7e80c 2018-05-20 stsp static const struct got_error *
785 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
786 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
788 61e69b96 2018-05-20 stsp const struct got_error *err;
789 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
790 b304db33 2018-05-20 stsp char *line;
791 b304db33 2018-05-20 stsp size_t len;
792 61e69b96 2018-05-20 stsp wchar_t *wline;
796 9c2de6ef 2018-05-20 stsp werase(tog_diff_view.window);
799 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
800 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
801 26ed57b2 2018-05-19 stsp if (line == NULL) {
805 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
806 26ed57b2 2018-05-19 stsp free(line);
810 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
812 61e69b96 2018-05-20 stsp free(line);
813 61e69b96 2018-05-20 stsp return err;
815 61e69b96 2018-05-20 stsp waddwstr(tog_diff_view.window, wline);
816 e0b650dd 2018-05-20 stsp if (width < COLS)
817 e0b650dd 2018-05-20 stsp waddch(tog_diff_view.window, '\n');
818 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
819 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
820 26ed57b2 2018-05-19 stsp free(line);
822 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
824 26ed57b2 2018-05-19 stsp update_panels();
825 26ed57b2 2018-05-19 stsp doupdate();
827 26ed57b2 2018-05-19 stsp return NULL;
830 4ed7e80c 2018-05-20 stsp static const struct got_error *
831 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
832 26ed57b2 2018-05-19 stsp struct got_repository *repo)
834 26ed57b2 2018-05-19 stsp const struct got_error *err;
836 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
837 b304db33 2018-05-20 stsp int eof, i;
839 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
840 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
841 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
843 511a516b 2018-05-19 stsp f = got_opentemp();
844 26ed57b2 2018-05-19 stsp if (f == NULL)
845 26ed57b2 2018-05-19 stsp return got_error_from_errno();
847 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
848 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
849 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
851 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
852 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
854 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
855 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
858 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
863 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
864 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
865 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
866 26ed57b2 2018-05-19 stsp return got_error_from_errno();
867 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
869 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
870 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
871 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
872 26ed57b2 2018-05-19 stsp return got_error_from_errno();
874 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
876 26ed57b2 2018-05-19 stsp while (!done) {
877 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
878 26ed57b2 2018-05-19 stsp &eof, LINES);
881 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
882 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
883 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
884 26ed57b2 2018-05-19 stsp switch (ch) {
889 26ed57b2 2018-05-19 stsp case KEY_UP:
890 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
891 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
892 b304db33 2018-05-20 stsp first_displayed_line--;
894 b304db33 2018-05-20 stsp case KEY_PPAGE:
896 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
897 d56caa55 2018-05-20 stsp first_displayed_line > 1)
898 26ed57b2 2018-05-19 stsp first_displayed_line--;
901 26ed57b2 2018-05-19 stsp case KEY_DOWN:
902 925e6f23 2018-05-20 stsp case KEY_ENTER:
905 26ed57b2 2018-05-19 stsp first_displayed_line++;
907 b304db33 2018-05-20 stsp case KEY_NPAGE:
910 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
911 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
912 b304db33 2018-05-20 stsp first_displayed_line++;
913 b304db33 2018-05-20 stsp if (line == NULL)
922 26ed57b2 2018-05-19 stsp return err;
925 4ed7e80c 2018-05-20 stsp static const struct got_error *
926 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
928 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
929 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
930 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
931 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
932 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
935 26ed57b2 2018-05-19 stsp #ifndef PROFILE
936 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
937 26ed57b2 2018-05-19 stsp err(1, "pledge");
940 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
941 26ed57b2 2018-05-19 stsp switch (ch) {
944 26ed57b2 2018-05-19 stsp /* NOTREACHED */
948 26ed57b2 2018-05-19 stsp argc -= optind;
949 26ed57b2 2018-05-19 stsp argv += optind;
951 26ed57b2 2018-05-19 stsp if (argc == 0) {
952 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
953 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
954 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
955 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
956 26ed57b2 2018-05-19 stsp return got_error_from_errno();
957 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
958 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
959 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
960 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
961 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
962 26ed57b2 2018-05-19 stsp return got_error_from_errno();
963 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
964 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
966 26ed57b2 2018-05-19 stsp usage_diff();
968 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
969 26ed57b2 2018-05-19 stsp free(repo_path);
973 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
977 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
981 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
983 26ed57b2 2018-05-19 stsp got_repo_close(repo);
985 26ed57b2 2018-05-19 stsp got_object_close(obj1);
987 26ed57b2 2018-05-19 stsp got_object_close(obj2);
988 26ed57b2 2018-05-19 stsp return error;
991 4ed7e80c 2018-05-20 stsp __dead static void
992 9f7d7167 2018-04-29 stsp usage_blame(void)
995 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
996 9f7d7167 2018-04-29 stsp getprogname());
1000 4ed7e80c 2018-05-20 stsp static const struct got_error *
1001 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1003 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
1006 5c5136c5 2018-05-20 stsp static void
1007 9f7d7167 2018-04-29 stsp init_curses(void)
1009 9f7d7167 2018-04-29 stsp initscr();
1013 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1014 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1015 1f475ad8 2018-05-10 stsp curs_set(0);
1018 4ed7e80c 2018-05-20 stsp __dead static void
1019 9f7d7167 2018-04-29 stsp usage(void)
1023 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1024 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1025 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1026 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1027 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1032 c2301be8 2018-04-30 stsp static char **
1033 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1035 c2301be8 2018-04-30 stsp char **argv;
1036 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1038 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1039 c2301be8 2018-04-30 stsp if (argv == NULL)
1040 c2301be8 2018-04-30 stsp err(1, "calloc");
1041 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1042 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1043 c2301be8 2018-04-30 stsp err(1, "calloc");
1044 c2301be8 2018-04-30 stsp if (arg1) {
1045 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1046 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1047 c2301be8 2018-04-30 stsp err(1, "calloc");
1050 c2301be8 2018-04-30 stsp return argv;
1054 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1056 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1057 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1058 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1059 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1061 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1063 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1064 9f7d7167 2018-04-29 stsp switch (ch) {
1066 9f7d7167 2018-04-29 stsp hflag = 1;
1070 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1074 9f7d7167 2018-04-29 stsp argc -= optind;
1075 9f7d7167 2018-04-29 stsp argv += optind;
1076 9f7d7167 2018-04-29 stsp optind = 0;
1077 c2301be8 2018-04-30 stsp optreset = 1;
1079 c2301be8 2018-04-30 stsp if (argc == 0) {
1080 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1081 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1082 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1087 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1088 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1089 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1090 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1091 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1092 9f7d7167 2018-04-29 stsp if (hflag)
1093 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1097 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1098 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1099 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1100 c2301be8 2018-04-30 stsp if (repo_path) {
1101 c2301be8 2018-04-30 stsp struct got_repository *repo;
1102 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1103 c2301be8 2018-04-30 stsp if (error == NULL)
1104 c2301be8 2018-04-30 stsp got_repo_close(repo);
1106 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1107 c2301be8 2018-04-30 stsp if (error) {
1108 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1109 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1110 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1111 ad7de8d9 2018-04-30 stsp free(repo_path);
1114 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1115 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1117 c2301be8 2018-04-30 stsp free(repo_path);
1121 5c5136c5 2018-05-20 stsp init_curses();
1123 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1124 9f7d7167 2018-04-29 stsp if (error)
1125 9f7d7167 2018-04-29 stsp goto done;
1128 c2301be8 2018-04-30 stsp free(cmd_argv);
1129 9f7d7167 2018-04-29 stsp if (error)
1130 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);