Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
59 static void
60 catch_sigint(int signo)
61 {
62 sigint_received = 1;
63 }
65 static void
66 catch_sigpipe(int signo)
67 {
68 sigpipe_received = 1;
69 }
72 struct got_cmd {
73 const char *cmd_name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
77 };
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_integrate(void);
101 __dead static void usage_stage(void);
102 __dead static void usage_unstage(void);
103 __dead static void usage_cat(void);
105 static const struct got_error* cmd_init(int, char *[]);
106 static const struct got_error* cmd_import(int, char *[]);
107 static const struct got_error* cmd_checkout(int, char *[]);
108 static const struct got_error* cmd_update(int, char *[]);
109 static const struct got_error* cmd_log(int, char *[]);
110 static const struct got_error* cmd_diff(int, char *[]);
111 static const struct got_error* cmd_blame(int, char *[]);
112 static const struct got_error* cmd_tree(int, char *[]);
113 static const struct got_error* cmd_status(int, char *[]);
114 static const struct got_error* cmd_ref(int, char *[]);
115 static const struct got_error* cmd_branch(int, char *[]);
116 static const struct got_error* cmd_tag(int, char *[]);
117 static const struct got_error* cmd_add(int, char *[]);
118 static const struct got_error* cmd_remove(int, char *[]);
119 static const struct got_error* cmd_revert(int, char *[]);
120 static const struct got_error* cmd_commit(int, char *[]);
121 static const struct got_error* cmd_cherrypick(int, char *[]);
122 static const struct got_error* cmd_backout(int, char *[]);
123 static const struct got_error* cmd_rebase(int, char *[]);
124 static const struct got_error* cmd_histedit(int, char *[]);
125 static const struct got_error* cmd_integrate(int, char *[]);
126 static const struct got_error* cmd_stage(int, char *[]);
127 static const struct got_error* cmd_unstage(int, char *[]);
128 static const struct got_error* cmd_cat(int, char *[]);
130 static struct got_cmd got_commands[] = {
131 { "init", cmd_init, usage_init, "in" },
132 { "import", cmd_import, usage_import, "im" },
133 { "checkout", cmd_checkout, usage_checkout, "co" },
134 { "update", cmd_update, usage_update, "up" },
135 { "log", cmd_log, usage_log, "" },
136 { "diff", cmd_diff, usage_diff, "di" },
137 { "blame", cmd_blame, usage_blame, "bl" },
138 { "tree", cmd_tree, usage_tree, "tr" },
139 { "status", cmd_status, usage_status, "st" },
140 { "ref", cmd_ref, usage_ref, "" },
141 { "branch", cmd_branch, usage_branch, "br" },
142 { "tag", cmd_tag, usage_tag, "" },
143 { "add", cmd_add, usage_add, "" },
144 { "remove", cmd_remove, usage_remove, "rm" },
145 { "revert", cmd_revert, usage_revert, "rv" },
146 { "commit", cmd_commit, usage_commit, "ci" },
147 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 { "backout", cmd_backout, usage_backout, "bo" },
149 { "rebase", cmd_rebase, usage_rebase, "rb" },
150 { "histedit", cmd_histedit, usage_histedit, "he" },
151 { "integrate", cmd_integrate, usage_integrate,"ig" },
152 { "stage", cmd_stage, usage_stage, "sg" },
153 { "unstage", cmd_unstage, usage_unstage, "ug" },
154 { "cat", cmd_cat, usage_cat, "" },
155 };
157 static void
158 list_commands(void)
160 int i;
162 fprintf(stderr, "commands:");
163 for (i = 0; i < nitems(got_commands); i++) {
164 struct got_cmd *cmd = &got_commands[i];
165 fprintf(stderr, " %s", cmd->cmd_name);
167 fputc('\n', stderr);
170 int
171 main(int argc, char *argv[])
173 struct got_cmd *cmd;
174 unsigned int i;
175 int ch;
176 int hflag = 0, Vflag = 0;
178 setlocale(LC_CTYPE, "");
180 while ((ch = getopt(argc, argv, "hV")) != -1) {
181 switch (ch) {
182 case 'h':
183 hflag = 1;
184 break;
185 case 'V':
186 Vflag = 1;
187 break;
188 default:
189 usage(hflag);
190 /* NOTREACHED */
194 argc -= optind;
195 argv += optind;
196 optind = 0;
198 if (Vflag) {
199 got_version_print_str();
200 return 1;
203 if (argc <= 0)
204 usage(hflag);
206 signal(SIGINT, catch_sigint);
207 signal(SIGPIPE, catch_sigpipe);
209 for (i = 0; i < nitems(got_commands); i++) {
210 const struct got_error *error;
212 cmd = &got_commands[i];
214 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 strcmp(cmd->cmd_alias, argv[0]) != 0)
216 continue;
218 if (hflag)
219 got_commands[i].cmd_usage();
221 error = got_commands[i].cmd_main(argc, argv);
222 if (error && !(sigint_received || sigpipe_received)) {
223 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
224 return 1;
227 return 0;
230 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
231 list_commands();
232 return 1;
235 __dead static void
236 usage(int hflag)
238 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
239 getprogname());
240 if (hflag)
241 list_commands();
242 exit(1);
245 static const struct got_error *
246 get_editor(char **abspath)
248 const struct got_error *err = NULL;
249 const char *editor;
251 *abspath = NULL;
253 editor = getenv("VISUAL");
254 if (editor == NULL)
255 editor = getenv("EDITOR");
257 if (editor) {
258 err = got_path_find_prog(abspath, editor);
259 if (err)
260 return err;
263 if (*abspath == NULL) {
264 *abspath = strdup("/bin/ed");
265 if (*abspath == NULL)
266 return got_error_from_errno("strdup");
269 return NULL;
272 static const struct got_error *
273 apply_unveil(const char *repo_path, int repo_read_only,
274 const char *worktree_path)
276 const struct got_error *err;
278 #ifdef PROFILE
279 if (unveil("gmon.out", "rwc") != 0)
280 return got_error_from_errno2("unveil", "gmon.out");
281 #endif
282 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
283 return got_error_from_errno2("unveil", repo_path);
285 if (worktree_path && unveil(worktree_path, "rwc") != 0)
286 return got_error_from_errno2("unveil", worktree_path);
288 if (unveil("/tmp", "rwc") != 0)
289 return got_error_from_errno2("unveil", "/tmp");
291 err = got_privsep_unveil_exec_helpers();
292 if (err != NULL)
293 return err;
295 if (unveil(NULL, NULL) != 0)
296 return got_error_from_errno("unveil");
298 return NULL;
301 __dead static void
302 usage_init(void)
304 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
305 exit(1);
308 static const struct got_error *
309 cmd_init(int argc, char *argv[])
311 const struct got_error *error = NULL;
312 char *repo_path = NULL;
313 int ch;
315 while ((ch = getopt(argc, argv, "")) != -1) {
316 switch (ch) {
317 default:
318 usage_init();
319 /* NOTREACHED */
323 argc -= optind;
324 argv += optind;
326 #ifndef PROFILE
327 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
328 err(1, "pledge");
329 #endif
330 if (argc != 1)
331 usage_init();
333 repo_path = strdup(argv[0]);
334 if (repo_path == NULL)
335 return got_error_from_errno("strdup");
337 got_path_strip_trailing_slashes(repo_path);
339 error = got_path_mkdir(repo_path);
340 if (error &&
341 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
342 goto done;
344 error = apply_unveil(repo_path, 0, NULL);
345 if (error)
346 goto done;
348 error = got_repo_init(repo_path);
349 if (error != NULL)
350 goto done;
352 done:
353 free(repo_path);
354 return error;
357 __dead static void
358 usage_import(void)
360 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
361 "[-r repository-path] [-I pattern] path\n", getprogname());
362 exit(1);
365 int
366 spawn_editor(const char *editor, const char *file)
368 pid_t pid;
369 sig_t sighup, sigint, sigquit;
370 int st = -1;
372 sighup = signal(SIGHUP, SIG_IGN);
373 sigint = signal(SIGINT, SIG_IGN);
374 sigquit = signal(SIGQUIT, SIG_IGN);
376 switch (pid = fork()) {
377 case -1:
378 goto doneediting;
379 case 0:
380 execl(editor, editor, file, (char *)NULL);
381 _exit(127);
384 while (waitpid(pid, &st, 0) == -1)
385 if (errno != EINTR)
386 break;
388 doneediting:
389 (void)signal(SIGHUP, sighup);
390 (void)signal(SIGINT, sigint);
391 (void)signal(SIGQUIT, sigquit);
393 if (!WIFEXITED(st)) {
394 errno = EINTR;
395 return -1;
398 return WEXITSTATUS(st);
401 static const struct got_error *
402 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
403 const char *initial_content)
405 const struct got_error *err = NULL;
406 char buf[1024];
407 struct stat st, st2;
408 FILE *fp;
409 int content_changed = 0;
410 size_t len;
412 *logmsg = NULL;
414 if (stat(logmsg_path, &st) == -1)
415 return got_error_from_errno2("stat", logmsg_path);
417 if (spawn_editor(editor, logmsg_path) == -1)
418 return got_error_from_errno("failed spawning editor");
420 if (stat(logmsg_path, &st2) == -1)
421 return got_error_from_errno("stat");
423 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
424 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
425 "no changes made to commit message, aborting");
427 *logmsg = malloc(st2.st_size + 1);
428 if (*logmsg == NULL)
429 return got_error_from_errno("malloc");
430 (*logmsg)[0] = '\0';
431 len = 0;
433 fp = fopen(logmsg_path, "r");
434 if (fp == NULL) {
435 err = got_error_from_errno("fopen");
436 goto done;
438 while (fgets(buf, sizeof(buf), fp) != NULL) {
439 if (!content_changed && strcmp(buf, initial_content) != 0)
440 content_changed = 1;
441 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
442 continue; /* remove comments and leading empty lines */
443 len = strlcat(*logmsg, buf, st2.st_size);
445 fclose(fp);
447 while (len > 0 && (*logmsg)[len - 1] == '\n') {
448 (*logmsg)[len - 1] = '\0';
449 len--;
452 if (len == 0 || !content_changed)
453 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
454 "commit message cannot be empty, aborting");
455 done:
456 if (err) {
457 free(*logmsg);
458 *logmsg = NULL;
460 return err;
463 static const struct got_error *
464 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
465 const char *branch_name)
467 char *initial_content = NULL, *logmsg_path = NULL;
468 const struct got_error *err = NULL;
469 int fd;
471 if (asprintf(&initial_content,
472 "\n# %s to be imported to branch %s\n", path_dir,
473 branch_name) == -1)
474 return got_error_from_errno("asprintf");
476 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
477 if (err)
478 goto done;
480 dprintf(fd, initial_content);
481 close(fd);
483 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
484 done:
485 free(initial_content);
486 free(logmsg_path);
487 return err;
490 static const struct got_error *
491 import_progress(void *arg, const char *path)
493 printf("A %s\n", path);
494 return NULL;
497 static const struct got_error *
498 get_author(char **author, struct got_repository *repo)
500 const struct got_error *err = NULL;
501 const char *got_author, *name, *email;
503 *author = NULL;
505 name = got_repo_get_gitconfig_author_name(repo);
506 email = got_repo_get_gitconfig_author_email(repo);
507 if (name && email) {
508 if (asprintf(author, "%s <%s>", name, email) == -1)
509 return got_error_from_errno("asprintf");
510 return NULL;
513 got_author = getenv("GOT_AUTHOR");
514 if (got_author == NULL) {
515 name = got_repo_get_global_gitconfig_author_name(repo);
516 email = got_repo_get_global_gitconfig_author_email(repo);
517 if (name && email) {
518 if (asprintf(author, "%s <%s>", name, email) == -1)
519 return got_error_from_errno("asprintf");
520 return NULL;
522 /* TODO: Look up user in password database? */
523 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
526 *author = strdup(got_author);
527 if (*author == NULL)
528 return got_error_from_errno("strdup");
530 /*
531 * Really dumb email address check; we're only doing this to
532 * avoid git's object parser breaking on commits we create.
533 */
534 while (*got_author && *got_author != '<')
535 got_author++;
536 if (*got_author != '<') {
537 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
538 goto done;
540 while (*got_author && *got_author != '@')
541 got_author++;
542 if (*got_author != '@') {
543 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
544 goto done;
546 while (*got_author && *got_author != '>')
547 got_author++;
548 if (*got_author != '>')
549 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
550 done:
551 if (err) {
552 free(*author);
553 *author = NULL;
555 return err;
558 static const struct got_error *
559 get_gitconfig_path(char **gitconfig_path)
561 const char *homedir = getenv("HOME");
563 *gitconfig_path = NULL;
564 if (homedir) {
565 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
566 return got_error_from_errno("asprintf");
569 return NULL;
572 static const struct got_error *
573 cmd_import(int argc, char *argv[])
575 const struct got_error *error = NULL;
576 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
577 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
578 const char *branch_name = "master";
579 char *refname = NULL, *id_str = NULL;
580 struct got_repository *repo = NULL;
581 struct got_reference *branch_ref = NULL, *head_ref = NULL;
582 struct got_object_id *new_commit_id = NULL;
583 int ch;
584 struct got_pathlist_head ignores;
585 struct got_pathlist_entry *pe;
587 TAILQ_INIT(&ignores);
589 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
590 switch (ch) {
591 case 'b':
592 branch_name = optarg;
593 break;
594 case 'm':
595 logmsg = strdup(optarg);
596 if (logmsg == NULL) {
597 error = got_error_from_errno("strdup");
598 goto done;
600 break;
601 case 'r':
602 repo_path = realpath(optarg, NULL);
603 if (repo_path == NULL) {
604 error = got_error_from_errno("realpath");
605 goto done;
607 break;
608 case 'I':
609 if (optarg[0] == '\0')
610 break;
611 error = got_pathlist_insert(&pe, &ignores, optarg,
612 NULL);
613 if (error)
614 goto done;
615 break;
616 default:
617 usage_import();
618 /* NOTREACHED */
622 argc -= optind;
623 argv += optind;
625 #ifndef PROFILE
626 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
627 "unveil",
628 NULL) == -1)
629 err(1, "pledge");
630 #endif
631 if (argc != 1)
632 usage_import();
634 if (repo_path == NULL) {
635 repo_path = getcwd(NULL, 0);
636 if (repo_path == NULL)
637 return got_error_from_errno("getcwd");
639 got_path_strip_trailing_slashes(repo_path);
640 error = get_gitconfig_path(&gitconfig_path);
641 if (error)
642 goto done;
643 error = got_repo_open(&repo, repo_path, gitconfig_path);
644 if (error)
645 goto done;
647 error = get_author(&author, repo);
648 if (error)
649 return error;
651 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
652 error = got_error_from_errno("asprintf");
653 goto done;
656 error = got_ref_open(&branch_ref, repo, refname, 0);
657 if (error) {
658 if (error->code != GOT_ERR_NOT_REF)
659 goto done;
660 } else {
661 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
662 "import target branch already exists");
663 goto done;
666 path_dir = realpath(argv[0], NULL);
667 if (path_dir == NULL) {
668 error = got_error_from_errno("realpath");
669 goto done;
671 got_path_strip_trailing_slashes(path_dir);
673 /*
674 * unveil(2) traverses exec(2); if an editor is used we have
675 * to apply unveil after the log message has been written.
676 */
677 if (logmsg == NULL || strlen(logmsg) == 0) {
678 error = get_editor(&editor);
679 if (error)
680 goto done;
681 free(logmsg);
682 error = collect_import_msg(&logmsg, editor, path_dir, refname);
683 if (error)
684 goto done;
687 if (unveil(path_dir, "r") != 0)
688 return got_error_from_errno2("unveil", path_dir);
690 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
691 if (error)
692 goto done;
694 error = got_repo_import(&new_commit_id, path_dir, logmsg,
695 author, &ignores, repo, import_progress, NULL);
696 if (error)
697 goto done;
699 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
700 if (error)
701 goto done;
703 error = got_ref_write(branch_ref, repo);
704 if (error)
705 goto done;
707 error = got_object_id_str(&id_str, new_commit_id);
708 if (error)
709 goto done;
711 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
712 if (error) {
713 if (error->code != GOT_ERR_NOT_REF)
714 goto done;
716 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
717 branch_ref);
718 if (error)
719 goto done;
721 error = got_ref_write(head_ref, repo);
722 if (error)
723 goto done;
726 printf("Created branch %s with commit %s\n",
727 got_ref_get_name(branch_ref), id_str);
728 done:
729 free(logmsg);
730 free(repo_path);
731 free(editor);
732 free(refname);
733 free(new_commit_id);
734 free(id_str);
735 free(author);
736 free(gitconfig_path);
737 if (branch_ref)
738 got_ref_close(branch_ref);
739 if (head_ref)
740 got_ref_close(head_ref);
741 return error;
744 __dead static void
745 usage_checkout(void)
747 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
748 "[-p prefix] repository-path [worktree-path]\n", getprogname());
749 exit(1);
752 static const struct got_error *
753 checkout_progress(void *arg, unsigned char status, const char *path)
755 char *worktree_path = arg;
757 /* Base commit bump happens silently. */
758 if (status == GOT_STATUS_BUMP_BASE)
759 return NULL;
761 while (path[0] == '/')
762 path++;
764 printf("%c %s/%s\n", status, worktree_path, path);
765 return NULL;
768 static const struct got_error *
769 check_cancelled(void *arg)
771 if (sigint_received || sigpipe_received)
772 return got_error(GOT_ERR_CANCELLED);
773 return NULL;
776 static const struct got_error *
777 check_linear_ancestry(struct got_object_id *commit_id,
778 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
779 struct got_repository *repo)
781 const struct got_error *err = NULL;
782 struct got_object_id *yca_id;
784 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
785 commit_id, base_commit_id, repo, check_cancelled, NULL);
786 if (err)
787 return err;
789 if (yca_id == NULL)
790 return got_error(GOT_ERR_ANCESTRY);
792 /*
793 * Require a straight line of history between the target commit
794 * and the work tree's base commit.
796 * Non-linear situations such as this require a rebase:
798 * (commit) D F (base_commit)
799 * \ /
800 * C E
801 * \ /
802 * B (yca)
803 * |
804 * A
806 * 'got update' only handles linear cases:
807 * Update forwards in time: A (base/yca) - B - C - D (commit)
808 * Update backwards in time: D (base) - C - B - A (commit/yca)
809 */
810 if (allow_forwards_in_time_only) {
811 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
812 return got_error(GOT_ERR_ANCESTRY);
813 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
814 got_object_id_cmp(base_commit_id, yca_id) != 0)
815 return got_error(GOT_ERR_ANCESTRY);
817 free(yca_id);
818 return NULL;
821 static const struct got_error *
822 check_same_branch(struct got_object_id *commit_id,
823 struct got_reference *head_ref, struct got_object_id *yca_id,
824 struct got_repository *repo)
826 const struct got_error *err = NULL;
827 struct got_commit_graph *graph = NULL;
828 struct got_object_id *head_commit_id = NULL;
829 int is_same_branch = 0;
831 err = got_ref_resolve(&head_commit_id, repo, head_ref);
832 if (err)
833 goto done;
835 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
836 is_same_branch = 1;
837 goto done;
839 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
840 is_same_branch = 1;
841 goto done;
844 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
845 if (err)
846 goto done;
848 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
849 check_cancelled, NULL);
850 if (err)
851 goto done;
853 for (;;) {
854 struct got_object_id *id;
855 err = got_commit_graph_iter_next(&id, graph);
856 if (err) {
857 if (err->code == GOT_ERR_ITER_COMPLETED) {
858 err = NULL;
859 break;
860 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
861 break;
862 err = got_commit_graph_fetch_commits(graph, 1,
863 repo, check_cancelled, NULL);
864 if (err)
865 break;
868 if (id) {
869 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
870 break;
871 if (got_object_id_cmp(id, commit_id) == 0) {
872 is_same_branch = 1;
873 break;
877 done:
878 if (graph)
879 got_commit_graph_close(graph);
880 free(head_commit_id);
881 if (!err && !is_same_branch)
882 err = got_error(GOT_ERR_ANCESTRY);
883 return err;
886 static const struct got_error *
887 resolve_commit_arg(struct got_object_id **commit_id,
888 const char *commit_id_arg, struct got_repository *repo)
890 const struct got_error *err;
891 struct got_reference *ref;
892 struct got_tag_object *tag;
894 err = got_repo_object_match_tag(&tag, commit_id_arg,
895 GOT_OBJ_TYPE_COMMIT, repo);
896 if (err == NULL) {
897 *commit_id = got_object_id_dup(
898 got_object_tag_get_object_id(tag));
899 if (*commit_id == NULL)
900 err = got_error_from_errno("got_object_id_dup");
901 got_object_tag_close(tag);
902 return err;
903 } else if (err->code != GOT_ERR_NO_OBJ)
904 return err;
906 err = got_ref_open(&ref, repo, commit_id_arg, 0);
907 if (err == NULL) {
908 err = got_ref_resolve(commit_id, repo, ref);
909 got_ref_close(ref);
910 } else {
911 if (err->code != GOT_ERR_NOT_REF)
912 return err;
913 err = got_repo_match_object_id_prefix(commit_id,
914 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
916 return err;
919 static const struct got_error *
920 cmd_checkout(int argc, char *argv[])
922 const struct got_error *error = NULL;
923 struct got_repository *repo = NULL;
924 struct got_reference *head_ref = NULL;
925 struct got_worktree *worktree = NULL;
926 char *repo_path = NULL;
927 char *worktree_path = NULL;
928 const char *path_prefix = "";
929 const char *branch_name = GOT_REF_HEAD;
930 char *commit_id_str = NULL;
931 int ch, same_path_prefix;
932 struct got_pathlist_head paths;
934 TAILQ_INIT(&paths);
936 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
937 switch (ch) {
938 case 'b':
939 branch_name = optarg;
940 break;
941 case 'c':
942 commit_id_str = strdup(optarg);
943 if (commit_id_str == NULL)
944 return got_error_from_errno("strdup");
945 break;
946 case 'p':
947 path_prefix = optarg;
948 break;
949 default:
950 usage_checkout();
951 /* NOTREACHED */
955 argc -= optind;
956 argv += optind;
958 #ifndef PROFILE
959 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
960 "unveil", NULL) == -1)
961 err(1, "pledge");
962 #endif
963 if (argc == 1) {
964 char *cwd, *base, *dotgit;
965 repo_path = realpath(argv[0], NULL);
966 if (repo_path == NULL)
967 return got_error_from_errno2("realpath", argv[0]);
968 cwd = getcwd(NULL, 0);
969 if (cwd == NULL) {
970 error = got_error_from_errno("getcwd");
971 goto done;
973 if (path_prefix[0]) {
974 base = basename(path_prefix);
975 if (base == NULL) {
976 error = got_error_from_errno2("basename",
977 path_prefix);
978 goto done;
980 } else {
981 base = basename(repo_path);
982 if (base == NULL) {
983 error = got_error_from_errno2("basename",
984 repo_path);
985 goto done;
988 dotgit = strstr(base, ".git");
989 if (dotgit)
990 *dotgit = '\0';
991 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
992 error = got_error_from_errno("asprintf");
993 free(cwd);
994 goto done;
996 free(cwd);
997 } else if (argc == 2) {
998 repo_path = realpath(argv[0], NULL);
999 if (repo_path == NULL) {
1000 error = got_error_from_errno2("realpath", argv[0]);
1001 goto done;
1003 worktree_path = realpath(argv[1], NULL);
1004 if (worktree_path == NULL) {
1005 if (errno != ENOENT) {
1006 error = got_error_from_errno2("realpath",
1007 argv[1]);
1008 goto done;
1010 worktree_path = strdup(argv[1]);
1011 if (worktree_path == NULL) {
1012 error = got_error_from_errno("strdup");
1013 goto done;
1016 } else
1017 usage_checkout();
1019 got_path_strip_trailing_slashes(repo_path);
1020 got_path_strip_trailing_slashes(worktree_path);
1022 error = got_repo_open(&repo, repo_path, NULL);
1023 if (error != NULL)
1024 goto done;
1026 /* Pre-create work tree path for unveil(2) */
1027 error = got_path_mkdir(worktree_path);
1028 if (error) {
1029 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1030 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1031 goto done;
1032 if (!got_path_dir_is_empty(worktree_path)) {
1033 error = got_error_path(worktree_path,
1034 GOT_ERR_DIR_NOT_EMPTY);
1035 goto done;
1039 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1040 if (error)
1041 goto done;
1043 error = got_ref_open(&head_ref, repo, branch_name, 0);
1044 if (error != NULL)
1045 goto done;
1047 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1048 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1049 goto done;
1051 error = got_worktree_open(&worktree, worktree_path);
1052 if (error != NULL)
1053 goto done;
1055 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1056 path_prefix);
1057 if (error != NULL)
1058 goto done;
1059 if (!same_path_prefix) {
1060 error = got_error(GOT_ERR_PATH_PREFIX);
1061 goto done;
1064 if (commit_id_str) {
1065 struct got_object_id *commit_id;
1066 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1067 if (error)
1068 goto done;
1069 error = check_linear_ancestry(commit_id,
1070 got_worktree_get_base_commit_id(worktree), 0, repo);
1071 if (error != NULL) {
1072 free(commit_id);
1073 goto done;
1075 error = check_same_branch(commit_id, head_ref, NULL, repo);
1076 if (error)
1077 goto done;
1078 error = got_worktree_set_base_commit_id(worktree, repo,
1079 commit_id);
1080 free(commit_id);
1081 if (error)
1082 goto done;
1085 error = got_pathlist_append(&paths, "", NULL);
1086 if (error)
1087 goto done;
1088 error = got_worktree_checkout_files(worktree, &paths, repo,
1089 checkout_progress, worktree_path, check_cancelled, NULL);
1090 if (error != NULL)
1091 goto done;
1093 printf("Now shut up and hack\n");
1095 done:
1096 got_pathlist_free(&paths);
1097 free(commit_id_str);
1098 free(repo_path);
1099 free(worktree_path);
1100 return error;
1103 __dead static void
1104 usage_update(void)
1106 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1107 getprogname());
1108 exit(1);
1111 static const struct got_error *
1112 update_progress(void *arg, unsigned char status, const char *path)
1114 int *did_something = arg;
1116 if (status == GOT_STATUS_EXISTS)
1117 return NULL;
1119 *did_something = 1;
1121 /* Base commit bump happens silently. */
1122 if (status == GOT_STATUS_BUMP_BASE)
1123 return NULL;
1125 while (path[0] == '/')
1126 path++;
1127 printf("%c %s\n", status, path);
1128 return NULL;
1131 static const struct got_error *
1132 switch_head_ref(struct got_reference *head_ref,
1133 struct got_object_id *commit_id, struct got_worktree *worktree,
1134 struct got_repository *repo)
1136 const struct got_error *err = NULL;
1137 char *base_id_str;
1138 int ref_has_moved = 0;
1140 /* Trivial case: switching between two different references. */
1141 if (strcmp(got_ref_get_name(head_ref),
1142 got_worktree_get_head_ref_name(worktree)) != 0) {
1143 printf("Switching work tree from %s to %s\n",
1144 got_worktree_get_head_ref_name(worktree),
1145 got_ref_get_name(head_ref));
1146 return got_worktree_set_head_ref(worktree, head_ref);
1149 err = check_linear_ancestry(commit_id,
1150 got_worktree_get_base_commit_id(worktree), 0, repo);
1151 if (err) {
1152 if (err->code != GOT_ERR_ANCESTRY)
1153 return err;
1154 ref_has_moved = 1;
1156 if (!ref_has_moved)
1157 return NULL;
1159 /* Switching to a rebased branch with the same reference name. */
1160 err = got_object_id_str(&base_id_str,
1161 got_worktree_get_base_commit_id(worktree));
1162 if (err)
1163 return err;
1164 printf("Reference %s now points at a different branch\n",
1165 got_worktree_get_head_ref_name(worktree));
1166 printf("Switching work tree from %s to %s\n", base_id_str,
1167 got_worktree_get_head_ref_name(worktree));
1168 return NULL;
1171 static const struct got_error *
1172 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1174 const struct got_error *err;
1175 int in_progress;
1177 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1178 if (err)
1179 return err;
1180 if (in_progress)
1181 return got_error(GOT_ERR_REBASING);
1183 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1184 if (err)
1185 return err;
1186 if (in_progress)
1187 return got_error(GOT_ERR_HISTEDIT_BUSY);
1189 return NULL;
1192 static const struct got_error *
1193 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1194 char *argv[], struct got_worktree *worktree)
1196 const struct got_error *err = NULL;
1197 char *path;
1198 int i;
1200 if (argc == 0) {
1201 path = strdup("");
1202 if (path == NULL)
1203 return got_error_from_errno("strdup");
1204 return got_pathlist_append(paths, path, NULL);
1207 for (i = 0; i < argc; i++) {
1208 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1209 if (err)
1210 break;
1211 err = got_pathlist_append(paths, path, NULL);
1212 if (err) {
1213 free(path);
1214 break;
1218 return err;
1221 static const struct got_error *
1222 cmd_update(int argc, char *argv[])
1224 const struct got_error *error = NULL;
1225 struct got_repository *repo = NULL;
1226 struct got_worktree *worktree = NULL;
1227 char *worktree_path = NULL;
1228 struct got_object_id *commit_id = NULL;
1229 char *commit_id_str = NULL;
1230 const char *branch_name = NULL;
1231 struct got_reference *head_ref = NULL;
1232 struct got_pathlist_head paths;
1233 struct got_pathlist_entry *pe;
1234 int ch, did_something = 0;
1236 TAILQ_INIT(&paths);
1238 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1239 switch (ch) {
1240 case 'b':
1241 branch_name = optarg;
1242 break;
1243 case 'c':
1244 commit_id_str = strdup(optarg);
1245 if (commit_id_str == NULL)
1246 return got_error_from_errno("strdup");
1247 break;
1248 default:
1249 usage_update();
1250 /* NOTREACHED */
1254 argc -= optind;
1255 argv += optind;
1257 #ifndef PROFILE
1258 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1259 "unveil", NULL) == -1)
1260 err(1, "pledge");
1261 #endif
1262 worktree_path = getcwd(NULL, 0);
1263 if (worktree_path == NULL) {
1264 error = got_error_from_errno("getcwd");
1265 goto done;
1267 error = got_worktree_open(&worktree, worktree_path);
1268 if (error)
1269 goto done;
1271 error = check_rebase_or_histedit_in_progress(worktree);
1272 if (error)
1273 goto done;
1275 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1276 NULL);
1277 if (error != NULL)
1278 goto done;
1280 error = apply_unveil(got_repo_get_path(repo), 0,
1281 got_worktree_get_root_path(worktree));
1282 if (error)
1283 goto done;
1285 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1286 if (error)
1287 goto done;
1289 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1290 got_worktree_get_head_ref_name(worktree), 0);
1291 if (error != NULL)
1292 goto done;
1293 if (commit_id_str == NULL) {
1294 error = got_ref_resolve(&commit_id, repo, head_ref);
1295 if (error != NULL)
1296 goto done;
1297 error = got_object_id_str(&commit_id_str, commit_id);
1298 if (error != NULL)
1299 goto done;
1300 } else {
1301 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1302 free(commit_id_str);
1303 commit_id_str = NULL;
1304 if (error)
1305 goto done;
1306 error = got_object_id_str(&commit_id_str, commit_id);
1307 if (error)
1308 goto done;
1311 if (branch_name) {
1312 struct got_object_id *head_commit_id;
1313 TAILQ_FOREACH(pe, &paths, entry) {
1314 if (pe->path_len == 0)
1315 continue;
1316 error = got_error_msg(GOT_ERR_BAD_PATH,
1317 "switching between branches requires that "
1318 "the entire work tree gets updated");
1319 goto done;
1321 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1322 if (error)
1323 goto done;
1324 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1325 repo);
1326 free(head_commit_id);
1327 if (error != NULL)
1328 goto done;
1329 error = check_same_branch(commit_id, head_ref, NULL, repo);
1330 if (error)
1331 goto done;
1332 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1333 if (error)
1334 goto done;
1335 } else {
1336 error = check_linear_ancestry(commit_id,
1337 got_worktree_get_base_commit_id(worktree), 0, repo);
1338 if (error != NULL) {
1339 if (error->code == GOT_ERR_ANCESTRY)
1340 error = got_error(GOT_ERR_BRANCH_MOVED);
1341 goto done;
1343 error = check_same_branch(commit_id, head_ref, NULL, repo);
1344 if (error)
1345 goto done;
1348 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1349 commit_id) != 0) {
1350 error = got_worktree_set_base_commit_id(worktree, repo,
1351 commit_id);
1352 if (error)
1353 goto done;
1356 error = got_worktree_checkout_files(worktree, &paths, repo,
1357 update_progress, &did_something, check_cancelled, NULL);
1358 if (error != NULL)
1359 goto done;
1361 if (did_something)
1362 printf("Updated to commit %s\n", commit_id_str);
1363 else
1364 printf("Already up-to-date\n");
1365 done:
1366 free(worktree_path);
1367 TAILQ_FOREACH(pe, &paths, entry)
1368 free((char *)pe->path);
1369 got_pathlist_free(&paths);
1370 free(commit_id);
1371 free(commit_id_str);
1372 return error;
1375 static const struct got_error *
1376 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1377 const char *path, int diff_context, int ignore_whitespace,
1378 struct got_repository *repo)
1380 const struct got_error *err = NULL;
1381 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1383 if (blob_id1) {
1384 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1385 if (err)
1386 goto done;
1389 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1390 if (err)
1391 goto done;
1393 while (path[0] == '/')
1394 path++;
1395 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1396 ignore_whitespace, stdout);
1397 done:
1398 if (blob1)
1399 got_object_blob_close(blob1);
1400 got_object_blob_close(blob2);
1401 return err;
1404 static const struct got_error *
1405 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1406 const char *path, int diff_context, int ignore_whitespace,
1407 struct got_repository *repo)
1409 const struct got_error *err = NULL;
1410 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1411 struct got_diff_blob_output_unidiff_arg arg;
1413 if (tree_id1) {
1414 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1415 if (err)
1416 goto done;
1419 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1420 if (err)
1421 goto done;
1423 arg.diff_context = diff_context;
1424 arg.ignore_whitespace = ignore_whitespace;
1425 arg.outfile = stdout;
1426 while (path[0] == '/')
1427 path++;
1428 err = got_diff_tree(tree1, tree2, path, path, repo,
1429 got_diff_blob_output_unidiff, &arg, 1);
1430 done:
1431 if (tree1)
1432 got_object_tree_close(tree1);
1433 if (tree2)
1434 got_object_tree_close(tree2);
1435 return err;
1438 static const struct got_error *
1439 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1440 const char *path, int diff_context, struct got_repository *repo)
1442 const struct got_error *err = NULL;
1443 struct got_commit_object *pcommit = NULL;
1444 char *id_str1 = NULL, *id_str2 = NULL;
1445 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1446 struct got_object_qid *qid;
1448 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1449 if (qid != NULL) {
1450 err = got_object_open_as_commit(&pcommit, repo,
1451 qid->id);
1452 if (err)
1453 return err;
1456 if (path && path[0] != '\0') {
1457 int obj_type;
1458 err = got_object_id_by_path(&obj_id2, repo, id, path);
1459 if (err)
1460 goto done;
1461 err = got_object_id_str(&id_str2, obj_id2);
1462 if (err) {
1463 free(obj_id2);
1464 goto done;
1466 if (pcommit) {
1467 err = got_object_id_by_path(&obj_id1, repo,
1468 qid->id, path);
1469 if (err) {
1470 free(obj_id2);
1471 goto done;
1473 err = got_object_id_str(&id_str1, obj_id1);
1474 if (err) {
1475 free(obj_id2);
1476 goto done;
1479 err = got_object_get_type(&obj_type, repo, obj_id2);
1480 if (err) {
1481 free(obj_id2);
1482 goto done;
1484 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1485 switch (obj_type) {
1486 case GOT_OBJ_TYPE_BLOB:
1487 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1488 0, repo);
1489 break;
1490 case GOT_OBJ_TYPE_TREE:
1491 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1492 0, repo);
1493 break;
1494 default:
1495 err = got_error(GOT_ERR_OBJ_TYPE);
1496 break;
1498 free(obj_id1);
1499 free(obj_id2);
1500 } else {
1501 obj_id2 = got_object_commit_get_tree_id(commit);
1502 err = got_object_id_str(&id_str2, obj_id2);
1503 if (err)
1504 goto done;
1505 obj_id1 = got_object_commit_get_tree_id(pcommit);
1506 err = got_object_id_str(&id_str1, obj_id1);
1507 if (err)
1508 goto done;
1509 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1510 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1513 done:
1514 free(id_str1);
1515 free(id_str2);
1516 if (pcommit)
1517 got_object_commit_close(pcommit);
1518 return err;
1521 static char *
1522 get_datestr(time_t *time, char *datebuf)
1524 struct tm mytm, *tm;
1525 char *p, *s;
1527 tm = gmtime_r(time, &mytm);
1528 if (tm == NULL)
1529 return NULL;
1530 s = asctime_r(tm, datebuf);
1531 if (s == NULL)
1532 return NULL;
1533 p = strchr(s, '\n');
1534 if (p)
1535 *p = '\0';
1536 return s;
1539 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1541 static const struct got_error *
1542 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1543 struct got_repository *repo, const char *path, int show_patch,
1544 int diff_context, struct got_reflist_head *refs)
1546 const struct got_error *err = NULL;
1547 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1548 char datebuf[26];
1549 time_t committer_time;
1550 const char *author, *committer;
1551 char *refs_str = NULL;
1552 struct got_reflist_entry *re;
1554 SIMPLEQ_FOREACH(re, refs, entry) {
1555 char *s;
1556 const char *name;
1557 struct got_tag_object *tag = NULL;
1558 int cmp;
1560 name = got_ref_get_name(re->ref);
1561 if (strcmp(name, GOT_REF_HEAD) == 0)
1562 continue;
1563 if (strncmp(name, "refs/", 5) == 0)
1564 name += 5;
1565 if (strncmp(name, "got/", 4) == 0)
1566 continue;
1567 if (strncmp(name, "heads/", 6) == 0)
1568 name += 6;
1569 if (strncmp(name, "remotes/", 8) == 0)
1570 name += 8;
1571 if (strncmp(name, "tags/", 5) == 0) {
1572 err = got_object_open_as_tag(&tag, repo, re->id);
1573 if (err) {
1574 if (err->code != GOT_ERR_OBJ_TYPE)
1575 return err;
1576 /* Ref points at something other than a tag. */
1577 err = NULL;
1578 tag = NULL;
1581 cmp = got_object_id_cmp(tag ?
1582 got_object_tag_get_object_id(tag) : re->id, id);
1583 if (tag)
1584 got_object_tag_close(tag);
1585 if (cmp != 0)
1586 continue;
1587 s = refs_str;
1588 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1589 name) == -1) {
1590 err = got_error_from_errno("asprintf");
1591 free(s);
1592 return err;
1594 free(s);
1596 err = got_object_id_str(&id_str, id);
1597 if (err)
1598 return err;
1600 printf(GOT_COMMIT_SEP_STR);
1601 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1602 refs_str ? refs_str : "", refs_str ? ")" : "");
1603 free(id_str);
1604 id_str = NULL;
1605 free(refs_str);
1606 refs_str = NULL;
1607 printf("from: %s\n", got_object_commit_get_author(commit));
1608 committer_time = got_object_commit_get_committer_time(commit);
1609 datestr = get_datestr(&committer_time, datebuf);
1610 if (datestr)
1611 printf("date: %s UTC\n", datestr);
1612 author = got_object_commit_get_author(commit);
1613 committer = got_object_commit_get_committer(commit);
1614 if (strcmp(author, committer) != 0)
1615 printf("via: %s\n", committer);
1616 if (got_object_commit_get_nparents(commit) > 1) {
1617 const struct got_object_id_queue *parent_ids;
1618 struct got_object_qid *qid;
1619 int n = 1;
1620 parent_ids = got_object_commit_get_parent_ids(commit);
1621 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1622 err = got_object_id_str(&id_str, qid->id);
1623 if (err)
1624 return err;
1625 printf("parent %d: %s\n", n++, id_str);
1626 free(id_str);
1630 err = got_object_commit_get_logmsg(&logmsg0, commit);
1631 if (err)
1632 return err;
1634 logmsg = logmsg0;
1635 do {
1636 line = strsep(&logmsg, "\n");
1637 if (line)
1638 printf(" %s\n", line);
1639 } while (line);
1640 free(logmsg0);
1642 if (show_patch) {
1643 err = print_patch(commit, id, path, diff_context, repo);
1644 if (err == 0)
1645 printf("\n");
1648 if (fflush(stdout) != 0 && err == NULL)
1649 err = got_error_from_errno("fflush");
1650 return err;
1653 static const struct got_error *
1654 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1655 char *path, int show_patch, int diff_context, int limit,
1656 int first_parent_traversal, struct got_reflist_head *refs)
1658 const struct got_error *err;
1659 struct got_commit_graph *graph;
1661 err = got_commit_graph_open(&graph, root_id, path,
1662 first_parent_traversal, repo);
1663 if (err)
1664 return err;
1665 err = got_commit_graph_iter_start(graph, root_id, repo,
1666 check_cancelled, NULL);
1667 if (err)
1668 goto done;
1669 for (;;) {
1670 struct got_commit_object *commit;
1671 struct got_object_id *id;
1673 if (sigint_received || sigpipe_received)
1674 break;
1676 err = got_commit_graph_iter_next(&id, graph);
1677 if (err) {
1678 if (err->code == GOT_ERR_ITER_COMPLETED) {
1679 err = NULL;
1680 break;
1682 if (err->code != GOT_ERR_ITER_NEED_MORE)
1683 break;
1684 err = got_commit_graph_fetch_commits(graph, 1, repo,
1685 check_cancelled, NULL);
1686 if (err)
1687 break;
1688 else
1689 continue;
1691 if (id == NULL)
1692 break;
1694 err = got_object_open_as_commit(&commit, repo, id);
1695 if (err)
1696 break;
1697 err = print_commit(commit, id, repo, path, show_patch,
1698 diff_context, refs);
1699 got_object_commit_close(commit);
1700 if (err || (limit && --limit == 0))
1701 break;
1703 done:
1704 got_commit_graph_close(graph);
1705 return err;
1708 __dead static void
1709 usage_log(void)
1711 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1712 "[-r repository-path] [path]\n", getprogname());
1713 exit(1);
1716 static int
1717 get_default_log_limit(void)
1719 const char *got_default_log_limit;
1720 long long n;
1721 const char *errstr;
1723 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1724 if (got_default_log_limit == NULL)
1725 return 0;
1726 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1727 if (errstr != NULL)
1728 return 0;
1729 return n;
1732 static const struct got_error *
1733 cmd_log(int argc, char *argv[])
1735 const struct got_error *error;
1736 struct got_repository *repo = NULL;
1737 struct got_worktree *worktree = NULL;
1738 struct got_commit_object *commit = NULL;
1739 struct got_object_id *id = NULL;
1740 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1741 char *start_commit = NULL;
1742 int diff_context = 3, ch;
1743 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1744 const char *errstr;
1745 struct got_reflist_head refs;
1747 SIMPLEQ_INIT(&refs);
1749 #ifndef PROFILE
1750 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1751 NULL)
1752 == -1)
1753 err(1, "pledge");
1754 #endif
1756 limit = get_default_log_limit();
1758 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1759 switch (ch) {
1760 case 'p':
1761 show_patch = 1;
1762 break;
1763 case 'c':
1764 start_commit = optarg;
1765 break;
1766 case 'C':
1767 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1768 &errstr);
1769 if (errstr != NULL)
1770 err(1, "-C option %s", errstr);
1771 break;
1772 case 'l':
1773 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1774 if (errstr != NULL)
1775 err(1, "-l option %s", errstr);
1776 break;
1777 case 'f':
1778 first_parent_traversal = 1;
1779 break;
1780 case 'r':
1781 repo_path = realpath(optarg, NULL);
1782 if (repo_path == NULL)
1783 err(1, "-r option");
1784 got_path_strip_trailing_slashes(repo_path);
1785 break;
1786 default:
1787 usage_log();
1788 /* NOTREACHED */
1792 argc -= optind;
1793 argv += optind;
1795 cwd = getcwd(NULL, 0);
1796 if (cwd == NULL) {
1797 error = got_error_from_errno("getcwd");
1798 goto done;
1801 error = got_worktree_open(&worktree, cwd);
1802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1803 goto done;
1804 error = NULL;
1806 if (argc == 0) {
1807 path = strdup("");
1808 if (path == NULL) {
1809 error = got_error_from_errno("strdup");
1810 goto done;
1812 } else if (argc == 1) {
1813 if (worktree) {
1814 error = got_worktree_resolve_path(&path, worktree,
1815 argv[0]);
1816 if (error)
1817 goto done;
1818 } else {
1819 path = strdup(argv[0]);
1820 if (path == NULL) {
1821 error = got_error_from_errno("strdup");
1822 goto done;
1825 } else
1826 usage_log();
1828 if (repo_path == NULL) {
1829 repo_path = worktree ?
1830 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1832 if (repo_path == NULL) {
1833 error = got_error_from_errno("strdup");
1834 goto done;
1837 error = got_repo_open(&repo, repo_path, NULL);
1838 if (error != NULL)
1839 goto done;
1841 error = apply_unveil(got_repo_get_path(repo), 1,
1842 worktree ? got_worktree_get_root_path(worktree) : NULL);
1843 if (error)
1844 goto done;
1846 if (start_commit == NULL) {
1847 struct got_reference *head_ref;
1848 error = got_ref_open(&head_ref, repo,
1849 worktree ? got_worktree_get_head_ref_name(worktree)
1850 : GOT_REF_HEAD, 0);
1851 if (error != NULL)
1852 return error;
1853 error = got_ref_resolve(&id, repo, head_ref);
1854 got_ref_close(head_ref);
1855 if (error != NULL)
1856 return error;
1857 error = got_object_open_as_commit(&commit, repo, id);
1858 } else {
1859 struct got_reference *ref;
1860 error = got_ref_open(&ref, repo, start_commit, 0);
1861 if (error == NULL) {
1862 int obj_type;
1863 error = got_ref_resolve(&id, repo, ref);
1864 got_ref_close(ref);
1865 if (error != NULL)
1866 goto done;
1867 error = got_object_get_type(&obj_type, repo, id);
1868 if (error != NULL)
1869 goto done;
1870 if (obj_type == GOT_OBJ_TYPE_TAG) {
1871 struct got_tag_object *tag;
1872 error = got_object_open_as_tag(&tag, repo, id);
1873 if (error != NULL)
1874 goto done;
1875 if (got_object_tag_get_object_type(tag) !=
1876 GOT_OBJ_TYPE_COMMIT) {
1877 got_object_tag_close(tag);
1878 error = got_error(GOT_ERR_OBJ_TYPE);
1879 goto done;
1881 free(id);
1882 id = got_object_id_dup(
1883 got_object_tag_get_object_id(tag));
1884 if (id == NULL)
1885 error = got_error_from_errno(
1886 "got_object_id_dup");
1887 got_object_tag_close(tag);
1888 if (error)
1889 goto done;
1890 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1891 error = got_error(GOT_ERR_OBJ_TYPE);
1892 goto done;
1894 error = got_object_open_as_commit(&commit, repo, id);
1895 if (error != NULL)
1896 goto done;
1898 if (commit == NULL) {
1899 error = got_repo_match_object_id_prefix(&id,
1900 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1901 if (error != NULL)
1902 return error;
1905 if (error != NULL)
1906 goto done;
1908 if (worktree) {
1909 const char *prefix = got_worktree_get_path_prefix(worktree);
1910 char *p;
1911 if (asprintf(&p, "%s%s%s", prefix,
1912 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1913 error = got_error_from_errno("asprintf");
1914 goto done;
1916 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1917 free(p);
1918 } else
1919 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1920 if (error != NULL)
1921 goto done;
1922 if (in_repo_path) {
1923 free(path);
1924 path = in_repo_path;
1927 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1928 if (error)
1929 goto done;
1931 error = print_commits(id, repo, path, show_patch,
1932 diff_context, limit, first_parent_traversal, &refs);
1933 done:
1934 free(path);
1935 free(repo_path);
1936 free(cwd);
1937 free(id);
1938 if (worktree)
1939 got_worktree_close(worktree);
1940 if (repo) {
1941 const struct got_error *repo_error;
1942 repo_error = got_repo_close(repo);
1943 if (error == NULL)
1944 error = repo_error;
1946 got_ref_list_free(&refs);
1947 return error;
1950 __dead static void
1951 usage_diff(void)
1953 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1954 "[-w] [object1 object2 | path]\n", getprogname());
1955 exit(1);
1958 struct print_diff_arg {
1959 struct got_repository *repo;
1960 struct got_worktree *worktree;
1961 int diff_context;
1962 const char *id_str;
1963 int header_shown;
1964 int diff_staged;
1965 int ignore_whitespace;
1968 static const struct got_error *
1969 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1970 const char *path, struct got_object_id *blob_id,
1971 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1973 struct print_diff_arg *a = arg;
1974 const struct got_error *err = NULL;
1975 struct got_blob_object *blob1 = NULL;
1976 FILE *f2 = NULL;
1977 char *abspath = NULL, *label1 = NULL;
1978 struct stat sb;
1980 if (a->diff_staged) {
1981 if (staged_status != GOT_STATUS_MODIFY &&
1982 staged_status != GOT_STATUS_ADD &&
1983 staged_status != GOT_STATUS_DELETE)
1984 return NULL;
1985 } else {
1986 if (staged_status == GOT_STATUS_DELETE)
1987 return NULL;
1988 if (status == GOT_STATUS_NONEXISTENT)
1989 return got_error_set_errno(ENOENT, path);
1990 if (status != GOT_STATUS_MODIFY &&
1991 status != GOT_STATUS_ADD &&
1992 status != GOT_STATUS_DELETE &&
1993 status != GOT_STATUS_CONFLICT)
1994 return NULL;
1997 if (!a->header_shown) {
1998 printf("diff %s %s%s\n", a->id_str,
1999 got_worktree_get_root_path(a->worktree),
2000 a->diff_staged ? " (staged changes)" : "");
2001 a->header_shown = 1;
2004 if (a->diff_staged) {
2005 const char *label1 = NULL, *label2 = NULL;
2006 switch (staged_status) {
2007 case GOT_STATUS_MODIFY:
2008 label1 = path;
2009 label2 = path;
2010 break;
2011 case GOT_STATUS_ADD:
2012 label2 = path;
2013 break;
2014 case GOT_STATUS_DELETE:
2015 label1 = path;
2016 break;
2017 default:
2018 return got_error(GOT_ERR_FILE_STATUS);
2020 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2021 label1, label2, a->diff_context, a->ignore_whitespace,
2022 a->repo, stdout);
2025 if (staged_status == GOT_STATUS_ADD ||
2026 staged_status == GOT_STATUS_MODIFY) {
2027 char *id_str;
2028 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2029 8192);
2030 if (err)
2031 goto done;
2032 err = got_object_id_str(&id_str, staged_blob_id);
2033 if (err)
2034 goto done;
2035 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2036 err = got_error_from_errno("asprintf");
2037 free(id_str);
2038 goto done;
2040 free(id_str);
2041 } else if (status != GOT_STATUS_ADD) {
2042 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2043 if (err)
2044 goto done;
2047 if (status != GOT_STATUS_DELETE) {
2048 if (asprintf(&abspath, "%s/%s",
2049 got_worktree_get_root_path(a->worktree), path) == -1) {
2050 err = got_error_from_errno("asprintf");
2051 goto done;
2054 f2 = fopen(abspath, "r");
2055 if (f2 == NULL) {
2056 err = got_error_from_errno2("fopen", abspath);
2057 goto done;
2059 if (lstat(abspath, &sb) == -1) {
2060 err = got_error_from_errno2("lstat", abspath);
2061 goto done;
2063 } else
2064 sb.st_size = 0;
2066 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2067 a->diff_context, a->ignore_whitespace, stdout);
2068 done:
2069 if (blob1)
2070 got_object_blob_close(blob1);
2071 if (f2 && fclose(f2) != 0 && err == NULL)
2072 err = got_error_from_errno("fclose");
2073 free(abspath);
2074 return err;
2077 static const struct got_error *
2078 match_object_id(struct got_object_id **id, char **label,
2079 const char *id_str, int obj_type, int resolve_tags,
2080 struct got_repository *repo)
2082 const struct got_error *err;
2083 struct got_tag_object *tag;
2084 struct got_reference *ref = NULL;
2086 *id = NULL;
2087 *label = NULL;
2089 if (resolve_tags) {
2090 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2091 repo);
2092 if (err == NULL) {
2093 *id = got_object_id_dup(
2094 got_object_tag_get_object_id(tag));
2095 if (*id == NULL)
2096 err = got_error_from_errno("got_object_id_dup");
2097 else if (asprintf(label, "refs/tags/%s",
2098 got_object_tag_get_name(tag)) == -1) {
2099 err = got_error_from_errno("asprintf");
2100 free(*id);
2101 *id = NULL;
2103 got_object_tag_close(tag);
2104 return err;
2105 } else if (err->code != GOT_ERR_NO_OBJ)
2106 return err;
2109 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2110 if (err) {
2111 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2112 return err;
2113 err = got_ref_open(&ref, repo, id_str, 0);
2114 if (err != NULL)
2115 goto done;
2116 *label = strdup(got_ref_get_name(ref));
2117 if (*label == NULL) {
2118 err = got_error_from_errno("strdup");
2119 goto done;
2121 err = got_ref_resolve(id, repo, ref);
2122 } else {
2123 err = got_object_id_str(label, *id);
2124 if (*label == NULL) {
2125 err = got_error_from_errno("strdup");
2126 goto done;
2129 done:
2130 if (ref)
2131 got_ref_close(ref);
2132 return err;
2136 static const struct got_error *
2137 cmd_diff(int argc, char *argv[])
2139 const struct got_error *error;
2140 struct got_repository *repo = NULL;
2141 struct got_worktree *worktree = NULL;
2142 char *cwd = NULL, *repo_path = NULL;
2143 struct got_object_id *id1 = NULL, *id2 = NULL;
2144 const char *id_str1 = NULL, *id_str2 = NULL;
2145 char *label1 = NULL, *label2 = NULL;
2146 int type1, type2;
2147 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2148 const char *errstr;
2149 char *path = NULL;
2151 #ifndef PROFILE
2152 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2153 NULL) == -1)
2154 err(1, "pledge");
2155 #endif
2157 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2158 switch (ch) {
2159 case 'C':
2160 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2161 if (errstr != NULL)
2162 err(1, "-C option %s", errstr);
2163 break;
2164 case 'r':
2165 repo_path = realpath(optarg, NULL);
2166 if (repo_path == NULL)
2167 err(1, "-r option");
2168 got_path_strip_trailing_slashes(repo_path);
2169 break;
2170 case 's':
2171 diff_staged = 1;
2172 break;
2173 case 'w':
2174 ignore_whitespace = 1;
2175 break;
2176 default:
2177 usage_diff();
2178 /* NOTREACHED */
2182 argc -= optind;
2183 argv += optind;
2185 cwd = getcwd(NULL, 0);
2186 if (cwd == NULL) {
2187 error = got_error_from_errno("getcwd");
2188 goto done;
2190 error = got_worktree_open(&worktree, cwd);
2191 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2192 goto done;
2193 if (argc <= 1) {
2194 if (worktree == NULL) {
2195 error = got_error(GOT_ERR_NOT_WORKTREE);
2196 goto done;
2198 if (repo_path)
2199 errx(1,
2200 "-r option can't be used when diffing a work tree");
2201 repo_path = strdup(got_worktree_get_repo_path(worktree));
2202 if (repo_path == NULL) {
2203 error = got_error_from_errno("strdup");
2204 goto done;
2206 if (argc == 1) {
2207 error = got_worktree_resolve_path(&path, worktree,
2208 argv[0]);
2209 if (error)
2210 goto done;
2211 } else {
2212 path = strdup("");
2213 if (path == NULL) {
2214 error = got_error_from_errno("strdup");
2215 goto done;
2218 } else if (argc == 2) {
2219 if (diff_staged)
2220 errx(1, "-s option can't be used when diffing "
2221 "objects in repository");
2222 id_str1 = argv[0];
2223 id_str2 = argv[1];
2224 if (worktree && repo_path == NULL) {
2225 repo_path =
2226 strdup(got_worktree_get_repo_path(worktree));
2227 if (repo_path == NULL) {
2228 error = got_error_from_errno("strdup");
2229 goto done;
2232 } else
2233 usage_diff();
2235 if (repo_path == NULL) {
2236 repo_path = getcwd(NULL, 0);
2237 if (repo_path == NULL)
2238 return got_error_from_errno("getcwd");
2241 error = got_repo_open(&repo, repo_path, NULL);
2242 free(repo_path);
2243 if (error != NULL)
2244 goto done;
2246 error = apply_unveil(got_repo_get_path(repo), 1,
2247 worktree ? got_worktree_get_root_path(worktree) : NULL);
2248 if (error)
2249 goto done;
2251 if (argc <= 1) {
2252 struct print_diff_arg arg;
2253 struct got_pathlist_head paths;
2254 char *id_str;
2256 TAILQ_INIT(&paths);
2258 error = got_object_id_str(&id_str,
2259 got_worktree_get_base_commit_id(worktree));
2260 if (error)
2261 goto done;
2262 arg.repo = repo;
2263 arg.worktree = worktree;
2264 arg.diff_context = diff_context;
2265 arg.id_str = id_str;
2266 arg.header_shown = 0;
2267 arg.diff_staged = diff_staged;
2268 arg.ignore_whitespace = ignore_whitespace;
2270 error = got_pathlist_append(&paths, path, NULL);
2271 if (error)
2272 goto done;
2274 error = got_worktree_status(worktree, &paths, repo, print_diff,
2275 &arg, check_cancelled, NULL);
2276 free(id_str);
2277 got_pathlist_free(&paths);
2278 goto done;
2281 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2282 repo);
2283 if (error)
2284 goto done;
2286 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2287 repo);
2288 if (error)
2289 goto done;
2291 error = got_object_get_type(&type1, repo, id1);
2292 if (error)
2293 goto done;
2295 error = got_object_get_type(&type2, repo, id2);
2296 if (error)
2297 goto done;
2299 if (type1 != type2) {
2300 error = got_error(GOT_ERR_OBJ_TYPE);
2301 goto done;
2304 switch (type1) {
2305 case GOT_OBJ_TYPE_BLOB:
2306 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2307 diff_context, ignore_whitespace, repo, stdout);
2308 break;
2309 case GOT_OBJ_TYPE_TREE:
2310 error = got_diff_objects_as_trees(id1, id2, "", "",
2311 diff_context, ignore_whitespace, repo, stdout);
2312 break;
2313 case GOT_OBJ_TYPE_COMMIT:
2314 printf("diff %s %s\n", label1, label2);
2315 error = got_diff_objects_as_commits(id1, id2, diff_context,
2316 ignore_whitespace, repo, stdout);
2317 break;
2318 default:
2319 error = got_error(GOT_ERR_OBJ_TYPE);
2322 done:
2323 free(label1);
2324 free(label2);
2325 free(id1);
2326 free(id2);
2327 free(path);
2328 if (worktree)
2329 got_worktree_close(worktree);
2330 if (repo) {
2331 const struct got_error *repo_error;
2332 repo_error = got_repo_close(repo);
2333 if (error == NULL)
2334 error = repo_error;
2336 return error;
2339 __dead static void
2340 usage_blame(void)
2342 fprintf(stderr,
2343 "usage: %s blame [-c commit] [-r repository-path] path\n",
2344 getprogname());
2345 exit(1);
2348 struct blame_line {
2349 int annotated;
2350 char *id_str;
2351 char *committer;
2352 char datebuf[9]; /* YY-MM-DD + NUL */
2355 struct blame_cb_args {
2356 struct blame_line *lines;
2357 int nlines;
2358 int nlines_prec;
2359 int lineno_cur;
2360 off_t *line_offsets;
2361 FILE *f;
2362 struct got_repository *repo;
2365 static const struct got_error *
2366 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2368 const struct got_error *err = NULL;
2369 struct blame_cb_args *a = arg;
2370 struct blame_line *bline;
2371 char *line = NULL;
2372 size_t linesize = 0;
2373 struct got_commit_object *commit = NULL;
2374 off_t offset;
2375 struct tm tm;
2376 time_t committer_time;
2378 if (nlines != a->nlines ||
2379 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2380 return got_error(GOT_ERR_RANGE);
2382 if (sigint_received)
2383 return got_error(GOT_ERR_ITER_COMPLETED);
2385 if (lineno == -1)
2386 return NULL; /* no change in this commit */
2388 /* Annotate this line. */
2389 bline = &a->lines[lineno - 1];
2390 if (bline->annotated)
2391 return NULL;
2392 err = got_object_id_str(&bline->id_str, id);
2393 if (err)
2394 return err;
2396 err = got_object_open_as_commit(&commit, a->repo, id);
2397 if (err)
2398 goto done;
2400 bline->committer = strdup(got_object_commit_get_committer(commit));
2401 if (bline->committer == NULL) {
2402 err = got_error_from_errno("strdup");
2403 goto done;
2406 committer_time = got_object_commit_get_committer_time(commit);
2407 if (localtime_r(&committer_time, &tm) == NULL)
2408 return got_error_from_errno("localtime_r");
2409 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2410 &tm) >= sizeof(bline->datebuf)) {
2411 err = got_error(GOT_ERR_NO_SPACE);
2412 goto done;
2414 bline->annotated = 1;
2416 /* Print lines annotated so far. */
2417 bline = &a->lines[a->lineno_cur - 1];
2418 if (!bline->annotated)
2419 goto done;
2421 offset = a->line_offsets[a->lineno_cur - 1];
2422 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2423 err = got_error_from_errno("fseeko");
2424 goto done;
2427 while (bline->annotated) {
2428 char *smallerthan, *at, *nl, *committer;
2429 size_t len;
2431 if (getline(&line, &linesize, a->f) == -1) {
2432 if (ferror(a->f))
2433 err = got_error_from_errno("getline");
2434 break;
2437 committer = bline->committer;
2438 smallerthan = strchr(committer, '<');
2439 if (smallerthan && smallerthan[1] != '\0')
2440 committer = smallerthan + 1;
2441 at = strchr(committer, '@');
2442 if (at)
2443 *at = '\0';
2444 len = strlen(committer);
2445 if (len >= 9)
2446 committer[8] = '\0';
2448 nl = strchr(line, '\n');
2449 if (nl)
2450 *nl = '\0';
2451 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2452 bline->id_str, bline->datebuf, committer, line);
2454 a->lineno_cur++;
2455 bline = &a->lines[a->lineno_cur - 1];
2457 done:
2458 if (commit)
2459 got_object_commit_close(commit);
2460 free(line);
2461 return err;
2464 static const struct got_error *
2465 cmd_blame(int argc, char *argv[])
2467 const struct got_error *error;
2468 struct got_repository *repo = NULL;
2469 struct got_worktree *worktree = NULL;
2470 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2471 struct got_object_id *obj_id = NULL;
2472 struct got_object_id *commit_id = NULL;
2473 struct got_blob_object *blob = NULL;
2474 char *commit_id_str = NULL;
2475 struct blame_cb_args bca;
2476 int ch, obj_type, i;
2477 size_t filesize;
2479 memset(&bca, 0, sizeof(bca));
2481 #ifndef PROFILE
2482 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2483 NULL) == -1)
2484 err(1, "pledge");
2485 #endif
2487 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2488 switch (ch) {
2489 case 'c':
2490 commit_id_str = optarg;
2491 break;
2492 case 'r':
2493 repo_path = realpath(optarg, NULL);
2494 if (repo_path == NULL)
2495 err(1, "-r option");
2496 got_path_strip_trailing_slashes(repo_path);
2497 break;
2498 default:
2499 usage_blame();
2500 /* NOTREACHED */
2504 argc -= optind;
2505 argv += optind;
2507 if (argc == 1)
2508 path = argv[0];
2509 else
2510 usage_blame();
2512 cwd = getcwd(NULL, 0);
2513 if (cwd == NULL) {
2514 error = got_error_from_errno("getcwd");
2515 goto done;
2517 if (repo_path == NULL) {
2518 error = got_worktree_open(&worktree, cwd);
2519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2520 goto done;
2521 else
2522 error = NULL;
2523 if (worktree) {
2524 repo_path =
2525 strdup(got_worktree_get_repo_path(worktree));
2526 if (repo_path == NULL)
2527 error = got_error_from_errno("strdup");
2528 if (error)
2529 goto done;
2530 } else {
2531 repo_path = strdup(cwd);
2532 if (repo_path == NULL) {
2533 error = got_error_from_errno("strdup");
2534 goto done;
2539 error = got_repo_open(&repo, repo_path, NULL);
2540 if (error != NULL)
2541 goto done;
2543 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2544 if (error)
2545 goto done;
2547 if (worktree) {
2548 const char *prefix = got_worktree_get_path_prefix(worktree);
2549 char *p, *worktree_subdir = cwd +
2550 strlen(got_worktree_get_root_path(worktree));
2551 if (asprintf(&p, "%s%s%s%s%s",
2552 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2553 worktree_subdir, worktree_subdir[0] ? "/" : "",
2554 path) == -1) {
2555 error = got_error_from_errno("asprintf");
2556 goto done;
2558 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2559 free(p);
2560 } else {
2561 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2563 if (error)
2564 goto done;
2566 if (commit_id_str == NULL) {
2567 struct got_reference *head_ref;
2568 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2569 if (error != NULL)
2570 goto done;
2571 error = got_ref_resolve(&commit_id, repo, head_ref);
2572 got_ref_close(head_ref);
2573 if (error != NULL)
2574 goto done;
2575 } else {
2576 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2577 if (error)
2578 goto done;
2581 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2582 if (error)
2583 goto done;
2584 if (obj_id == NULL) {
2585 error = got_error(GOT_ERR_NO_OBJ);
2586 goto done;
2589 error = got_object_get_type(&obj_type, repo, obj_id);
2590 if (error)
2591 goto done;
2593 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2594 error = got_error(GOT_ERR_OBJ_TYPE);
2595 goto done;
2598 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2599 if (error)
2600 goto done;
2601 bca.f = got_opentemp();
2602 if (bca.f == NULL) {
2603 error = got_error_from_errno("got_opentemp");
2604 goto done;
2606 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2607 &bca.line_offsets, bca.f, blob);
2608 if (error || bca.nlines == 0)
2609 goto done;
2611 /* Don't include \n at EOF in the blame line count. */
2612 if (bca.line_offsets[bca.nlines - 1] == filesize)
2613 bca.nlines--;
2615 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2616 if (bca.lines == NULL) {
2617 error = got_error_from_errno("calloc");
2618 goto done;
2620 bca.lineno_cur = 1;
2621 bca.nlines_prec = 0;
2622 i = bca.nlines;
2623 while (i > 0) {
2624 i /= 10;
2625 bca.nlines_prec++;
2627 bca.repo = repo;
2629 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2630 check_cancelled, NULL);
2631 if (error)
2632 goto done;
2633 done:
2634 free(in_repo_path);
2635 free(repo_path);
2636 free(cwd);
2637 free(commit_id);
2638 free(obj_id);
2639 if (blob)
2640 got_object_blob_close(blob);
2641 if (worktree)
2642 got_worktree_close(worktree);
2643 if (repo) {
2644 const struct got_error *repo_error;
2645 repo_error = got_repo_close(repo);
2646 if (error == NULL)
2647 error = repo_error;
2649 if (bca.lines) {
2650 for (i = 0; i < bca.nlines; i++) {
2651 struct blame_line *bline = &bca.lines[i];
2652 free(bline->id_str);
2653 free(bline->committer);
2655 free(bca.lines);
2657 free(bca.line_offsets);
2658 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2659 error = got_error_from_errno("fclose");
2660 return error;
2663 __dead static void
2664 usage_tree(void)
2666 fprintf(stderr,
2667 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2668 getprogname());
2669 exit(1);
2672 static void
2673 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2674 const char *root_path)
2676 int is_root_path = (strcmp(path, root_path) == 0);
2677 const char *modestr = "";
2679 path += strlen(root_path);
2680 while (path[0] == '/')
2681 path++;
2683 if (got_object_tree_entry_is_submodule(te))
2684 modestr = "$";
2685 else if (S_ISLNK(te->mode))
2686 modestr = "@";
2687 else if (S_ISDIR(te->mode))
2688 modestr = "/";
2689 else if (te->mode & S_IXUSR)
2690 modestr = "*";
2692 printf("%s%s%s%s%s\n", id ? id : "", path,
2693 is_root_path ? "" : "/", te->name, modestr);
2696 static const struct got_error *
2697 print_tree(const char *path, struct got_object_id *commit_id,
2698 int show_ids, int recurse, const char *root_path,
2699 struct got_repository *repo)
2701 const struct got_error *err = NULL;
2702 struct got_object_id *tree_id = NULL;
2703 struct got_tree_object *tree = NULL;
2704 const struct got_tree_entries *entries;
2705 struct got_tree_entry *te;
2707 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2708 if (err)
2709 goto done;
2711 err = got_object_open_as_tree(&tree, repo, tree_id);
2712 if (err)
2713 goto done;
2714 entries = got_object_tree_get_entries(tree);
2715 te = SIMPLEQ_FIRST(&entries->head);
2716 while (te) {
2717 char *id = NULL;
2719 if (sigint_received || sigpipe_received)
2720 break;
2722 if (show_ids) {
2723 char *id_str;
2724 err = got_object_id_str(&id_str, te->id);
2725 if (err)
2726 goto done;
2727 if (asprintf(&id, "%s ", id_str) == -1) {
2728 err = got_error_from_errno("asprintf");
2729 free(id_str);
2730 goto done;
2732 free(id_str);
2734 print_entry(te, id, path, root_path);
2735 free(id);
2737 if (recurse && S_ISDIR(te->mode)) {
2738 char *child_path;
2739 if (asprintf(&child_path, "%s%s%s", path,
2740 path[0] == '/' && path[1] == '\0' ? "" : "/",
2741 te->name) == -1) {
2742 err = got_error_from_errno("asprintf");
2743 goto done;
2745 err = print_tree(child_path, commit_id, show_ids, 1,
2746 root_path, repo);
2747 free(child_path);
2748 if (err)
2749 goto done;
2752 te = SIMPLEQ_NEXT(te, entry);
2754 done:
2755 if (tree)
2756 got_object_tree_close(tree);
2757 free(tree_id);
2758 return err;
2761 static const struct got_error *
2762 cmd_tree(int argc, char *argv[])
2764 const struct got_error *error;
2765 struct got_repository *repo = NULL;
2766 struct got_worktree *worktree = NULL;
2767 const char *path;
2768 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2769 struct got_object_id *commit_id = NULL;
2770 char *commit_id_str = NULL;
2771 int show_ids = 0, recurse = 0;
2772 int ch;
2774 #ifndef PROFILE
2775 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2776 NULL) == -1)
2777 err(1, "pledge");
2778 #endif
2780 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2781 switch (ch) {
2782 case 'c':
2783 commit_id_str = optarg;
2784 break;
2785 case 'r':
2786 repo_path = realpath(optarg, NULL);
2787 if (repo_path == NULL)
2788 err(1, "-r option");
2789 got_path_strip_trailing_slashes(repo_path);
2790 break;
2791 case 'i':
2792 show_ids = 1;
2793 break;
2794 case 'R':
2795 recurse = 1;
2796 break;
2797 default:
2798 usage_tree();
2799 /* NOTREACHED */
2803 argc -= optind;
2804 argv += optind;
2806 if (argc == 1)
2807 path = argv[0];
2808 else if (argc > 1)
2809 usage_tree();
2810 else
2811 path = NULL;
2813 cwd = getcwd(NULL, 0);
2814 if (cwd == NULL) {
2815 error = got_error_from_errno("getcwd");
2816 goto done;
2818 if (repo_path == NULL) {
2819 error = got_worktree_open(&worktree, cwd);
2820 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2821 goto done;
2822 else
2823 error = NULL;
2824 if (worktree) {
2825 repo_path =
2826 strdup(got_worktree_get_repo_path(worktree));
2827 if (repo_path == NULL)
2828 error = got_error_from_errno("strdup");
2829 if (error)
2830 goto done;
2831 } else {
2832 repo_path = strdup(cwd);
2833 if (repo_path == NULL) {
2834 error = got_error_from_errno("strdup");
2835 goto done;
2840 error = got_repo_open(&repo, repo_path, NULL);
2841 if (error != NULL)
2842 goto done;
2844 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2845 if (error)
2846 goto done;
2848 if (path == NULL) {
2849 if (worktree) {
2850 char *p, *worktree_subdir = cwd +
2851 strlen(got_worktree_get_root_path(worktree));
2852 if (asprintf(&p, "%s/%s",
2853 got_worktree_get_path_prefix(worktree),
2854 worktree_subdir) == -1) {
2855 error = got_error_from_errno("asprintf");
2856 goto done;
2858 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2859 free(p);
2860 if (error)
2861 goto done;
2862 } else
2863 path = "/";
2865 if (in_repo_path == NULL) {
2866 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2867 if (error != NULL)
2868 goto done;
2871 if (commit_id_str == NULL) {
2872 struct got_reference *head_ref;
2873 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2874 if (error != NULL)
2875 goto done;
2876 error = got_ref_resolve(&commit_id, repo, head_ref);
2877 got_ref_close(head_ref);
2878 if (error != NULL)
2879 goto done;
2880 } else {
2881 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2882 if (error)
2883 goto done;
2886 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2887 in_repo_path, repo);
2888 done:
2889 free(in_repo_path);
2890 free(repo_path);
2891 free(cwd);
2892 free(commit_id);
2893 if (worktree)
2894 got_worktree_close(worktree);
2895 if (repo) {
2896 const struct got_error *repo_error;
2897 repo_error = got_repo_close(repo);
2898 if (error == NULL)
2899 error = repo_error;
2901 return error;
2904 __dead static void
2905 usage_status(void)
2907 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2908 exit(1);
2911 static const struct got_error *
2912 print_status(void *arg, unsigned char status, unsigned char staged_status,
2913 const char *path, struct got_object_id *blob_id,
2914 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2916 if (status == staged_status && (status == GOT_STATUS_DELETE))
2917 status = GOT_STATUS_NO_CHANGE;
2918 printf("%c%c %s\n", status, staged_status, path);
2919 return NULL;
2922 static const struct got_error *
2923 cmd_status(int argc, char *argv[])
2925 const struct got_error *error = NULL;
2926 struct got_repository *repo = NULL;
2927 struct got_worktree *worktree = NULL;
2928 char *cwd = NULL;
2929 struct got_pathlist_head paths;
2930 struct got_pathlist_entry *pe;
2931 int ch;
2933 TAILQ_INIT(&paths);
2935 while ((ch = getopt(argc, argv, "")) != -1) {
2936 switch (ch) {
2937 default:
2938 usage_status();
2939 /* NOTREACHED */
2943 argc -= optind;
2944 argv += optind;
2946 #ifndef PROFILE
2947 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2948 NULL) == -1)
2949 err(1, "pledge");
2950 #endif
2951 cwd = getcwd(NULL, 0);
2952 if (cwd == NULL) {
2953 error = got_error_from_errno("getcwd");
2954 goto done;
2957 error = got_worktree_open(&worktree, cwd);
2958 if (error != NULL)
2959 goto done;
2961 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2962 NULL);
2963 if (error != NULL)
2964 goto done;
2966 error = apply_unveil(got_repo_get_path(repo), 1,
2967 got_worktree_get_root_path(worktree));
2968 if (error)
2969 goto done;
2971 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2972 if (error)
2973 goto done;
2975 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2976 check_cancelled, NULL);
2977 done:
2978 TAILQ_FOREACH(pe, &paths, entry)
2979 free((char *)pe->path);
2980 got_pathlist_free(&paths);
2981 free(cwd);
2982 return error;
2985 __dead static void
2986 usage_ref(void)
2988 fprintf(stderr,
2989 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2990 getprogname());
2991 exit(1);
2994 static const struct got_error *
2995 list_refs(struct got_repository *repo)
2997 static const struct got_error *err = NULL;
2998 struct got_reflist_head refs;
2999 struct got_reflist_entry *re;
3001 SIMPLEQ_INIT(&refs);
3002 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3003 if (err)
3004 return err;
3006 SIMPLEQ_FOREACH(re, &refs, entry) {
3007 char *refstr;
3008 refstr = got_ref_to_str(re->ref);
3009 if (refstr == NULL)
3010 return got_error_from_errno("got_ref_to_str");
3011 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3012 free(refstr);
3015 got_ref_list_free(&refs);
3016 return NULL;
3019 static const struct got_error *
3020 delete_ref(struct got_repository *repo, const char *refname)
3022 const struct got_error *err = NULL;
3023 struct got_reference *ref;
3025 err = got_ref_open(&ref, repo, refname, 0);
3026 if (err)
3027 return err;
3029 err = got_ref_delete(ref, repo);
3030 got_ref_close(ref);
3031 return err;
3034 static const struct got_error *
3035 add_ref(struct got_repository *repo, const char *refname, const char *target)
3037 const struct got_error *err = NULL;
3038 struct got_object_id *id;
3039 struct got_reference *ref = NULL;
3042 * Don't let the user create a reference named '-'.
3043 * While technically a valid reference name, this case is usually
3044 * an unintended typo.
3046 if (refname[0] == '-' && refname[1] == '\0')
3047 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3049 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3050 repo);
3051 if (err) {
3052 struct got_reference *target_ref;
3054 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3055 return err;
3056 err = got_ref_open(&target_ref, repo, target, 0);
3057 if (err)
3058 return err;
3059 err = got_ref_resolve(&id, repo, target_ref);
3060 got_ref_close(target_ref);
3061 if (err)
3062 return err;
3065 err = got_ref_alloc(&ref, refname, id);
3066 if (err)
3067 goto done;
3069 err = got_ref_write(ref, repo);
3070 done:
3071 if (ref)
3072 got_ref_close(ref);
3073 free(id);
3074 return err;
3077 static const struct got_error *
3078 add_symref(struct got_repository *repo, const char *refname, const char *target)
3080 const struct got_error *err = NULL;
3081 struct got_reference *ref = NULL;
3082 struct got_reference *target_ref = NULL;
3085 * Don't let the user create a reference named '-'.
3086 * While technically a valid reference name, this case is usually
3087 * an unintended typo.
3089 if (refname[0] == '-' && refname[1] == '\0')
3090 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3092 err = got_ref_open(&target_ref, repo, target, 0);
3093 if (err)
3094 return err;
3096 err = got_ref_alloc_symref(&ref, refname, target_ref);
3097 if (err)
3098 goto done;
3100 err = got_ref_write(ref, repo);
3101 done:
3102 if (target_ref)
3103 got_ref_close(target_ref);
3104 if (ref)
3105 got_ref_close(ref);
3106 return err;
3109 static const struct got_error *
3110 cmd_ref(int argc, char *argv[])
3112 const struct got_error *error = NULL;
3113 struct got_repository *repo = NULL;
3114 struct got_worktree *worktree = NULL;
3115 char *cwd = NULL, *repo_path = NULL;
3116 int ch, do_list = 0, create_symref = 0;
3117 const char *delref = NULL;
3119 /* TODO: Add -s option for adding symbolic references. */
3120 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3121 switch (ch) {
3122 case 'd':
3123 delref = optarg;
3124 break;
3125 case 'r':
3126 repo_path = realpath(optarg, NULL);
3127 if (repo_path == NULL)
3128 err(1, "-r option");
3129 got_path_strip_trailing_slashes(repo_path);
3130 break;
3131 case 'l':
3132 do_list = 1;
3133 break;
3134 case 's':
3135 create_symref = 1;
3136 break;
3137 default:
3138 usage_ref();
3139 /* NOTREACHED */
3143 if (do_list && delref)
3144 errx(1, "-l and -d options are mutually exclusive\n");
3146 argc -= optind;
3147 argv += optind;
3149 if (do_list || delref) {
3150 if (create_symref)
3151 errx(1, "-s option cannot be used together with the "
3152 "-l or -d options");
3153 if (argc > 0)
3154 usage_ref();
3155 } else if (argc != 2)
3156 usage_ref();
3158 #ifndef PROFILE
3159 if (do_list) {
3160 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3161 NULL) == -1)
3162 err(1, "pledge");
3163 } else {
3164 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3165 "sendfd unveil", NULL) == -1)
3166 err(1, "pledge");
3168 #endif
3169 cwd = getcwd(NULL, 0);
3170 if (cwd == NULL) {
3171 error = got_error_from_errno("getcwd");
3172 goto done;
3175 if (repo_path == NULL) {
3176 error = got_worktree_open(&worktree, cwd);
3177 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3178 goto done;
3179 else
3180 error = NULL;
3181 if (worktree) {
3182 repo_path =
3183 strdup(got_worktree_get_repo_path(worktree));
3184 if (repo_path == NULL)
3185 error = got_error_from_errno("strdup");
3186 if (error)
3187 goto done;
3188 } else {
3189 repo_path = strdup(cwd);
3190 if (repo_path == NULL) {
3191 error = got_error_from_errno("strdup");
3192 goto done;
3197 error = got_repo_open(&repo, repo_path, NULL);
3198 if (error != NULL)
3199 goto done;
3201 error = apply_unveil(got_repo_get_path(repo), do_list,
3202 worktree ? got_worktree_get_root_path(worktree) : NULL);
3203 if (error)
3204 goto done;
3206 if (do_list)
3207 error = list_refs(repo);
3208 else if (delref)
3209 error = delete_ref(repo, delref);
3210 else if (create_symref)
3211 error = add_symref(repo, argv[0], argv[1]);
3212 else
3213 error = add_ref(repo, argv[0], argv[1]);
3214 done:
3215 if (repo)
3216 got_repo_close(repo);
3217 if (worktree)
3218 got_worktree_close(worktree);
3219 free(cwd);
3220 free(repo_path);
3221 return error;
3224 __dead static void
3225 usage_branch(void)
3227 fprintf(stderr,
3228 "usage: %s branch [-r repository] [-l] | -d name | "
3229 "[name [commit]]\n", getprogname());
3230 exit(1);
3233 static const struct got_error *
3234 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3235 struct got_reference *ref)
3237 const struct got_error *err = NULL;
3238 const char *refname, *marker = " ";
3239 char *refstr;
3241 refname = got_ref_get_name(ref);
3242 if (worktree && strcmp(refname,
3243 got_worktree_get_head_ref_name(worktree)) == 0) {
3244 struct got_object_id *id = NULL;
3246 err = got_ref_resolve(&id, repo, ref);
3247 if (err)
3248 return err;
3249 if (got_object_id_cmp(id,
3250 got_worktree_get_base_commit_id(worktree)) == 0)
3251 marker = "* ";
3252 else
3253 marker = "~ ";
3254 free(id);
3257 if (strncmp(refname, "refs/heads/", 11) == 0)
3258 refname += 11;
3259 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3260 refname += 18;
3262 refstr = got_ref_to_str(ref);
3263 if (refstr == NULL)
3264 return got_error_from_errno("got_ref_to_str");
3266 printf("%s%s: %s\n", marker, refname, refstr);
3267 free(refstr);
3268 return NULL;
3271 static const struct got_error *
3272 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3274 const char *refname;
3276 if (worktree == NULL)
3277 return got_error(GOT_ERR_NOT_WORKTREE);
3279 refname = got_worktree_get_head_ref_name(worktree);
3281 if (strncmp(refname, "refs/heads/", 11) == 0)
3282 refname += 11;
3283 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3284 refname += 18;
3286 printf("%s\n", refname);
3288 return NULL;
3291 static const struct got_error *
3292 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3294 static const struct got_error *err = NULL;
3295 struct got_reflist_head refs;
3296 struct got_reflist_entry *re;
3297 struct got_reference *temp_ref = NULL;
3298 int rebase_in_progress, histedit_in_progress;
3300 SIMPLEQ_INIT(&refs);
3302 if (worktree) {
3303 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3304 worktree);
3305 if (err)
3306 return err;
3308 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3309 worktree);
3310 if (err)
3311 return err;
3313 if (rebase_in_progress || histedit_in_progress) {
3314 err = got_ref_open(&temp_ref, repo,
3315 got_worktree_get_head_ref_name(worktree), 0);
3316 if (err)
3317 return err;
3318 list_branch(repo, worktree, temp_ref);
3319 got_ref_close(temp_ref);
3323 err = got_ref_list(&refs, repo, "refs/heads",
3324 got_ref_cmp_by_name, NULL);
3325 if (err)
3326 return err;
3328 SIMPLEQ_FOREACH(re, &refs, entry)
3329 list_branch(repo, worktree, re->ref);
3331 got_ref_list_free(&refs);
3332 return NULL;
3335 static const struct got_error *
3336 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3337 const char *branch_name)
3339 const struct got_error *err = NULL;
3340 struct got_reference *ref = NULL;
3341 char *refname;
3343 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3344 return got_error_from_errno("asprintf");
3346 err = got_ref_open(&ref, repo, refname, 0);
3347 if (err)
3348 goto done;
3350 if (worktree &&
3351 strcmp(got_worktree_get_head_ref_name(worktree),
3352 got_ref_get_name(ref)) == 0) {
3353 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3354 "will not delete this work tree's current branch");
3355 goto done;
3358 err = got_ref_delete(ref, repo);
3359 done:
3360 if (ref)
3361 got_ref_close(ref);
3362 free(refname);
3363 return err;
3366 static const struct got_error *
3367 add_branch(struct got_repository *repo, const char *branch_name,
3368 const char *base_branch)
3370 const struct got_error *err = NULL;
3371 struct got_object_id *id = NULL;
3372 char *label;
3373 struct got_reference *ref = NULL;
3374 char *base_refname = NULL, *refname = NULL;
3377 * Don't let the user create a branch named '-'.
3378 * While technically a valid reference name, this case is usually
3379 * an unintended typo.
3381 if (branch_name[0] == '-' && branch_name[1] == '\0')
3382 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3384 err = match_object_id(&id, &label, base_branch,
3385 GOT_OBJ_TYPE_COMMIT, 1, repo);
3386 if (err)
3387 return err;
3389 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3390 err = got_error_from_errno("asprintf");
3391 goto done;
3394 err = got_ref_open(&ref, repo, refname, 0);
3395 if (err == NULL) {
3396 err = got_error(GOT_ERR_BRANCH_EXISTS);
3397 goto done;
3398 } else if (err->code != GOT_ERR_NOT_REF)
3399 goto done;
3401 err = got_ref_alloc(&ref, refname, id);
3402 if (err)
3403 goto done;
3405 err = got_ref_write(ref, repo);
3406 done:
3407 if (ref)
3408 got_ref_close(ref);
3409 free(id);
3410 free(base_refname);
3411 free(refname);
3412 return err;
3415 static const struct got_error *
3416 cmd_branch(int argc, char *argv[])
3418 const struct got_error *error = NULL;
3419 struct got_repository *repo = NULL;
3420 struct got_worktree *worktree = NULL;
3421 char *cwd = NULL, *repo_path = NULL;
3422 int ch, do_list = 0, do_show = 0;
3423 const char *delref = NULL;
3425 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3426 switch (ch) {
3427 case 'd':
3428 delref = optarg;
3429 break;
3430 case 'r':
3431 repo_path = realpath(optarg, NULL);
3432 if (repo_path == NULL)
3433 err(1, "-r option");
3434 got_path_strip_trailing_slashes(repo_path);
3435 break;
3436 case 'l':
3437 do_list = 1;
3438 break;
3439 default:
3440 usage_branch();
3441 /* NOTREACHED */
3445 if (do_list && delref)
3446 errx(1, "-l and -d options are mutually exclusive\n");
3448 argc -= optind;
3449 argv += optind;
3451 if (!do_list && !delref && argc == 0)
3452 do_show = 1;
3454 if (do_list || delref) {
3455 if (argc > 0)
3456 usage_branch();
3457 } else if (!do_show && (argc < 1 || argc > 2))
3458 usage_branch();
3460 #ifndef PROFILE
3461 if (do_list || do_show) {
3462 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3463 NULL) == -1)
3464 err(1, "pledge");
3465 } else {
3466 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3467 "sendfd unveil", NULL) == -1)
3468 err(1, "pledge");
3470 #endif
3471 cwd = getcwd(NULL, 0);
3472 if (cwd == NULL) {
3473 error = got_error_from_errno("getcwd");
3474 goto done;
3477 if (repo_path == NULL) {
3478 error = got_worktree_open(&worktree, cwd);
3479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3480 goto done;
3481 else
3482 error = NULL;
3483 if (worktree) {
3484 repo_path =
3485 strdup(got_worktree_get_repo_path(worktree));
3486 if (repo_path == NULL)
3487 error = got_error_from_errno("strdup");
3488 if (error)
3489 goto done;
3490 } else {
3491 repo_path = strdup(cwd);
3492 if (repo_path == NULL) {
3493 error = got_error_from_errno("strdup");
3494 goto done;
3499 error = got_repo_open(&repo, repo_path, NULL);
3500 if (error != NULL)
3501 goto done;
3503 error = apply_unveil(got_repo_get_path(repo), do_list,
3504 worktree ? got_worktree_get_root_path(worktree) : NULL);
3505 if (error)
3506 goto done;
3508 if (do_show)
3509 error = show_current_branch(repo, worktree);
3510 else if (do_list)
3511 error = list_branches(repo, worktree);
3512 else if (delref)
3513 error = delete_branch(repo, worktree, delref);
3514 else {
3515 const char *base_branch;
3516 if (argc == 1) {
3517 base_branch = worktree ?
3518 got_worktree_get_head_ref_name(worktree) :
3519 GOT_REF_HEAD;
3520 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3521 base_branch += 11;
3522 } else
3523 base_branch = argv[1];
3524 error = add_branch(repo, argv[0], base_branch);
3526 done:
3527 if (repo)
3528 got_repo_close(repo);
3529 if (worktree)
3530 got_worktree_close(worktree);
3531 free(cwd);
3532 free(repo_path);
3533 return error;
3537 __dead static void
3538 usage_tag(void)
3540 fprintf(stderr,
3541 "usage: %s tag [-r repository] | -l | "
3542 "[-m message] name [commit]\n", getprogname());
3543 exit(1);
3546 #if 0
3547 static const struct got_error *
3548 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3550 const struct got_error *err = NULL;
3551 struct got_reflist_entry *re, *se, *new;
3552 struct got_object_id *re_id, *se_id;
3553 struct got_tag_object *re_tag, *se_tag;
3554 time_t re_time, se_time;
3556 SIMPLEQ_FOREACH(re, tags, entry) {
3557 se = SIMPLEQ_FIRST(sorted);
3558 if (se == NULL) {
3559 err = got_reflist_entry_dup(&new, re);
3560 if (err)
3561 return err;
3562 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3563 continue;
3564 } else {
3565 err = got_ref_resolve(&re_id, repo, re->ref);
3566 if (err)
3567 break;
3568 err = got_object_open_as_tag(&re_tag, repo, re_id);
3569 free(re_id);
3570 if (err)
3571 break;
3572 re_time = got_object_tag_get_tagger_time(re_tag);
3573 got_object_tag_close(re_tag);
3576 while (se) {
3577 err = got_ref_resolve(&se_id, repo, re->ref);
3578 if (err)
3579 break;
3580 err = got_object_open_as_tag(&se_tag, repo, se_id);
3581 free(se_id);
3582 if (err)
3583 break;
3584 se_time = got_object_tag_get_tagger_time(se_tag);
3585 got_object_tag_close(se_tag);
3587 if (se_time > re_time) {
3588 err = got_reflist_entry_dup(&new, re);
3589 if (err)
3590 return err;
3591 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3592 break;
3594 se = SIMPLEQ_NEXT(se, entry);
3595 continue;
3598 done:
3599 return err;
3601 #endif
3603 static const struct got_error *
3604 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3605 struct got_reference *ref2)
3607 const struct got_error *err = NULL;
3608 struct got_repository *repo = arg;
3609 struct got_object_id *id1, *id2 = NULL;
3610 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3611 time_t time1, time2;
3613 *cmp = 0;
3615 err = got_ref_resolve(&id1, repo, ref1);
3616 if (err)
3617 return err;
3618 err = got_object_open_as_tag(&tag1, repo, id1);
3619 if (err)
3620 goto done;
3622 err = got_ref_resolve(&id2, repo, ref2);
3623 if (err)
3624 goto done;
3625 err = got_object_open_as_tag(&tag2, repo, id2);
3626 if (err)
3627 goto done;
3629 time1 = got_object_tag_get_tagger_time(tag1);
3630 time2 = got_object_tag_get_tagger_time(tag2);
3632 /* Put latest tags first. */
3633 if (time1 < time2)
3634 *cmp = 1;
3635 else if (time1 > time2)
3636 *cmp = -1;
3637 else
3638 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3639 done:
3640 free(id1);
3641 free(id2);
3642 if (tag1)
3643 got_object_tag_close(tag1);
3644 if (tag2)
3645 got_object_tag_close(tag2);
3646 return err;
3649 static const struct got_error *
3650 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3652 static const struct got_error *err = NULL;
3653 struct got_reflist_head refs;
3654 struct got_reflist_entry *re;
3656 SIMPLEQ_INIT(&refs);
3658 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3659 if (err)
3660 return err;
3662 SIMPLEQ_FOREACH(re, &refs, entry) {
3663 const char *refname;
3664 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3665 char datebuf[26];
3666 time_t tagger_time;
3667 struct got_object_id *id;
3668 struct got_tag_object *tag;
3670 refname = got_ref_get_name(re->ref);
3671 if (strncmp(refname, "refs/tags/", 10) != 0)
3672 continue;
3673 refname += 10;
3674 refstr = got_ref_to_str(re->ref);
3675 if (refstr == NULL) {
3676 err = got_error_from_errno("got_ref_to_str");
3677 break;
3679 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3680 free(refstr);
3682 err = got_ref_resolve(&id, repo, re->ref);
3683 if (err)
3684 break;
3685 err = got_object_open_as_tag(&tag, repo, id);
3686 free(id);
3687 if (err)
3688 break;
3689 printf("from: %s\n", got_object_tag_get_tagger(tag));
3690 tagger_time = got_object_tag_get_tagger_time(tag);
3691 datestr = get_datestr(&tagger_time, datebuf);
3692 if (datestr)
3693 printf("date: %s UTC\n", datestr);
3694 err = got_object_id_str(&id_str,
3695 got_object_tag_get_object_id(tag));
3696 if (err)
3697 break;
3698 switch (got_object_tag_get_object_type(tag)) {
3699 case GOT_OBJ_TYPE_BLOB:
3700 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3701 break;
3702 case GOT_OBJ_TYPE_TREE:
3703 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3704 break;
3705 case GOT_OBJ_TYPE_COMMIT:
3706 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3707 break;
3708 case GOT_OBJ_TYPE_TAG:
3709 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3710 break;
3711 default:
3712 break;
3714 free(id_str);
3715 tagmsg0 = strdup(got_object_tag_get_message(tag));
3716 got_object_tag_close(tag);
3717 if (tagmsg0 == NULL) {
3718 err = got_error_from_errno("strdup");
3719 break;
3722 tagmsg = tagmsg0;
3723 do {
3724 line = strsep(&tagmsg, "\n");
3725 if (line)
3726 printf(" %s\n", line);
3727 } while (line);
3728 free(tagmsg0);
3731 got_ref_list_free(&refs);
3732 return NULL;
3735 static const struct got_error *
3736 get_tag_message(char **tagmsg, const char *commit_id_str,
3737 const char *tag_name, const char *repo_path)
3739 const struct got_error *err = NULL;
3740 char *template = NULL, *initial_content = NULL;
3741 char *tagmsg_path = NULL, *editor = NULL;
3742 int fd = -1;
3744 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3745 err = got_error_from_errno("asprintf");
3746 goto done;
3749 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3750 commit_id_str, tag_name) == -1) {
3751 err = got_error_from_errno("asprintf");
3752 goto done;
3755 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3756 if (err)
3757 goto done;
3759 dprintf(fd, initial_content);
3760 close(fd);
3762 err = get_editor(&editor);
3763 if (err)
3764 goto done;
3765 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3766 done:
3767 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3768 unlink(tagmsg_path);
3769 free(tagmsg_path);
3770 tagmsg_path = NULL;
3772 free(initial_content);
3773 free(template);
3774 free(editor);
3776 /* Editor is done; we can now apply unveil(2) */
3777 if (err == NULL) {
3778 err = apply_unveil(repo_path, 0, NULL);
3779 if (err) {
3780 free(*tagmsg);
3781 *tagmsg = NULL;
3784 return err;
3787 static const struct got_error *
3788 add_tag(struct got_repository *repo, const char *tag_name,
3789 const char *commit_arg, const char *tagmsg_arg)
3791 const struct got_error *err = NULL;
3792 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3793 char *label = NULL, *commit_id_str = NULL;
3794 struct got_reference *ref = NULL;
3795 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3798 * Don't let the user create a tag named '-'.
3799 * While technically a valid reference name, this case is usually
3800 * an unintended typo.
3802 if (tag_name[0] == '-' && tag_name[1] == '\0')
3803 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3805 err = get_author(&tagger, repo);
3806 if (err)
3807 return err;
3809 err = match_object_id(&commit_id, &label, commit_arg,
3810 GOT_OBJ_TYPE_COMMIT, 1, repo);
3811 if (err)
3812 goto done;
3814 err = got_object_id_str(&commit_id_str, commit_id);
3815 if (err)
3816 goto done;
3818 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3819 refname = strdup(tag_name);
3820 if (refname == NULL) {
3821 err = got_error_from_errno("strdup");
3822 goto done;
3824 tag_name += 10;
3825 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3826 err = got_error_from_errno("asprintf");
3827 goto done;
3830 err = got_ref_open(&ref, repo, refname, 0);
3831 if (err == NULL) {
3832 err = got_error(GOT_ERR_TAG_EXISTS);
3833 goto done;
3834 } else if (err->code != GOT_ERR_NOT_REF)
3835 goto done;
3837 if (tagmsg_arg == NULL) {
3838 err = get_tag_message(&tagmsg, commit_id_str,
3839 tag_name, got_repo_get_path(repo));
3840 if (err)
3841 goto done;
3844 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3845 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3846 if (err)
3847 goto done;
3849 err = got_ref_alloc(&ref, refname, tag_id);
3850 if (err)
3851 goto done;
3853 err = got_ref_write(ref, repo);
3855 if (err == NULL) {
3856 char *tag_id_str;
3857 err = got_object_id_str(&tag_id_str, tag_id);
3858 printf("Created tag %s\n", tag_id_str);
3859 free(tag_id_str);
3861 done:
3862 if (ref)
3863 got_ref_close(ref);
3864 free(commit_id);
3865 free(commit_id_str);
3866 free(refname);
3867 free(tagmsg);
3868 free(tagger);
3869 return err;
3872 static const struct got_error *
3873 cmd_tag(int argc, char *argv[])
3875 const struct got_error *error = NULL;
3876 struct got_repository *repo = NULL;
3877 struct got_worktree *worktree = NULL;
3878 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3879 char *gitconfig_path = NULL;
3880 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3881 int ch, do_list = 0;
3883 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3884 switch (ch) {
3885 case 'm':
3886 tagmsg = optarg;
3887 break;
3888 case 'r':
3889 repo_path = realpath(optarg, NULL);
3890 if (repo_path == NULL)
3891 err(1, "-r option");
3892 got_path_strip_trailing_slashes(repo_path);
3893 break;
3894 case 'l':
3895 do_list = 1;
3896 break;
3897 default:
3898 usage_tag();
3899 /* NOTREACHED */
3903 argc -= optind;
3904 argv += optind;
3906 if (do_list) {
3907 if (tagmsg)
3908 errx(1, "-l and -m options are mutually exclusive\n");
3909 if (argc > 0)
3910 usage_tag();
3911 } else if (argc < 1 || argc > 2)
3912 usage_tag();
3913 else if (argc > 1)
3914 commit_id_arg = argv[1];
3915 tag_name = argv[0];
3917 #ifndef PROFILE
3918 if (do_list) {
3919 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3920 NULL) == -1)
3921 err(1, "pledge");
3922 } else {
3923 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3924 "sendfd unveil", NULL) == -1)
3925 err(1, "pledge");
3927 #endif
3928 cwd = getcwd(NULL, 0);
3929 if (cwd == NULL) {
3930 error = got_error_from_errno("getcwd");
3931 goto done;
3934 if (repo_path == NULL) {
3935 error = got_worktree_open(&worktree, cwd);
3936 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3937 goto done;
3938 else
3939 error = NULL;
3940 if (worktree) {
3941 repo_path =
3942 strdup(got_worktree_get_repo_path(worktree));
3943 if (repo_path == NULL)
3944 error = got_error_from_errno("strdup");
3945 if (error)
3946 goto done;
3947 } else {
3948 repo_path = strdup(cwd);
3949 if (repo_path == NULL) {
3950 error = got_error_from_errno("strdup");
3951 goto done;
3956 if (do_list) {
3957 error = got_repo_open(&repo, repo_path, NULL);
3958 if (error != NULL)
3959 goto done;
3960 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3961 if (error)
3962 goto done;
3963 error = list_tags(repo, worktree);
3964 } else {
3965 error = get_gitconfig_path(&gitconfig_path);
3966 if (error)
3967 goto done;
3968 error = got_repo_open(&repo, repo_path, gitconfig_path);
3969 if (error != NULL)
3970 goto done;
3972 if (tagmsg) {
3973 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3974 if (error)
3975 goto done;
3978 if (commit_id_arg == NULL) {
3979 struct got_reference *head_ref;
3980 struct got_object_id *commit_id;
3981 error = got_ref_open(&head_ref, repo,
3982 worktree ? got_worktree_get_head_ref_name(worktree)
3983 : GOT_REF_HEAD, 0);
3984 if (error)
3985 goto done;
3986 error = got_ref_resolve(&commit_id, repo, head_ref);
3987 got_ref_close(head_ref);
3988 if (error)
3989 goto done;
3990 error = got_object_id_str(&commit_id_str, commit_id);
3991 free(commit_id);
3992 if (error)
3993 goto done;
3996 error = add_tag(repo, tag_name,
3997 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3999 done:
4000 if (repo)
4001 got_repo_close(repo);
4002 if (worktree)
4003 got_worktree_close(worktree);
4004 free(cwd);
4005 free(repo_path);
4006 free(gitconfig_path);
4007 free(commit_id_str);
4008 return error;
4011 __dead static void
4012 usage_add(void)
4014 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4015 exit(1);
4018 static const struct got_error *
4019 cmd_add(int argc, char *argv[])
4021 const struct got_error *error = NULL;
4022 struct got_repository *repo = NULL;
4023 struct got_worktree *worktree = NULL;
4024 char *cwd = NULL;
4025 struct got_pathlist_head paths;
4026 struct got_pathlist_entry *pe;
4027 int ch;
4029 TAILQ_INIT(&paths);
4031 while ((ch = getopt(argc, argv, "")) != -1) {
4032 switch (ch) {
4033 default:
4034 usage_add();
4035 /* NOTREACHED */
4039 argc -= optind;
4040 argv += optind;
4042 #ifndef PROFILE
4043 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4044 NULL) == -1)
4045 err(1, "pledge");
4046 #endif
4047 if (argc < 1)
4048 usage_add();
4050 cwd = getcwd(NULL, 0);
4051 if (cwd == NULL) {
4052 error = got_error_from_errno("getcwd");
4053 goto done;
4056 error = got_worktree_open(&worktree, cwd);
4057 if (error)
4058 goto done;
4060 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4061 NULL);
4062 if (error != NULL)
4063 goto done;
4065 error = apply_unveil(got_repo_get_path(repo), 1,
4066 got_worktree_get_root_path(worktree));
4067 if (error)
4068 goto done;
4070 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4071 if (error)
4072 goto done;
4074 error = got_worktree_schedule_add(worktree, &paths, print_status,
4075 NULL, repo);
4076 done:
4077 if (repo)
4078 got_repo_close(repo);
4079 if (worktree)
4080 got_worktree_close(worktree);
4081 TAILQ_FOREACH(pe, &paths, entry)
4082 free((char *)pe->path);
4083 got_pathlist_free(&paths);
4084 free(cwd);
4085 return error;
4088 __dead static void
4089 usage_remove(void)
4091 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4092 exit(1);
4095 static const struct got_error *
4096 print_remove_status(void *arg, unsigned char status,
4097 unsigned char staged_status, const char *path,
4098 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4099 struct got_object_id *commit_id)
4101 if (status == GOT_STATUS_NONEXISTENT)
4102 return NULL;
4103 if (status == staged_status && (status == GOT_STATUS_DELETE))
4104 status = GOT_STATUS_NO_CHANGE;
4105 printf("%c%c %s\n", status, staged_status, path);
4106 return NULL;
4109 static const struct got_error *
4110 cmd_remove(int argc, char *argv[])
4112 const struct got_error *error = NULL;
4113 struct got_worktree *worktree = NULL;
4114 struct got_repository *repo = NULL;
4115 char *cwd = NULL;
4116 struct got_pathlist_head paths;
4117 struct got_pathlist_entry *pe;
4118 int ch, delete_local_mods = 0;
4120 TAILQ_INIT(&paths);
4122 while ((ch = getopt(argc, argv, "f")) != -1) {
4123 switch (ch) {
4124 case 'f':
4125 delete_local_mods = 1;
4126 break;
4127 default:
4128 usage_add();
4129 /* NOTREACHED */
4133 argc -= optind;
4134 argv += optind;
4136 #ifndef PROFILE
4137 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4138 NULL) == -1)
4139 err(1, "pledge");
4140 #endif
4141 if (argc < 1)
4142 usage_remove();
4144 cwd = getcwd(NULL, 0);
4145 if (cwd == NULL) {
4146 error = got_error_from_errno("getcwd");
4147 goto done;
4149 error = got_worktree_open(&worktree, cwd);
4150 if (error)
4151 goto done;
4153 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4154 NULL);
4155 if (error)
4156 goto done;
4158 error = apply_unveil(got_repo_get_path(repo), 1,
4159 got_worktree_get_root_path(worktree));
4160 if (error)
4161 goto done;
4163 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4164 if (error)
4165 goto done;
4167 error = got_worktree_schedule_delete(worktree, &paths,
4168 delete_local_mods, print_remove_status, NULL, repo);
4169 if (error)
4170 goto done;
4171 done:
4172 if (repo)
4173 got_repo_close(repo);
4174 if (worktree)
4175 got_worktree_close(worktree);
4176 TAILQ_FOREACH(pe, &paths, entry)
4177 free((char *)pe->path);
4178 got_pathlist_free(&paths);
4179 free(cwd);
4180 return error;
4183 __dead static void
4184 usage_revert(void)
4186 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4187 "path ...\n", getprogname());
4188 exit(1);
4191 static const struct got_error *
4192 revert_progress(void *arg, unsigned char status, const char *path)
4194 while (path[0] == '/')
4195 path++;
4196 printf("%c %s\n", status, path);
4197 return NULL;
4200 struct choose_patch_arg {
4201 FILE *patch_script_file;
4202 const char *action;
4205 static const struct got_error *
4206 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4207 int nchanges, const char *action)
4209 char *line = NULL;
4210 size_t linesize = 0;
4211 ssize_t linelen;
4213 switch (status) {
4214 case GOT_STATUS_ADD:
4215 printf("A %s\n%s this addition? [y/n] ", path, action);
4216 break;
4217 case GOT_STATUS_DELETE:
4218 printf("D %s\n%s this deletion? [y/n] ", path, action);
4219 break;
4220 case GOT_STATUS_MODIFY:
4221 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4222 return got_error_from_errno("fseek");
4223 printf(GOT_COMMIT_SEP_STR);
4224 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4225 printf("%s", line);
4226 if (ferror(patch_file))
4227 return got_error_from_errno("getline");
4228 printf(GOT_COMMIT_SEP_STR);
4229 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4230 path, n, nchanges, action);
4231 break;
4232 default:
4233 return got_error_path(path, GOT_ERR_FILE_STATUS);
4236 return NULL;
4239 static const struct got_error *
4240 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4241 FILE *patch_file, int n, int nchanges)
4243 const struct got_error *err = NULL;
4244 char *line = NULL;
4245 size_t linesize = 0;
4246 ssize_t linelen;
4247 int resp = ' ';
4248 struct choose_patch_arg *a = arg;
4250 *choice = GOT_PATCH_CHOICE_NONE;
4252 if (a->patch_script_file) {
4253 char *nl;
4254 err = show_change(status, path, patch_file, n, nchanges,
4255 a->action);
4256 if (err)
4257 return err;
4258 linelen = getline(&line, &linesize, a->patch_script_file);
4259 if (linelen == -1) {
4260 if (ferror(a->patch_script_file))
4261 return got_error_from_errno("getline");
4262 return NULL;
4264 nl = strchr(line, '\n');
4265 if (nl)
4266 *nl = '\0';
4267 if (strcmp(line, "y") == 0) {
4268 *choice = GOT_PATCH_CHOICE_YES;
4269 printf("y\n");
4270 } else if (strcmp(line, "n") == 0) {
4271 *choice = GOT_PATCH_CHOICE_NO;
4272 printf("n\n");
4273 } else if (strcmp(line, "q") == 0 &&
4274 status == GOT_STATUS_MODIFY) {
4275 *choice = GOT_PATCH_CHOICE_QUIT;
4276 printf("q\n");
4277 } else
4278 printf("invalid response '%s'\n", line);
4279 free(line);
4280 return NULL;
4283 while (resp != 'y' && resp != 'n' && resp != 'q') {
4284 err = show_change(status, path, patch_file, n, nchanges,
4285 a->action);
4286 if (err)
4287 return err;
4288 resp = getchar();
4289 if (resp == '\n')
4290 resp = getchar();
4291 if (status == GOT_STATUS_MODIFY) {
4292 if (resp != 'y' && resp != 'n' && resp != 'q') {
4293 printf("invalid response '%c'\n", resp);
4294 resp = ' ';
4296 } else if (resp != 'y' && resp != 'n') {
4297 printf("invalid response '%c'\n", resp);
4298 resp = ' ';
4302 if (resp == 'y')
4303 *choice = GOT_PATCH_CHOICE_YES;
4304 else if (resp == 'n')
4305 *choice = GOT_PATCH_CHOICE_NO;
4306 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4307 *choice = GOT_PATCH_CHOICE_QUIT;
4309 return NULL;
4313 static const struct got_error *
4314 cmd_revert(int argc, char *argv[])
4316 const struct got_error *error = NULL;
4317 struct got_worktree *worktree = NULL;
4318 struct got_repository *repo = NULL;
4319 char *cwd = NULL, *path = NULL;
4320 struct got_pathlist_head paths;
4321 struct got_pathlist_entry *pe;
4322 int ch, can_recurse = 0, pflag = 0;
4323 FILE *patch_script_file = NULL;
4324 const char *patch_script_path = NULL;
4325 struct choose_patch_arg cpa;
4327 TAILQ_INIT(&paths);
4329 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4330 switch (ch) {
4331 case 'p':
4332 pflag = 1;
4333 break;
4334 case 'F':
4335 patch_script_path = optarg;
4336 break;
4337 case 'R':
4338 can_recurse = 1;
4339 break;
4340 default:
4341 usage_revert();
4342 /* NOTREACHED */
4346 argc -= optind;
4347 argv += optind;
4349 #ifndef PROFILE
4350 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4351 "unveil", NULL) == -1)
4352 err(1, "pledge");
4353 #endif
4354 if (argc < 1)
4355 usage_revert();
4356 if (patch_script_path && !pflag)
4357 errx(1, "-F option can only be used together with -p option");
4359 cwd = getcwd(NULL, 0);
4360 if (cwd == NULL) {
4361 error = got_error_from_errno("getcwd");
4362 goto done;
4364 error = got_worktree_open(&worktree, cwd);
4365 if (error)
4366 goto done;
4368 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4369 NULL);
4370 if (error != NULL)
4371 goto done;
4373 if (patch_script_path) {
4374 patch_script_file = fopen(patch_script_path, "r");
4375 if (patch_script_file == NULL) {
4376 error = got_error_from_errno2("fopen",
4377 patch_script_path);
4378 goto done;
4381 error = apply_unveil(got_repo_get_path(repo), 1,
4382 got_worktree_get_root_path(worktree));
4383 if (error)
4384 goto done;
4386 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4387 if (error)
4388 goto done;
4390 if (!can_recurse) {
4391 char *ondisk_path;
4392 struct stat sb;
4393 TAILQ_FOREACH(pe, &paths, entry) {
4394 if (asprintf(&ondisk_path, "%s/%s",
4395 got_worktree_get_root_path(worktree),
4396 pe->path) == -1) {
4397 error = got_error_from_errno("asprintf");
4398 goto done;
4400 if (lstat(ondisk_path, &sb) == -1) {
4401 if (errno == ENOENT) {
4402 free(ondisk_path);
4403 continue;
4405 error = got_error_from_errno2("lstat",
4406 ondisk_path);
4407 free(ondisk_path);
4408 goto done;
4410 free(ondisk_path);
4411 if (S_ISDIR(sb.st_mode)) {
4412 error = got_error_msg(GOT_ERR_BAD_PATH,
4413 "reverting directories requires -R option");
4414 goto done;
4419 cpa.patch_script_file = patch_script_file;
4420 cpa.action = "revert";
4421 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4422 pflag ? choose_patch : NULL, &cpa, repo);
4423 if (error)
4424 goto done;
4425 done:
4426 if (patch_script_file && fclose(patch_script_file) == EOF &&
4427 error == NULL)
4428 error = got_error_from_errno2("fclose", patch_script_path);
4429 if (repo)
4430 got_repo_close(repo);
4431 if (worktree)
4432 got_worktree_close(worktree);
4433 free(path);
4434 free(cwd);
4435 return error;
4438 __dead static void
4439 usage_commit(void)
4441 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4442 getprogname());
4443 exit(1);
4446 struct collect_commit_logmsg_arg {
4447 const char *cmdline_log;
4448 const char *editor;
4449 const char *worktree_path;
4450 const char *branch_name;
4451 const char *repo_path;
4452 char *logmsg_path;
4456 static const struct got_error *
4457 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4458 void *arg)
4460 char *initial_content = NULL;
4461 struct got_pathlist_entry *pe;
4462 const struct got_error *err = NULL;
4463 char *template = NULL;
4464 struct collect_commit_logmsg_arg *a = arg;
4465 int fd;
4466 size_t len;
4468 /* if a message was specified on the command line, just use it */
4469 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4470 len = strlen(a->cmdline_log) + 1;
4471 *logmsg = malloc(len + 1);
4472 if (*logmsg == NULL)
4473 return got_error_from_errno("malloc");
4474 strlcpy(*logmsg, a->cmdline_log, len);
4475 return NULL;
4478 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4479 return got_error_from_errno("asprintf");
4481 if (asprintf(&initial_content,
4482 "\n# changes to be committed on branch %s:\n",
4483 a->branch_name) == -1)
4484 return got_error_from_errno("asprintf");
4486 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4487 if (err)
4488 goto done;
4490 dprintf(fd, initial_content);
4492 TAILQ_FOREACH(pe, commitable_paths, entry) {
4493 struct got_commitable *ct = pe->data;
4494 dprintf(fd, "# %c %s\n",
4495 got_commitable_get_status(ct),
4496 got_commitable_get_path(ct));
4498 close(fd);
4500 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4501 done:
4502 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4503 unlink(a->logmsg_path);
4504 free(a->logmsg_path);
4505 a->logmsg_path = NULL;
4507 free(initial_content);
4508 free(template);
4510 /* Editor is done; we can now apply unveil(2) */
4511 if (err == NULL) {
4512 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4513 if (err) {
4514 free(*logmsg);
4515 *logmsg = NULL;
4518 return err;
4521 static const struct got_error *
4522 cmd_commit(int argc, char *argv[])
4524 const struct got_error *error = NULL;
4525 struct got_worktree *worktree = NULL;
4526 struct got_repository *repo = NULL;
4527 char *cwd = NULL, *id_str = NULL;
4528 struct got_object_id *id = NULL;
4529 const char *logmsg = NULL;
4530 struct collect_commit_logmsg_arg cl_arg;
4531 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4532 int ch, rebase_in_progress, histedit_in_progress;
4533 struct got_pathlist_head paths;
4535 TAILQ_INIT(&paths);
4536 cl_arg.logmsg_path = NULL;
4538 while ((ch = getopt(argc, argv, "m:")) != -1) {
4539 switch (ch) {
4540 case 'm':
4541 logmsg = optarg;
4542 break;
4543 default:
4544 usage_commit();
4545 /* NOTREACHED */
4549 argc -= optind;
4550 argv += optind;
4552 #ifndef PROFILE
4553 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4554 "unveil", NULL) == -1)
4555 err(1, "pledge");
4556 #endif
4557 cwd = getcwd(NULL, 0);
4558 if (cwd == NULL) {
4559 error = got_error_from_errno("getcwd");
4560 goto done;
4562 error = got_worktree_open(&worktree, cwd);
4563 if (error)
4564 goto done;
4566 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4567 if (error)
4568 goto done;
4569 if (rebase_in_progress) {
4570 error = got_error(GOT_ERR_REBASING);
4571 goto done;
4574 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4575 worktree);
4576 if (error)
4577 goto done;
4579 error = get_gitconfig_path(&gitconfig_path);
4580 if (error)
4581 goto done;
4582 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4583 gitconfig_path);
4584 if (error != NULL)
4585 goto done;
4587 error = get_author(&author, repo);
4588 if (error)
4589 return error;
4592 * unveil(2) traverses exec(2); if an editor is used we have
4593 * to apply unveil after the log message has been written.
4595 if (logmsg == NULL || strlen(logmsg) == 0)
4596 error = get_editor(&editor);
4597 else
4598 error = apply_unveil(got_repo_get_path(repo), 0,
4599 got_worktree_get_root_path(worktree));
4600 if (error)
4601 goto done;
4603 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4604 if (error)
4605 goto done;
4607 cl_arg.editor = editor;
4608 cl_arg.cmdline_log = logmsg;
4609 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4610 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4611 if (!histedit_in_progress) {
4612 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4613 error = got_error(GOT_ERR_COMMIT_BRANCH);
4614 goto done;
4616 cl_arg.branch_name += 11;
4618 cl_arg.repo_path = got_repo_get_path(repo);
4619 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4620 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4621 if (error) {
4622 if (cl_arg.logmsg_path)
4623 fprintf(stderr, "%s: log message preserved in %s\n",
4624 getprogname(), cl_arg.logmsg_path);
4625 goto done;
4628 if (cl_arg.logmsg_path)
4629 unlink(cl_arg.logmsg_path);
4631 error = got_object_id_str(&id_str, id);
4632 if (error)
4633 goto done;
4634 printf("Created commit %s\n", id_str);
4635 done:
4636 free(cl_arg.logmsg_path);
4637 if (repo)
4638 got_repo_close(repo);
4639 if (worktree)
4640 got_worktree_close(worktree);
4641 free(cwd);
4642 free(id_str);
4643 free(gitconfig_path);
4644 free(editor);
4645 free(author);
4646 return error;
4649 __dead static void
4650 usage_cherrypick(void)
4652 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4653 exit(1);
4656 static const struct got_error *
4657 cmd_cherrypick(int argc, char *argv[])
4659 const struct got_error *error = NULL;
4660 struct got_worktree *worktree = NULL;
4661 struct got_repository *repo = NULL;
4662 char *cwd = NULL, *commit_id_str = NULL;
4663 struct got_object_id *commit_id = NULL;
4664 struct got_commit_object *commit = NULL;
4665 struct got_object_qid *pid;
4666 struct got_reference *head_ref = NULL;
4667 int ch, did_something = 0;
4669 while ((ch = getopt(argc, argv, "")) != -1) {
4670 switch (ch) {
4671 default:
4672 usage_cherrypick();
4673 /* NOTREACHED */
4677 argc -= optind;
4678 argv += optind;
4680 #ifndef PROFILE
4681 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4682 "unveil", NULL) == -1)
4683 err(1, "pledge");
4684 #endif
4685 if (argc != 1)
4686 usage_cherrypick();
4688 cwd = getcwd(NULL, 0);
4689 if (cwd == NULL) {
4690 error = got_error_from_errno("getcwd");
4691 goto done;
4693 error = got_worktree_open(&worktree, cwd);
4694 if (error)
4695 goto done;
4697 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4698 NULL);
4699 if (error != NULL)
4700 goto done;
4702 error = apply_unveil(got_repo_get_path(repo), 0,
4703 got_worktree_get_root_path(worktree));
4704 if (error)
4705 goto done;
4707 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4708 GOT_OBJ_TYPE_COMMIT, repo);
4709 if (error != NULL) {
4710 struct got_reference *ref;
4711 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4712 goto done;
4713 error = got_ref_open(&ref, repo, argv[0], 0);
4714 if (error != NULL)
4715 goto done;
4716 error = got_ref_resolve(&commit_id, repo, ref);
4717 got_ref_close(ref);
4718 if (error != NULL)
4719 goto done;
4721 error = got_object_id_str(&commit_id_str, commit_id);
4722 if (error)
4723 goto done;
4725 error = got_ref_open(&head_ref, repo,
4726 got_worktree_get_head_ref_name(worktree), 0);
4727 if (error != NULL)
4728 goto done;
4730 error = check_same_branch(commit_id, head_ref, NULL, repo);
4731 if (error) {
4732 if (error->code != GOT_ERR_ANCESTRY)
4733 goto done;
4734 error = NULL;
4735 } else {
4736 error = got_error(GOT_ERR_SAME_BRANCH);
4737 goto done;
4740 error = got_object_open_as_commit(&commit, repo, commit_id);
4741 if (error)
4742 goto done;
4743 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4744 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4745 commit_id, repo, update_progress, &did_something, check_cancelled,
4746 NULL);
4747 if (error != NULL)
4748 goto done;
4750 if (did_something)
4751 printf("Merged commit %s\n", commit_id_str);
4752 done:
4753 if (commit)
4754 got_object_commit_close(commit);
4755 free(commit_id_str);
4756 if (head_ref)
4757 got_ref_close(head_ref);
4758 if (worktree)
4759 got_worktree_close(worktree);
4760 if (repo)
4761 got_repo_close(repo);
4762 return error;
4765 __dead static void
4766 usage_backout(void)
4768 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4769 exit(1);
4772 static const struct got_error *
4773 cmd_backout(int argc, char *argv[])
4775 const struct got_error *error = NULL;
4776 struct got_worktree *worktree = NULL;
4777 struct got_repository *repo = NULL;
4778 char *cwd = NULL, *commit_id_str = NULL;
4779 struct got_object_id *commit_id = NULL;
4780 struct got_commit_object *commit = NULL;
4781 struct got_object_qid *pid;
4782 struct got_reference *head_ref = NULL;
4783 int ch, did_something = 0;
4785 while ((ch = getopt(argc, argv, "")) != -1) {
4786 switch (ch) {
4787 default:
4788 usage_backout();
4789 /* NOTREACHED */
4793 argc -= optind;
4794 argv += optind;
4796 #ifndef PROFILE
4797 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4798 "unveil", NULL) == -1)
4799 err(1, "pledge");
4800 #endif
4801 if (argc != 1)
4802 usage_backout();
4804 cwd = getcwd(NULL, 0);
4805 if (cwd == NULL) {
4806 error = got_error_from_errno("getcwd");
4807 goto done;
4809 error = got_worktree_open(&worktree, cwd);
4810 if (error)
4811 goto done;
4813 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4814 NULL);
4815 if (error != NULL)
4816 goto done;
4818 error = apply_unveil(got_repo_get_path(repo), 0,
4819 got_worktree_get_root_path(worktree));
4820 if (error)
4821 goto done;
4823 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4824 GOT_OBJ_TYPE_COMMIT, repo);
4825 if (error != NULL) {
4826 struct got_reference *ref;
4827 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4828 goto done;
4829 error = got_ref_open(&ref, repo, argv[0], 0);
4830 if (error != NULL)
4831 goto done;
4832 error = got_ref_resolve(&commit_id, repo, ref);
4833 got_ref_close(ref);
4834 if (error != NULL)
4835 goto done;
4837 error = got_object_id_str(&commit_id_str, commit_id);
4838 if (error)
4839 goto done;
4841 error = got_ref_open(&head_ref, repo,
4842 got_worktree_get_head_ref_name(worktree), 0);
4843 if (error != NULL)
4844 goto done;
4846 error = check_same_branch(commit_id, head_ref, NULL, repo);
4847 if (error)
4848 goto done;
4850 error = got_object_open_as_commit(&commit, repo, commit_id);
4851 if (error)
4852 goto done;
4853 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4854 if (pid == NULL) {
4855 error = got_error(GOT_ERR_ROOT_COMMIT);
4856 goto done;
4859 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4860 update_progress, &did_something, check_cancelled, NULL);
4861 if (error != NULL)
4862 goto done;
4864 if (did_something)
4865 printf("Backed out commit %s\n", commit_id_str);
4866 done:
4867 if (commit)
4868 got_object_commit_close(commit);
4869 free(commit_id_str);
4870 if (head_ref)
4871 got_ref_close(head_ref);
4872 if (worktree)
4873 got_worktree_close(worktree);
4874 if (repo)
4875 got_repo_close(repo);
4876 return error;
4879 __dead static void
4880 usage_rebase(void)
4882 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4883 getprogname());
4884 exit(1);
4887 void
4888 trim_logmsg(char *logmsg, int limit)
4890 char *nl;
4891 size_t len;
4893 len = strlen(logmsg);
4894 if (len > limit)
4895 len = limit;
4896 logmsg[len] = '\0';
4897 nl = strchr(logmsg, '\n');
4898 if (nl)
4899 *nl = '\0';
4902 static const struct got_error *
4903 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4905 const struct got_error *err;
4906 char *logmsg0 = NULL;
4907 const char *s;
4909 err = got_object_commit_get_logmsg(&logmsg0, commit);
4910 if (err)
4911 return err;
4913 s = logmsg0;
4914 while (isspace((unsigned char)s[0]))
4915 s++;
4917 *logmsg = strdup(s);
4918 if (*logmsg == NULL) {
4919 err = got_error_from_errno("strdup");
4920 goto done;
4923 trim_logmsg(*logmsg, limit);
4924 done:
4925 free(logmsg0);
4926 return err;
4929 static const struct got_error *
4930 show_rebase_progress(struct got_commit_object *commit,
4931 struct got_object_id *old_id, struct got_object_id *new_id)
4933 const struct got_error *err;
4934 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4936 err = got_object_id_str(&old_id_str, old_id);
4937 if (err)
4938 goto done;
4940 if (new_id) {
4941 err = got_object_id_str(&new_id_str, new_id);
4942 if (err)
4943 goto done;
4946 old_id_str[12] = '\0';
4947 if (new_id_str)
4948 new_id_str[12] = '\0';
4950 err = get_short_logmsg(&logmsg, 42, commit);
4951 if (err)
4952 goto done;
4954 printf("%s -> %s: %s\n", old_id_str,
4955 new_id_str ? new_id_str : "no-op change", logmsg);
4956 done:
4957 free(old_id_str);
4958 free(new_id_str);
4959 return err;
4962 static const struct got_error *
4963 rebase_progress(void *arg, unsigned char status, const char *path)
4965 unsigned char *rebase_status = arg;
4967 while (path[0] == '/')
4968 path++;
4969 printf("%c %s\n", status, path);
4971 if (*rebase_status == GOT_STATUS_CONFLICT)
4972 return NULL;
4973 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4974 *rebase_status = status;
4975 return NULL;
4978 static const struct got_error *
4979 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4980 struct got_reference *branch, struct got_reference *new_base_branch,
4981 struct got_reference *tmp_branch, struct got_repository *repo)
4983 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4984 return got_worktree_rebase_complete(worktree, fileindex,
4985 new_base_branch, tmp_branch, branch, repo);
4988 static const struct got_error *
4989 rebase_commit(struct got_pathlist_head *merged_paths,
4990 struct got_worktree *worktree, struct got_fileindex *fileindex,
4991 struct got_reference *tmp_branch,
4992 struct got_object_id *commit_id, struct got_repository *repo)
4994 const struct got_error *error;
4995 struct got_commit_object *commit;
4996 struct got_object_id *new_commit_id;
4998 error = got_object_open_as_commit(&commit, repo, commit_id);
4999 if (error)
5000 return error;
5002 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5003 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5004 if (error) {
5005 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5006 goto done;
5007 error = show_rebase_progress(commit, commit_id, NULL);
5008 } else {
5009 error = show_rebase_progress(commit, commit_id, new_commit_id);
5010 free(new_commit_id);
5012 done:
5013 got_object_commit_close(commit);
5014 return error;
5017 struct check_path_prefix_arg {
5018 const char *path_prefix;
5019 size_t len;
5020 int errcode;
5023 static const struct got_error *
5024 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5025 struct got_blob_object *blob2, struct got_object_id *id1,
5026 struct got_object_id *id2, const char *path1, const char *path2,
5027 struct got_repository *repo)
5029 struct check_path_prefix_arg *a = arg;
5031 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5032 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5033 return got_error(a->errcode);
5035 return NULL;
5038 static const struct got_error *
5039 check_path_prefix(struct got_object_id *parent_id,
5040 struct got_object_id *commit_id, const char *path_prefix,
5041 int errcode, struct got_repository *repo)
5043 const struct got_error *err;
5044 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5045 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5046 struct check_path_prefix_arg cpp_arg;
5048 if (got_path_is_root_dir(path_prefix))
5049 return NULL;
5051 err = got_object_open_as_commit(&commit, repo, commit_id);
5052 if (err)
5053 goto done;
5055 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5056 if (err)
5057 goto done;
5059 err = got_object_open_as_tree(&tree1, repo,
5060 got_object_commit_get_tree_id(parent_commit));
5061 if (err)
5062 goto done;
5064 err = got_object_open_as_tree(&tree2, repo,
5065 got_object_commit_get_tree_id(commit));
5066 if (err)
5067 goto done;
5069 cpp_arg.path_prefix = path_prefix;
5070 while (cpp_arg.path_prefix[0] == '/')
5071 cpp_arg.path_prefix++;
5072 cpp_arg.len = strlen(cpp_arg.path_prefix);
5073 cpp_arg.errcode = errcode;
5074 err = got_diff_tree(tree1, tree2, "", "", repo,
5075 check_path_prefix_in_diff, &cpp_arg, 0);
5076 done:
5077 if (tree1)
5078 got_object_tree_close(tree1);
5079 if (tree2)
5080 got_object_tree_close(tree2);
5081 if (commit)
5082 got_object_commit_close(commit);
5083 if (parent_commit)
5084 got_object_commit_close(parent_commit);
5085 return err;
5088 static const struct got_error *
5089 collect_commits(struct got_object_id_queue *commits,
5090 struct got_object_id *initial_commit_id,
5091 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5092 const char *path_prefix, int path_prefix_errcode,
5093 struct got_repository *repo)
5095 const struct got_error *err = NULL;
5096 struct got_commit_graph *graph = NULL;
5097 struct got_object_id *parent_id = NULL;
5098 struct got_object_qid *qid;
5099 struct got_object_id *commit_id = initial_commit_id;
5101 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5102 if (err)
5103 return err;
5105 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5106 check_cancelled, NULL);
5107 if (err)
5108 goto done;
5109 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5110 err = got_commit_graph_iter_next(&parent_id, graph);
5111 if (err) {
5112 if (err->code == GOT_ERR_ITER_COMPLETED) {
5113 err = got_error_msg(GOT_ERR_ANCESTRY,
5114 "ran out of commits to rebase before "
5115 "youngest common ancestor commit has "
5116 "been reached?!?");
5117 goto done;
5118 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5119 goto done;
5120 err = got_commit_graph_fetch_commits(graph, 1, repo,
5121 check_cancelled, NULL);
5122 if (err)
5123 goto done;
5124 } else {
5125 err = check_path_prefix(parent_id, commit_id,
5126 path_prefix, path_prefix_errcode, repo);
5127 if (err)
5128 goto done;
5130 err = got_object_qid_alloc(&qid, commit_id);
5131 if (err)
5132 goto done;
5133 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5134 commit_id = parent_id;
5137 done:
5138 got_commit_graph_close(graph);
5139 return err;
5142 static const struct got_error *
5143 cmd_rebase(int argc, char *argv[])
5145 const struct got_error *error = NULL;
5146 struct got_worktree *worktree = NULL;
5147 struct got_repository *repo = NULL;
5148 struct got_fileindex *fileindex = NULL;
5149 char *cwd = NULL;
5150 struct got_reference *branch = NULL;
5151 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5152 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5153 struct got_object_id *resume_commit_id = NULL;
5154 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5155 struct got_commit_object *commit = NULL;
5156 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5157 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5158 struct got_object_id_queue commits;
5159 struct got_pathlist_head merged_paths;
5160 const struct got_object_id_queue *parent_ids;
5161 struct got_object_qid *qid, *pid;
5163 SIMPLEQ_INIT(&commits);
5164 TAILQ_INIT(&merged_paths);
5166 while ((ch = getopt(argc, argv, "ac")) != -1) {
5167 switch (ch) {
5168 case 'a':
5169 abort_rebase = 1;
5170 break;
5171 case 'c':
5172 continue_rebase = 1;
5173 break;
5174 default:
5175 usage_rebase();
5176 /* NOTREACHED */
5180 argc -= optind;
5181 argv += optind;
5183 #ifndef PROFILE
5184 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5185 "unveil", NULL) == -1)
5186 err(1, "pledge");
5187 #endif
5188 if (abort_rebase && continue_rebase)
5189 usage_rebase();
5190 else if (abort_rebase || continue_rebase) {
5191 if (argc != 0)
5192 usage_rebase();
5193 } else if (argc != 1)
5194 usage_rebase();
5196 cwd = getcwd(NULL, 0);
5197 if (cwd == NULL) {
5198 error = got_error_from_errno("getcwd");
5199 goto done;
5201 error = got_worktree_open(&worktree, cwd);
5202 if (error)
5203 goto done;
5205 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5206 NULL);
5207 if (error != NULL)
5208 goto done;
5210 error = apply_unveil(got_repo_get_path(repo), 0,
5211 got_worktree_get_root_path(worktree));
5212 if (error)
5213 goto done;
5215 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5216 if (error)
5217 goto done;
5219 if (abort_rebase) {
5220 int did_something;
5221 if (!rebase_in_progress) {
5222 error = got_error(GOT_ERR_NOT_REBASING);
5223 goto done;
5225 error = got_worktree_rebase_continue(&resume_commit_id,
5226 &new_base_branch, &tmp_branch, &branch, &fileindex,
5227 worktree, repo);
5228 if (error)
5229 goto done;
5230 printf("Switching work tree to %s\n",
5231 got_ref_get_symref_target(new_base_branch));
5232 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5233 new_base_branch, update_progress, &did_something);
5234 if (error)
5235 goto done;
5236 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5237 goto done; /* nothing else to do */
5240 if (continue_rebase) {
5241 if (!rebase_in_progress) {
5242 error = got_error(GOT_ERR_NOT_REBASING);
5243 goto done;
5245 error = got_worktree_rebase_continue(&resume_commit_id,
5246 &new_base_branch, &tmp_branch, &branch, &fileindex,
5247 worktree, repo);
5248 if (error)
5249 goto done;
5251 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5252 resume_commit_id, repo);
5253 if (error)
5254 goto done;
5256 yca_id = got_object_id_dup(resume_commit_id);
5257 if (yca_id == NULL) {
5258 error = got_error_from_errno("got_object_id_dup");
5259 goto done;
5261 } else {
5262 error = got_ref_open(&branch, repo, argv[0], 0);
5263 if (error != NULL)
5264 goto done;
5267 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5268 if (error)
5269 goto done;
5271 if (!continue_rebase) {
5272 struct got_object_id *base_commit_id;
5274 base_commit_id = got_worktree_get_base_commit_id(worktree);
5275 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5276 base_commit_id, branch_head_commit_id, repo,
5277 check_cancelled, NULL);
5278 if (error)
5279 goto done;
5280 if (yca_id == NULL) {
5281 error = got_error_msg(GOT_ERR_ANCESTRY,
5282 "specified branch shares no common ancestry "
5283 "with work tree's branch");
5284 goto done;
5287 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5288 if (error) {
5289 if (error->code != GOT_ERR_ANCESTRY)
5290 goto done;
5291 error = NULL;
5292 } else {
5293 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5294 "specified branch resolves to a commit which "
5295 "is already contained in work tree's branch");
5296 goto done;
5298 error = got_worktree_rebase_prepare(&new_base_branch,
5299 &tmp_branch, &fileindex, worktree, branch, repo);
5300 if (error)
5301 goto done;
5304 commit_id = branch_head_commit_id;
5305 error = got_object_open_as_commit(&commit, repo, commit_id);
5306 if (error)
5307 goto done;
5309 parent_ids = got_object_commit_get_parent_ids(commit);
5310 pid = SIMPLEQ_FIRST(parent_ids);
5311 if (pid == NULL) {
5312 if (!continue_rebase) {
5313 int did_something;
5314 error = got_worktree_rebase_abort(worktree, fileindex,
5315 repo, new_base_branch, update_progress,
5316 &did_something);
5317 if (error)
5318 goto done;
5319 printf("Rebase of %s aborted\n",
5320 got_ref_get_name(branch));
5322 error = got_error(GOT_ERR_EMPTY_REBASE);
5323 goto done;
5325 error = collect_commits(&commits, commit_id, pid->id,
5326 yca_id, got_worktree_get_path_prefix(worktree),
5327 GOT_ERR_REBASE_PATH, repo);
5328 got_object_commit_close(commit);
5329 commit = NULL;
5330 if (error)
5331 goto done;
5333 if (SIMPLEQ_EMPTY(&commits)) {
5334 if (continue_rebase)
5335 error = rebase_complete(worktree, fileindex,
5336 branch, new_base_branch, tmp_branch, repo);
5337 else
5338 error = got_error(GOT_ERR_EMPTY_REBASE);
5339 goto done;
5342 pid = NULL;
5343 SIMPLEQ_FOREACH(qid, &commits, entry) {
5344 commit_id = qid->id;
5345 parent_id = pid ? pid->id : yca_id;
5346 pid = qid;
5348 error = got_worktree_rebase_merge_files(&merged_paths,
5349 worktree, fileindex, parent_id, commit_id, repo,
5350 rebase_progress, &rebase_status, check_cancelled, NULL);
5351 if (error)
5352 goto done;
5354 if (rebase_status == GOT_STATUS_CONFLICT) {
5355 got_worktree_rebase_pathlist_free(&merged_paths);
5356 break;
5359 error = rebase_commit(&merged_paths, worktree, fileindex,
5360 tmp_branch, commit_id, repo);
5361 got_worktree_rebase_pathlist_free(&merged_paths);
5362 if (error)
5363 goto done;
5366 if (rebase_status == GOT_STATUS_CONFLICT) {
5367 error = got_worktree_rebase_postpone(worktree, fileindex);
5368 if (error)
5369 goto done;
5370 error = got_error_msg(GOT_ERR_CONFLICTS,
5371 "conflicts must be resolved before rebasing can continue");
5372 } else
5373 error = rebase_complete(worktree, fileindex, branch,
5374 new_base_branch, tmp_branch, repo);
5375 done:
5376 got_object_id_queue_free(&commits);
5377 free(branch_head_commit_id);
5378 free(resume_commit_id);
5379 free(yca_id);
5380 if (commit)
5381 got_object_commit_close(commit);
5382 if (branch)
5383 got_ref_close(branch);
5384 if (new_base_branch)
5385 got_ref_close(new_base_branch);
5386 if (tmp_branch)
5387 got_ref_close(tmp_branch);
5388 if (worktree)
5389 got_worktree_close(worktree);
5390 if (repo)
5391 got_repo_close(repo);
5392 return error;
5395 __dead static void
5396 usage_histedit(void)
5398 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5399 getprogname());
5400 exit(1);
5403 #define GOT_HISTEDIT_PICK 'p'
5404 #define GOT_HISTEDIT_EDIT 'e'
5405 #define GOT_HISTEDIT_FOLD 'f'
5406 #define GOT_HISTEDIT_DROP 'd'
5407 #define GOT_HISTEDIT_MESG 'm'
5409 static struct got_histedit_cmd {
5410 unsigned char code;
5411 const char *name;
5412 const char *desc;
5413 } got_histedit_cmds[] = {
5414 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5415 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5416 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5417 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5418 { GOT_HISTEDIT_MESG, "mesg",
5419 "single-line log message for commit above (open editor if empty)" },
5422 struct got_histedit_list_entry {
5423 TAILQ_ENTRY(got_histedit_list_entry) entry;
5424 struct got_object_id *commit_id;
5425 const struct got_histedit_cmd *cmd;
5426 char *logmsg;
5428 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5430 static const struct got_error *
5431 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5432 FILE *f, struct got_repository *repo)
5434 const struct got_error *err = NULL;
5435 char *logmsg = NULL, *id_str = NULL;
5436 struct got_commit_object *commit = NULL;
5437 int n;
5439 err = got_object_open_as_commit(&commit, repo, commit_id);
5440 if (err)
5441 goto done;
5443 err = get_short_logmsg(&logmsg, 34, commit);
5444 if (err)
5445 goto done;
5447 err = got_object_id_str(&id_str, commit_id);
5448 if (err)
5449 goto done;
5451 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5452 if (n < 0)
5453 err = got_ferror(f, GOT_ERR_IO);
5454 done:
5455 if (commit)
5456 got_object_commit_close(commit);
5457 free(id_str);
5458 free(logmsg);
5459 return err;
5462 static const struct got_error *
5463 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5464 struct got_repository *repo)
5466 const struct got_error *err = NULL;
5467 struct got_object_qid *qid;
5469 if (SIMPLEQ_EMPTY(commits))
5470 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5472 SIMPLEQ_FOREACH(qid, commits, entry) {
5473 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5474 f, repo);
5475 if (err)
5476 break;
5479 return err;
5482 static const struct got_error *
5483 write_cmd_list(FILE *f)
5485 const struct got_error *err = NULL;
5486 int n, i;
5488 n = fprintf(f, "# Available histedit commands:\n");
5489 if (n < 0)
5490 return got_ferror(f, GOT_ERR_IO);
5492 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5493 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5494 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5495 cmd->desc);
5496 if (n < 0) {
5497 err = got_ferror(f, GOT_ERR_IO);
5498 break;
5501 n = fprintf(f, "# Commits will be processed in order from top to "
5502 "bottom of this file.\n");
5503 if (n < 0)
5504 return got_ferror(f, GOT_ERR_IO);
5505 return err;
5508 static const struct got_error *
5509 histedit_syntax_error(int lineno)
5511 static char msg[42];
5512 int ret;
5514 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5515 lineno);
5516 if (ret == -1 || ret >= sizeof(msg))
5517 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5519 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5522 static const struct got_error *
5523 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5524 char *logmsg, struct got_repository *repo)
5526 const struct got_error *err;
5527 struct got_commit_object *folded_commit = NULL;
5528 char *id_str, *folded_logmsg = NULL;
5530 err = got_object_id_str(&id_str, hle->commit_id);
5531 if (err)
5532 return err;
5534 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5535 if (err)
5536 goto done;
5538 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5539 if (err)
5540 goto done;
5541 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5542 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5543 folded_logmsg) == -1) {
5544 err = got_error_from_errno("asprintf");
5545 goto done;
5547 done:
5548 if (folded_commit)
5549 got_object_commit_close(folded_commit);
5550 free(id_str);
5551 free(folded_logmsg);
5552 return err;
5555 static struct got_histedit_list_entry *
5556 get_folded_commits(struct got_histedit_list_entry *hle)
5558 struct got_histedit_list_entry *prev, *folded = NULL;
5560 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5561 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5562 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5563 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5564 folded = prev;
5565 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5568 return folded;
5571 static const struct got_error *
5572 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5573 struct got_repository *repo)
5575 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5576 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5577 const struct got_error *err = NULL;
5578 struct got_commit_object *commit = NULL;
5579 int fd;
5580 struct got_histedit_list_entry *folded = NULL;
5582 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5583 if (err)
5584 return err;
5586 folded = get_folded_commits(hle);
5587 if (folded) {
5588 while (folded != hle) {
5589 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5590 folded = TAILQ_NEXT(folded, entry);
5591 continue;
5593 err = append_folded_commit_msg(&new_msg, folded,
5594 logmsg, repo);
5595 if (err)
5596 goto done;
5597 free(logmsg);
5598 logmsg = new_msg;
5599 folded = TAILQ_NEXT(folded, entry);
5603 err = got_object_id_str(&id_str, hle->commit_id);
5604 if (err)
5605 goto done;
5606 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5607 if (err)
5608 goto done;
5609 if (asprintf(&new_msg,
5610 "%s\n# original log message of commit %s: %s",
5611 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5612 err = got_error_from_errno("asprintf");
5613 goto done;
5615 free(logmsg);
5616 logmsg = new_msg;
5618 err = got_object_id_str(&id_str, hle->commit_id);
5619 if (err)
5620 goto done;
5622 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5623 if (err)
5624 goto done;
5626 dprintf(fd, logmsg);
5627 close(fd);
5629 err = get_editor(&editor);
5630 if (err)
5631 goto done;
5633 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5634 if (err) {
5635 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5636 goto done;
5637 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5639 done:
5640 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5641 err = got_error_from_errno2("unlink", logmsg_path);
5642 free(logmsg_path);
5643 free(logmsg);
5644 free(orig_logmsg);
5645 free(editor);
5646 if (commit)
5647 got_object_commit_close(commit);
5648 return err;
5651 static const struct got_error *
5652 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5653 FILE *f, struct got_repository *repo)
5655 const struct got_error *err = NULL;
5656 char *line = NULL, *p, *end;
5657 size_t size;
5658 ssize_t len;
5659 int lineno = 0, i;
5660 const struct got_histedit_cmd *cmd;
5661 struct got_object_id *commit_id = NULL;
5662 struct got_histedit_list_entry *hle = NULL;
5664 for (;;) {
5665 len = getline(&line, &size, f);
5666 if (len == -1) {
5667 const struct got_error *getline_err;
5668 if (feof(f))
5669 break;
5670 getline_err = got_error_from_errno("getline");
5671 err = got_ferror(f, getline_err->code);
5672 break;
5674 lineno++;
5675 p = line;
5676 while (isspace((unsigned char)p[0]))
5677 p++;
5678 if (p[0] == '#' || p[0] == '\0') {
5679 free(line);
5680 line = NULL;
5681 continue;
5683 cmd = NULL;
5684 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5685 cmd = &got_histedit_cmds[i];
5686 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5687 isspace((unsigned char)p[strlen(cmd->name)])) {
5688 p += strlen(cmd->name);
5689 break;
5691 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5692 p++;
5693 break;
5696 if (i == nitems(got_histedit_cmds)) {
5697 err = histedit_syntax_error(lineno);
5698 break;
5700 while (isspace((unsigned char)p[0]))
5701 p++;
5702 if (cmd->code == GOT_HISTEDIT_MESG) {
5703 if (hle == NULL || hle->logmsg != NULL) {
5704 err = got_error(GOT_ERR_HISTEDIT_CMD);
5705 break;
5707 if (p[0] == '\0') {
5708 err = histedit_edit_logmsg(hle, repo);
5709 if (err)
5710 break;
5711 } else {
5712 hle->logmsg = strdup(p);
5713 if (hle->logmsg == NULL) {
5714 err = got_error_from_errno("strdup");
5715 break;
5718 free(line);
5719 line = NULL;
5720 continue;
5721 } else {
5722 end = p;
5723 while (end[0] && !isspace((unsigned char)end[0]))
5724 end++;
5725 *end = '\0';
5727 err = got_object_resolve_id_str(&commit_id, repo, p);
5728 if (err) {
5729 /* override error code */
5730 err = histedit_syntax_error(lineno);
5731 break;
5734 hle = malloc(sizeof(*hle));
5735 if (hle == NULL) {
5736 err = got_error_from_errno("malloc");
5737 break;
5739 hle->cmd = cmd;
5740 hle->commit_id = commit_id;
5741 hle->logmsg = NULL;
5742 commit_id = NULL;
5743 free(line);
5744 line = NULL;
5745 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5748 free(line);
5749 free(commit_id);
5750 return err;
5753 static const struct got_error *
5754 histedit_check_script(struct got_histedit_list *histedit_cmds,
5755 struct got_object_id_queue *commits, struct got_repository *repo)
5757 const struct got_error *err = NULL;
5758 struct got_object_qid *qid;
5759 struct got_histedit_list_entry *hle;
5760 static char msg[80];
5761 char *id_str;
5763 if (TAILQ_EMPTY(histedit_cmds))
5764 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5765 "histedit script contains no commands");
5766 if (SIMPLEQ_EMPTY(commits))
5767 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5769 SIMPLEQ_FOREACH(qid, commits, entry) {
5770 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5771 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5772 break;
5774 if (hle == NULL) {
5775 err = got_object_id_str(&id_str, qid->id);
5776 if (err)
5777 return err;
5778 snprintf(msg, sizeof(msg),
5779 "commit %s missing from histedit script", id_str);
5780 free(id_str);
5781 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5785 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5786 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5787 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5788 "last commit in histedit script cannot be folded");
5790 return NULL;
5793 static const struct got_error *
5794 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5795 const char *path, struct got_object_id_queue *commits,
5796 struct got_repository *repo)
5798 const struct got_error *err = NULL;
5799 char *editor;
5800 FILE *f = NULL;
5802 err = get_editor(&editor);
5803 if (err)
5804 return err;
5806 if (spawn_editor(editor, path) == -1) {
5807 err = got_error_from_errno("failed spawning editor");
5808 goto done;
5811 f = fopen(path, "r");
5812 if (f == NULL) {
5813 err = got_error_from_errno("fopen");
5814 goto done;
5816 err = histedit_parse_list(histedit_cmds, f, repo);
5817 if (err)
5818 goto done;
5820 err = histedit_check_script(histedit_cmds, commits, repo);
5821 done:
5822 if (f && fclose(f) != 0 && err == NULL)
5823 err = got_error_from_errno("fclose");
5824 free(editor);
5825 return err;
5828 static const struct got_error *
5829 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5830 struct got_object_id_queue *, const char *, struct got_repository *);
5832 static const struct got_error *
5833 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5834 struct got_object_id_queue *commits, struct got_repository *repo)
5836 const struct got_error *err;
5837 FILE *f = NULL;
5838 char *path = NULL;
5840 err = got_opentemp_named(&path, &f, "got-histedit");
5841 if (err)
5842 return err;
5844 err = write_cmd_list(f);
5845 if (err)
5846 goto done;
5848 err = histedit_write_commit_list(commits, f, repo);
5849 if (err)
5850 goto done;
5852 if (fclose(f) != 0) {
5853 err = got_error_from_errno("fclose");
5854 goto done;
5856 f = NULL;
5858 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5859 if (err) {
5860 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5861 err->code != GOT_ERR_HISTEDIT_CMD)
5862 goto done;
5863 err = histedit_edit_list_retry(histedit_cmds, err,
5864 commits, path, repo);
5866 done:
5867 if (f && fclose(f) != 0 && err == NULL)
5868 err = got_error_from_errno("fclose");
5869 if (path && unlink(path) != 0 && err == NULL)
5870 err = got_error_from_errno2("unlink", path);
5871 free(path);
5872 return err;
5875 static const struct got_error *
5876 histedit_save_list(struct got_histedit_list *histedit_cmds,
5877 struct got_worktree *worktree, struct got_repository *repo)
5879 const struct got_error *err = NULL;
5880 char *path = NULL;
5881 FILE *f = NULL;
5882 struct got_histedit_list_entry *hle;
5883 struct got_commit_object *commit = NULL;
5885 err = got_worktree_get_histedit_script_path(&path, worktree);
5886 if (err)
5887 return err;
5889 f = fopen(path, "w");
5890 if (f == NULL) {
5891 err = got_error_from_errno2("fopen", path);
5892 goto done;
5894 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5895 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5896 repo);
5897 if (err)
5898 break;
5900 if (hle->logmsg) {
5901 int n = fprintf(f, "%c %s\n",
5902 GOT_HISTEDIT_MESG, hle->logmsg);
5903 if (n < 0) {
5904 err = got_ferror(f, GOT_ERR_IO);
5905 break;
5909 done:
5910 if (f && fclose(f) != 0 && err == NULL)
5911 err = got_error_from_errno("fclose");
5912 free(path);
5913 if (commit)
5914 got_object_commit_close(commit);
5915 return err;
5918 void
5919 histedit_free_list(struct got_histedit_list *histedit_cmds)
5921 struct got_histedit_list_entry *hle;
5923 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5924 TAILQ_REMOVE(histedit_cmds, hle, entry);
5925 free(hle);
5929 static const struct got_error *
5930 histedit_load_list(struct got_histedit_list *histedit_cmds,
5931 const char *path, struct got_repository *repo)
5933 const struct got_error *err = NULL;
5934 FILE *f = NULL;
5936 f = fopen(path, "r");
5937 if (f == NULL) {
5938 err = got_error_from_errno2("fopen", path);
5939 goto done;
5942 err = histedit_parse_list(histedit_cmds, f, repo);
5943 done:
5944 if (f && fclose(f) != 0 && err == NULL)
5945 err = got_error_from_errno("fclose");
5946 return err;
5949 static const struct got_error *
5950 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5951 const struct got_error *edit_err, struct got_object_id_queue *commits,
5952 const char *path, struct got_repository *repo)
5954 const struct got_error *err = NULL, *prev_err = edit_err;
5955 int resp = ' ';
5957 while (resp != 'c' && resp != 'r' && resp != 'a') {
5958 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5959 "or (a)bort: ", getprogname(), prev_err->msg);
5960 resp = getchar();
5961 if (resp == '\n')
5962 resp = getchar();
5963 if (resp == 'c') {
5964 histedit_free_list(histedit_cmds);
5965 err = histedit_run_editor(histedit_cmds, path, commits,
5966 repo);
5967 if (err) {
5968 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5969 err->code != GOT_ERR_HISTEDIT_CMD)
5970 break;
5971 prev_err = err;
5972 resp = ' ';
5973 continue;
5975 break;
5976 } else if (resp == 'r') {
5977 histedit_free_list(histedit_cmds);
5978 err = histedit_edit_script(histedit_cmds,
5979 commits, repo);
5980 if (err) {
5981 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5982 err->code != GOT_ERR_HISTEDIT_CMD)
5983 break;
5984 prev_err = err;
5985 resp = ' ';
5986 continue;
5988 break;
5989 } else if (resp == 'a') {
5990 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5991 break;
5992 } else
5993 printf("invalid response '%c'\n", resp);
5996 return err;
5999 static const struct got_error *
6000 histedit_complete(struct got_worktree *worktree,
6001 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6002 struct got_reference *branch, struct got_repository *repo)
6004 printf("Switching work tree to %s\n",
6005 got_ref_get_symref_target(branch));
6006 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6007 branch, repo);
6010 static const struct got_error *
6011 show_histedit_progress(struct got_commit_object *commit,
6012 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6014 const struct got_error *err;
6015 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6017 err = got_object_id_str(&old_id_str, hle->commit_id);
6018 if (err)
6019 goto done;
6021 if (new_id) {
6022 err = got_object_id_str(&new_id_str, new_id);
6023 if (err)
6024 goto done;
6027 old_id_str[12] = '\0';
6028 if (new_id_str)
6029 new_id_str[12] = '\0';
6031 if (hle->logmsg) {
6032 logmsg = strdup(hle->logmsg);
6033 if (logmsg == NULL) {
6034 err = got_error_from_errno("strdup");
6035 goto done;
6037 trim_logmsg(logmsg, 42);
6038 } else {
6039 err = get_short_logmsg(&logmsg, 42, commit);
6040 if (err)
6041 goto done;
6044 switch (hle->cmd->code) {
6045 case GOT_HISTEDIT_PICK:
6046 case GOT_HISTEDIT_EDIT:
6047 printf("%s -> %s: %s\n", old_id_str,
6048 new_id_str ? new_id_str : "no-op change", logmsg);
6049 break;
6050 case GOT_HISTEDIT_DROP:
6051 case GOT_HISTEDIT_FOLD:
6052 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6053 logmsg);
6054 break;
6055 default:
6056 break;
6059 done:
6060 free(old_id_str);
6061 free(new_id_str);
6062 return err;
6065 static const struct got_error *
6066 histedit_commit(struct got_pathlist_head *merged_paths,
6067 struct got_worktree *worktree, struct got_fileindex *fileindex,
6068 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6069 struct got_repository *repo)
6071 const struct got_error *err;
6072 struct got_commit_object *commit;
6073 struct got_object_id *new_commit_id;
6075 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6076 && hle->logmsg == NULL) {
6077 err = histedit_edit_logmsg(hle, repo);
6078 if (err)
6079 return err;
6082 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6083 if (err)
6084 return err;
6086 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6087 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6088 hle->logmsg, repo);
6089 if (err) {
6090 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6091 goto done;
6092 err = show_histedit_progress(commit, hle, NULL);
6093 } else {
6094 err = show_histedit_progress(commit, hle, new_commit_id);
6095 free(new_commit_id);
6097 done:
6098 got_object_commit_close(commit);
6099 return err;
6102 static const struct got_error *
6103 histedit_skip_commit(struct got_histedit_list_entry *hle,
6104 struct got_worktree *worktree, struct got_repository *repo)
6106 const struct got_error *error;
6107 struct got_commit_object *commit;
6109 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6110 repo);
6111 if (error)
6112 return error;
6114 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6115 if (error)
6116 return error;
6118 error = show_histedit_progress(commit, hle, NULL);
6119 got_object_commit_close(commit);
6120 return error;
6123 static const struct got_error *
6124 cmd_histedit(int argc, char *argv[])
6126 const struct got_error *error = NULL;
6127 struct got_worktree *worktree = NULL;
6128 struct got_fileindex *fileindex = NULL;
6129 struct got_repository *repo = NULL;
6130 char *cwd = NULL;
6131 struct got_reference *branch = NULL;
6132 struct got_reference *tmp_branch = NULL;
6133 struct got_object_id *resume_commit_id = NULL;
6134 struct got_object_id *base_commit_id = NULL;
6135 struct got_object_id *head_commit_id = NULL;
6136 struct got_commit_object *commit = NULL;
6137 int ch, rebase_in_progress = 0, did_something;
6138 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6139 const char *edit_script_path = NULL;
6140 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6141 struct got_object_id_queue commits;
6142 struct got_pathlist_head merged_paths;
6143 const struct got_object_id_queue *parent_ids;
6144 struct got_object_qid *pid;
6145 struct got_histedit_list histedit_cmds;
6146 struct got_histedit_list_entry *hle;
6148 SIMPLEQ_INIT(&commits);
6149 TAILQ_INIT(&histedit_cmds);
6150 TAILQ_INIT(&merged_paths);
6152 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6153 switch (ch) {
6154 case 'a':
6155 abort_edit = 1;
6156 break;
6157 case 'c':
6158 continue_edit = 1;
6159 break;
6160 case 'F':
6161 edit_script_path = optarg;
6162 break;
6163 default:
6164 usage_histedit();
6165 /* NOTREACHED */
6169 argc -= optind;
6170 argv += optind;
6172 #ifndef PROFILE
6173 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6174 "unveil", NULL) == -1)
6175 err(1, "pledge");
6176 #endif
6177 if (abort_edit && continue_edit)
6178 usage_histedit();
6179 if (argc != 0)
6180 usage_histedit();
6183 * This command cannot apply unveil(2) in all cases because the
6184 * user may choose to run an editor to edit the histedit script
6185 * and to edit individual commit log messages.
6186 * unveil(2) traverses exec(2); if an editor is used we have to
6187 * apply unveil after edit script and log messages have been written.
6188 * XXX TODO: Make use of unveil(2) where possible.
6191 cwd = getcwd(NULL, 0);
6192 if (cwd == NULL) {
6193 error = got_error_from_errno("getcwd");
6194 goto done;
6196 error = got_worktree_open(&worktree, cwd);
6197 if (error)
6198 goto done;
6200 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6201 NULL);
6202 if (error != NULL)
6203 goto done;
6205 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6206 if (error)
6207 goto done;
6208 if (rebase_in_progress) {
6209 error = got_error(GOT_ERR_REBASING);
6210 goto done;
6213 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6214 if (error)
6215 goto done;
6217 if (edit_in_progress && abort_edit) {
6218 error = got_worktree_histedit_continue(&resume_commit_id,
6219 &tmp_branch, &branch, &base_commit_id, &fileindex,
6220 worktree, repo);
6221 if (error)
6222 goto done;
6223 printf("Switching work tree to %s\n",
6224 got_ref_get_symref_target(branch));
6225 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6226 branch, base_commit_id, update_progress, &did_something);
6227 if (error)
6228 goto done;
6229 printf("Histedit of %s aborted\n",
6230 got_ref_get_symref_target(branch));
6231 goto done; /* nothing else to do */
6232 } else if (abort_edit) {
6233 error = got_error(GOT_ERR_NOT_HISTEDIT);
6234 goto done;
6237 if (continue_edit) {
6238 char *path;
6240 if (!edit_in_progress) {
6241 error = got_error(GOT_ERR_NOT_HISTEDIT);
6242 goto done;
6245 error = got_worktree_get_histedit_script_path(&path, worktree);
6246 if (error)
6247 goto done;
6249 error = histedit_load_list(&histedit_cmds, path, repo);
6250 free(path);
6251 if (error)
6252 goto done;
6254 error = got_worktree_histedit_continue(&resume_commit_id,
6255 &tmp_branch, &branch, &base_commit_id, &fileindex,
6256 worktree, repo);
6257 if (error)
6258 goto done;
6260 error = got_ref_resolve(&head_commit_id, repo, branch);
6261 if (error)
6262 goto done;
6264 error = got_object_open_as_commit(&commit, repo,
6265 head_commit_id);
6266 if (error)
6267 goto done;
6268 parent_ids = got_object_commit_get_parent_ids(commit);
6269 pid = SIMPLEQ_FIRST(parent_ids);
6270 if (pid == NULL) {
6271 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6272 goto done;
6274 error = collect_commits(&commits, head_commit_id, pid->id,
6275 base_commit_id, got_worktree_get_path_prefix(worktree),
6276 GOT_ERR_HISTEDIT_PATH, repo);
6277 got_object_commit_close(commit);
6278 commit = NULL;
6279 if (error)
6280 goto done;
6281 } else {
6282 if (edit_in_progress) {
6283 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6284 goto done;
6287 error = got_ref_open(&branch, repo,
6288 got_worktree_get_head_ref_name(worktree), 0);
6289 if (error != NULL)
6290 goto done;
6292 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6293 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6294 "will not edit commit history of a branch outside "
6295 "the \"refs/heads/\" reference namespace");
6296 goto done;
6299 error = got_ref_resolve(&head_commit_id, repo, branch);
6300 got_ref_close(branch);
6301 branch = NULL;
6302 if (error)
6303 goto done;
6305 error = got_object_open_as_commit(&commit, repo,
6306 head_commit_id);
6307 if (error)
6308 goto done;
6309 parent_ids = got_object_commit_get_parent_ids(commit);
6310 pid = SIMPLEQ_FIRST(parent_ids);
6311 if (pid == NULL) {
6312 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6313 goto done;
6315 error = collect_commits(&commits, head_commit_id, pid->id,
6316 got_worktree_get_base_commit_id(worktree),
6317 got_worktree_get_path_prefix(worktree),
6318 GOT_ERR_HISTEDIT_PATH, repo);
6319 got_object_commit_close(commit);
6320 commit = NULL;
6321 if (error)
6322 goto done;
6324 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6325 &base_commit_id, &fileindex, worktree, repo);
6326 if (error)
6327 goto done;
6329 if (edit_script_path) {
6330 error = histedit_load_list(&histedit_cmds,
6331 edit_script_path, repo);
6332 if (error) {
6333 got_worktree_histedit_abort(worktree, fileindex,
6334 repo, branch, base_commit_id,
6335 update_progress, &did_something);
6336 goto done;
6338 } else {
6339 error = histedit_edit_script(&histedit_cmds, &commits,
6340 repo);
6341 if (error) {
6342 got_worktree_histedit_abort(worktree, fileindex,
6343 repo, branch, base_commit_id,
6344 update_progress, &did_something);
6345 goto done;
6350 error = histedit_save_list(&histedit_cmds, worktree,
6351 repo);
6352 if (error) {
6353 got_worktree_histedit_abort(worktree, fileindex,
6354 repo, branch, base_commit_id,
6355 update_progress, &did_something);
6356 goto done;
6361 error = histedit_check_script(&histedit_cmds, &commits, repo);
6362 if (error)
6363 goto done;
6365 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6366 if (resume_commit_id) {
6367 if (got_object_id_cmp(hle->commit_id,
6368 resume_commit_id) != 0)
6369 continue;
6371 resume_commit_id = NULL;
6372 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6373 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6374 error = histedit_skip_commit(hle, worktree,
6375 repo);
6376 } else {
6377 error = histedit_commit(NULL, worktree,
6378 fileindex, tmp_branch, hle, repo);
6380 if (error)
6381 goto done;
6382 continue;
6385 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6386 error = histedit_skip_commit(hle, worktree, repo);
6387 if (error)
6388 goto done;
6389 continue;
6392 error = got_object_open_as_commit(&commit, repo,
6393 hle->commit_id);
6394 if (error)
6395 goto done;
6396 parent_ids = got_object_commit_get_parent_ids(commit);
6397 pid = SIMPLEQ_FIRST(parent_ids);
6399 error = got_worktree_histedit_merge_files(&merged_paths,
6400 worktree, fileindex, pid->id, hle->commit_id, repo,
6401 rebase_progress, &rebase_status, check_cancelled, NULL);
6402 if (error)
6403 goto done;
6404 got_object_commit_close(commit);
6405 commit = NULL;
6407 if (rebase_status == GOT_STATUS_CONFLICT) {
6408 got_worktree_rebase_pathlist_free(&merged_paths);
6409 break;
6412 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6413 char *id_str;
6414 error = got_object_id_str(&id_str, hle->commit_id);
6415 if (error)
6416 goto done;
6417 printf("Stopping histedit for amending commit %s\n",
6418 id_str);
6419 free(id_str);
6420 got_worktree_rebase_pathlist_free(&merged_paths);
6421 error = got_worktree_histedit_postpone(worktree,
6422 fileindex);
6423 goto done;
6426 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6427 error = histedit_skip_commit(hle, worktree, repo);
6428 if (error)
6429 goto done;
6430 continue;
6433 error = histedit_commit(&merged_paths, worktree, fileindex,
6434 tmp_branch, hle, repo);
6435 got_worktree_rebase_pathlist_free(&merged_paths);
6436 if (error)
6437 goto done;
6440 if (rebase_status == GOT_STATUS_CONFLICT) {
6441 error = got_worktree_histedit_postpone(worktree, fileindex);
6442 if (error)
6443 goto done;
6444 error = got_error_msg(GOT_ERR_CONFLICTS,
6445 "conflicts must be resolved before rebasing can continue");
6446 } else
6447 error = histedit_complete(worktree, fileindex, tmp_branch,
6448 branch, repo);
6449 done:
6450 got_object_id_queue_free(&commits);
6451 histedit_free_list(&histedit_cmds);
6452 free(head_commit_id);
6453 free(base_commit_id);
6454 free(resume_commit_id);
6455 if (commit)
6456 got_object_commit_close(commit);
6457 if (branch)
6458 got_ref_close(branch);
6459 if (tmp_branch)
6460 got_ref_close(tmp_branch);
6461 if (worktree)
6462 got_worktree_close(worktree);
6463 if (repo)
6464 got_repo_close(repo);
6465 return error;
6468 __dead static void
6469 usage_integrate(void)
6471 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6472 exit(1);
6475 static const struct got_error *
6476 cmd_integrate(int argc, char *argv[])
6478 const struct got_error *error = NULL;
6479 struct got_repository *repo = NULL;
6480 struct got_worktree *worktree = NULL;
6481 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6482 const char *branch_arg = NULL;
6483 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6484 struct got_fileindex *fileindex = NULL;
6485 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6486 int ch, did_something = 0;
6488 while ((ch = getopt(argc, argv, "")) != -1) {
6489 switch (ch) {
6490 default:
6491 usage_integrate();
6492 /* NOTREACHED */
6496 argc -= optind;
6497 argv += optind;
6499 if (argc != 1)
6500 usage_integrate();
6501 branch_arg = argv[0];
6503 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6504 "unveil", NULL) == -1)
6505 err(1, "pledge");
6507 cwd = getcwd(NULL, 0);
6508 if (cwd == NULL) {
6509 error = got_error_from_errno("getcwd");
6510 goto done;
6513 error = got_worktree_open(&worktree, cwd);
6514 if (error)
6515 goto done;
6517 error = check_rebase_or_histedit_in_progress(worktree);
6518 if (error)
6519 goto done;
6521 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6522 NULL);
6523 if (error != NULL)
6524 goto done;
6526 error = apply_unveil(got_repo_get_path(repo), 0,
6527 got_worktree_get_root_path(worktree));
6528 if (error)
6529 goto done;
6531 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6532 error = got_error_from_errno("asprintf");
6533 goto done;
6536 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6537 &base_branch_ref, worktree, refname, repo);
6538 if (error)
6539 goto done;
6541 refname = strdup(got_ref_get_name(branch_ref));
6542 if (refname == NULL) {
6543 error = got_error_from_errno("strdup");
6544 got_worktree_integrate_abort(worktree, fileindex, repo,
6545 branch_ref, base_branch_ref);
6546 goto done;
6548 base_refname = strdup(got_ref_get_name(base_branch_ref));
6549 if (base_refname == NULL) {
6550 error = got_error_from_errno("strdup");
6551 got_worktree_integrate_abort(worktree, fileindex, repo,
6552 branch_ref, base_branch_ref);
6553 goto done;
6556 error = got_ref_resolve(&commit_id, repo, branch_ref);
6557 if (error)
6558 goto done;
6560 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6561 if (error)
6562 goto done;
6564 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6565 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6566 "specified branch has already been integrated");
6567 got_worktree_integrate_abort(worktree, fileindex, repo,
6568 branch_ref, base_branch_ref);
6569 goto done;
6572 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6573 if (error) {
6574 if (error->code == GOT_ERR_ANCESTRY)
6575 error = got_error(GOT_ERR_REBASE_REQUIRED);
6576 got_worktree_integrate_abort(worktree, fileindex, repo,
6577 branch_ref, base_branch_ref);
6578 goto done;
6581 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6582 branch_ref, base_branch_ref, update_progress, &did_something,
6583 check_cancelled, NULL);
6584 if (error)
6585 goto done;
6587 printf("Integrated %s into %s\n", refname, base_refname);
6588 done:
6589 if (repo)
6590 got_repo_close(repo);
6591 if (worktree)
6592 got_worktree_close(worktree);
6593 free(cwd);
6594 free(base_commit_id);
6595 free(commit_id);
6596 free(refname);
6597 free(base_refname);
6598 return error;
6601 __dead static void
6602 usage_stage(void)
6604 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6605 "[file-path ...]\n",
6606 getprogname());
6607 exit(1);
6610 static const struct got_error *
6611 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6612 const char *path, struct got_object_id *blob_id,
6613 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6615 const struct got_error *err = NULL;
6616 char *id_str = NULL;
6618 if (staged_status != GOT_STATUS_ADD &&
6619 staged_status != GOT_STATUS_MODIFY &&
6620 staged_status != GOT_STATUS_DELETE)
6621 return NULL;
6623 if (staged_status == GOT_STATUS_ADD ||
6624 staged_status == GOT_STATUS_MODIFY)
6625 err = got_object_id_str(&id_str, staged_blob_id);
6626 else
6627 err = got_object_id_str(&id_str, blob_id);
6628 if (err)
6629 return err;
6631 printf("%s %c %s\n", id_str, staged_status, path);
6632 free(id_str);
6633 return NULL;
6636 static const struct got_error *
6637 cmd_stage(int argc, char *argv[])
6639 const struct got_error *error = NULL;
6640 struct got_repository *repo = NULL;
6641 struct got_worktree *worktree = NULL;
6642 char *cwd = NULL;
6643 struct got_pathlist_head paths;
6644 struct got_pathlist_entry *pe;
6645 int ch, list_stage = 0, pflag = 0;
6646 FILE *patch_script_file = NULL;
6647 const char *patch_script_path = NULL;
6648 struct choose_patch_arg cpa;
6650 TAILQ_INIT(&paths);
6652 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6653 switch (ch) {
6654 case 'l':
6655 list_stage = 1;
6656 break;
6657 case 'p':
6658 pflag = 1;
6659 break;
6660 case 'F':
6661 patch_script_path = optarg;
6662 break;
6663 default:
6664 usage_stage();
6665 /* NOTREACHED */
6669 argc -= optind;
6670 argv += optind;
6672 #ifndef PROFILE
6673 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6674 "unveil", NULL) == -1)
6675 err(1, "pledge");
6676 #endif
6677 if (list_stage && (pflag || patch_script_path))
6678 errx(1, "-l option cannot be used with other options");
6679 if (patch_script_path && !pflag)
6680 errx(1, "-F option can only be used together with -p option");
6682 cwd = getcwd(NULL, 0);
6683 if (cwd == NULL) {
6684 error = got_error_from_errno("getcwd");
6685 goto done;
6688 error = got_worktree_open(&worktree, cwd);
6689 if (error)
6690 goto done;
6692 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6693 NULL);
6694 if (error != NULL)
6695 goto done;
6697 if (patch_script_path) {
6698 patch_script_file = fopen(patch_script_path, "r");
6699 if (patch_script_file == NULL) {
6700 error = got_error_from_errno2("fopen",
6701 patch_script_path);
6702 goto done;
6705 error = apply_unveil(got_repo_get_path(repo), 0,
6706 got_worktree_get_root_path(worktree));
6707 if (error)
6708 goto done;
6710 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6711 if (error)
6712 goto done;
6714 if (list_stage)
6715 error = got_worktree_status(worktree, &paths, repo,
6716 print_stage, NULL, check_cancelled, NULL);
6717 else {
6718 cpa.patch_script_file = patch_script_file;
6719 cpa.action = "stage";
6720 error = got_worktree_stage(worktree, &paths,
6721 pflag ? NULL : print_status, NULL,
6722 pflag ? choose_patch : NULL, &cpa, repo);
6724 done:
6725 if (patch_script_file && fclose(patch_script_file) == EOF &&
6726 error == NULL)
6727 error = got_error_from_errno2("fclose", patch_script_path);
6728 if (repo)
6729 got_repo_close(repo);
6730 if (worktree)
6731 got_worktree_close(worktree);
6732 TAILQ_FOREACH(pe, &paths, entry)
6733 free((char *)pe->path);
6734 got_pathlist_free(&paths);
6735 free(cwd);
6736 return error;
6739 __dead static void
6740 usage_unstage(void)
6742 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6743 "[file-path ...]\n",
6744 getprogname());
6745 exit(1);
6749 static const struct got_error *
6750 cmd_unstage(int argc, char *argv[])
6752 const struct got_error *error = NULL;
6753 struct got_repository *repo = NULL;
6754 struct got_worktree *worktree = NULL;
6755 char *cwd = NULL;
6756 struct got_pathlist_head paths;
6757 struct got_pathlist_entry *pe;
6758 int ch, did_something = 0, pflag = 0;
6759 FILE *patch_script_file = NULL;
6760 const char *patch_script_path = NULL;
6761 struct choose_patch_arg cpa;
6763 TAILQ_INIT(&paths);
6765 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6766 switch (ch) {
6767 case 'p':
6768 pflag = 1;
6769 break;
6770 case 'F':
6771 patch_script_path = optarg;
6772 break;
6773 default:
6774 usage_unstage();
6775 /* NOTREACHED */
6779 argc -= optind;
6780 argv += optind;
6782 #ifndef PROFILE
6783 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6784 "unveil", NULL) == -1)
6785 err(1, "pledge");
6786 #endif
6787 if (patch_script_path && !pflag)
6788 errx(1, "-F option can only be used together with -p option");
6790 cwd = getcwd(NULL, 0);
6791 if (cwd == NULL) {
6792 error = got_error_from_errno("getcwd");
6793 goto done;
6796 error = got_worktree_open(&worktree, cwd);
6797 if (error)
6798 goto done;
6800 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6801 NULL);
6802 if (error != NULL)
6803 goto done;
6805 if (patch_script_path) {
6806 patch_script_file = fopen(patch_script_path, "r");
6807 if (patch_script_file == NULL) {
6808 error = got_error_from_errno2("fopen",
6809 patch_script_path);
6810 goto done;
6814 error = apply_unveil(got_repo_get_path(repo), 0,
6815 got_worktree_get_root_path(worktree));
6816 if (error)
6817 goto done;
6819 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6820 if (error)
6821 goto done;
6823 cpa.patch_script_file = patch_script_file;
6824 cpa.action = "unstage";
6825 error = got_worktree_unstage(worktree, &paths, update_progress,
6826 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6827 done:
6828 if (patch_script_file && fclose(patch_script_file) == EOF &&
6829 error == NULL)
6830 error = got_error_from_errno2("fclose", patch_script_path);
6831 if (repo)
6832 got_repo_close(repo);
6833 if (worktree)
6834 got_worktree_close(worktree);
6835 TAILQ_FOREACH(pe, &paths, entry)
6836 free((char *)pe->path);
6837 got_pathlist_free(&paths);
6838 free(cwd);
6839 return error;
6842 __dead static void
6843 usage_cat(void)
6845 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6846 "arg1 [arg2 ...]\n", getprogname());
6847 exit(1);
6850 static const struct got_error *
6851 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6853 const struct got_error *err;
6854 struct got_blob_object *blob;
6856 err = got_object_open_as_blob(&blob, repo, id, 8192);
6857 if (err)
6858 return err;
6860 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6861 got_object_blob_close(blob);
6862 return err;
6865 static const struct got_error *
6866 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6868 const struct got_error *err;
6869 struct got_tree_object *tree;
6870 const struct got_tree_entries *entries;
6871 struct got_tree_entry *te;
6873 err = got_object_open_as_tree(&tree, repo, id);
6874 if (err)
6875 return err;
6877 entries = got_object_tree_get_entries(tree);
6878 te = SIMPLEQ_FIRST(&entries->head);
6879 while (te) {
6880 char *id_str;
6881 if (sigint_received || sigpipe_received)
6882 break;
6883 err = got_object_id_str(&id_str, te->id);
6884 if (err)
6885 break;
6886 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6887 free(id_str);
6888 te = SIMPLEQ_NEXT(te, entry);
6891 got_object_tree_close(tree);
6892 return err;
6895 static const struct got_error *
6896 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6898 const struct got_error *err;
6899 struct got_commit_object *commit;
6900 const struct got_object_id_queue *parent_ids;
6901 struct got_object_qid *pid;
6902 char *id_str = NULL;
6903 const char *logmsg = NULL;
6905 err = got_object_open_as_commit(&commit, repo, id);
6906 if (err)
6907 return err;
6909 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6910 if (err)
6911 goto done;
6913 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6914 parent_ids = got_object_commit_get_parent_ids(commit);
6915 fprintf(outfile, "numparents %d\n",
6916 got_object_commit_get_nparents(commit));
6917 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6918 char *pid_str;
6919 err = got_object_id_str(&pid_str, pid->id);
6920 if (err)
6921 goto done;
6922 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6923 free(pid_str);
6925 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6926 got_object_commit_get_author(commit),
6927 got_object_commit_get_author_time(commit));
6929 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6930 got_object_commit_get_author(commit),
6931 got_object_commit_get_committer_time(commit));
6933 logmsg = got_object_commit_get_logmsg_raw(commit);
6934 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6935 fprintf(outfile, "%s", logmsg);
6936 done:
6937 free(id_str);
6938 got_object_commit_close(commit);
6939 return err;
6942 static const struct got_error *
6943 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6945 const struct got_error *err;
6946 struct got_tag_object *tag;
6947 char *id_str = NULL;
6948 const char *tagmsg = NULL;
6950 err = got_object_open_as_tag(&tag, repo, id);
6951 if (err)
6952 return err;
6954 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6955 if (err)
6956 goto done;
6958 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6960 switch (got_object_tag_get_object_type(tag)) {
6961 case GOT_OBJ_TYPE_BLOB:
6962 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6963 GOT_OBJ_LABEL_BLOB);
6964 break;
6965 case GOT_OBJ_TYPE_TREE:
6966 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6967 GOT_OBJ_LABEL_TREE);
6968 break;
6969 case GOT_OBJ_TYPE_COMMIT:
6970 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6971 GOT_OBJ_LABEL_COMMIT);
6972 break;
6973 case GOT_OBJ_TYPE_TAG:
6974 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6975 GOT_OBJ_LABEL_TAG);
6976 break;
6977 default:
6978 break;
6981 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6982 got_object_tag_get_name(tag));
6984 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6985 got_object_tag_get_tagger(tag),
6986 got_object_tag_get_tagger_time(tag));
6988 tagmsg = got_object_tag_get_message(tag);
6989 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6990 fprintf(outfile, "%s", tagmsg);
6991 done:
6992 free(id_str);
6993 got_object_tag_close(tag);
6994 return err;
6997 static const struct got_error *
6998 cmd_cat(int argc, char *argv[])
7000 const struct got_error *error;
7001 struct got_repository *repo = NULL;
7002 struct got_worktree *worktree = NULL;
7003 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7004 const char *commit_id_str = NULL;
7005 struct got_object_id *id = NULL, *commit_id = NULL;
7006 int ch, obj_type, i, force_path = 0;
7008 #ifndef PROFILE
7009 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7010 NULL) == -1)
7011 err(1, "pledge");
7012 #endif
7014 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7015 switch (ch) {
7016 case 'c':
7017 commit_id_str = optarg;
7018 break;
7019 case 'r':
7020 repo_path = realpath(optarg, NULL);
7021 if (repo_path == NULL)
7022 err(1, "-r option");
7023 got_path_strip_trailing_slashes(repo_path);
7024 break;
7025 case 'P':
7026 force_path = 1;
7027 break;
7028 default:
7029 usage_cat();
7030 /* NOTREACHED */
7034 argc -= optind;
7035 argv += optind;
7037 cwd = getcwd(NULL, 0);
7038 if (cwd == NULL) {
7039 error = got_error_from_errno("getcwd");
7040 goto done;
7042 error = got_worktree_open(&worktree, cwd);
7043 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7044 goto done;
7045 if (worktree) {
7046 if (repo_path == NULL) {
7047 repo_path = strdup(
7048 got_worktree_get_repo_path(worktree));
7049 if (repo_path == NULL) {
7050 error = got_error_from_errno("strdup");
7051 goto done;
7056 if (repo_path == NULL) {
7057 repo_path = getcwd(NULL, 0);
7058 if (repo_path == NULL)
7059 return got_error_from_errno("getcwd");
7062 error = got_repo_open(&repo, repo_path, NULL);
7063 free(repo_path);
7064 if (error != NULL)
7065 goto done;
7067 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7068 if (error)
7069 goto done;
7071 if (commit_id_str == NULL)
7072 commit_id_str = GOT_REF_HEAD;
7073 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7074 if (error)
7075 goto done;
7077 for (i = 0; i < argc; i++) {
7078 if (force_path) {
7079 error = got_object_id_by_path(&id, repo, commit_id,
7080 argv[i]);
7081 if (error)
7082 break;
7083 } else {
7084 error = match_object_id(&id, &label, argv[i],
7085 GOT_OBJ_TYPE_ANY, 0, repo);
7086 if (error) {
7087 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7088 error->code != GOT_ERR_NOT_REF)
7089 break;
7090 error = got_object_id_by_path(&id, repo,
7091 commit_id, argv[i]);
7092 if (error)
7093 break;
7097 error = got_object_get_type(&obj_type, repo, id);
7098 if (error)
7099 break;
7101 switch (obj_type) {
7102 case GOT_OBJ_TYPE_BLOB:
7103 error = cat_blob(id, repo, stdout);
7104 break;
7105 case GOT_OBJ_TYPE_TREE:
7106 error = cat_tree(id, repo, stdout);
7107 break;
7108 case GOT_OBJ_TYPE_COMMIT:
7109 error = cat_commit(id, repo, stdout);
7110 break;
7111 case GOT_OBJ_TYPE_TAG:
7112 error = cat_tag(id, repo, stdout);
7113 break;
7114 default:
7115 error = got_error(GOT_ERR_OBJ_TYPE);
7116 break;
7118 if (error)
7119 break;
7120 free(label);
7121 label = NULL;
7122 free(id);
7123 id = NULL;
7126 done:
7127 free(label);
7128 free(id);
7129 free(commit_id);
7130 if (worktree)
7131 got_worktree_close(worktree);
7132 if (repo) {
7133 const struct got_error *repo_error;
7134 repo_error = got_repo_close(repo);
7135 if (error == NULL)
7136 error = repo_error;
7138 return error;