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 9f7d7167 2018-04-29 stsp #include <curses.h>
21 9f7d7167 2018-04-29 stsp #include <panel.h>
22 9f7d7167 2018-04-29 stsp #include <locale.h>
23 9f7d7167 2018-04-29 stsp #include <stdlib.h>
24 9f7d7167 2018-04-29 stsp #include <getopt.h>
25 9f7d7167 2018-04-29 stsp #include <string.h>
26 9f7d7167 2018-04-29 stsp #include <err.h>
27 80ddbec8 2018-04-29 stsp #include <unistd.h>
29 9f7d7167 2018-04-29 stsp #include "got_error.h"
30 80ddbec8 2018-04-29 stsp #include "got_object.h"
31 80ddbec8 2018-04-29 stsp #include "got_reference.h"
32 80ddbec8 2018-04-29 stsp #include "got_repository.h"
33 80ddbec8 2018-04-29 stsp #include "got_diff.h"
36 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
39 9f7d7167 2018-04-29 stsp #ifndef nitems
40 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 9f7d7167 2018-04-29 stsp enum tog_view_id {
44 9f7d7167 2018-04-29 stsp TOG_VIEW_LOG,
45 9f7d7167 2018-04-29 stsp TOG_VIEW_DIFF,
46 9f7d7167 2018-04-29 stsp TOG_VIEW_BLAME,
49 9f7d7167 2018-04-29 stsp struct tog_cmd {
50 c2301be8 2018-04-30 stsp const char *name;
51 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
52 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
53 9f7d7167 2018-04-29 stsp enum tog_view_id view;
54 c2301be8 2018-04-30 stsp const char *descr;
57 9f7d7167 2018-04-29 stsp __dead void usage(void);
58 9f7d7167 2018-04-29 stsp __dead void usage_log(void);
59 9f7d7167 2018-04-29 stsp __dead void usage_diff(void);
60 9f7d7167 2018-04-29 stsp __dead void usage_blame(void);
62 9f7d7167 2018-04-29 stsp const struct got_error* cmd_log(int, char *[]);
63 9f7d7167 2018-04-29 stsp const struct got_error* cmd_diff(int, char *[]);
64 9f7d7167 2018-04-29 stsp const struct got_error* cmd_blame(int, char *[]);
66 9f7d7167 2018-04-29 stsp struct tog_cmd tog_commands[] = {
67 9f7d7167 2018-04-29 stsp { "log", cmd_log, usage_log, TOG_VIEW_LOG,
68 9f7d7167 2018-04-29 stsp "show repository history" },
69 9f7d7167 2018-04-29 stsp { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
70 9f7d7167 2018-04-29 stsp "compare files and directories" },
71 9f7d7167 2018-04-29 stsp { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
72 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
75 9f7d7167 2018-04-29 stsp /* globals */
76 9f7d7167 2018-04-29 stsp WINDOW *tog_main_win;
77 9f7d7167 2018-04-29 stsp PANEL *tog_main_panel;
78 80ddbec8 2018-04-29 stsp static struct tog_log_view {
79 80ddbec8 2018-04-29 stsp WINDOW *window;
80 80ddbec8 2018-04-29 stsp PANEL *panel;
81 80ddbec8 2018-04-29 stsp } tog_log_view;
84 9f7d7167 2018-04-29 stsp usage_log(void)
87 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
88 9f7d7167 2018-04-29 stsp getprogname());
92 80ddbec8 2018-04-29 stsp static const struct got_error *
93 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
95 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
96 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
97 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
98 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
99 80ddbec8 2018-04-29 stsp char *line = NULL;
100 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
101 881b2d3e 2018-04-30 stsp const size_t id_display_len = 8;
102 881b2d3e 2018-04-30 stsp const size_t author_display_len = 16;
103 881b2d3e 2018-04-30 stsp size_t id_len, author_len, logmsg_len, avail;
104 881b2d3e 2018-04-30 stsp int i, col;
106 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
108 80ddbec8 2018-04-29 stsp return err;
109 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
111 80ddbec8 2018-04-29 stsp logmsg0 = strdup(commit->logmsg);
112 6d9fbc00 2018-04-29 stsp if (logmsg0 == NULL) {
113 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
116 80ddbec8 2018-04-29 stsp logmsg = logmsg0;
117 80ddbec8 2018-04-29 stsp while (*logmsg == '\n')
119 80ddbec8 2018-04-29 stsp newline = strchr(logmsg, '\n');
120 6d9fbc00 2018-04-29 stsp if (newline)
121 80ddbec8 2018-04-29 stsp *newline = '\0';
122 881b2d3e 2018-04-30 stsp logmsg_len = strlen(logmsg);
124 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
125 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
126 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
129 6d9fbc00 2018-04-29 stsp author = author0;
130 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
131 6d9fbc00 2018-04-29 stsp if (smallerthan)
132 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
134 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
136 6d9fbc00 2018-04-29 stsp *at = '\0';
138 881b2d3e 2018-04-30 stsp author_len = strlen(author);
140 881b2d3e 2018-04-30 stsp avail = COLS - 1;
141 881b2d3e 2018-04-30 stsp line = calloc(avail + 1, sizeof(*line));
142 881b2d3e 2018-04-30 stsp if (line == NULL) {
143 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
148 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(id_display_len, id_len); i++) {
149 881b2d3e 2018-04-30 stsp if (col >= avail)
151 881b2d3e 2018-04-30 stsp line[col++] = id_str[i];
153 881b2d3e 2018-04-30 stsp while (i < id_display_len) {
154 881b2d3e 2018-04-30 stsp if (col >= avail)
156 881b2d3e 2018-04-30 stsp line[col++] = ' ';
159 881b2d3e 2018-04-30 stsp if (col >= avail)
161 881b2d3e 2018-04-30 stsp line[col++] = ' ';
162 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(author_display_len, author_len); i++) {
163 881b2d3e 2018-04-30 stsp if (col >= avail)
165 881b2d3e 2018-04-30 stsp line[col++] = author[i];
167 881b2d3e 2018-04-30 stsp while (i < author_display_len) {
168 881b2d3e 2018-04-30 stsp if (col >= avail)
170 881b2d3e 2018-04-30 stsp line[col++] = ' ';
173 881b2d3e 2018-04-30 stsp if (col >= avail)
175 881b2d3e 2018-04-30 stsp line[col++] = ' ';
177 881b2d3e 2018-04-30 stsp while (col < avail && *logmsg)
178 881b2d3e 2018-04-30 stsp line[col++] = *logmsg++;
179 881b2d3e 2018-04-30 stsp while (col < avail)
180 881b2d3e 2018-04-30 stsp line[col++] = ' ';
182 881b2d3e 2018-04-30 stsp waddstr(tog_log_view.window, line);
183 80ddbec8 2018-04-29 stsp waddch(tog_log_view.window, '\n');
185 80ddbec8 2018-04-29 stsp free(logmsg0);
186 6d9fbc00 2018-04-29 stsp free(author0);
187 80ddbec8 2018-04-29 stsp free(line);
188 6d9fbc00 2018-04-29 stsp free(id_str);
189 80ddbec8 2018-04-29 stsp return err;
191 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
192 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
193 80ddbec8 2018-04-29 stsp struct got_object_id *id;
194 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
196 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
198 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
199 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
200 899d86c2 2018-05-10 stsp struct got_object_id *id)
202 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
204 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
205 80ddbec8 2018-04-29 stsp if (entry == NULL)
206 899d86c2 2018-05-10 stsp return NULL;
208 899d86c2 2018-05-10 stsp entry->id = id;
209 99db9666 2018-05-07 stsp entry->commit = commit;
210 899d86c2 2018-05-10 stsp return entry;
213 99db9666 2018-05-07 stsp static void
214 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
216 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
218 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
219 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
220 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
221 99db9666 2018-05-07 stsp free(entry->id);
222 99db9666 2018-05-07 stsp free(entry);
225 99db9666 2018-05-07 stsp static void
226 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
228 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
229 99db9666 2018-05-07 stsp pop_commit(commits);
233 c4972b91 2018-05-07 stsp static const struct got_error *
234 899d86c2 2018-05-10 stsp fetch_parent_commit(struct commit_queue_entry **pentry,
235 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, struct got_repository *repo)
237 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
238 c4972b91 2018-05-07 stsp struct got_object *obj = NULL;
239 899d86c2 2018-05-10 stsp struct got_commit_object *commit;
240 899d86c2 2018-05-10 stsp struct got_object_id *id;
241 c4972b91 2018-05-07 stsp struct got_parent_id *pid;
243 899d86c2 2018-05-10 stsp *pentry = NULL;
245 c4972b91 2018-05-07 stsp /* Follow the first parent (TODO: handle merge commits). */
246 c4972b91 2018-05-07 stsp pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
247 c4972b91 2018-05-07 stsp if (pid == NULL)
248 c4972b91 2018-05-07 stsp return NULL;
249 c4972b91 2018-05-07 stsp err = got_object_open(&obj, repo, pid->id);
251 c4972b91 2018-05-07 stsp return err;
252 c4972b91 2018-05-07 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
253 c4972b91 2018-05-07 stsp err = got_error(GOT_ERR_OBJ_TYPE);
254 c4972b91 2018-05-07 stsp got_object_close(obj);
255 c4972b91 2018-05-07 stsp return err;
258 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, obj);
259 c4972b91 2018-05-07 stsp got_object_close(obj);
261 c4972b91 2018-05-07 stsp return err;
263 899d86c2 2018-05-10 stsp id = got_object_id_dup(pid->id);
264 899d86c2 2018-05-10 stsp if (id == NULL) {
265 c4972b91 2018-05-07 stsp err = got_error_from_errno();
266 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
267 899d86c2 2018-05-10 stsp return err;;
270 899d86c2 2018-05-10 stsp *pentry = alloc_commit_queue_entry(commit, id);
271 899d86c2 2018-05-10 stsp if (*pentry == NULL) {
272 c4972b91 2018-05-07 stsp err = got_error_from_errno();
273 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
276 899d86c2 2018-05-10 stsp return err;;
279 899d86c2 2018-05-10 stsp static const struct got_error *
280 899d86c2 2018-05-10 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
282 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
283 899d86c2 2018-05-10 stsp struct got_reference *head_ref;
285 899d86c2 2018-05-10 stsp *head_id = NULL;
287 899d86c2 2018-05-10 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
289 899d86c2 2018-05-10 stsp return err;
291 899d86c2 2018-05-10 stsp err = got_ref_resolve(head_id, repo, head_ref);
292 899d86c2 2018-05-10 stsp got_ref_close(head_ref);
294 899d86c2 2018-05-10 stsp *head_id = NULL;
295 899d86c2 2018-05-10 stsp return err;
298 c4972b91 2018-05-07 stsp return NULL;
301 99db9666 2018-05-07 stsp static const struct got_error *
302 899d86c2 2018-05-10 stsp prepend_commits(int *ncommits, struct commit_queue *commits,
303 899d86c2 2018-05-10 stsp struct got_object_id *first_id, struct got_object_id *last_id,
304 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
306 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
307 899d86c2 2018-05-10 stsp struct got_object *first_obj = NULL, *last_obj = NULL;
308 899d86c2 2018-05-10 stsp struct got_commit_object *commit = NULL;
309 899d86c2 2018-05-10 stsp struct got_object_id *id = NULL;
310 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, *old_head_entry;
312 899d86c2 2018-05-10 stsp *ncommits = 0;
314 899d86c2 2018-05-10 stsp err = got_object_open(&first_obj, repo, first_id);
317 899d86c2 2018-05-10 stsp if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
318 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
321 899d86c2 2018-05-10 stsp err = got_object_open(&last_obj, repo, last_id);
324 899d86c2 2018-05-10 stsp if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
325 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
329 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, first_obj);
333 899d86c2 2018-05-10 stsp id = got_object_id_dup(first_id);
334 899d86c2 2018-05-10 stsp if (id == NULL) {
335 899d86c2 2018-05-10 stsp err = got_error_from_errno();
339 899d86c2 2018-05-10 stsp entry = alloc_commit_queue_entry(commit, id);
340 899d86c2 2018-05-10 stsp if (entry == NULL)
341 899d86c2 2018-05-10 stsp return got_error_from_errno();
343 899d86c2 2018-05-10 stsp old_head_entry = TAILQ_FIRST(commits);
344 899d86c2 2018-05-10 stsp if (old_head_entry)
345 899d86c2 2018-05-10 stsp TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
347 899d86c2 2018-05-10 stsp TAILQ_INSERT_HEAD(commits, entry, entry);
349 899d86c2 2018-05-10 stsp *ncommits = 1;
352 899d86c2 2018-05-10 stsp * Fetch parent commits.
353 899d86c2 2018-05-10 stsp * XXX If first and last commit aren't ancestrally related this loop
354 899d86c2 2018-05-10 stsp * we will keep iterating until a root commit is encountered.
356 899d86c2 2018-05-10 stsp while (1) {
357 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
359 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
362 899d86c2 2018-05-10 stsp if (pentry == NULL)
366 899d86c2 2018-05-10 stsp * Fill up to old HEAD commit if commit queue was not empty.
367 899d86c2 2018-05-10 stsp * We must not leave a gap in history.
369 899d86c2 2018-05-10 stsp if (old_head_entry &&
370 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
373 899d86c2 2018-05-10 stsp TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
374 899d86c2 2018-05-10 stsp (*ncommits)++;
375 899d86c2 2018-05-10 stsp if (*ncommits >= limit)
378 899d86c2 2018-05-10 stsp /* Fill up to last requested commit if queue was empty. */
379 899d86c2 2018-05-10 stsp if (old_head_entry == NULL &&
380 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, last_id) == 0)
383 899d86c2 2018-05-10 stsp entry = pentry;
387 899d86c2 2018-05-10 stsp if (first_obj)
388 899d86c2 2018-05-10 stsp got_object_close(first_obj);
389 899d86c2 2018-05-10 stsp if (last_obj)
390 899d86c2 2018-05-10 stsp got_object_close(last_obj);
391 899d86c2 2018-05-10 stsp return err;
394 899d86c2 2018-05-10 stsp static const struct got_error *
395 899d86c2 2018-05-10 stsp fetch_commits(struct commit_queue_entry **start_entry,
396 899d86c2 2018-05-10 stsp struct got_object_id *start_id, struct commit_queue *commits,
397 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
399 99db9666 2018-05-07 stsp const struct got_error *err;
400 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
401 99db9666 2018-05-07 stsp int ncommits = 0;
402 899d86c2 2018-05-10 stsp struct got_object_id *head_id = NULL;
404 899d86c2 2018-05-10 stsp *start_entry = NULL;
406 899d86c2 2018-05-10 stsp err = get_head_commit_id(&head_id, repo);
408 99db9666 2018-05-07 stsp return err;
410 899d86c2 2018-05-10 stsp /* Prepend HEAD commit and all ancestors up to start commit. */
411 899d86c2 2018-05-10 stsp err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
414 99db9666 2018-05-07 stsp return err;
416 899d86c2 2018-05-10 stsp if (got_object_id_cmp(head_id, start_id) == 0)
417 899d86c2 2018-05-10 stsp *start_entry = TAILQ_FIRST(commits);
419 899d86c2 2018-05-10 stsp *start_entry = TAILQ_LAST(commits, commit_queue);
421 899d86c2 2018-05-10 stsp if (ncommits >= limit)
422 899d86c2 2018-05-10 stsp return NULL;
424 899d86c2 2018-05-10 stsp /* Append more commits from start commit up to the requested limit. */
425 899d86c2 2018-05-10 stsp entry = TAILQ_LAST(commits, commit_queue);
426 899d86c2 2018-05-10 stsp while (entry && ncommits < limit) {
427 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
429 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
432 899d86c2 2018-05-10 stsp if (pentry)
433 899d86c2 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
434 899d86c2 2018-05-10 stsp entry = pentry;
435 0553a4e3 2018-04-30 stsp ncommits++;
439 899d86c2 2018-05-10 stsp *start_entry = NULL;
440 0553a4e3 2018-04-30 stsp return err;
443 0553a4e3 2018-04-30 stsp static const struct got_error *
444 4a7f7875 2018-05-10 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
445 4a7f7875 2018-05-10 stsp int selected, int limit)
447 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
448 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
449 0553a4e3 2018-04-30 stsp int ncommits = 0;
451 0553a4e3 2018-04-30 stsp wclear(tog_log_view.window);
453 899d86c2 2018-05-10 stsp entry = first;
454 899d86c2 2018-05-10 stsp *last = first;
455 899d86c2 2018-05-10 stsp while (entry) {
456 899d86c2 2018-05-10 stsp if (ncommits == limit)
458 0553a4e3 2018-04-30 stsp if (ncommits == selected)
459 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
460 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
461 0553a4e3 2018-04-30 stsp if (ncommits == selected)
462 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
465 0553a4e3 2018-04-30 stsp ncommits++;
466 899d86c2 2018-05-10 stsp *last = entry;
467 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
470 80ddbec8 2018-04-29 stsp update_panels();
471 80ddbec8 2018-04-29 stsp doupdate();
473 80ddbec8 2018-04-29 stsp return err;
476 07b55e75 2018-05-10 stsp static void
477 07b55e75 2018-05-10 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
478 07b55e75 2018-05-10 stsp struct commit_queue *commits)
480 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
481 07b55e75 2018-05-10 stsp int nscrolled = 0;
483 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
484 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
487 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
488 07b55e75 2018-05-10 stsp while (entry && nscrolled < n) {
489 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
490 07b55e75 2018-05-10 stsp if (entry) {
491 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
492 07b55e75 2018-05-10 stsp nscrolled++;
497 aa075928 2018-05-10 stsp static const struct got_error *
498 aa075928 2018-05-10 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
499 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
500 aa075928 2018-05-10 stsp struct commit_queue *commits, struct got_repository *repo)
502 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
503 aa075928 2018-05-10 stsp struct commit_queue_entry *entry;
504 aa075928 2018-05-10 stsp int nscrolled = 0;
506 aa075928 2018-05-10 stsp if (last_displayed_entry->commit->nparents == 0)
507 aa075928 2018-05-10 stsp return NULL;
509 aa075928 2018-05-10 stsp entry = *first_displayed_entry;
511 aa075928 2018-05-10 stsp struct commit_queue_entry *pentry;
513 aa075928 2018-05-10 stsp pentry = TAILQ_NEXT(entry, entry);
514 aa075928 2018-05-10 stsp if (pentry == NULL) {
515 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
518 aa075928 2018-05-10 stsp if (pentry == NULL) {
519 aa075928 2018-05-10 stsp *first_displayed_entry = entry;
520 aa075928 2018-05-10 stsp return NULL;
522 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
523 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
526 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
527 aa075928 2018-05-10 stsp entry = pentry;
529 aa075928 2018-05-10 stsp if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
530 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, last_displayed_entry,
534 aa075928 2018-05-10 stsp if (pentry) {
535 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
536 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
539 aa075928 2018-05-10 stsp } while (++nscrolled < n);
541 aa075928 2018-05-10 stsp return NULL;
545 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
547 4a7f7875 2018-05-10 stsp int nparents = 0;
549 4a7f7875 2018-05-10 stsp while (entry) {
550 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
551 4a7f7875 2018-05-10 stsp nparents++;
554 4a7f7875 2018-05-10 stsp return nparents;
557 80ddbec8 2018-04-29 stsp static const struct got_error *
558 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
560 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
561 c4972b91 2018-05-07 stsp struct got_object_id *id;
562 4a7f7875 2018-05-10 stsp int ch, done = 0, selected = 0, nparents;
563 0553a4e3 2018-04-30 stsp struct commit_queue commits;
564 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
565 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
567 c4972b91 2018-05-07 stsp id = got_object_id_dup(start_id);
568 c4972b91 2018-05-07 stsp if (id == NULL)
569 c4972b91 2018-05-07 stsp return got_error_from_errno();
571 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
572 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
573 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
574 80ddbec8 2018-04-29 stsp return got_error_from_errno();
575 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
577 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
578 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
579 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
580 80ddbec8 2018-04-29 stsp return got_error_from_errno();
583 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
584 899d86c2 2018-05-10 stsp err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
587 899d86c2 2018-05-10 stsp while (!done) {
588 899d86c2 2018-05-10 stsp err = draw_commits(&last_displayed_entry, first_displayed_entry,
589 899d86c2 2018-05-10 stsp selected, LINES);
593 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
594 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
595 80ddbec8 2018-04-29 stsp switch (ch) {
597 31120ada 2018-04-30 stsp if (errno) {
598 31120ada 2018-04-30 stsp err = got_error_from_errno();
606 80ddbec8 2018-04-29 stsp case KEY_UP:
607 80ddbec8 2018-04-29 stsp if (selected > 0)
608 80ddbec8 2018-04-29 stsp selected--;
609 8ec9698d 2018-05-10 stsp if (selected > 0)
611 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
613 48531068 2018-05-10 stsp case KEY_PPAGE:
614 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
615 dfc1d240 2018-05-10 stsp first_displayed_entry) {
616 dfc1d240 2018-05-10 stsp selected = 0;
619 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
623 80ddbec8 2018-04-29 stsp case KEY_DOWN:
624 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
625 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
626 06abe2cd 2018-05-10 stsp selected < nparents - 1)
627 80ddbec8 2018-04-29 stsp selected++;
628 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
629 06abe2cd 2018-05-10 stsp selected < nparents - 1)
631 aa075928 2018-05-10 stsp err = scroll_down(&first_displayed_entry, 1,
632 aa075928 2018-05-10 stsp last_displayed_entry, &commits, repo);
636 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
637 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
638 dfc1d240 2018-05-10 stsp if (nparents < LINES - 1 &&
639 dfc1d240 2018-05-10 stsp selected < nparents - 1) {
640 dfc1d240 2018-05-10 stsp selected = nparents - 1;
643 4a7f7875 2018-05-10 stsp err = scroll_down(&first_displayed_entry,
644 4a7f7875 2018-05-10 stsp MIN(nparents, LINES), last_displayed_entry,
645 4a7f7875 2018-05-10 stsp &commits, repo);
648 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
649 dfc1d240 2018-05-10 stsp if (selected > nparents)
650 dfc1d240 2018-05-10 stsp selected = nparents - 1;
652 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
653 31120ada 2018-04-30 stsp if (selected > LINES)
654 31120ada 2018-04-30 stsp selected = LINES - 1;
659 80ddbec8 2018-04-29 stsp nodelay(stdscr, TRUE);
662 0553a4e3 2018-04-30 stsp free_commits(&commits);
663 80ddbec8 2018-04-29 stsp return err;
666 9f7d7167 2018-04-29 stsp const struct got_error *
667 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
669 80ddbec8 2018-04-29 stsp const struct got_error *error;
670 80ddbec8 2018-04-29 stsp struct got_repository *repo;
671 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
672 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
673 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
676 80ddbec8 2018-04-29 stsp #ifndef PROFILE
677 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
678 80ddbec8 2018-04-29 stsp err(1, "pledge");
681 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
682 80ddbec8 2018-04-29 stsp switch (ch) {
684 80ddbec8 2018-04-29 stsp start_commit = optarg;
688 80ddbec8 2018-04-29 stsp /* NOTREACHED */
692 80ddbec8 2018-04-29 stsp argc -= optind;
693 80ddbec8 2018-04-29 stsp argv += optind;
695 80ddbec8 2018-04-29 stsp if (argc == 0) {
696 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
697 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
698 80ddbec8 2018-04-29 stsp return got_error_from_errno();
699 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
700 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
701 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
702 80ddbec8 2018-04-29 stsp return got_error_from_errno();
704 80ddbec8 2018-04-29 stsp usage_log();
706 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
707 80ddbec8 2018-04-29 stsp free(repo_path);
708 80ddbec8 2018-04-29 stsp if (error != NULL)
709 80ddbec8 2018-04-29 stsp return error;
711 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
712 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
713 80ddbec8 2018-04-29 stsp if (error != NULL)
714 80ddbec8 2018-04-29 stsp return error;
716 80ddbec8 2018-04-29 stsp struct got_object *obj;
717 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
718 80ddbec8 2018-04-29 stsp if (error == NULL) {
719 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
720 899d86c2 2018-05-10 stsp if (start_id == NULL)
721 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
724 80ddbec8 2018-04-29 stsp if (error != NULL)
725 80ddbec8 2018-04-29 stsp return error;
726 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
727 899d86c2 2018-05-10 stsp free(start_id);
728 80ddbec8 2018-04-29 stsp got_repo_close(repo);
729 80ddbec8 2018-04-29 stsp return error;
732 9f7d7167 2018-04-29 stsp __dead void
733 9f7d7167 2018-04-29 stsp usage_diff(void)
736 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
737 9f7d7167 2018-04-29 stsp getprogname());
741 9f7d7167 2018-04-29 stsp const struct got_error *
742 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
744 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
747 9f7d7167 2018-04-29 stsp __dead void
748 9f7d7167 2018-04-29 stsp usage_blame(void)
751 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
752 9f7d7167 2018-04-29 stsp getprogname());
756 9f7d7167 2018-04-29 stsp const struct got_error *
757 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
759 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
762 9f7d7167 2018-04-29 stsp static const struct got_error *
763 9f7d7167 2018-04-29 stsp init_curses(void)
769 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
770 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
771 1f475ad8 2018-05-10 stsp curs_set(0);
773 9f7d7167 2018-04-29 stsp tog_main_win = newwin(0, 0, 0, 0);
774 9f7d7167 2018-04-29 stsp if (tog_main_win == NULL)
775 9f7d7167 2018-04-29 stsp return got_error_from_errno();
776 9f7d7167 2018-04-29 stsp tog_main_panel = new_panel(tog_main_win);
777 9f7d7167 2018-04-29 stsp if (tog_main_panel == NULL)
778 9f7d7167 2018-04-29 stsp return got_error_from_errno();
780 9f7d7167 2018-04-29 stsp return NULL;
783 9f7d7167 2018-04-29 stsp __dead void
784 9f7d7167 2018-04-29 stsp usage(void)
788 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
789 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
790 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
791 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
792 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
797 c2301be8 2018-04-30 stsp static char **
798 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
800 c2301be8 2018-04-30 stsp char **argv;
801 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
803 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
804 c2301be8 2018-04-30 stsp if (argv == NULL)
805 c2301be8 2018-04-30 stsp err(1, "calloc");
806 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
807 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
808 c2301be8 2018-04-30 stsp err(1, "calloc");
809 c2301be8 2018-04-30 stsp if (arg1) {
810 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
811 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
812 c2301be8 2018-04-30 stsp err(1, "calloc");
815 c2301be8 2018-04-30 stsp return argv;
819 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
821 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
822 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
823 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
824 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
826 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
828 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
829 9f7d7167 2018-04-29 stsp switch (ch) {
835 9f7d7167 2018-04-29 stsp /* NOTREACHED */
839 9f7d7167 2018-04-29 stsp argc -= optind;
840 9f7d7167 2018-04-29 stsp argv += optind;
841 9f7d7167 2018-04-29 stsp optind = 0;
842 c2301be8 2018-04-30 stsp optreset = 1;
844 c2301be8 2018-04-30 stsp if (argc == 0) {
845 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
846 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
847 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
852 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
853 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
854 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
855 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
856 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
858 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
862 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
863 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
864 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
865 c2301be8 2018-04-30 stsp if (repo_path) {
866 c2301be8 2018-04-30 stsp struct got_repository *repo;
867 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
868 c2301be8 2018-04-30 stsp if (error == NULL)
869 c2301be8 2018-04-30 stsp got_repo_close(repo);
871 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
872 c2301be8 2018-04-30 stsp if (error) {
873 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
874 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
875 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
876 ad7de8d9 2018-04-30 stsp free(repo_path);
879 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
880 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
882 c2301be8 2018-04-30 stsp free(repo_path);
886 9f7d7167 2018-04-29 stsp error = init_curses();
887 9f7d7167 2018-04-29 stsp if (error) {
888 80ddbec8 2018-04-29 stsp fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
892 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
897 c2301be8 2018-04-30 stsp free(cmd_argv);
899 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);