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>
37 #include <regex.h>
39 #include "got_version.h"
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_diff.h"
48 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
51 #include "got_opentemp.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 static volatile sig_atomic_t sigint_received;
58 static volatile sig_atomic_t sigpipe_received;
60 static void
61 catch_sigint(int signo)
62 {
63 sigint_received = 1;
64 }
66 static void
67 catch_sigpipe(int signo)
68 {
69 sigpipe_received = 1;
70 }
73 struct got_cmd {
74 const char *cmd_name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
78 };
80 __dead static void usage(int);
81 __dead static void usage_init(void);
82 __dead static void usage_import(void);
83 __dead static void usage_checkout(void);
84 __dead static void usage_update(void);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_status(void);
90 __dead static void usage_ref(void);
91 __dead static void usage_branch(void);
92 __dead static void usage_tag(void);
93 __dead static void usage_add(void);
94 __dead static void usage_remove(void);
95 __dead static void usage_revert(void);
96 __dead static void usage_commit(void);
97 __dead static void usage_cherrypick(void);
98 __dead static void usage_backout(void);
99 __dead static void usage_rebase(void);
100 __dead static void usage_histedit(void);
101 __dead static void usage_integrate(void);
102 __dead static void usage_stage(void);
103 __dead static void usage_unstage(void);
104 __dead static void usage_cat(void);
106 static const struct got_error* cmd_init(int, char *[]);
107 static const struct got_error* cmd_import(int, char *[]);
108 static const struct got_error* cmd_checkout(int, char *[]);
109 static const struct got_error* cmd_update(int, char *[]);
110 static const struct got_error* cmd_log(int, char *[]);
111 static const struct got_error* cmd_diff(int, char *[]);
112 static const struct got_error* cmd_blame(int, char *[]);
113 static const struct got_error* cmd_tree(int, char *[]);
114 static const struct got_error* cmd_status(int, char *[]);
115 static const struct got_error* cmd_ref(int, char *[]);
116 static const struct got_error* cmd_branch(int, char *[]);
117 static const struct got_error* cmd_tag(int, char *[]);
118 static const struct got_error* cmd_add(int, char *[]);
119 static const struct got_error* cmd_remove(int, char *[]);
120 static const struct got_error* cmd_revert(int, char *[]);
121 static const struct got_error* cmd_commit(int, char *[]);
122 static const struct got_error* cmd_cherrypick(int, char *[]);
123 static const struct got_error* cmd_backout(int, char *[]);
124 static const struct got_error* cmd_rebase(int, char *[]);
125 static const struct got_error* cmd_histedit(int, char *[]);
126 static const struct got_error* cmd_integrate(int, char *[]);
127 static const struct got_error* cmd_stage(int, char *[]);
128 static const struct got_error* cmd_unstage(int, char *[]);
129 static const struct got_error* cmd_cat(int, char *[]);
131 static struct got_cmd got_commands[] = {
132 { "init", cmd_init, usage_init, "in" },
133 { "import", cmd_import, usage_import, "im" },
134 { "checkout", cmd_checkout, usage_checkout, "co" },
135 { "update", cmd_update, usage_update, "up" },
136 { "log", cmd_log, usage_log, "" },
137 { "diff", cmd_diff, usage_diff, "di" },
138 { "blame", cmd_blame, usage_blame, "bl" },
139 { "tree", cmd_tree, usage_tree, "tr" },
140 { "status", cmd_status, usage_status, "st" },
141 { "ref", cmd_ref, usage_ref, "" },
142 { "branch", cmd_branch, usage_branch, "br" },
143 { "tag", cmd_tag, usage_tag, "" },
144 { "add", cmd_add, usage_add, "" },
145 { "remove", cmd_remove, usage_remove, "rm" },
146 { "revert", cmd_revert, usage_revert, "rv" },
147 { "commit", cmd_commit, usage_commit, "ci" },
148 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
149 { "backout", cmd_backout, usage_backout, "bo" },
150 { "rebase", cmd_rebase, usage_rebase, "rb" },
151 { "histedit", cmd_histedit, usage_histedit, "he" },
152 { "integrate", cmd_integrate, usage_integrate,"ig" },
153 { "stage", cmd_stage, usage_stage, "sg" },
154 { "unstage", cmd_unstage, usage_unstage, "ug" },
155 { "cat", cmd_cat, usage_cat, "" },
156 };
158 static void
159 list_commands(void)
161 int i;
163 fprintf(stderr, "commands:");
164 for (i = 0; i < nitems(got_commands); i++) {
165 struct got_cmd *cmd = &got_commands[i];
166 fprintf(stderr, " %s", cmd->cmd_name);
168 fputc('\n', stderr);
171 int
172 main(int argc, char *argv[])
174 struct got_cmd *cmd;
175 unsigned int i;
176 int ch;
177 int hflag = 0, Vflag = 0;
179 setlocale(LC_CTYPE, "");
181 while ((ch = getopt(argc, argv, "hV")) != -1) {
182 switch (ch) {
183 case 'h':
184 hflag = 1;
185 break;
186 case 'V':
187 Vflag = 1;
188 break;
189 default:
190 usage(hflag);
191 /* NOTREACHED */
195 argc -= optind;
196 argv += optind;
197 optind = 0;
199 if (Vflag) {
200 got_version_print_str();
201 return 1;
204 if (argc <= 0)
205 usage(hflag);
207 signal(SIGINT, catch_sigint);
208 signal(SIGPIPE, catch_sigpipe);
210 for (i = 0; i < nitems(got_commands); i++) {
211 const struct got_error *error;
213 cmd = &got_commands[i];
215 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
216 strcmp(cmd->cmd_alias, argv[0]) != 0)
217 continue;
219 if (hflag)
220 got_commands[i].cmd_usage();
222 error = got_commands[i].cmd_main(argc, argv);
223 if (error && error->code != GOT_ERR_CANCELLED &&
224 error->code != GOT_ERR_PRIVSEP_EXIT &&
225 !(sigpipe_received &&
226 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
227 !(sigint_received &&
228 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
229 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
230 return 1;
233 return 0;
236 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
237 list_commands();
238 return 1;
241 __dead static void
242 usage(int hflag)
244 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
245 getprogname());
246 if (hflag)
247 list_commands();
248 exit(1);
251 static const struct got_error *
252 get_editor(char **abspath)
254 const struct got_error *err = NULL;
255 const char *editor;
257 *abspath = NULL;
259 editor = getenv("VISUAL");
260 if (editor == NULL)
261 editor = getenv("EDITOR");
263 if (editor) {
264 err = got_path_find_prog(abspath, editor);
265 if (err)
266 return err;
269 if (*abspath == NULL) {
270 *abspath = strdup("/bin/ed");
271 if (*abspath == NULL)
272 return got_error_from_errno("strdup");
275 return NULL;
278 static const struct got_error *
279 apply_unveil(const char *repo_path, int repo_read_only,
280 const char *worktree_path)
282 const struct got_error *err;
284 #ifdef PROFILE
285 if (unveil("gmon.out", "rwc") != 0)
286 return got_error_from_errno2("unveil", "gmon.out");
287 #endif
288 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
289 return got_error_from_errno2("unveil", repo_path);
291 if (worktree_path && unveil(worktree_path, "rwc") != 0)
292 return got_error_from_errno2("unveil", worktree_path);
294 if (unveil("/tmp", "rwc") != 0)
295 return got_error_from_errno2("unveil", "/tmp");
297 err = got_privsep_unveil_exec_helpers();
298 if (err != NULL)
299 return err;
301 if (unveil(NULL, NULL) != 0)
302 return got_error_from_errno("unveil");
304 return NULL;
307 __dead static void
308 usage_init(void)
310 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
311 exit(1);
314 static const struct got_error *
315 cmd_init(int argc, char *argv[])
317 const struct got_error *error = NULL;
318 char *repo_path = NULL;
319 int ch;
321 while ((ch = getopt(argc, argv, "")) != -1) {
322 switch (ch) {
323 default:
324 usage_init();
325 /* NOTREACHED */
329 argc -= optind;
330 argv += optind;
332 #ifndef PROFILE
333 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
334 err(1, "pledge");
335 #endif
336 if (argc != 1)
337 usage_init();
339 repo_path = strdup(argv[0]);
340 if (repo_path == NULL)
341 return got_error_from_errno("strdup");
343 got_path_strip_trailing_slashes(repo_path);
345 error = got_path_mkdir(repo_path);
346 if (error &&
347 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
348 goto done;
350 error = apply_unveil(repo_path, 0, NULL);
351 if (error)
352 goto done;
354 error = got_repo_init(repo_path);
355 if (error != NULL)
356 goto done;
358 done:
359 free(repo_path);
360 return error;
363 __dead static void
364 usage_import(void)
366 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
367 "[-r repository-path] [-I pattern] path\n", getprogname());
368 exit(1);
371 int
372 spawn_editor(const char *editor, const char *file)
374 pid_t pid;
375 sig_t sighup, sigint, sigquit;
376 int st = -1;
378 sighup = signal(SIGHUP, SIG_IGN);
379 sigint = signal(SIGINT, SIG_IGN);
380 sigquit = signal(SIGQUIT, SIG_IGN);
382 switch (pid = fork()) {
383 case -1:
384 goto doneediting;
385 case 0:
386 execl(editor, editor, file, (char *)NULL);
387 _exit(127);
390 while (waitpid(pid, &st, 0) == -1)
391 if (errno != EINTR)
392 break;
394 doneediting:
395 (void)signal(SIGHUP, sighup);
396 (void)signal(SIGINT, sigint);
397 (void)signal(SIGQUIT, sigquit);
399 if (!WIFEXITED(st)) {
400 errno = EINTR;
401 return -1;
404 return WEXITSTATUS(st);
407 static const struct got_error *
408 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
409 const char *initial_content)
411 const struct got_error *err = NULL;
412 char buf[1024];
413 struct stat st, st2;
414 FILE *fp;
415 int content_changed = 0;
416 size_t len;
418 *logmsg = NULL;
420 if (stat(logmsg_path, &st) == -1)
421 return got_error_from_errno2("stat", logmsg_path);
423 if (spawn_editor(editor, logmsg_path) == -1)
424 return got_error_from_errno("failed spawning editor");
426 if (stat(logmsg_path, &st2) == -1)
427 return got_error_from_errno("stat");
429 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
430 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
431 "no changes made to commit message, aborting");
433 *logmsg = malloc(st2.st_size + 1);
434 if (*logmsg == NULL)
435 return got_error_from_errno("malloc");
436 (*logmsg)[0] = '\0';
437 len = 0;
439 fp = fopen(logmsg_path, "r");
440 if (fp == NULL) {
441 err = got_error_from_errno("fopen");
442 goto done;
444 while (fgets(buf, sizeof(buf), fp) != NULL) {
445 if (!content_changed && strcmp(buf, initial_content) != 0)
446 content_changed = 1;
447 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
448 continue; /* remove comments and leading empty lines */
449 len = strlcat(*logmsg, buf, st2.st_size);
451 fclose(fp);
453 while (len > 0 && (*logmsg)[len - 1] == '\n') {
454 (*logmsg)[len - 1] = '\0';
455 len--;
458 if (len == 0 || !content_changed)
459 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 "commit message cannot be empty, aborting");
461 done:
462 if (err) {
463 free(*logmsg);
464 *logmsg = NULL;
466 return err;
469 static const struct got_error *
470 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
471 const char *path_dir, const char *branch_name)
473 char *initial_content = NULL;
474 const struct got_error *err = NULL;
475 int fd;
477 if (asprintf(&initial_content,
478 "\n# %s to be imported to branch %s\n", path_dir,
479 branch_name) == -1)
480 return got_error_from_errno("asprintf");
482 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
483 if (err)
484 goto done;
486 dprintf(fd, initial_content);
487 close(fd);
489 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
490 done:
491 free(initial_content);
492 return err;
495 static const struct got_error *
496 import_progress(void *arg, const char *path)
498 printf("A %s\n", path);
499 return NULL;
502 static const struct got_error *
503 get_author(char **author, struct got_repository *repo)
505 const struct got_error *err = NULL;
506 const char *got_author, *name, *email;
508 *author = NULL;
510 name = got_repo_get_gitconfig_author_name(repo);
511 email = got_repo_get_gitconfig_author_email(repo);
512 if (name && email) {
513 if (asprintf(author, "%s <%s>", name, email) == -1)
514 return got_error_from_errno("asprintf");
515 return NULL;
518 got_author = getenv("GOT_AUTHOR");
519 if (got_author == NULL) {
520 name = got_repo_get_global_gitconfig_author_name(repo);
521 email = got_repo_get_global_gitconfig_author_email(repo);
522 if (name && email) {
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
525 return NULL;
527 /* TODO: Look up user in password database? */
528 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
531 *author = strdup(got_author);
532 if (*author == NULL)
533 return got_error_from_errno("strdup");
535 /*
536 * Really dumb email address check; we're only doing this to
537 * avoid git's object parser breaking on commits we create.
538 */
539 while (*got_author && *got_author != '<')
540 got_author++;
541 if (*got_author != '<') {
542 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
543 goto done;
545 while (*got_author && *got_author != '@')
546 got_author++;
547 if (*got_author != '@') {
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
549 goto done;
551 while (*got_author && *got_author != '>')
552 got_author++;
553 if (*got_author != '>')
554 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 done:
556 if (err) {
557 free(*author);
558 *author = NULL;
560 return err;
563 static const struct got_error *
564 get_gitconfig_path(char **gitconfig_path)
566 const char *homedir = getenv("HOME");
568 *gitconfig_path = NULL;
569 if (homedir) {
570 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
571 return got_error_from_errno("asprintf");
574 return NULL;
577 static const struct got_error *
578 cmd_import(int argc, char *argv[])
580 const struct got_error *error = NULL;
581 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
582 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
583 const char *branch_name = "main";
584 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
585 struct got_repository *repo = NULL;
586 struct got_reference *branch_ref = NULL, *head_ref = NULL;
587 struct got_object_id *new_commit_id = NULL;
588 int ch;
589 struct got_pathlist_head ignores;
590 struct got_pathlist_entry *pe;
591 int preserve_logmsg = 0;
593 TAILQ_INIT(&ignores);
595 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
596 switch (ch) {
597 case 'b':
598 branch_name = optarg;
599 break;
600 case 'm':
601 logmsg = strdup(optarg);
602 if (logmsg == NULL) {
603 error = got_error_from_errno("strdup");
604 goto done;
606 break;
607 case 'r':
608 repo_path = realpath(optarg, NULL);
609 if (repo_path == NULL) {
610 error = got_error_from_errno2("realpath",
611 optarg);
612 goto done;
614 break;
615 case 'I':
616 if (optarg[0] == '\0')
617 break;
618 error = got_pathlist_insert(&pe, &ignores, optarg,
619 NULL);
620 if (error)
621 goto done;
622 break;
623 default:
624 usage_import();
625 /* NOTREACHED */
629 argc -= optind;
630 argv += optind;
632 #ifndef PROFILE
633 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
634 "unveil",
635 NULL) == -1)
636 err(1, "pledge");
637 #endif
638 if (argc != 1)
639 usage_import();
641 if (repo_path == NULL) {
642 repo_path = getcwd(NULL, 0);
643 if (repo_path == NULL)
644 return got_error_from_errno("getcwd");
646 got_path_strip_trailing_slashes(repo_path);
647 error = get_gitconfig_path(&gitconfig_path);
648 if (error)
649 goto done;
650 error = got_repo_open(&repo, repo_path, gitconfig_path);
651 if (error)
652 goto done;
654 error = get_author(&author, repo);
655 if (error)
656 return error;
658 /*
659 * Don't let the user create a branch name with a leading '-'.
660 * While technically a valid reference name, this case is usually
661 * an unintended typo.
662 */
663 if (branch_name[0] == '-')
664 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
666 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
667 error = got_error_from_errno("asprintf");
668 goto done;
671 error = got_ref_open(&branch_ref, repo, refname, 0);
672 if (error) {
673 if (error->code != GOT_ERR_NOT_REF)
674 goto done;
675 } else {
676 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
677 "import target branch already exists");
678 goto done;
681 path_dir = realpath(argv[0], NULL);
682 if (path_dir == NULL) {
683 error = got_error_from_errno2("realpath", argv[0]);
684 goto done;
686 got_path_strip_trailing_slashes(path_dir);
688 /*
689 * unveil(2) traverses exec(2); if an editor is used we have
690 * to apply unveil after the log message has been written.
691 */
692 if (logmsg == NULL || strlen(logmsg) == 0) {
693 error = get_editor(&editor);
694 if (error)
695 goto done;
696 free(logmsg);
697 error = collect_import_msg(&logmsg, &logmsg_path, editor,
698 path_dir, refname);
699 if (error) {
700 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
701 logmsg_path != NULL)
702 preserve_logmsg = 1;
703 goto done;
707 if (unveil(path_dir, "r") != 0) {
708 error = got_error_from_errno2("unveil", path_dir);
709 if (logmsg_path)
710 preserve_logmsg = 1;
711 goto done;
714 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
715 if (error) {
716 if (logmsg_path)
717 preserve_logmsg = 1;
718 goto done;
721 error = got_repo_import(&new_commit_id, path_dir, logmsg,
722 author, &ignores, repo, import_progress, NULL);
723 if (error) {
724 if (logmsg_path)
725 preserve_logmsg = 1;
726 goto done;
729 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
730 if (error) {
731 if (logmsg_path)
732 preserve_logmsg = 1;
733 goto done;
736 error = got_ref_write(branch_ref, repo);
737 if (error) {
738 if (logmsg_path)
739 preserve_logmsg = 1;
740 goto done;
743 error = got_object_id_str(&id_str, new_commit_id);
744 if (error) {
745 if (logmsg_path)
746 preserve_logmsg = 1;
747 goto done;
750 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
751 if (error) {
752 if (error->code != GOT_ERR_NOT_REF) {
753 if (logmsg_path)
754 preserve_logmsg = 1;
755 goto done;
758 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
759 branch_ref);
760 if (error) {
761 if (logmsg_path)
762 preserve_logmsg = 1;
763 goto done;
766 error = got_ref_write(head_ref, repo);
767 if (error) {
768 if (logmsg_path)
769 preserve_logmsg = 1;
770 goto done;
774 printf("Created branch %s with commit %s\n",
775 got_ref_get_name(branch_ref), id_str);
776 done:
777 if (preserve_logmsg) {
778 fprintf(stderr, "%s: log message preserved in %s\n",
779 getprogname(), logmsg_path);
780 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
781 error = got_error_from_errno2("unlink", logmsg_path);
782 free(logmsg);
783 free(logmsg_path);
784 free(repo_path);
785 free(editor);
786 free(refname);
787 free(new_commit_id);
788 free(id_str);
789 free(author);
790 free(gitconfig_path);
791 if (branch_ref)
792 got_ref_close(branch_ref);
793 if (head_ref)
794 got_ref_close(head_ref);
795 return error;
798 __dead static void
799 usage_checkout(void)
801 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
802 "[-p prefix] repository-path [worktree-path]\n", getprogname());
803 exit(1);
806 static const struct got_error *
807 checkout_progress(void *arg, unsigned char status, const char *path)
809 char *worktree_path = arg;
811 /* Base commit bump happens silently. */
812 if (status == GOT_STATUS_BUMP_BASE)
813 return NULL;
815 while (path[0] == '/')
816 path++;
818 printf("%c %s/%s\n", status, worktree_path, path);
819 return NULL;
822 static const struct got_error *
823 check_cancelled(void *arg)
825 if (sigint_received || sigpipe_received)
826 return got_error(GOT_ERR_CANCELLED);
827 return NULL;
830 static const struct got_error *
831 check_linear_ancestry(struct got_object_id *commit_id,
832 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
833 struct got_repository *repo)
835 const struct got_error *err = NULL;
836 struct got_object_id *yca_id;
838 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
839 commit_id, base_commit_id, repo, check_cancelled, NULL);
840 if (err)
841 return err;
843 if (yca_id == NULL)
844 return got_error(GOT_ERR_ANCESTRY);
846 /*
847 * Require a straight line of history between the target commit
848 * and the work tree's base commit.
850 * Non-linear situations such as this require a rebase:
852 * (commit) D F (base_commit)
853 * \ /
854 * C E
855 * \ /
856 * B (yca)
857 * |
858 * A
860 * 'got update' only handles linear cases:
861 * Update forwards in time: A (base/yca) - B - C - D (commit)
862 * Update backwards in time: D (base) - C - B - A (commit/yca)
863 */
864 if (allow_forwards_in_time_only) {
865 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
866 return got_error(GOT_ERR_ANCESTRY);
867 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
868 got_object_id_cmp(base_commit_id, yca_id) != 0)
869 return got_error(GOT_ERR_ANCESTRY);
871 free(yca_id);
872 return NULL;
875 static const struct got_error *
876 check_same_branch(struct got_object_id *commit_id,
877 struct got_reference *head_ref, struct got_object_id *yca_id,
878 struct got_repository *repo)
880 const struct got_error *err = NULL;
881 struct got_commit_graph *graph = NULL;
882 struct got_object_id *head_commit_id = NULL;
883 int is_same_branch = 0;
885 err = got_ref_resolve(&head_commit_id, repo, head_ref);
886 if (err)
887 goto done;
889 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
890 is_same_branch = 1;
891 goto done;
893 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
894 is_same_branch = 1;
895 goto done;
898 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
899 if (err)
900 goto done;
902 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
903 check_cancelled, NULL);
904 if (err)
905 goto done;
907 for (;;) {
908 struct got_object_id *id;
909 err = got_commit_graph_iter_next(&id, graph);
910 if (err) {
911 if (err->code == GOT_ERR_ITER_COMPLETED) {
912 err = NULL;
913 break;
914 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
915 break;
916 err = got_commit_graph_fetch_commits(graph, 1,
917 repo, check_cancelled, NULL);
918 if (err)
919 break;
922 if (id) {
923 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
924 break;
925 if (got_object_id_cmp(id, commit_id) == 0) {
926 is_same_branch = 1;
927 break;
931 done:
932 if (graph)
933 got_commit_graph_close(graph);
934 free(head_commit_id);
935 if (!err && !is_same_branch)
936 err = got_error(GOT_ERR_ANCESTRY);
937 return err;
940 static const struct got_error *
941 resolve_commit_arg(struct got_object_id **commit_id,
942 const char *commit_id_arg, struct got_repository *repo)
944 const struct got_error *err;
945 struct got_reference *ref;
946 struct got_tag_object *tag;
948 err = got_repo_object_match_tag(&tag, commit_id_arg,
949 GOT_OBJ_TYPE_COMMIT, repo);
950 if (err == NULL) {
951 *commit_id = got_object_id_dup(
952 got_object_tag_get_object_id(tag));
953 if (*commit_id == NULL)
954 err = got_error_from_errno("got_object_id_dup");
955 got_object_tag_close(tag);
956 return err;
957 } else if (err->code != GOT_ERR_NO_OBJ)
958 return err;
960 err = got_ref_open(&ref, repo, commit_id_arg, 0);
961 if (err == NULL) {
962 err = got_ref_resolve(commit_id, repo, ref);
963 got_ref_close(ref);
964 } else {
965 if (err->code != GOT_ERR_NOT_REF)
966 return err;
967 err = got_repo_match_object_id_prefix(commit_id,
968 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
970 return err;
973 static const struct got_error *
974 cmd_checkout(int argc, char *argv[])
976 const struct got_error *error = NULL;
977 struct got_repository *repo = NULL;
978 struct got_reference *head_ref = NULL;
979 struct got_worktree *worktree = NULL;
980 char *repo_path = NULL;
981 char *worktree_path = NULL;
982 const char *path_prefix = "";
983 const char *branch_name = GOT_REF_HEAD;
984 char *commit_id_str = NULL;
985 int ch, same_path_prefix;
986 struct got_pathlist_head paths;
988 TAILQ_INIT(&paths);
990 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
991 switch (ch) {
992 case 'b':
993 branch_name = optarg;
994 break;
995 case 'c':
996 commit_id_str = strdup(optarg);
997 if (commit_id_str == NULL)
998 return got_error_from_errno("strdup");
999 break;
1000 case 'p':
1001 path_prefix = optarg;
1002 break;
1003 default:
1004 usage_checkout();
1005 /* NOTREACHED */
1009 argc -= optind;
1010 argv += optind;
1012 #ifndef PROFILE
1013 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1014 "unveil", NULL) == -1)
1015 err(1, "pledge");
1016 #endif
1017 if (argc == 1) {
1018 char *cwd, *base, *dotgit;
1019 repo_path = realpath(argv[0], NULL);
1020 if (repo_path == NULL)
1021 return got_error_from_errno2("realpath", argv[0]);
1022 cwd = getcwd(NULL, 0);
1023 if (cwd == NULL) {
1024 error = got_error_from_errno("getcwd");
1025 goto done;
1027 if (path_prefix[0]) {
1028 base = basename(path_prefix);
1029 if (base == NULL) {
1030 error = got_error_from_errno2("basename",
1031 path_prefix);
1032 goto done;
1034 } else {
1035 base = basename(repo_path);
1036 if (base == NULL) {
1037 error = got_error_from_errno2("basename",
1038 repo_path);
1039 goto done;
1042 dotgit = strstr(base, ".git");
1043 if (dotgit)
1044 *dotgit = '\0';
1045 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1046 error = got_error_from_errno("asprintf");
1047 free(cwd);
1048 goto done;
1050 free(cwd);
1051 } else if (argc == 2) {
1052 repo_path = realpath(argv[0], NULL);
1053 if (repo_path == NULL) {
1054 error = got_error_from_errno2("realpath", argv[0]);
1055 goto done;
1057 worktree_path = realpath(argv[1], NULL);
1058 if (worktree_path == NULL) {
1059 if (errno != ENOENT) {
1060 error = got_error_from_errno2("realpath",
1061 argv[1]);
1062 goto done;
1064 worktree_path = strdup(argv[1]);
1065 if (worktree_path == NULL) {
1066 error = got_error_from_errno("strdup");
1067 goto done;
1070 } else
1071 usage_checkout();
1073 got_path_strip_trailing_slashes(repo_path);
1074 got_path_strip_trailing_slashes(worktree_path);
1076 error = got_repo_open(&repo, repo_path, NULL);
1077 if (error != NULL)
1078 goto done;
1080 /* Pre-create work tree path for unveil(2) */
1081 error = got_path_mkdir(worktree_path);
1082 if (error) {
1083 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1084 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1085 goto done;
1086 if (!got_path_dir_is_empty(worktree_path)) {
1087 error = got_error_path(worktree_path,
1088 GOT_ERR_DIR_NOT_EMPTY);
1089 goto done;
1093 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1094 if (error)
1095 goto done;
1097 error = got_ref_open(&head_ref, repo, branch_name, 0);
1098 if (error != NULL)
1099 goto done;
1101 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1102 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1103 goto done;
1105 error = got_worktree_open(&worktree, worktree_path);
1106 if (error != NULL)
1107 goto done;
1109 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1110 path_prefix);
1111 if (error != NULL)
1112 goto done;
1113 if (!same_path_prefix) {
1114 error = got_error(GOT_ERR_PATH_PREFIX);
1115 goto done;
1118 if (commit_id_str) {
1119 struct got_object_id *commit_id;
1120 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1121 if (error)
1122 goto done;
1123 error = check_linear_ancestry(commit_id,
1124 got_worktree_get_base_commit_id(worktree), 0, repo);
1125 if (error != NULL) {
1126 free(commit_id);
1127 goto done;
1129 error = check_same_branch(commit_id, head_ref, NULL, repo);
1130 if (error)
1131 goto done;
1132 error = got_worktree_set_base_commit_id(worktree, repo,
1133 commit_id);
1134 free(commit_id);
1135 if (error)
1136 goto done;
1139 error = got_pathlist_append(&paths, "", NULL);
1140 if (error)
1141 goto done;
1142 error = got_worktree_checkout_files(worktree, &paths, repo,
1143 checkout_progress, worktree_path, check_cancelled, NULL);
1144 if (error != NULL)
1145 goto done;
1147 printf("Now shut up and hack\n");
1149 done:
1150 got_pathlist_free(&paths);
1151 free(commit_id_str);
1152 free(repo_path);
1153 free(worktree_path);
1154 return error;
1157 __dead static void
1158 usage_update(void)
1160 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1161 getprogname());
1162 exit(1);
1165 static const struct got_error *
1166 update_progress(void *arg, unsigned char status, const char *path)
1168 int *did_something = arg;
1170 if (status == GOT_STATUS_EXISTS)
1171 return NULL;
1173 *did_something = 1;
1175 /* Base commit bump happens silently. */
1176 if (status == GOT_STATUS_BUMP_BASE)
1177 return NULL;
1179 while (path[0] == '/')
1180 path++;
1181 printf("%c %s\n", status, path);
1182 return NULL;
1185 static const struct got_error *
1186 switch_head_ref(struct got_reference *head_ref,
1187 struct got_object_id *commit_id, struct got_worktree *worktree,
1188 struct got_repository *repo)
1190 const struct got_error *err = NULL;
1191 char *base_id_str;
1192 int ref_has_moved = 0;
1194 /* Trivial case: switching between two different references. */
1195 if (strcmp(got_ref_get_name(head_ref),
1196 got_worktree_get_head_ref_name(worktree)) != 0) {
1197 printf("Switching work tree from %s to %s\n",
1198 got_worktree_get_head_ref_name(worktree),
1199 got_ref_get_name(head_ref));
1200 return got_worktree_set_head_ref(worktree, head_ref);
1203 err = check_linear_ancestry(commit_id,
1204 got_worktree_get_base_commit_id(worktree), 0, repo);
1205 if (err) {
1206 if (err->code != GOT_ERR_ANCESTRY)
1207 return err;
1208 ref_has_moved = 1;
1210 if (!ref_has_moved)
1211 return NULL;
1213 /* Switching to a rebased branch with the same reference name. */
1214 err = got_object_id_str(&base_id_str,
1215 got_worktree_get_base_commit_id(worktree));
1216 if (err)
1217 return err;
1218 printf("Reference %s now points at a different branch\n",
1219 got_worktree_get_head_ref_name(worktree));
1220 printf("Switching work tree from %s to %s\n", base_id_str,
1221 got_worktree_get_head_ref_name(worktree));
1222 return NULL;
1225 static const struct got_error *
1226 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1228 const struct got_error *err;
1229 int in_progress;
1231 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1232 if (err)
1233 return err;
1234 if (in_progress)
1235 return got_error(GOT_ERR_REBASING);
1237 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1238 if (err)
1239 return err;
1240 if (in_progress)
1241 return got_error(GOT_ERR_HISTEDIT_BUSY);
1243 return NULL;
1246 static const struct got_error *
1247 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1248 char *argv[], struct got_worktree *worktree)
1250 const struct got_error *err = NULL;
1251 char *path;
1252 int i;
1254 if (argc == 0) {
1255 path = strdup("");
1256 if (path == NULL)
1257 return got_error_from_errno("strdup");
1258 return got_pathlist_append(paths, path, NULL);
1261 for (i = 0; i < argc; i++) {
1262 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1263 if (err)
1264 break;
1265 err = got_pathlist_append(paths, path, NULL);
1266 if (err) {
1267 free(path);
1268 break;
1272 return err;
1275 static const struct got_error *
1276 cmd_update(int argc, char *argv[])
1278 const struct got_error *error = NULL;
1279 struct got_repository *repo = NULL;
1280 struct got_worktree *worktree = NULL;
1281 char *worktree_path = NULL;
1282 struct got_object_id *commit_id = NULL;
1283 char *commit_id_str = NULL;
1284 const char *branch_name = NULL;
1285 struct got_reference *head_ref = NULL;
1286 struct got_pathlist_head paths;
1287 struct got_pathlist_entry *pe;
1288 int ch, did_something = 0;
1290 TAILQ_INIT(&paths);
1292 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1293 switch (ch) {
1294 case 'b':
1295 branch_name = optarg;
1296 break;
1297 case 'c':
1298 commit_id_str = strdup(optarg);
1299 if (commit_id_str == NULL)
1300 return got_error_from_errno("strdup");
1301 break;
1302 default:
1303 usage_update();
1304 /* NOTREACHED */
1308 argc -= optind;
1309 argv += optind;
1311 #ifndef PROFILE
1312 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1313 "unveil", NULL) == -1)
1314 err(1, "pledge");
1315 #endif
1316 worktree_path = getcwd(NULL, 0);
1317 if (worktree_path == NULL) {
1318 error = got_error_from_errno("getcwd");
1319 goto done;
1321 error = got_worktree_open(&worktree, worktree_path);
1322 if (error)
1323 goto done;
1325 error = check_rebase_or_histedit_in_progress(worktree);
1326 if (error)
1327 goto done;
1329 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1330 NULL);
1331 if (error != NULL)
1332 goto done;
1334 error = apply_unveil(got_repo_get_path(repo), 0,
1335 got_worktree_get_root_path(worktree));
1336 if (error)
1337 goto done;
1339 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1340 if (error)
1341 goto done;
1343 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1344 got_worktree_get_head_ref_name(worktree), 0);
1345 if (error != NULL)
1346 goto done;
1347 if (commit_id_str == NULL) {
1348 error = got_ref_resolve(&commit_id, repo, head_ref);
1349 if (error != NULL)
1350 goto done;
1351 error = got_object_id_str(&commit_id_str, commit_id);
1352 if (error != NULL)
1353 goto done;
1354 } else {
1355 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1356 free(commit_id_str);
1357 commit_id_str = NULL;
1358 if (error)
1359 goto done;
1360 error = got_object_id_str(&commit_id_str, commit_id);
1361 if (error)
1362 goto done;
1365 if (branch_name) {
1366 struct got_object_id *head_commit_id;
1367 TAILQ_FOREACH(pe, &paths, entry) {
1368 if (pe->path_len == 0)
1369 continue;
1370 error = got_error_msg(GOT_ERR_BAD_PATH,
1371 "switching between branches requires that "
1372 "the entire work tree gets updated");
1373 goto done;
1375 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1376 if (error)
1377 goto done;
1378 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1379 repo);
1380 free(head_commit_id);
1381 if (error != NULL)
1382 goto done;
1383 error = check_same_branch(commit_id, head_ref, NULL, repo);
1384 if (error)
1385 goto done;
1386 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1387 if (error)
1388 goto done;
1389 } else {
1390 error = check_linear_ancestry(commit_id,
1391 got_worktree_get_base_commit_id(worktree), 0, repo);
1392 if (error != NULL) {
1393 if (error->code == GOT_ERR_ANCESTRY)
1394 error = got_error(GOT_ERR_BRANCH_MOVED);
1395 goto done;
1397 error = check_same_branch(commit_id, head_ref, NULL, repo);
1398 if (error)
1399 goto done;
1402 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1403 commit_id) != 0) {
1404 error = got_worktree_set_base_commit_id(worktree, repo,
1405 commit_id);
1406 if (error)
1407 goto done;
1410 error = got_worktree_checkout_files(worktree, &paths, repo,
1411 update_progress, &did_something, check_cancelled, NULL);
1412 if (error != NULL)
1413 goto done;
1415 if (did_something)
1416 printf("Updated to commit %s\n", commit_id_str);
1417 else
1418 printf("Already up-to-date\n");
1419 done:
1420 free(worktree_path);
1421 TAILQ_FOREACH(pe, &paths, entry)
1422 free((char *)pe->path);
1423 got_pathlist_free(&paths);
1424 free(commit_id);
1425 free(commit_id_str);
1426 return error;
1429 static const struct got_error *
1430 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1431 const char *path, int diff_context, int ignore_whitespace,
1432 struct got_repository *repo)
1434 const struct got_error *err = NULL;
1435 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1437 if (blob_id1) {
1438 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1439 if (err)
1440 goto done;
1443 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1444 if (err)
1445 goto done;
1447 while (path[0] == '/')
1448 path++;
1449 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1450 ignore_whitespace, stdout);
1451 done:
1452 if (blob1)
1453 got_object_blob_close(blob1);
1454 got_object_blob_close(blob2);
1455 return err;
1458 static const struct got_error *
1459 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1460 const char *path, int diff_context, int ignore_whitespace,
1461 struct got_repository *repo)
1463 const struct got_error *err = NULL;
1464 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1465 struct got_diff_blob_output_unidiff_arg arg;
1467 if (tree_id1) {
1468 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1469 if (err)
1470 goto done;
1473 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1474 if (err)
1475 goto done;
1477 arg.diff_context = diff_context;
1478 arg.ignore_whitespace = ignore_whitespace;
1479 arg.outfile = stdout;
1480 while (path[0] == '/')
1481 path++;
1482 err = got_diff_tree(tree1, tree2, path, path, repo,
1483 got_diff_blob_output_unidiff, &arg, 1);
1484 done:
1485 if (tree1)
1486 got_object_tree_close(tree1);
1487 if (tree2)
1488 got_object_tree_close(tree2);
1489 return err;
1492 static const struct got_error *
1493 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1494 const char *path, int diff_context, struct got_repository *repo)
1496 const struct got_error *err = NULL;
1497 struct got_commit_object *pcommit = NULL;
1498 char *id_str1 = NULL, *id_str2 = NULL;
1499 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1500 struct got_object_qid *qid;
1502 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1503 if (qid != NULL) {
1504 err = got_object_open_as_commit(&pcommit, repo,
1505 qid->id);
1506 if (err)
1507 return err;
1510 if (path && path[0] != '\0') {
1511 int obj_type;
1512 err = got_object_id_by_path(&obj_id2, repo, id, path);
1513 if (err)
1514 goto done;
1515 err = got_object_id_str(&id_str2, obj_id2);
1516 if (err) {
1517 free(obj_id2);
1518 goto done;
1520 if (pcommit) {
1521 err = got_object_id_by_path(&obj_id1, repo,
1522 qid->id, path);
1523 if (err) {
1524 free(obj_id2);
1525 goto done;
1527 err = got_object_id_str(&id_str1, obj_id1);
1528 if (err) {
1529 free(obj_id2);
1530 goto done;
1533 err = got_object_get_type(&obj_type, repo, obj_id2);
1534 if (err) {
1535 free(obj_id2);
1536 goto done;
1538 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1539 switch (obj_type) {
1540 case GOT_OBJ_TYPE_BLOB:
1541 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1542 0, repo);
1543 break;
1544 case GOT_OBJ_TYPE_TREE:
1545 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1546 0, repo);
1547 break;
1548 default:
1549 err = got_error(GOT_ERR_OBJ_TYPE);
1550 break;
1552 free(obj_id1);
1553 free(obj_id2);
1554 } else {
1555 obj_id2 = got_object_commit_get_tree_id(commit);
1556 err = got_object_id_str(&id_str2, obj_id2);
1557 if (err)
1558 goto done;
1559 obj_id1 = got_object_commit_get_tree_id(pcommit);
1560 err = got_object_id_str(&id_str1, obj_id1);
1561 if (err)
1562 goto done;
1563 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1564 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1567 done:
1568 free(id_str1);
1569 free(id_str2);
1570 if (pcommit)
1571 got_object_commit_close(pcommit);
1572 return err;
1575 static char *
1576 get_datestr(time_t *time, char *datebuf)
1578 struct tm mytm, *tm;
1579 char *p, *s;
1581 tm = gmtime_r(time, &mytm);
1582 if (tm == NULL)
1583 return NULL;
1584 s = asctime_r(tm, datebuf);
1585 if (s == NULL)
1586 return NULL;
1587 p = strchr(s, '\n');
1588 if (p)
1589 *p = '\0';
1590 return s;
1593 static const struct got_error *
1594 match_logmsg(int *have_match, struct got_object_id *id,
1595 struct got_commit_object *commit, regex_t *regex)
1597 const struct got_error *err = NULL;
1598 regmatch_t regmatch;
1599 char *id_str = NULL, *logmsg = NULL;
1601 *have_match = 0;
1603 err = got_object_id_str(&id_str, id);
1604 if (err)
1605 return err;
1607 err = got_object_commit_get_logmsg(&logmsg, commit);
1608 if (err)
1609 goto done;
1611 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1612 *have_match = 1;
1613 done:
1614 free(id_str);
1615 free(logmsg);
1616 return err;
1619 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1621 static const struct got_error *
1622 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1623 struct got_repository *repo, const char *path, int show_patch,
1624 int diff_context, struct got_reflist_head *refs)
1626 const struct got_error *err = NULL;
1627 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1628 char datebuf[26];
1629 time_t committer_time;
1630 const char *author, *committer;
1631 char *refs_str = NULL;
1632 struct got_reflist_entry *re;
1634 SIMPLEQ_FOREACH(re, refs, entry) {
1635 char *s;
1636 const char *name;
1637 struct got_tag_object *tag = NULL;
1638 int cmp;
1640 name = got_ref_get_name(re->ref);
1641 if (strcmp(name, GOT_REF_HEAD) == 0)
1642 continue;
1643 if (strncmp(name, "refs/", 5) == 0)
1644 name += 5;
1645 if (strncmp(name, "got/", 4) == 0)
1646 continue;
1647 if (strncmp(name, "heads/", 6) == 0)
1648 name += 6;
1649 if (strncmp(name, "remotes/", 8) == 0)
1650 name += 8;
1651 if (strncmp(name, "tags/", 5) == 0) {
1652 err = got_object_open_as_tag(&tag, repo, re->id);
1653 if (err) {
1654 if (err->code != GOT_ERR_OBJ_TYPE)
1655 return err;
1656 /* Ref points at something other than a tag. */
1657 err = NULL;
1658 tag = NULL;
1661 cmp = got_object_id_cmp(tag ?
1662 got_object_tag_get_object_id(tag) : re->id, id);
1663 if (tag)
1664 got_object_tag_close(tag);
1665 if (cmp != 0)
1666 continue;
1667 s = refs_str;
1668 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1669 name) == -1) {
1670 err = got_error_from_errno("asprintf");
1671 free(s);
1672 return err;
1674 free(s);
1676 err = got_object_id_str(&id_str, id);
1677 if (err)
1678 return err;
1680 printf(GOT_COMMIT_SEP_STR);
1681 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1682 refs_str ? refs_str : "", refs_str ? ")" : "");
1683 free(id_str);
1684 id_str = NULL;
1685 free(refs_str);
1686 refs_str = NULL;
1687 printf("from: %s\n", got_object_commit_get_author(commit));
1688 committer_time = got_object_commit_get_committer_time(commit);
1689 datestr = get_datestr(&committer_time, datebuf);
1690 if (datestr)
1691 printf("date: %s UTC\n", datestr);
1692 author = got_object_commit_get_author(commit);
1693 committer = got_object_commit_get_committer(commit);
1694 if (strcmp(author, committer) != 0)
1695 printf("via: %s\n", committer);
1696 if (got_object_commit_get_nparents(commit) > 1) {
1697 const struct got_object_id_queue *parent_ids;
1698 struct got_object_qid *qid;
1699 int n = 1;
1700 parent_ids = got_object_commit_get_parent_ids(commit);
1701 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1702 err = got_object_id_str(&id_str, qid->id);
1703 if (err)
1704 return err;
1705 printf("parent %d: %s\n", n++, id_str);
1706 free(id_str);
1710 err = got_object_commit_get_logmsg(&logmsg0, commit);
1711 if (err)
1712 return err;
1714 logmsg = logmsg0;
1715 do {
1716 line = strsep(&logmsg, "\n");
1717 if (line)
1718 printf(" %s\n", line);
1719 } while (line);
1720 free(logmsg0);
1722 if (show_patch) {
1723 err = print_patch(commit, id, path, diff_context, repo);
1724 if (err == 0)
1725 printf("\n");
1728 if (fflush(stdout) != 0 && err == NULL)
1729 err = got_error_from_errno("fflush");
1730 return err;
1733 static const struct got_error *
1734 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1735 char *path, int show_patch, char *search_pattern, int diff_context,
1736 int limit, int first_parent_traversal, struct got_reflist_head *refs)
1738 const struct got_error *err;
1739 struct got_commit_graph *graph;
1740 regex_t regex;
1741 int have_match;
1743 if (search_pattern &&
1744 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1745 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1747 err = got_commit_graph_open(&graph, root_id, path,
1748 first_parent_traversal, repo);
1749 if (err)
1750 return err;
1751 err = got_commit_graph_iter_start(graph, root_id, repo,
1752 check_cancelled, NULL);
1753 if (err)
1754 goto done;
1755 for (;;) {
1756 struct got_commit_object *commit;
1757 struct got_object_id *id;
1759 if (sigint_received || sigpipe_received)
1760 break;
1762 err = got_commit_graph_iter_next(&id, graph);
1763 if (err) {
1764 if (err->code == GOT_ERR_ITER_COMPLETED) {
1765 err = NULL;
1766 break;
1768 if (err->code != GOT_ERR_ITER_NEED_MORE)
1769 break;
1770 err = got_commit_graph_fetch_commits(graph, 1, repo,
1771 check_cancelled, NULL);
1772 if (err)
1773 break;
1774 else
1775 continue;
1777 if (id == NULL)
1778 break;
1780 err = got_object_open_as_commit(&commit, repo, id);
1781 if (err)
1782 break;
1784 if (search_pattern) {
1785 err = match_logmsg(&have_match, id, commit, &regex);
1786 if (err) {
1787 got_object_commit_close(commit);
1788 break;
1790 if (have_match == 0) {
1791 got_object_commit_close(commit);
1792 continue;
1796 err = print_commit(commit, id, repo, path, show_patch,
1797 diff_context, refs);
1798 got_object_commit_close(commit);
1799 if (err || (limit && --limit == 0))
1800 break;
1802 done:
1803 if (search_pattern)
1804 regfree(&regex);
1805 got_commit_graph_close(graph);
1806 return err;
1809 __dead static void
1810 usage_log(void)
1812 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1813 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1814 exit(1);
1817 static int
1818 get_default_log_limit(void)
1820 const char *got_default_log_limit;
1821 long long n;
1822 const char *errstr;
1824 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1825 if (got_default_log_limit == NULL)
1826 return 0;
1827 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1828 if (errstr != NULL)
1829 return 0;
1830 return n;
1833 static const struct got_error *
1834 cmd_log(int argc, char *argv[])
1836 const struct got_error *error;
1837 struct got_repository *repo = NULL;
1838 struct got_worktree *worktree = NULL;
1839 struct got_commit_object *commit = NULL;
1840 struct got_object_id *id = NULL;
1841 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1842 char *start_commit = NULL, *search_pattern = NULL;
1843 int diff_context = -1, ch;
1844 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1845 const char *errstr;
1846 struct got_reflist_head refs;
1848 SIMPLEQ_INIT(&refs);
1850 #ifndef PROFILE
1851 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1852 NULL)
1853 == -1)
1854 err(1, "pledge");
1855 #endif
1857 limit = get_default_log_limit();
1859 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1860 switch (ch) {
1861 case 'p':
1862 show_patch = 1;
1863 break;
1864 case 'c':
1865 start_commit = optarg;
1866 break;
1867 case 'C':
1868 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1869 &errstr);
1870 if (errstr != NULL)
1871 err(1, "-C option %s", errstr);
1872 break;
1873 case 'l':
1874 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1875 if (errstr != NULL)
1876 err(1, "-l option %s", errstr);
1877 break;
1878 case 'f':
1879 first_parent_traversal = 1;
1880 break;
1881 case 'r':
1882 repo_path = realpath(optarg, NULL);
1883 if (repo_path == NULL)
1884 return got_error_from_errno2("realpath",
1885 optarg);
1886 got_path_strip_trailing_slashes(repo_path);
1887 break;
1888 case 's':
1889 search_pattern = optarg;
1890 break;
1891 default:
1892 usage_log();
1893 /* NOTREACHED */
1897 argc -= optind;
1898 argv += optind;
1900 if (diff_context == -1)
1901 diff_context = 3;
1902 else if (!show_patch)
1903 errx(1, "-C reguires -p");
1905 cwd = getcwd(NULL, 0);
1906 if (cwd == NULL) {
1907 error = got_error_from_errno("getcwd");
1908 goto done;
1911 error = got_worktree_open(&worktree, cwd);
1912 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1913 goto done;
1914 error = NULL;
1916 if (argc == 0) {
1917 path = strdup("");
1918 if (path == NULL) {
1919 error = got_error_from_errno("strdup");
1920 goto done;
1922 } else if (argc == 1) {
1923 if (worktree) {
1924 error = got_worktree_resolve_path(&path, worktree,
1925 argv[0]);
1926 if (error)
1927 goto done;
1928 } else {
1929 path = strdup(argv[0]);
1930 if (path == NULL) {
1931 error = got_error_from_errno("strdup");
1932 goto done;
1935 } else
1936 usage_log();
1938 if (repo_path == NULL) {
1939 repo_path = worktree ?
1940 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1942 if (repo_path == NULL) {
1943 error = got_error_from_errno("strdup");
1944 goto done;
1947 error = got_repo_open(&repo, repo_path, NULL);
1948 if (error != NULL)
1949 goto done;
1951 error = apply_unveil(got_repo_get_path(repo), 1,
1952 worktree ? got_worktree_get_root_path(worktree) : NULL);
1953 if (error)
1954 goto done;
1956 if (start_commit == NULL) {
1957 struct got_reference *head_ref;
1958 error = got_ref_open(&head_ref, repo,
1959 worktree ? got_worktree_get_head_ref_name(worktree)
1960 : GOT_REF_HEAD, 0);
1961 if (error != NULL)
1962 return error;
1963 error = got_ref_resolve(&id, repo, head_ref);
1964 got_ref_close(head_ref);
1965 if (error != NULL)
1966 return error;
1967 error = got_object_open_as_commit(&commit, repo, id);
1968 } else {
1969 struct got_reference *ref;
1970 error = got_ref_open(&ref, repo, start_commit, 0);
1971 if (error == NULL) {
1972 int obj_type;
1973 error = got_ref_resolve(&id, repo, ref);
1974 got_ref_close(ref);
1975 if (error != NULL)
1976 goto done;
1977 error = got_object_get_type(&obj_type, repo, id);
1978 if (error != NULL)
1979 goto done;
1980 if (obj_type == GOT_OBJ_TYPE_TAG) {
1981 struct got_tag_object *tag;
1982 error = got_object_open_as_tag(&tag, repo, id);
1983 if (error != NULL)
1984 goto done;
1985 if (got_object_tag_get_object_type(tag) !=
1986 GOT_OBJ_TYPE_COMMIT) {
1987 got_object_tag_close(tag);
1988 error = got_error(GOT_ERR_OBJ_TYPE);
1989 goto done;
1991 free(id);
1992 id = got_object_id_dup(
1993 got_object_tag_get_object_id(tag));
1994 if (id == NULL)
1995 error = got_error_from_errno(
1996 "got_object_id_dup");
1997 got_object_tag_close(tag);
1998 if (error)
1999 goto done;
2000 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2001 error = got_error(GOT_ERR_OBJ_TYPE);
2002 goto done;
2004 error = got_object_open_as_commit(&commit, repo, id);
2005 if (error != NULL)
2006 goto done;
2008 if (commit == NULL) {
2009 error = got_repo_match_object_id_prefix(&id,
2010 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2011 if (error != NULL)
2012 return error;
2015 if (error != NULL)
2016 goto done;
2018 if (worktree) {
2019 const char *prefix = got_worktree_get_path_prefix(worktree);
2020 char *p;
2021 if (asprintf(&p, "%s%s%s", prefix,
2022 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2023 error = got_error_from_errno("asprintf");
2024 goto done;
2026 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2027 free(p);
2028 } else
2029 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2030 if (error != NULL)
2031 goto done;
2032 if (in_repo_path) {
2033 free(path);
2034 path = in_repo_path;
2037 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2038 if (error)
2039 goto done;
2041 error = print_commits(id, repo, path, show_patch, search_pattern,
2042 diff_context, limit, first_parent_traversal, &refs);
2043 done:
2044 free(path);
2045 free(repo_path);
2046 free(cwd);
2047 free(id);
2048 if (worktree)
2049 got_worktree_close(worktree);
2050 if (repo) {
2051 const struct got_error *repo_error;
2052 repo_error = got_repo_close(repo);
2053 if (error == NULL)
2054 error = repo_error;
2056 got_ref_list_free(&refs);
2057 return error;
2060 __dead static void
2061 usage_diff(void)
2063 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2064 "[-w] [object1 object2 | path]\n", getprogname());
2065 exit(1);
2068 struct print_diff_arg {
2069 struct got_repository *repo;
2070 struct got_worktree *worktree;
2071 int diff_context;
2072 const char *id_str;
2073 int header_shown;
2074 int diff_staged;
2075 int ignore_whitespace;
2078 static const struct got_error *
2079 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2080 const char *path, struct got_object_id *blob_id,
2081 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2083 struct print_diff_arg *a = arg;
2084 const struct got_error *err = NULL;
2085 struct got_blob_object *blob1 = NULL;
2086 FILE *f2 = NULL;
2087 char *abspath = NULL, *label1 = NULL;
2088 struct stat sb;
2090 if (a->diff_staged) {
2091 if (staged_status != GOT_STATUS_MODIFY &&
2092 staged_status != GOT_STATUS_ADD &&
2093 staged_status != GOT_STATUS_DELETE)
2094 return NULL;
2095 } else {
2096 if (staged_status == GOT_STATUS_DELETE)
2097 return NULL;
2098 if (status == GOT_STATUS_NONEXISTENT)
2099 return got_error_set_errno(ENOENT, path);
2100 if (status != GOT_STATUS_MODIFY &&
2101 status != GOT_STATUS_ADD &&
2102 status != GOT_STATUS_DELETE &&
2103 status != GOT_STATUS_CONFLICT)
2104 return NULL;
2107 if (!a->header_shown) {
2108 printf("diff %s %s%s\n", a->id_str,
2109 got_worktree_get_root_path(a->worktree),
2110 a->diff_staged ? " (staged changes)" : "");
2111 a->header_shown = 1;
2114 if (a->diff_staged) {
2115 const char *label1 = NULL, *label2 = NULL;
2116 switch (staged_status) {
2117 case GOT_STATUS_MODIFY:
2118 label1 = path;
2119 label2 = path;
2120 break;
2121 case GOT_STATUS_ADD:
2122 label2 = path;
2123 break;
2124 case GOT_STATUS_DELETE:
2125 label1 = path;
2126 break;
2127 default:
2128 return got_error(GOT_ERR_FILE_STATUS);
2130 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2131 label1, label2, a->diff_context, a->ignore_whitespace,
2132 a->repo, stdout);
2135 if (staged_status == GOT_STATUS_ADD ||
2136 staged_status == GOT_STATUS_MODIFY) {
2137 char *id_str;
2138 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2139 8192);
2140 if (err)
2141 goto done;
2142 err = got_object_id_str(&id_str, staged_blob_id);
2143 if (err)
2144 goto done;
2145 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2146 err = got_error_from_errno("asprintf");
2147 free(id_str);
2148 goto done;
2150 free(id_str);
2151 } else if (status != GOT_STATUS_ADD) {
2152 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2153 if (err)
2154 goto done;
2157 if (status != GOT_STATUS_DELETE) {
2158 if (asprintf(&abspath, "%s/%s",
2159 got_worktree_get_root_path(a->worktree), path) == -1) {
2160 err = got_error_from_errno("asprintf");
2161 goto done;
2164 f2 = fopen(abspath, "r");
2165 if (f2 == NULL) {
2166 err = got_error_from_errno2("fopen", abspath);
2167 goto done;
2169 if (lstat(abspath, &sb) == -1) {
2170 err = got_error_from_errno2("lstat", abspath);
2171 goto done;
2173 } else
2174 sb.st_size = 0;
2176 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2177 a->diff_context, a->ignore_whitespace, stdout);
2178 done:
2179 if (blob1)
2180 got_object_blob_close(blob1);
2181 if (f2 && fclose(f2) != 0 && err == NULL)
2182 err = got_error_from_errno("fclose");
2183 free(abspath);
2184 return err;
2187 static const struct got_error *
2188 match_object_id(struct got_object_id **id, char **label,
2189 const char *id_str, int obj_type, int resolve_tags,
2190 struct got_repository *repo)
2192 const struct got_error *err;
2193 struct got_tag_object *tag;
2194 struct got_reference *ref = NULL;
2196 *id = NULL;
2197 *label = NULL;
2199 if (resolve_tags) {
2200 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2201 repo);
2202 if (err == NULL) {
2203 *id = got_object_id_dup(
2204 got_object_tag_get_object_id(tag));
2205 if (*id == NULL)
2206 err = got_error_from_errno("got_object_id_dup");
2207 else if (asprintf(label, "refs/tags/%s",
2208 got_object_tag_get_name(tag)) == -1) {
2209 err = got_error_from_errno("asprintf");
2210 free(*id);
2211 *id = NULL;
2213 got_object_tag_close(tag);
2214 return err;
2215 } else if (err->code != GOT_ERR_NO_OBJ)
2216 return err;
2219 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2220 if (err) {
2221 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2222 return err;
2223 err = got_ref_open(&ref, repo, id_str, 0);
2224 if (err != NULL)
2225 goto done;
2226 *label = strdup(got_ref_get_name(ref));
2227 if (*label == NULL) {
2228 err = got_error_from_errno("strdup");
2229 goto done;
2231 err = got_ref_resolve(id, repo, ref);
2232 } else {
2233 err = got_object_id_str(label, *id);
2234 if (*label == NULL) {
2235 err = got_error_from_errno("strdup");
2236 goto done;
2239 done:
2240 if (ref)
2241 got_ref_close(ref);
2242 return err;
2246 static const struct got_error *
2247 cmd_diff(int argc, char *argv[])
2249 const struct got_error *error;
2250 struct got_repository *repo = NULL;
2251 struct got_worktree *worktree = NULL;
2252 char *cwd = NULL, *repo_path = NULL;
2253 struct got_object_id *id1 = NULL, *id2 = NULL;
2254 const char *id_str1 = NULL, *id_str2 = NULL;
2255 char *label1 = NULL, *label2 = NULL;
2256 int type1, type2;
2257 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2258 const char *errstr;
2259 char *path = NULL;
2261 #ifndef PROFILE
2262 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2263 NULL) == -1)
2264 err(1, "pledge");
2265 #endif
2267 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2268 switch (ch) {
2269 case 'C':
2270 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2271 &errstr);
2272 if (errstr != NULL)
2273 err(1, "-C option %s", errstr);
2274 break;
2275 case 'r':
2276 repo_path = realpath(optarg, NULL);
2277 if (repo_path == NULL)
2278 return got_error_from_errno2("realpath",
2279 optarg);
2280 got_path_strip_trailing_slashes(repo_path);
2281 break;
2282 case 's':
2283 diff_staged = 1;
2284 break;
2285 case 'w':
2286 ignore_whitespace = 1;
2287 break;
2288 default:
2289 usage_diff();
2290 /* NOTREACHED */
2294 argc -= optind;
2295 argv += optind;
2297 cwd = getcwd(NULL, 0);
2298 if (cwd == NULL) {
2299 error = got_error_from_errno("getcwd");
2300 goto done;
2302 error = got_worktree_open(&worktree, cwd);
2303 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2304 goto done;
2305 if (argc <= 1) {
2306 if (worktree == NULL) {
2307 error = got_error(GOT_ERR_NOT_WORKTREE);
2308 goto done;
2310 if (repo_path)
2311 errx(1,
2312 "-r option can't be used when diffing a work tree");
2313 repo_path = strdup(got_worktree_get_repo_path(worktree));
2314 if (repo_path == NULL) {
2315 error = got_error_from_errno("strdup");
2316 goto done;
2318 if (argc == 1) {
2319 error = got_worktree_resolve_path(&path, worktree,
2320 argv[0]);
2321 if (error)
2322 goto done;
2323 } else {
2324 path = strdup("");
2325 if (path == NULL) {
2326 error = got_error_from_errno("strdup");
2327 goto done;
2330 } else if (argc == 2) {
2331 if (diff_staged)
2332 errx(1, "-s option can't be used when diffing "
2333 "objects in repository");
2334 id_str1 = argv[0];
2335 id_str2 = argv[1];
2336 if (worktree && repo_path == NULL) {
2337 repo_path =
2338 strdup(got_worktree_get_repo_path(worktree));
2339 if (repo_path == NULL) {
2340 error = got_error_from_errno("strdup");
2341 goto done;
2344 } else
2345 usage_diff();
2347 if (repo_path == NULL) {
2348 repo_path = getcwd(NULL, 0);
2349 if (repo_path == NULL)
2350 return got_error_from_errno("getcwd");
2353 error = got_repo_open(&repo, repo_path, NULL);
2354 free(repo_path);
2355 if (error != NULL)
2356 goto done;
2358 error = apply_unveil(got_repo_get_path(repo), 1,
2359 worktree ? got_worktree_get_root_path(worktree) : NULL);
2360 if (error)
2361 goto done;
2363 if (argc <= 1) {
2364 struct print_diff_arg arg;
2365 struct got_pathlist_head paths;
2366 char *id_str;
2368 TAILQ_INIT(&paths);
2370 error = got_object_id_str(&id_str,
2371 got_worktree_get_base_commit_id(worktree));
2372 if (error)
2373 goto done;
2374 arg.repo = repo;
2375 arg.worktree = worktree;
2376 arg.diff_context = diff_context;
2377 arg.id_str = id_str;
2378 arg.header_shown = 0;
2379 arg.diff_staged = diff_staged;
2380 arg.ignore_whitespace = ignore_whitespace;
2382 error = got_pathlist_append(&paths, path, NULL);
2383 if (error)
2384 goto done;
2386 error = got_worktree_status(worktree, &paths, repo, print_diff,
2387 &arg, check_cancelled, NULL);
2388 free(id_str);
2389 got_pathlist_free(&paths);
2390 goto done;
2393 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2394 repo);
2395 if (error)
2396 goto done;
2398 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2399 repo);
2400 if (error)
2401 goto done;
2403 error = got_object_get_type(&type1, repo, id1);
2404 if (error)
2405 goto done;
2407 error = got_object_get_type(&type2, repo, id2);
2408 if (error)
2409 goto done;
2411 if (type1 != type2) {
2412 error = got_error(GOT_ERR_OBJ_TYPE);
2413 goto done;
2416 switch (type1) {
2417 case GOT_OBJ_TYPE_BLOB:
2418 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2419 diff_context, ignore_whitespace, repo, stdout);
2420 break;
2421 case GOT_OBJ_TYPE_TREE:
2422 error = got_diff_objects_as_trees(id1, id2, "", "",
2423 diff_context, ignore_whitespace, repo, stdout);
2424 break;
2425 case GOT_OBJ_TYPE_COMMIT:
2426 printf("diff %s %s\n", label1, label2);
2427 error = got_diff_objects_as_commits(id1, id2, diff_context,
2428 ignore_whitespace, repo, stdout);
2429 break;
2430 default:
2431 error = got_error(GOT_ERR_OBJ_TYPE);
2434 done:
2435 free(label1);
2436 free(label2);
2437 free(id1);
2438 free(id2);
2439 free(path);
2440 if (worktree)
2441 got_worktree_close(worktree);
2442 if (repo) {
2443 const struct got_error *repo_error;
2444 repo_error = got_repo_close(repo);
2445 if (error == NULL)
2446 error = repo_error;
2448 return error;
2451 __dead static void
2452 usage_blame(void)
2454 fprintf(stderr,
2455 "usage: %s blame [-c commit] [-r repository-path] path\n",
2456 getprogname());
2457 exit(1);
2460 struct blame_line {
2461 int annotated;
2462 char *id_str;
2463 char *committer;
2464 char datebuf[11]; /* YYYY-MM-DD + NUL */
2467 struct blame_cb_args {
2468 struct blame_line *lines;
2469 int nlines;
2470 int nlines_prec;
2471 int lineno_cur;
2472 off_t *line_offsets;
2473 FILE *f;
2474 struct got_repository *repo;
2477 static const struct got_error *
2478 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2480 const struct got_error *err = NULL;
2481 struct blame_cb_args *a = arg;
2482 struct blame_line *bline;
2483 char *line = NULL;
2484 size_t linesize = 0;
2485 struct got_commit_object *commit = NULL;
2486 off_t offset;
2487 struct tm tm;
2488 time_t committer_time;
2490 if (nlines != a->nlines ||
2491 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2492 return got_error(GOT_ERR_RANGE);
2494 if (sigint_received)
2495 return got_error(GOT_ERR_ITER_COMPLETED);
2497 if (lineno == -1)
2498 return NULL; /* no change in this commit */
2500 /* Annotate this line. */
2501 bline = &a->lines[lineno - 1];
2502 if (bline->annotated)
2503 return NULL;
2504 err = got_object_id_str(&bline->id_str, id);
2505 if (err)
2506 return err;
2508 err = got_object_open_as_commit(&commit, a->repo, id);
2509 if (err)
2510 goto done;
2512 bline->committer = strdup(got_object_commit_get_committer(commit));
2513 if (bline->committer == NULL) {
2514 err = got_error_from_errno("strdup");
2515 goto done;
2518 committer_time = got_object_commit_get_committer_time(commit);
2519 if (localtime_r(&committer_time, &tm) == NULL)
2520 return got_error_from_errno("localtime_r");
2521 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2522 &tm) >= sizeof(bline->datebuf)) {
2523 err = got_error(GOT_ERR_NO_SPACE);
2524 goto done;
2526 bline->annotated = 1;
2528 /* Print lines annotated so far. */
2529 bline = &a->lines[a->lineno_cur - 1];
2530 if (!bline->annotated)
2531 goto done;
2533 offset = a->line_offsets[a->lineno_cur - 1];
2534 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2535 err = got_error_from_errno("fseeko");
2536 goto done;
2539 while (bline->annotated) {
2540 char *smallerthan, *at, *nl, *committer;
2541 size_t len;
2543 if (getline(&line, &linesize, a->f) == -1) {
2544 if (ferror(a->f))
2545 err = got_error_from_errno("getline");
2546 break;
2549 committer = bline->committer;
2550 smallerthan = strchr(committer, '<');
2551 if (smallerthan && smallerthan[1] != '\0')
2552 committer = smallerthan + 1;
2553 at = strchr(committer, '@');
2554 if (at)
2555 *at = '\0';
2556 len = strlen(committer);
2557 if (len >= 9)
2558 committer[8] = '\0';
2560 nl = strchr(line, '\n');
2561 if (nl)
2562 *nl = '\0';
2563 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2564 bline->id_str, bline->datebuf, committer, line);
2566 a->lineno_cur++;
2567 bline = &a->lines[a->lineno_cur - 1];
2569 done:
2570 if (commit)
2571 got_object_commit_close(commit);
2572 free(line);
2573 return err;
2576 static const struct got_error *
2577 cmd_blame(int argc, char *argv[])
2579 const struct got_error *error;
2580 struct got_repository *repo = NULL;
2581 struct got_worktree *worktree = NULL;
2582 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2583 struct got_object_id *obj_id = NULL;
2584 struct got_object_id *commit_id = NULL;
2585 struct got_blob_object *blob = NULL;
2586 char *commit_id_str = NULL;
2587 struct blame_cb_args bca;
2588 int ch, obj_type, i;
2589 size_t filesize;
2591 memset(&bca, 0, sizeof(bca));
2593 #ifndef PROFILE
2594 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2595 NULL) == -1)
2596 err(1, "pledge");
2597 #endif
2599 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2600 switch (ch) {
2601 case 'c':
2602 commit_id_str = optarg;
2603 break;
2604 case 'r':
2605 repo_path = realpath(optarg, NULL);
2606 if (repo_path == NULL)
2607 return got_error_from_errno2("realpath",
2608 optarg);
2609 got_path_strip_trailing_slashes(repo_path);
2610 break;
2611 default:
2612 usage_blame();
2613 /* NOTREACHED */
2617 argc -= optind;
2618 argv += optind;
2620 if (argc == 1)
2621 path = argv[0];
2622 else
2623 usage_blame();
2625 cwd = getcwd(NULL, 0);
2626 if (cwd == NULL) {
2627 error = got_error_from_errno("getcwd");
2628 goto done;
2630 if (repo_path == NULL) {
2631 error = got_worktree_open(&worktree, cwd);
2632 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2633 goto done;
2634 else
2635 error = NULL;
2636 if (worktree) {
2637 repo_path =
2638 strdup(got_worktree_get_repo_path(worktree));
2639 if (repo_path == NULL)
2640 error = got_error_from_errno("strdup");
2641 if (error)
2642 goto done;
2643 } else {
2644 repo_path = strdup(cwd);
2645 if (repo_path == NULL) {
2646 error = got_error_from_errno("strdup");
2647 goto done;
2652 error = got_repo_open(&repo, repo_path, NULL);
2653 if (error != NULL)
2654 goto done;
2656 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2657 if (error)
2658 goto done;
2660 if (worktree) {
2661 const char *prefix = got_worktree_get_path_prefix(worktree);
2662 char *p, *worktree_subdir = cwd +
2663 strlen(got_worktree_get_root_path(worktree));
2664 if (asprintf(&p, "%s%s%s%s%s",
2665 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2666 worktree_subdir, worktree_subdir[0] ? "/" : "",
2667 path) == -1) {
2668 error = got_error_from_errno("asprintf");
2669 goto done;
2671 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2672 free(p);
2673 } else {
2674 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2676 if (error)
2677 goto done;
2679 if (commit_id_str == NULL) {
2680 struct got_reference *head_ref;
2681 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2682 if (error != NULL)
2683 goto done;
2684 error = got_ref_resolve(&commit_id, repo, head_ref);
2685 got_ref_close(head_ref);
2686 if (error != NULL)
2687 goto done;
2688 } else {
2689 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2690 if (error)
2691 goto done;
2694 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2695 if (error)
2696 goto done;
2697 if (obj_id == NULL) {
2698 error = got_error(GOT_ERR_NO_OBJ);
2699 goto done;
2702 error = got_object_get_type(&obj_type, repo, obj_id);
2703 if (error)
2704 goto done;
2706 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2707 error = got_error(GOT_ERR_OBJ_TYPE);
2708 goto done;
2711 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2712 if (error)
2713 goto done;
2714 bca.f = got_opentemp();
2715 if (bca.f == NULL) {
2716 error = got_error_from_errno("got_opentemp");
2717 goto done;
2719 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2720 &bca.line_offsets, bca.f, blob);
2721 if (error || bca.nlines == 0)
2722 goto done;
2724 /* Don't include \n at EOF in the blame line count. */
2725 if (bca.line_offsets[bca.nlines - 1] == filesize)
2726 bca.nlines--;
2728 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2729 if (bca.lines == NULL) {
2730 error = got_error_from_errno("calloc");
2731 goto done;
2733 bca.lineno_cur = 1;
2734 bca.nlines_prec = 0;
2735 i = bca.nlines;
2736 while (i > 0) {
2737 i /= 10;
2738 bca.nlines_prec++;
2740 bca.repo = repo;
2742 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2743 check_cancelled, NULL);
2744 if (error)
2745 goto done;
2746 done:
2747 free(in_repo_path);
2748 free(repo_path);
2749 free(cwd);
2750 free(commit_id);
2751 free(obj_id);
2752 if (blob)
2753 got_object_blob_close(blob);
2754 if (worktree)
2755 got_worktree_close(worktree);
2756 if (repo) {
2757 const struct got_error *repo_error;
2758 repo_error = got_repo_close(repo);
2759 if (error == NULL)
2760 error = repo_error;
2762 if (bca.lines) {
2763 for (i = 0; i < bca.nlines; i++) {
2764 struct blame_line *bline = &bca.lines[i];
2765 free(bline->id_str);
2766 free(bline->committer);
2768 free(bca.lines);
2770 free(bca.line_offsets);
2771 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2772 error = got_error_from_errno("fclose");
2773 return error;
2776 __dead static void
2777 usage_tree(void)
2779 fprintf(stderr,
2780 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2781 getprogname());
2782 exit(1);
2785 static void
2786 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2787 const char *root_path)
2789 int is_root_path = (strcmp(path, root_path) == 0);
2790 const char *modestr = "";
2791 mode_t mode = got_tree_entry_get_mode(te);
2793 path += strlen(root_path);
2794 while (path[0] == '/')
2795 path++;
2797 if (got_object_tree_entry_is_submodule(te))
2798 modestr = "$";
2799 else if (S_ISLNK(mode))
2800 modestr = "@";
2801 else if (S_ISDIR(mode))
2802 modestr = "/";
2803 else if (mode & S_IXUSR)
2804 modestr = "*";
2806 printf("%s%s%s%s%s\n", id ? id : "", path,
2807 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2810 static const struct got_error *
2811 print_tree(const char *path, struct got_object_id *commit_id,
2812 int show_ids, int recurse, const char *root_path,
2813 struct got_repository *repo)
2815 const struct got_error *err = NULL;
2816 struct got_object_id *tree_id = NULL;
2817 struct got_tree_object *tree = NULL;
2818 int nentries, i;
2820 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2821 if (err)
2822 goto done;
2824 err = got_object_open_as_tree(&tree, repo, tree_id);
2825 if (err)
2826 goto done;
2827 nentries = got_object_tree_get_nentries(tree);
2828 for (i = 0; i < nentries; i++) {
2829 struct got_tree_entry *te;
2830 char *id = NULL;
2832 if (sigint_received || sigpipe_received)
2833 break;
2835 te = got_object_tree_get_entry(tree, i);
2836 if (show_ids) {
2837 char *id_str;
2838 err = got_object_id_str(&id_str,
2839 got_tree_entry_get_id(te));
2840 if (err)
2841 goto done;
2842 if (asprintf(&id, "%s ", id_str) == -1) {
2843 err = got_error_from_errno("asprintf");
2844 free(id_str);
2845 goto done;
2847 free(id_str);
2849 print_entry(te, id, path, root_path);
2850 free(id);
2852 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2853 char *child_path;
2854 if (asprintf(&child_path, "%s%s%s", path,
2855 path[0] == '/' && path[1] == '\0' ? "" : "/",
2856 got_tree_entry_get_name(te)) == -1) {
2857 err = got_error_from_errno("asprintf");
2858 goto done;
2860 err = print_tree(child_path, commit_id, show_ids, 1,
2861 root_path, repo);
2862 free(child_path);
2863 if (err)
2864 goto done;
2867 done:
2868 if (tree)
2869 got_object_tree_close(tree);
2870 free(tree_id);
2871 return err;
2874 static const struct got_error *
2875 cmd_tree(int argc, char *argv[])
2877 const struct got_error *error;
2878 struct got_repository *repo = NULL;
2879 struct got_worktree *worktree = NULL;
2880 const char *path;
2881 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2882 struct got_object_id *commit_id = NULL;
2883 char *commit_id_str = NULL;
2884 int show_ids = 0, recurse = 0;
2885 int ch;
2887 #ifndef PROFILE
2888 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2889 NULL) == -1)
2890 err(1, "pledge");
2891 #endif
2893 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2894 switch (ch) {
2895 case 'c':
2896 commit_id_str = optarg;
2897 break;
2898 case 'r':
2899 repo_path = realpath(optarg, NULL);
2900 if (repo_path == NULL)
2901 return got_error_from_errno2("realpath",
2902 optarg);
2903 got_path_strip_trailing_slashes(repo_path);
2904 break;
2905 case 'i':
2906 show_ids = 1;
2907 break;
2908 case 'R':
2909 recurse = 1;
2910 break;
2911 default:
2912 usage_tree();
2913 /* NOTREACHED */
2917 argc -= optind;
2918 argv += optind;
2920 if (argc == 1)
2921 path = argv[0];
2922 else if (argc > 1)
2923 usage_tree();
2924 else
2925 path = NULL;
2927 cwd = getcwd(NULL, 0);
2928 if (cwd == NULL) {
2929 error = got_error_from_errno("getcwd");
2930 goto done;
2932 if (repo_path == NULL) {
2933 error = got_worktree_open(&worktree, cwd);
2934 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2935 goto done;
2936 else
2937 error = NULL;
2938 if (worktree) {
2939 repo_path =
2940 strdup(got_worktree_get_repo_path(worktree));
2941 if (repo_path == NULL)
2942 error = got_error_from_errno("strdup");
2943 if (error)
2944 goto done;
2945 } else {
2946 repo_path = strdup(cwd);
2947 if (repo_path == NULL) {
2948 error = got_error_from_errno("strdup");
2949 goto done;
2954 error = got_repo_open(&repo, repo_path, NULL);
2955 if (error != NULL)
2956 goto done;
2958 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2959 if (error)
2960 goto done;
2962 if (path == NULL) {
2963 if (worktree) {
2964 char *p, *worktree_subdir = cwd +
2965 strlen(got_worktree_get_root_path(worktree));
2966 if (asprintf(&p, "%s/%s",
2967 got_worktree_get_path_prefix(worktree),
2968 worktree_subdir) == -1) {
2969 error = got_error_from_errno("asprintf");
2970 goto done;
2972 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2973 free(p);
2974 if (error)
2975 goto done;
2976 } else
2977 path = "/";
2979 if (in_repo_path == NULL) {
2980 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2981 if (error != NULL)
2982 goto done;
2985 if (commit_id_str == NULL) {
2986 struct got_reference *head_ref;
2987 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2988 if (error != NULL)
2989 goto done;
2990 error = got_ref_resolve(&commit_id, repo, head_ref);
2991 got_ref_close(head_ref);
2992 if (error != NULL)
2993 goto done;
2994 } else {
2995 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2996 if (error)
2997 goto done;
3000 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3001 in_repo_path, repo);
3002 done:
3003 free(in_repo_path);
3004 free(repo_path);
3005 free(cwd);
3006 free(commit_id);
3007 if (worktree)
3008 got_worktree_close(worktree);
3009 if (repo) {
3010 const struct got_error *repo_error;
3011 repo_error = got_repo_close(repo);
3012 if (error == NULL)
3013 error = repo_error;
3015 return error;
3018 __dead static void
3019 usage_status(void)
3021 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3022 exit(1);
3025 static const struct got_error *
3026 print_status(void *arg, unsigned char status, unsigned char staged_status,
3027 const char *path, struct got_object_id *blob_id,
3028 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3030 if (status == staged_status && (status == GOT_STATUS_DELETE))
3031 status = GOT_STATUS_NO_CHANGE;
3032 printf("%c%c %s\n", status, staged_status, path);
3033 return NULL;
3036 static const struct got_error *
3037 cmd_status(int argc, char *argv[])
3039 const struct got_error *error = NULL;
3040 struct got_repository *repo = NULL;
3041 struct got_worktree *worktree = NULL;
3042 char *cwd = NULL;
3043 struct got_pathlist_head paths;
3044 struct got_pathlist_entry *pe;
3045 int ch;
3047 TAILQ_INIT(&paths);
3049 while ((ch = getopt(argc, argv, "")) != -1) {
3050 switch (ch) {
3051 default:
3052 usage_status();
3053 /* NOTREACHED */
3057 argc -= optind;
3058 argv += optind;
3060 #ifndef PROFILE
3061 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3062 NULL) == -1)
3063 err(1, "pledge");
3064 #endif
3065 cwd = getcwd(NULL, 0);
3066 if (cwd == NULL) {
3067 error = got_error_from_errno("getcwd");
3068 goto done;
3071 error = got_worktree_open(&worktree, cwd);
3072 if (error != NULL)
3073 goto done;
3075 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3076 NULL);
3077 if (error != NULL)
3078 goto done;
3080 error = apply_unveil(got_repo_get_path(repo), 1,
3081 got_worktree_get_root_path(worktree));
3082 if (error)
3083 goto done;
3085 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3086 if (error)
3087 goto done;
3089 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3090 check_cancelled, NULL);
3091 done:
3092 TAILQ_FOREACH(pe, &paths, entry)
3093 free((char *)pe->path);
3094 got_pathlist_free(&paths);
3095 free(cwd);
3096 return error;
3099 __dead static void
3100 usage_ref(void)
3102 fprintf(stderr,
3103 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3104 getprogname());
3105 exit(1);
3108 static const struct got_error *
3109 list_refs(struct got_repository *repo)
3111 static const struct got_error *err = NULL;
3112 struct got_reflist_head refs;
3113 struct got_reflist_entry *re;
3115 SIMPLEQ_INIT(&refs);
3116 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3117 if (err)
3118 return err;
3120 SIMPLEQ_FOREACH(re, &refs, entry) {
3121 char *refstr;
3122 refstr = got_ref_to_str(re->ref);
3123 if (refstr == NULL)
3124 return got_error_from_errno("got_ref_to_str");
3125 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3126 free(refstr);
3129 got_ref_list_free(&refs);
3130 return NULL;
3133 static const struct got_error *
3134 delete_ref(struct got_repository *repo, const char *refname)
3136 const struct got_error *err = NULL;
3137 struct got_reference *ref;
3139 err = got_ref_open(&ref, repo, refname, 0);
3140 if (err)
3141 return err;
3143 err = got_ref_delete(ref, repo);
3144 got_ref_close(ref);
3145 return err;
3148 static const struct got_error *
3149 add_ref(struct got_repository *repo, const char *refname, const char *target)
3151 const struct got_error *err = NULL;
3152 struct got_object_id *id;
3153 struct got_reference *ref = NULL;
3156 * Don't let the user create a reference name with a leading '-'.
3157 * While technically a valid reference name, this case is usually
3158 * an unintended typo.
3160 if (refname[0] == '-')
3161 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3163 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3164 repo);
3165 if (err) {
3166 struct got_reference *target_ref;
3168 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3169 return err;
3170 err = got_ref_open(&target_ref, repo, target, 0);
3171 if (err)
3172 return err;
3173 err = got_ref_resolve(&id, repo, target_ref);
3174 got_ref_close(target_ref);
3175 if (err)
3176 return err;
3179 err = got_ref_alloc(&ref, refname, id);
3180 if (err)
3181 goto done;
3183 err = got_ref_write(ref, repo);
3184 done:
3185 if (ref)
3186 got_ref_close(ref);
3187 free(id);
3188 return err;
3191 static const struct got_error *
3192 add_symref(struct got_repository *repo, const char *refname, const char *target)
3194 const struct got_error *err = NULL;
3195 struct got_reference *ref = NULL;
3196 struct got_reference *target_ref = NULL;
3199 * Don't let the user create a reference name with a leading '-'.
3200 * While technically a valid reference name, this case is usually
3201 * an unintended typo.
3203 if (refname[0] == '-')
3204 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3206 err = got_ref_open(&target_ref, repo, target, 0);
3207 if (err)
3208 return err;
3210 err = got_ref_alloc_symref(&ref, refname, target_ref);
3211 if (err)
3212 goto done;
3214 err = got_ref_write(ref, repo);
3215 done:
3216 if (target_ref)
3217 got_ref_close(target_ref);
3218 if (ref)
3219 got_ref_close(ref);
3220 return err;
3223 static const struct got_error *
3224 cmd_ref(int argc, char *argv[])
3226 const struct got_error *error = NULL;
3227 struct got_repository *repo = NULL;
3228 struct got_worktree *worktree = NULL;
3229 char *cwd = NULL, *repo_path = NULL;
3230 int ch, do_list = 0, create_symref = 0;
3231 const char *delref = NULL;
3233 /* TODO: Add -s option for adding symbolic references. */
3234 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3235 switch (ch) {
3236 case 'd':
3237 delref = optarg;
3238 break;
3239 case 'r':
3240 repo_path = realpath(optarg, NULL);
3241 if (repo_path == NULL)
3242 return got_error_from_errno2("realpath",
3243 optarg);
3244 got_path_strip_trailing_slashes(repo_path);
3245 break;
3246 case 'l':
3247 do_list = 1;
3248 break;
3249 case 's':
3250 create_symref = 1;
3251 break;
3252 default:
3253 usage_ref();
3254 /* NOTREACHED */
3258 if (do_list && delref)
3259 errx(1, "-l and -d options are mutually exclusive\n");
3261 argc -= optind;
3262 argv += optind;
3264 if (do_list || delref) {
3265 if (create_symref)
3266 errx(1, "-s option cannot be used together with the "
3267 "-l or -d options");
3268 if (argc > 0)
3269 usage_ref();
3270 } else if (argc != 2)
3271 usage_ref();
3273 #ifndef PROFILE
3274 if (do_list) {
3275 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3276 NULL) == -1)
3277 err(1, "pledge");
3278 } else {
3279 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3280 "sendfd unveil", NULL) == -1)
3281 err(1, "pledge");
3283 #endif
3284 cwd = getcwd(NULL, 0);
3285 if (cwd == NULL) {
3286 error = got_error_from_errno("getcwd");
3287 goto done;
3290 if (repo_path == NULL) {
3291 error = got_worktree_open(&worktree, cwd);
3292 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3293 goto done;
3294 else
3295 error = NULL;
3296 if (worktree) {
3297 repo_path =
3298 strdup(got_worktree_get_repo_path(worktree));
3299 if (repo_path == NULL)
3300 error = got_error_from_errno("strdup");
3301 if (error)
3302 goto done;
3303 } else {
3304 repo_path = strdup(cwd);
3305 if (repo_path == NULL) {
3306 error = got_error_from_errno("strdup");
3307 goto done;
3312 error = got_repo_open(&repo, repo_path, NULL);
3313 if (error != NULL)
3314 goto done;
3316 error = apply_unveil(got_repo_get_path(repo), do_list,
3317 worktree ? got_worktree_get_root_path(worktree) : NULL);
3318 if (error)
3319 goto done;
3321 if (do_list)
3322 error = list_refs(repo);
3323 else if (delref)
3324 error = delete_ref(repo, delref);
3325 else if (create_symref)
3326 error = add_symref(repo, argv[0], argv[1]);
3327 else
3328 error = add_ref(repo, argv[0], argv[1]);
3329 done:
3330 if (repo)
3331 got_repo_close(repo);
3332 if (worktree)
3333 got_worktree_close(worktree);
3334 free(cwd);
3335 free(repo_path);
3336 return error;
3339 __dead static void
3340 usage_branch(void)
3342 fprintf(stderr,
3343 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3344 "[name]\n", getprogname());
3345 exit(1);
3348 static const struct got_error *
3349 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3350 struct got_reference *ref)
3352 const struct got_error *err = NULL;
3353 const char *refname, *marker = " ";
3354 char *refstr;
3356 refname = got_ref_get_name(ref);
3357 if (worktree && strcmp(refname,
3358 got_worktree_get_head_ref_name(worktree)) == 0) {
3359 struct got_object_id *id = NULL;
3361 err = got_ref_resolve(&id, repo, ref);
3362 if (err)
3363 return err;
3364 if (got_object_id_cmp(id,
3365 got_worktree_get_base_commit_id(worktree)) == 0)
3366 marker = "* ";
3367 else
3368 marker = "~ ";
3369 free(id);
3372 if (strncmp(refname, "refs/heads/", 11) == 0)
3373 refname += 11;
3374 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3375 refname += 18;
3377 refstr = got_ref_to_str(ref);
3378 if (refstr == NULL)
3379 return got_error_from_errno("got_ref_to_str");
3381 printf("%s%s: %s\n", marker, refname, refstr);
3382 free(refstr);
3383 return NULL;
3386 static const struct got_error *
3387 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3389 const char *refname;
3391 if (worktree == NULL)
3392 return got_error(GOT_ERR_NOT_WORKTREE);
3394 refname = got_worktree_get_head_ref_name(worktree);
3396 if (strncmp(refname, "refs/heads/", 11) == 0)
3397 refname += 11;
3398 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3399 refname += 18;
3401 printf("%s\n", refname);
3403 return NULL;
3406 static const struct got_error *
3407 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3409 static const struct got_error *err = NULL;
3410 struct got_reflist_head refs;
3411 struct got_reflist_entry *re;
3412 struct got_reference *temp_ref = NULL;
3413 int rebase_in_progress, histedit_in_progress;
3415 SIMPLEQ_INIT(&refs);
3417 if (worktree) {
3418 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3419 worktree);
3420 if (err)
3421 return err;
3423 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3424 worktree);
3425 if (err)
3426 return err;
3428 if (rebase_in_progress || histedit_in_progress) {
3429 err = got_ref_open(&temp_ref, repo,
3430 got_worktree_get_head_ref_name(worktree), 0);
3431 if (err)
3432 return err;
3433 list_branch(repo, worktree, temp_ref);
3434 got_ref_close(temp_ref);
3438 err = got_ref_list(&refs, repo, "refs/heads",
3439 got_ref_cmp_by_name, NULL);
3440 if (err)
3441 return err;
3443 SIMPLEQ_FOREACH(re, &refs, entry)
3444 list_branch(repo, worktree, re->ref);
3446 got_ref_list_free(&refs);
3447 return NULL;
3450 static const struct got_error *
3451 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3452 const char *branch_name)
3454 const struct got_error *err = NULL;
3455 struct got_reference *ref = NULL;
3456 char *refname;
3458 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3459 return got_error_from_errno("asprintf");
3461 err = got_ref_open(&ref, repo, refname, 0);
3462 if (err)
3463 goto done;
3465 if (worktree &&
3466 strcmp(got_worktree_get_head_ref_name(worktree),
3467 got_ref_get_name(ref)) == 0) {
3468 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3469 "will not delete this work tree's current branch");
3470 goto done;
3473 err = got_ref_delete(ref, repo);
3474 done:
3475 if (ref)
3476 got_ref_close(ref);
3477 free(refname);
3478 return err;
3481 static const struct got_error *
3482 add_branch(struct got_repository *repo, const char *branch_name,
3483 struct got_object_id *base_commit_id)
3485 const struct got_error *err = NULL;
3486 struct got_reference *ref = NULL;
3487 char *base_refname = NULL, *refname = NULL;
3490 * Don't let the user create a branch name with a leading '-'.
3491 * While technically a valid reference name, this case is usually
3492 * an unintended typo.
3494 if (branch_name[0] == '-')
3495 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3497 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3498 err = got_error_from_errno("asprintf");
3499 goto done;
3502 err = got_ref_open(&ref, repo, refname, 0);
3503 if (err == NULL) {
3504 err = got_error(GOT_ERR_BRANCH_EXISTS);
3505 goto done;
3506 } else if (err->code != GOT_ERR_NOT_REF)
3507 goto done;
3509 err = got_ref_alloc(&ref, refname, base_commit_id);
3510 if (err)
3511 goto done;
3513 err = got_ref_write(ref, repo);
3514 done:
3515 if (ref)
3516 got_ref_close(ref);
3517 free(base_refname);
3518 free(refname);
3519 return err;
3522 static const struct got_error *
3523 cmd_branch(int argc, char *argv[])
3525 const struct got_error *error = NULL;
3526 struct got_repository *repo = NULL;
3527 struct got_worktree *worktree = NULL;
3528 char *cwd = NULL, *repo_path = NULL;
3529 int ch, do_list = 0, do_show = 0;
3530 const char *delref = NULL, *commit_id_arg = NULL;
3532 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3533 switch (ch) {
3534 case 'c':
3535 commit_id_arg = optarg;
3536 break;
3537 case 'd':
3538 delref = optarg;
3539 break;
3540 case 'r':
3541 repo_path = realpath(optarg, NULL);
3542 if (repo_path == NULL)
3543 return got_error_from_errno2("realpath",
3544 optarg);
3545 got_path_strip_trailing_slashes(repo_path);
3546 break;
3547 case 'l':
3548 do_list = 1;
3549 break;
3550 default:
3551 usage_branch();
3552 /* NOTREACHED */
3556 if (do_list && delref)
3557 errx(1, "-l and -d options are mutually exclusive\n");
3559 argc -= optind;
3560 argv += optind;
3562 if (!do_list && !delref && argc == 0)
3563 do_show = 1;
3565 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3566 errx(1, "-c option can only be used when creating a branch");
3568 if (do_list || delref) {
3569 if (argc > 0)
3570 usage_branch();
3571 } else if (!do_show && argc != 1)
3572 usage_branch();
3574 #ifndef PROFILE
3575 if (do_list || do_show) {
3576 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3577 NULL) == -1)
3578 err(1, "pledge");
3579 } else {
3580 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3581 "sendfd unveil", NULL) == -1)
3582 err(1, "pledge");
3584 #endif
3585 cwd = getcwd(NULL, 0);
3586 if (cwd == NULL) {
3587 error = got_error_from_errno("getcwd");
3588 goto done;
3591 if (repo_path == NULL) {
3592 error = got_worktree_open(&worktree, cwd);
3593 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3594 goto done;
3595 else
3596 error = NULL;
3597 if (worktree) {
3598 repo_path =
3599 strdup(got_worktree_get_repo_path(worktree));
3600 if (repo_path == NULL)
3601 error = got_error_from_errno("strdup");
3602 if (error)
3603 goto done;
3604 } else {
3605 repo_path = strdup(cwd);
3606 if (repo_path == NULL) {
3607 error = got_error_from_errno("strdup");
3608 goto done;
3613 error = got_repo_open(&repo, repo_path, NULL);
3614 if (error != NULL)
3615 goto done;
3617 error = apply_unveil(got_repo_get_path(repo), do_list,
3618 worktree ? got_worktree_get_root_path(worktree) : NULL);
3619 if (error)
3620 goto done;
3622 if (do_show)
3623 error = show_current_branch(repo, worktree);
3624 else if (do_list)
3625 error = list_branches(repo, worktree);
3626 else if (delref)
3627 error = delete_branch(repo, worktree, delref);
3628 else {
3629 struct got_object_id *commit_id;
3630 if (commit_id_arg == NULL)
3631 commit_id_arg = worktree ?
3632 got_worktree_get_head_ref_name(worktree) :
3633 GOT_REF_HEAD;
3634 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3635 if (error)
3636 goto done;
3637 error = add_branch(repo, argv[0], commit_id);
3638 free(commit_id);
3640 done:
3641 if (repo)
3642 got_repo_close(repo);
3643 if (worktree)
3644 got_worktree_close(worktree);
3645 free(cwd);
3646 free(repo_path);
3647 return error;
3651 __dead static void
3652 usage_tag(void)
3654 fprintf(stderr,
3655 "usage: %s tag [-r repository] | -l | "
3656 "[-m message] name [commit]\n", getprogname());
3657 exit(1);
3660 #if 0
3661 static const struct got_error *
3662 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3664 const struct got_error *err = NULL;
3665 struct got_reflist_entry *re, *se, *new;
3666 struct got_object_id *re_id, *se_id;
3667 struct got_tag_object *re_tag, *se_tag;
3668 time_t re_time, se_time;
3670 SIMPLEQ_FOREACH(re, tags, entry) {
3671 se = SIMPLEQ_FIRST(sorted);
3672 if (se == NULL) {
3673 err = got_reflist_entry_dup(&new, re);
3674 if (err)
3675 return err;
3676 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3677 continue;
3678 } else {
3679 err = got_ref_resolve(&re_id, repo, re->ref);
3680 if (err)
3681 break;
3682 err = got_object_open_as_tag(&re_tag, repo, re_id);
3683 free(re_id);
3684 if (err)
3685 break;
3686 re_time = got_object_tag_get_tagger_time(re_tag);
3687 got_object_tag_close(re_tag);
3690 while (se) {
3691 err = got_ref_resolve(&se_id, repo, re->ref);
3692 if (err)
3693 break;
3694 err = got_object_open_as_tag(&se_tag, repo, se_id);
3695 free(se_id);
3696 if (err)
3697 break;
3698 se_time = got_object_tag_get_tagger_time(se_tag);
3699 got_object_tag_close(se_tag);
3701 if (se_time > re_time) {
3702 err = got_reflist_entry_dup(&new, re);
3703 if (err)
3704 return err;
3705 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3706 break;
3708 se = SIMPLEQ_NEXT(se, entry);
3709 continue;
3712 done:
3713 return err;
3715 #endif
3717 static const struct got_error *
3718 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3719 struct got_reference *ref2)
3721 const struct got_error *err = NULL;
3722 struct got_repository *repo = arg;
3723 struct got_object_id *id1, *id2 = NULL;
3724 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3725 time_t time1, time2;
3727 *cmp = 0;
3729 err = got_ref_resolve(&id1, repo, ref1);
3730 if (err)
3731 return err;
3732 err = got_object_open_as_tag(&tag1, repo, id1);
3733 if (err)
3734 goto done;
3736 err = got_ref_resolve(&id2, repo, ref2);
3737 if (err)
3738 goto done;
3739 err = got_object_open_as_tag(&tag2, repo, id2);
3740 if (err)
3741 goto done;
3743 time1 = got_object_tag_get_tagger_time(tag1);
3744 time2 = got_object_tag_get_tagger_time(tag2);
3746 /* Put latest tags first. */
3747 if (time1 < time2)
3748 *cmp = 1;
3749 else if (time1 > time2)
3750 *cmp = -1;
3751 else
3752 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3753 done:
3754 free(id1);
3755 free(id2);
3756 if (tag1)
3757 got_object_tag_close(tag1);
3758 if (tag2)
3759 got_object_tag_close(tag2);
3760 return err;
3763 static const struct got_error *
3764 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3766 static const struct got_error *err = NULL;
3767 struct got_reflist_head refs;
3768 struct got_reflist_entry *re;
3770 SIMPLEQ_INIT(&refs);
3772 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3773 if (err)
3774 return err;
3776 SIMPLEQ_FOREACH(re, &refs, entry) {
3777 const char *refname;
3778 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3779 char datebuf[26];
3780 time_t tagger_time;
3781 struct got_object_id *id;
3782 struct got_tag_object *tag;
3784 refname = got_ref_get_name(re->ref);
3785 if (strncmp(refname, "refs/tags/", 10) != 0)
3786 continue;
3787 refname += 10;
3788 refstr = got_ref_to_str(re->ref);
3789 if (refstr == NULL) {
3790 err = got_error_from_errno("got_ref_to_str");
3791 break;
3793 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3794 free(refstr);
3796 err = got_ref_resolve(&id, repo, re->ref);
3797 if (err)
3798 break;
3799 err = got_object_open_as_tag(&tag, repo, id);
3800 free(id);
3801 if (err)
3802 break;
3803 printf("from: %s\n", got_object_tag_get_tagger(tag));
3804 tagger_time = got_object_tag_get_tagger_time(tag);
3805 datestr = get_datestr(&tagger_time, datebuf);
3806 if (datestr)
3807 printf("date: %s UTC\n", datestr);
3808 err = got_object_id_str(&id_str,
3809 got_object_tag_get_object_id(tag));
3810 if (err)
3811 break;
3812 switch (got_object_tag_get_object_type(tag)) {
3813 case GOT_OBJ_TYPE_BLOB:
3814 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3815 break;
3816 case GOT_OBJ_TYPE_TREE:
3817 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3818 break;
3819 case GOT_OBJ_TYPE_COMMIT:
3820 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3821 break;
3822 case GOT_OBJ_TYPE_TAG:
3823 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3824 break;
3825 default:
3826 break;
3828 free(id_str);
3829 tagmsg0 = strdup(got_object_tag_get_message(tag));
3830 got_object_tag_close(tag);
3831 if (tagmsg0 == NULL) {
3832 err = got_error_from_errno("strdup");
3833 break;
3836 tagmsg = tagmsg0;
3837 do {
3838 line = strsep(&tagmsg, "\n");
3839 if (line)
3840 printf(" %s\n", line);
3841 } while (line);
3842 free(tagmsg0);
3845 got_ref_list_free(&refs);
3846 return NULL;
3849 static const struct got_error *
3850 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3851 const char *tag_name, const char *repo_path)
3853 const struct got_error *err = NULL;
3854 char *template = NULL, *initial_content = NULL;
3855 char *editor = NULL;
3856 int fd = -1;
3858 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3859 err = got_error_from_errno("asprintf");
3860 goto done;
3863 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3864 commit_id_str, tag_name) == -1) {
3865 err = got_error_from_errno("asprintf");
3866 goto done;
3869 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3870 if (err)
3871 goto done;
3873 dprintf(fd, initial_content);
3874 close(fd);
3876 err = get_editor(&editor);
3877 if (err)
3878 goto done;
3879 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3880 done:
3881 free(initial_content);
3882 free(template);
3883 free(editor);
3885 /* Editor is done; we can now apply unveil(2) */
3886 if (err == NULL) {
3887 err = apply_unveil(repo_path, 0, NULL);
3888 if (err) {
3889 free(*tagmsg);
3890 *tagmsg = NULL;
3893 return err;
3896 static const struct got_error *
3897 add_tag(struct got_repository *repo, const char *tag_name,
3898 const char *commit_arg, const char *tagmsg_arg)
3900 const struct got_error *err = NULL;
3901 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3902 char *label = NULL, *commit_id_str = NULL;
3903 struct got_reference *ref = NULL;
3904 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3905 char *tagmsg_path = NULL, *tag_id_str = NULL;
3906 int preserve_tagmsg = 0;
3909 * Don't let the user create a tag name with a leading '-'.
3910 * While technically a valid reference name, this case is usually
3911 * an unintended typo.
3913 if (tag_name[0] == '-')
3914 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3916 err = get_author(&tagger, repo);
3917 if (err)
3918 return err;
3920 err = match_object_id(&commit_id, &label, commit_arg,
3921 GOT_OBJ_TYPE_COMMIT, 1, repo);
3922 if (err)
3923 goto done;
3925 err = got_object_id_str(&commit_id_str, commit_id);
3926 if (err)
3927 goto done;
3929 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3930 refname = strdup(tag_name);
3931 if (refname == NULL) {
3932 err = got_error_from_errno("strdup");
3933 goto done;
3935 tag_name += 10;
3936 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3937 err = got_error_from_errno("asprintf");
3938 goto done;
3941 err = got_ref_open(&ref, repo, refname, 0);
3942 if (err == NULL) {
3943 err = got_error(GOT_ERR_TAG_EXISTS);
3944 goto done;
3945 } else if (err->code != GOT_ERR_NOT_REF)
3946 goto done;
3948 if (tagmsg_arg == NULL) {
3949 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3950 tag_name, got_repo_get_path(repo));
3951 if (err) {
3952 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3953 tagmsg_path != NULL)
3954 preserve_tagmsg = 1;
3955 goto done;
3959 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3960 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3961 if (err) {
3962 if (tagmsg_path)
3963 preserve_tagmsg = 1;
3964 goto done;
3967 err = got_ref_alloc(&ref, refname, tag_id);
3968 if (err) {
3969 if (tagmsg_path)
3970 preserve_tagmsg = 1;
3971 goto done;
3974 err = got_ref_write(ref, repo);
3975 if (err) {
3976 if (tagmsg_path)
3977 preserve_tagmsg = 1;
3978 goto done;
3981 err = got_object_id_str(&tag_id_str, tag_id);
3982 if (err) {
3983 if (tagmsg_path)
3984 preserve_tagmsg = 1;
3985 goto done;
3987 printf("Created tag %s\n", tag_id_str);
3988 done:
3989 if (preserve_tagmsg) {
3990 fprintf(stderr, "%s: tag message preserved in %s\n",
3991 getprogname(), tagmsg_path);
3992 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3993 err = got_error_from_errno2("unlink", tagmsg_path);
3994 free(tag_id_str);
3995 if (ref)
3996 got_ref_close(ref);
3997 free(commit_id);
3998 free(commit_id_str);
3999 free(refname);
4000 free(tagmsg);
4001 free(tagmsg_path);
4002 free(tagger);
4003 return err;
4006 static const struct got_error *
4007 cmd_tag(int argc, char *argv[])
4009 const struct got_error *error = NULL;
4010 struct got_repository *repo = NULL;
4011 struct got_worktree *worktree = NULL;
4012 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4013 char *gitconfig_path = NULL;
4014 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4015 int ch, do_list = 0;
4017 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4018 switch (ch) {
4019 case 'm':
4020 tagmsg = optarg;
4021 break;
4022 case 'r':
4023 repo_path = realpath(optarg, NULL);
4024 if (repo_path == NULL)
4025 return got_error_from_errno2("realpath",
4026 optarg);
4027 got_path_strip_trailing_slashes(repo_path);
4028 break;
4029 case 'l':
4030 do_list = 1;
4031 break;
4032 default:
4033 usage_tag();
4034 /* NOTREACHED */
4038 argc -= optind;
4039 argv += optind;
4041 if (do_list) {
4042 if (tagmsg)
4043 errx(1, "-l and -m options are mutually exclusive\n");
4044 if (argc > 0)
4045 usage_tag();
4046 } else if (argc < 1 || argc > 2)
4047 usage_tag();
4048 else if (argc > 1)
4049 commit_id_arg = argv[1];
4050 tag_name = argv[0];
4052 #ifndef PROFILE
4053 if (do_list) {
4054 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4055 NULL) == -1)
4056 err(1, "pledge");
4057 } else {
4058 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4059 "sendfd unveil", NULL) == -1)
4060 err(1, "pledge");
4062 #endif
4063 cwd = getcwd(NULL, 0);
4064 if (cwd == NULL) {
4065 error = got_error_from_errno("getcwd");
4066 goto done;
4069 if (repo_path == NULL) {
4070 error = got_worktree_open(&worktree, cwd);
4071 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4072 goto done;
4073 else
4074 error = NULL;
4075 if (worktree) {
4076 repo_path =
4077 strdup(got_worktree_get_repo_path(worktree));
4078 if (repo_path == NULL)
4079 error = got_error_from_errno("strdup");
4080 if (error)
4081 goto done;
4082 } else {
4083 repo_path = strdup(cwd);
4084 if (repo_path == NULL) {
4085 error = got_error_from_errno("strdup");
4086 goto done;
4091 if (do_list) {
4092 error = got_repo_open(&repo, repo_path, NULL);
4093 if (error != NULL)
4094 goto done;
4095 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4096 if (error)
4097 goto done;
4098 error = list_tags(repo, worktree);
4099 } else {
4100 error = get_gitconfig_path(&gitconfig_path);
4101 if (error)
4102 goto done;
4103 error = got_repo_open(&repo, repo_path, gitconfig_path);
4104 if (error != NULL)
4105 goto done;
4107 if (tagmsg) {
4108 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4109 if (error)
4110 goto done;
4113 if (commit_id_arg == NULL) {
4114 struct got_reference *head_ref;
4115 struct got_object_id *commit_id;
4116 error = got_ref_open(&head_ref, repo,
4117 worktree ? got_worktree_get_head_ref_name(worktree)
4118 : GOT_REF_HEAD, 0);
4119 if (error)
4120 goto done;
4121 error = got_ref_resolve(&commit_id, repo, head_ref);
4122 got_ref_close(head_ref);
4123 if (error)
4124 goto done;
4125 error = got_object_id_str(&commit_id_str, commit_id);
4126 free(commit_id);
4127 if (error)
4128 goto done;
4131 error = add_tag(repo, tag_name,
4132 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4134 done:
4135 if (repo)
4136 got_repo_close(repo);
4137 if (worktree)
4138 got_worktree_close(worktree);
4139 free(cwd);
4140 free(repo_path);
4141 free(gitconfig_path);
4142 free(commit_id_str);
4143 return error;
4146 __dead static void
4147 usage_add(void)
4149 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4150 exit(1);
4153 static const struct got_error *
4154 add_progress(void *arg, unsigned char status, const char *path)
4156 while (path[0] == '/')
4157 path++;
4158 printf("%c %s\n", status, path);
4159 return NULL;
4162 static const struct got_error *
4163 cmd_add(int argc, char *argv[])
4165 const struct got_error *error = NULL;
4166 struct got_repository *repo = NULL;
4167 struct got_worktree *worktree = NULL;
4168 char *cwd = NULL;
4169 struct got_pathlist_head paths;
4170 struct got_pathlist_entry *pe;
4171 int ch, can_recurse = 0;
4173 TAILQ_INIT(&paths);
4175 while ((ch = getopt(argc, argv, "R")) != -1) {
4176 switch (ch) {
4177 case 'R':
4178 can_recurse = 1;
4179 break;
4180 default:
4181 usage_add();
4182 /* NOTREACHED */
4186 argc -= optind;
4187 argv += optind;
4189 #ifndef PROFILE
4190 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4191 NULL) == -1)
4192 err(1, "pledge");
4193 #endif
4194 if (argc < 1)
4195 usage_add();
4197 cwd = getcwd(NULL, 0);
4198 if (cwd == NULL) {
4199 error = got_error_from_errno("getcwd");
4200 goto done;
4203 error = got_worktree_open(&worktree, cwd);
4204 if (error)
4205 goto done;
4207 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4208 NULL);
4209 if (error != NULL)
4210 goto done;
4212 error = apply_unveil(got_repo_get_path(repo), 1,
4213 got_worktree_get_root_path(worktree));
4214 if (error)
4215 goto done;
4217 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4218 if (error)
4219 goto done;
4221 if (!can_recurse) {
4222 char *ondisk_path;
4223 struct stat sb;
4224 TAILQ_FOREACH(pe, &paths, entry) {
4225 if (asprintf(&ondisk_path, "%s/%s",
4226 got_worktree_get_root_path(worktree),
4227 pe->path) == -1) {
4228 error = got_error_from_errno("asprintf");
4229 goto done;
4231 if (lstat(ondisk_path, &sb) == -1) {
4232 if (errno == ENOENT) {
4233 free(ondisk_path);
4234 continue;
4236 error = got_error_from_errno2("lstat",
4237 ondisk_path);
4238 free(ondisk_path);
4239 goto done;
4241 free(ondisk_path);
4242 if (S_ISDIR(sb.st_mode)) {
4243 error = got_error_msg(GOT_ERR_BAD_PATH,
4244 "adding directories requires -R option");
4245 goto done;
4249 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4250 NULL, repo);
4251 done:
4252 if (repo)
4253 got_repo_close(repo);
4254 if (worktree)
4255 got_worktree_close(worktree);
4256 TAILQ_FOREACH(pe, &paths, entry)
4257 free((char *)pe->path);
4258 got_pathlist_free(&paths);
4259 free(cwd);
4260 return error;
4263 __dead static void
4264 usage_remove(void)
4266 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4267 exit(1);
4270 static const struct got_error *
4271 print_remove_status(void *arg, unsigned char status,
4272 unsigned char staged_status, const char *path,
4273 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4274 struct got_object_id *commit_id)
4276 if (status == GOT_STATUS_NONEXISTENT)
4277 return NULL;
4278 if (status == staged_status && (status == GOT_STATUS_DELETE))
4279 status = GOT_STATUS_NO_CHANGE;
4280 printf("%c%c %s\n", status, staged_status, path);
4281 return NULL;
4284 static const struct got_error *
4285 cmd_remove(int argc, char *argv[])
4287 const struct got_error *error = NULL;
4288 struct got_worktree *worktree = NULL;
4289 struct got_repository *repo = NULL;
4290 char *cwd = NULL;
4291 struct got_pathlist_head paths;
4292 struct got_pathlist_entry *pe;
4293 int ch, delete_local_mods = 0;
4295 TAILQ_INIT(&paths);
4297 while ((ch = getopt(argc, argv, "f")) != -1) {
4298 switch (ch) {
4299 case 'f':
4300 delete_local_mods = 1;
4301 break;
4302 default:
4303 usage_add();
4304 /* NOTREACHED */
4308 argc -= optind;
4309 argv += optind;
4311 #ifndef PROFILE
4312 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4313 NULL) == -1)
4314 err(1, "pledge");
4315 #endif
4316 if (argc < 1)
4317 usage_remove();
4319 cwd = getcwd(NULL, 0);
4320 if (cwd == NULL) {
4321 error = got_error_from_errno("getcwd");
4322 goto done;
4324 error = got_worktree_open(&worktree, cwd);
4325 if (error)
4326 goto done;
4328 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4329 NULL);
4330 if (error)
4331 goto done;
4333 error = apply_unveil(got_repo_get_path(repo), 1,
4334 got_worktree_get_root_path(worktree));
4335 if (error)
4336 goto done;
4338 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4339 if (error)
4340 goto done;
4342 error = got_worktree_schedule_delete(worktree, &paths,
4343 delete_local_mods, print_remove_status, NULL, repo);
4344 if (error)
4345 goto done;
4346 done:
4347 if (repo)
4348 got_repo_close(repo);
4349 if (worktree)
4350 got_worktree_close(worktree);
4351 TAILQ_FOREACH(pe, &paths, entry)
4352 free((char *)pe->path);
4353 got_pathlist_free(&paths);
4354 free(cwd);
4355 return error;
4358 __dead static void
4359 usage_revert(void)
4361 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4362 "path ...\n", getprogname());
4363 exit(1);
4366 static const struct got_error *
4367 revert_progress(void *arg, unsigned char status, const char *path)
4369 while (path[0] == '/')
4370 path++;
4371 printf("%c %s\n", status, path);
4372 return NULL;
4375 struct choose_patch_arg {
4376 FILE *patch_script_file;
4377 const char *action;
4380 static const struct got_error *
4381 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4382 int nchanges, const char *action)
4384 char *line = NULL;
4385 size_t linesize = 0;
4386 ssize_t linelen;
4388 switch (status) {
4389 case GOT_STATUS_ADD:
4390 printf("A %s\n%s this addition? [y/n] ", path, action);
4391 break;
4392 case GOT_STATUS_DELETE:
4393 printf("D %s\n%s this deletion? [y/n] ", path, action);
4394 break;
4395 case GOT_STATUS_MODIFY:
4396 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4397 return got_error_from_errno("fseek");
4398 printf(GOT_COMMIT_SEP_STR);
4399 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4400 printf("%s", line);
4401 if (ferror(patch_file))
4402 return got_error_from_errno("getline");
4403 printf(GOT_COMMIT_SEP_STR);
4404 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4405 path, n, nchanges, action);
4406 break;
4407 default:
4408 return got_error_path(path, GOT_ERR_FILE_STATUS);
4411 return NULL;
4414 static const struct got_error *
4415 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4416 FILE *patch_file, int n, int nchanges)
4418 const struct got_error *err = NULL;
4419 char *line = NULL;
4420 size_t linesize = 0;
4421 ssize_t linelen;
4422 int resp = ' ';
4423 struct choose_patch_arg *a = arg;
4425 *choice = GOT_PATCH_CHOICE_NONE;
4427 if (a->patch_script_file) {
4428 char *nl;
4429 err = show_change(status, path, patch_file, n, nchanges,
4430 a->action);
4431 if (err)
4432 return err;
4433 linelen = getline(&line, &linesize, a->patch_script_file);
4434 if (linelen == -1) {
4435 if (ferror(a->patch_script_file))
4436 return got_error_from_errno("getline");
4437 return NULL;
4439 nl = strchr(line, '\n');
4440 if (nl)
4441 *nl = '\0';
4442 if (strcmp(line, "y") == 0) {
4443 *choice = GOT_PATCH_CHOICE_YES;
4444 printf("y\n");
4445 } else if (strcmp(line, "n") == 0) {
4446 *choice = GOT_PATCH_CHOICE_NO;
4447 printf("n\n");
4448 } else if (strcmp(line, "q") == 0 &&
4449 status == GOT_STATUS_MODIFY) {
4450 *choice = GOT_PATCH_CHOICE_QUIT;
4451 printf("q\n");
4452 } else
4453 printf("invalid response '%s'\n", line);
4454 free(line);
4455 return NULL;
4458 while (resp != 'y' && resp != 'n' && resp != 'q') {
4459 err = show_change(status, path, patch_file, n, nchanges,
4460 a->action);
4461 if (err)
4462 return err;
4463 resp = getchar();
4464 if (resp == '\n')
4465 resp = getchar();
4466 if (status == GOT_STATUS_MODIFY) {
4467 if (resp != 'y' && resp != 'n' && resp != 'q') {
4468 printf("invalid response '%c'\n", resp);
4469 resp = ' ';
4471 } else if (resp != 'y' && resp != 'n') {
4472 printf("invalid response '%c'\n", resp);
4473 resp = ' ';
4477 if (resp == 'y')
4478 *choice = GOT_PATCH_CHOICE_YES;
4479 else if (resp == 'n')
4480 *choice = GOT_PATCH_CHOICE_NO;
4481 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4482 *choice = GOT_PATCH_CHOICE_QUIT;
4484 return NULL;
4488 static const struct got_error *
4489 cmd_revert(int argc, char *argv[])
4491 const struct got_error *error = NULL;
4492 struct got_worktree *worktree = NULL;
4493 struct got_repository *repo = NULL;
4494 char *cwd = NULL, *path = NULL;
4495 struct got_pathlist_head paths;
4496 struct got_pathlist_entry *pe;
4497 int ch, can_recurse = 0, pflag = 0;
4498 FILE *patch_script_file = NULL;
4499 const char *patch_script_path = NULL;
4500 struct choose_patch_arg cpa;
4502 TAILQ_INIT(&paths);
4504 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4505 switch (ch) {
4506 case 'p':
4507 pflag = 1;
4508 break;
4509 case 'F':
4510 patch_script_path = optarg;
4511 break;
4512 case 'R':
4513 can_recurse = 1;
4514 break;
4515 default:
4516 usage_revert();
4517 /* NOTREACHED */
4521 argc -= optind;
4522 argv += optind;
4524 #ifndef PROFILE
4525 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4526 "unveil", NULL) == -1)
4527 err(1, "pledge");
4528 #endif
4529 if (argc < 1)
4530 usage_revert();
4531 if (patch_script_path && !pflag)
4532 errx(1, "-F option can only be used together with -p option");
4534 cwd = getcwd(NULL, 0);
4535 if (cwd == NULL) {
4536 error = got_error_from_errno("getcwd");
4537 goto done;
4539 error = got_worktree_open(&worktree, cwd);
4540 if (error)
4541 goto done;
4543 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4544 NULL);
4545 if (error != NULL)
4546 goto done;
4548 if (patch_script_path) {
4549 patch_script_file = fopen(patch_script_path, "r");
4550 if (patch_script_file == NULL) {
4551 error = got_error_from_errno2("fopen",
4552 patch_script_path);
4553 goto done;
4556 error = apply_unveil(got_repo_get_path(repo), 1,
4557 got_worktree_get_root_path(worktree));
4558 if (error)
4559 goto done;
4561 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4562 if (error)
4563 goto done;
4565 if (!can_recurse) {
4566 char *ondisk_path;
4567 struct stat sb;
4568 TAILQ_FOREACH(pe, &paths, entry) {
4569 if (asprintf(&ondisk_path, "%s/%s",
4570 got_worktree_get_root_path(worktree),
4571 pe->path) == -1) {
4572 error = got_error_from_errno("asprintf");
4573 goto done;
4575 if (lstat(ondisk_path, &sb) == -1) {
4576 if (errno == ENOENT) {
4577 free(ondisk_path);
4578 continue;
4580 error = got_error_from_errno2("lstat",
4581 ondisk_path);
4582 free(ondisk_path);
4583 goto done;
4585 free(ondisk_path);
4586 if (S_ISDIR(sb.st_mode)) {
4587 error = got_error_msg(GOT_ERR_BAD_PATH,
4588 "reverting directories requires -R option");
4589 goto done;
4594 cpa.patch_script_file = patch_script_file;
4595 cpa.action = "revert";
4596 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4597 pflag ? choose_patch : NULL, &cpa, repo);
4598 if (error)
4599 goto done;
4600 done:
4601 if (patch_script_file && fclose(patch_script_file) == EOF &&
4602 error == NULL)
4603 error = got_error_from_errno2("fclose", patch_script_path);
4604 if (repo)
4605 got_repo_close(repo);
4606 if (worktree)
4607 got_worktree_close(worktree);
4608 free(path);
4609 free(cwd);
4610 return error;
4613 __dead static void
4614 usage_commit(void)
4616 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4617 getprogname());
4618 exit(1);
4621 struct collect_commit_logmsg_arg {
4622 const char *cmdline_log;
4623 const char *editor;
4624 const char *worktree_path;
4625 const char *branch_name;
4626 const char *repo_path;
4627 char *logmsg_path;
4631 static const struct got_error *
4632 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4633 void *arg)
4635 char *initial_content = NULL;
4636 struct got_pathlist_entry *pe;
4637 const struct got_error *err = NULL;
4638 char *template = NULL;
4639 struct collect_commit_logmsg_arg *a = arg;
4640 int fd;
4641 size_t len;
4643 /* if a message was specified on the command line, just use it */
4644 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4645 len = strlen(a->cmdline_log) + 1;
4646 *logmsg = malloc(len + 1);
4647 if (*logmsg == NULL)
4648 return got_error_from_errno("malloc");
4649 strlcpy(*logmsg, a->cmdline_log, len);
4650 return NULL;
4653 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4654 return got_error_from_errno("asprintf");
4656 if (asprintf(&initial_content,
4657 "\n# changes to be committed on branch %s:\n",
4658 a->branch_name) == -1)
4659 return got_error_from_errno("asprintf");
4661 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4662 if (err)
4663 goto done;
4665 dprintf(fd, initial_content);
4667 TAILQ_FOREACH(pe, commitable_paths, entry) {
4668 struct got_commitable *ct = pe->data;
4669 dprintf(fd, "# %c %s\n",
4670 got_commitable_get_status(ct),
4671 got_commitable_get_path(ct));
4673 close(fd);
4675 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4676 done:
4677 free(initial_content);
4678 free(template);
4680 /* Editor is done; we can now apply unveil(2) */
4681 if (err == NULL) {
4682 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4683 if (err) {
4684 free(*logmsg);
4685 *logmsg = NULL;
4688 return err;
4691 static const struct got_error *
4692 cmd_commit(int argc, char *argv[])
4694 const struct got_error *error = NULL;
4695 struct got_worktree *worktree = NULL;
4696 struct got_repository *repo = NULL;
4697 char *cwd = NULL, *id_str = NULL;
4698 struct got_object_id *id = NULL;
4699 const char *logmsg = NULL;
4700 struct collect_commit_logmsg_arg cl_arg;
4701 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4702 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4703 struct got_pathlist_head paths;
4705 TAILQ_INIT(&paths);
4706 cl_arg.logmsg_path = NULL;
4708 while ((ch = getopt(argc, argv, "m:")) != -1) {
4709 switch (ch) {
4710 case 'm':
4711 logmsg = optarg;
4712 break;
4713 default:
4714 usage_commit();
4715 /* NOTREACHED */
4719 argc -= optind;
4720 argv += optind;
4722 #ifndef PROFILE
4723 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4724 "unveil", NULL) == -1)
4725 err(1, "pledge");
4726 #endif
4727 cwd = getcwd(NULL, 0);
4728 if (cwd == NULL) {
4729 error = got_error_from_errno("getcwd");
4730 goto done;
4732 error = got_worktree_open(&worktree, cwd);
4733 if (error)
4734 goto done;
4736 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4737 if (error)
4738 goto done;
4739 if (rebase_in_progress) {
4740 error = got_error(GOT_ERR_REBASING);
4741 goto done;
4744 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4745 worktree);
4746 if (error)
4747 goto done;
4749 error = get_gitconfig_path(&gitconfig_path);
4750 if (error)
4751 goto done;
4752 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4753 gitconfig_path);
4754 if (error != NULL)
4755 goto done;
4757 error = get_author(&author, repo);
4758 if (error)
4759 return error;
4762 * unveil(2) traverses exec(2); if an editor is used we have
4763 * to apply unveil after the log message has been written.
4765 if (logmsg == NULL || strlen(logmsg) == 0)
4766 error = get_editor(&editor);
4767 else
4768 error = apply_unveil(got_repo_get_path(repo), 0,
4769 got_worktree_get_root_path(worktree));
4770 if (error)
4771 goto done;
4773 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4774 if (error)
4775 goto done;
4777 cl_arg.editor = editor;
4778 cl_arg.cmdline_log = logmsg;
4779 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4780 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4781 if (!histedit_in_progress) {
4782 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4783 error = got_error(GOT_ERR_COMMIT_BRANCH);
4784 goto done;
4786 cl_arg.branch_name += 11;
4788 cl_arg.repo_path = got_repo_get_path(repo);
4789 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4790 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4791 if (error) {
4792 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4793 cl_arg.logmsg_path != NULL)
4794 preserve_logmsg = 1;
4795 goto done;
4798 error = got_object_id_str(&id_str, id);
4799 if (error)
4800 goto done;
4801 printf("Created commit %s\n", id_str);
4802 done:
4803 if (preserve_logmsg) {
4804 fprintf(stderr, "%s: log message preserved in %s\n",
4805 getprogname(), cl_arg.logmsg_path);
4806 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4807 error == NULL)
4808 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4809 free(cl_arg.logmsg_path);
4810 if (repo)
4811 got_repo_close(repo);
4812 if (worktree)
4813 got_worktree_close(worktree);
4814 free(cwd);
4815 free(id_str);
4816 free(gitconfig_path);
4817 free(editor);
4818 free(author);
4819 return error;
4822 __dead static void
4823 usage_cherrypick(void)
4825 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4826 exit(1);
4829 static const struct got_error *
4830 cmd_cherrypick(int argc, char *argv[])
4832 const struct got_error *error = NULL;
4833 struct got_worktree *worktree = NULL;
4834 struct got_repository *repo = NULL;
4835 char *cwd = NULL, *commit_id_str = NULL;
4836 struct got_object_id *commit_id = NULL;
4837 struct got_commit_object *commit = NULL;
4838 struct got_object_qid *pid;
4839 struct got_reference *head_ref = NULL;
4840 int ch, did_something = 0;
4842 while ((ch = getopt(argc, argv, "")) != -1) {
4843 switch (ch) {
4844 default:
4845 usage_cherrypick();
4846 /* NOTREACHED */
4850 argc -= optind;
4851 argv += optind;
4853 #ifndef PROFILE
4854 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4855 "unveil", NULL) == -1)
4856 err(1, "pledge");
4857 #endif
4858 if (argc != 1)
4859 usage_cherrypick();
4861 cwd = getcwd(NULL, 0);
4862 if (cwd == NULL) {
4863 error = got_error_from_errno("getcwd");
4864 goto done;
4866 error = got_worktree_open(&worktree, cwd);
4867 if (error)
4868 goto done;
4870 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4871 NULL);
4872 if (error != NULL)
4873 goto done;
4875 error = apply_unveil(got_repo_get_path(repo), 0,
4876 got_worktree_get_root_path(worktree));
4877 if (error)
4878 goto done;
4880 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4881 GOT_OBJ_TYPE_COMMIT, repo);
4882 if (error != NULL) {
4883 struct got_reference *ref;
4884 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4885 goto done;
4886 error = got_ref_open(&ref, repo, argv[0], 0);
4887 if (error != NULL)
4888 goto done;
4889 error = got_ref_resolve(&commit_id, repo, ref);
4890 got_ref_close(ref);
4891 if (error != NULL)
4892 goto done;
4894 error = got_object_id_str(&commit_id_str, commit_id);
4895 if (error)
4896 goto done;
4898 error = got_ref_open(&head_ref, repo,
4899 got_worktree_get_head_ref_name(worktree), 0);
4900 if (error != NULL)
4901 goto done;
4903 error = check_same_branch(commit_id, head_ref, NULL, repo);
4904 if (error) {
4905 if (error->code != GOT_ERR_ANCESTRY)
4906 goto done;
4907 error = NULL;
4908 } else {
4909 error = got_error(GOT_ERR_SAME_BRANCH);
4910 goto done;
4913 error = got_object_open_as_commit(&commit, repo, commit_id);
4914 if (error)
4915 goto done;
4916 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4917 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4918 commit_id, repo, update_progress, &did_something, check_cancelled,
4919 NULL);
4920 if (error != NULL)
4921 goto done;
4923 if (did_something)
4924 printf("Merged commit %s\n", commit_id_str);
4925 done:
4926 if (commit)
4927 got_object_commit_close(commit);
4928 free(commit_id_str);
4929 if (head_ref)
4930 got_ref_close(head_ref);
4931 if (worktree)
4932 got_worktree_close(worktree);
4933 if (repo)
4934 got_repo_close(repo);
4935 return error;
4938 __dead static void
4939 usage_backout(void)
4941 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4942 exit(1);
4945 static const struct got_error *
4946 cmd_backout(int argc, char *argv[])
4948 const struct got_error *error = NULL;
4949 struct got_worktree *worktree = NULL;
4950 struct got_repository *repo = NULL;
4951 char *cwd = NULL, *commit_id_str = NULL;
4952 struct got_object_id *commit_id = NULL;
4953 struct got_commit_object *commit = NULL;
4954 struct got_object_qid *pid;
4955 struct got_reference *head_ref = NULL;
4956 int ch, did_something = 0;
4958 while ((ch = getopt(argc, argv, "")) != -1) {
4959 switch (ch) {
4960 default:
4961 usage_backout();
4962 /* NOTREACHED */
4966 argc -= optind;
4967 argv += optind;
4969 #ifndef PROFILE
4970 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4971 "unveil", NULL) == -1)
4972 err(1, "pledge");
4973 #endif
4974 if (argc != 1)
4975 usage_backout();
4977 cwd = getcwd(NULL, 0);
4978 if (cwd == NULL) {
4979 error = got_error_from_errno("getcwd");
4980 goto done;
4982 error = got_worktree_open(&worktree, cwd);
4983 if (error)
4984 goto done;
4986 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4987 NULL);
4988 if (error != NULL)
4989 goto done;
4991 error = apply_unveil(got_repo_get_path(repo), 0,
4992 got_worktree_get_root_path(worktree));
4993 if (error)
4994 goto done;
4996 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4997 GOT_OBJ_TYPE_COMMIT, repo);
4998 if (error != NULL) {
4999 struct got_reference *ref;
5000 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5001 goto done;
5002 error = got_ref_open(&ref, repo, argv[0], 0);
5003 if (error != NULL)
5004 goto done;
5005 error = got_ref_resolve(&commit_id, repo, ref);
5006 got_ref_close(ref);
5007 if (error != NULL)
5008 goto done;
5010 error = got_object_id_str(&commit_id_str, commit_id);
5011 if (error)
5012 goto done;
5014 error = got_ref_open(&head_ref, repo,
5015 got_worktree_get_head_ref_name(worktree), 0);
5016 if (error != NULL)
5017 goto done;
5019 error = check_same_branch(commit_id, head_ref, NULL, repo);
5020 if (error)
5021 goto done;
5023 error = got_object_open_as_commit(&commit, repo, commit_id);
5024 if (error)
5025 goto done;
5026 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5027 if (pid == NULL) {
5028 error = got_error(GOT_ERR_ROOT_COMMIT);
5029 goto done;
5032 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5033 update_progress, &did_something, check_cancelled, NULL);
5034 if (error != NULL)
5035 goto done;
5037 if (did_something)
5038 printf("Backed out commit %s\n", commit_id_str);
5039 done:
5040 if (commit)
5041 got_object_commit_close(commit);
5042 free(commit_id_str);
5043 if (head_ref)
5044 got_ref_close(head_ref);
5045 if (worktree)
5046 got_worktree_close(worktree);
5047 if (repo)
5048 got_repo_close(repo);
5049 return error;
5052 __dead static void
5053 usage_rebase(void)
5055 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5056 getprogname());
5057 exit(1);
5060 void
5061 trim_logmsg(char *logmsg, int limit)
5063 char *nl;
5064 size_t len;
5066 len = strlen(logmsg);
5067 if (len > limit)
5068 len = limit;
5069 logmsg[len] = '\0';
5070 nl = strchr(logmsg, '\n');
5071 if (nl)
5072 *nl = '\0';
5075 static const struct got_error *
5076 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5078 const struct got_error *err;
5079 char *logmsg0 = NULL;
5080 const char *s;
5082 err = got_object_commit_get_logmsg(&logmsg0, commit);
5083 if (err)
5084 return err;
5086 s = logmsg0;
5087 while (isspace((unsigned char)s[0]))
5088 s++;
5090 *logmsg = strdup(s);
5091 if (*logmsg == NULL) {
5092 err = got_error_from_errno("strdup");
5093 goto done;
5096 trim_logmsg(*logmsg, limit);
5097 done:
5098 free(logmsg0);
5099 return err;
5102 static const struct got_error *
5103 show_rebase_progress(struct got_commit_object *commit,
5104 struct got_object_id *old_id, struct got_object_id *new_id)
5106 const struct got_error *err;
5107 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5109 err = got_object_id_str(&old_id_str, old_id);
5110 if (err)
5111 goto done;
5113 if (new_id) {
5114 err = got_object_id_str(&new_id_str, new_id);
5115 if (err)
5116 goto done;
5119 old_id_str[12] = '\0';
5120 if (new_id_str)
5121 new_id_str[12] = '\0';
5123 err = get_short_logmsg(&logmsg, 42, commit);
5124 if (err)
5125 goto done;
5127 printf("%s -> %s: %s\n", old_id_str,
5128 new_id_str ? new_id_str : "no-op change", logmsg);
5129 done:
5130 free(old_id_str);
5131 free(new_id_str);
5132 return err;
5135 static const struct got_error *
5136 rebase_progress(void *arg, unsigned char status, const char *path)
5138 unsigned char *rebase_status = arg;
5140 while (path[0] == '/')
5141 path++;
5142 printf("%c %s\n", status, path);
5144 if (*rebase_status == GOT_STATUS_CONFLICT)
5145 return NULL;
5146 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5147 *rebase_status = status;
5148 return NULL;
5151 static const struct got_error *
5152 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5153 struct got_reference *branch, struct got_reference *new_base_branch,
5154 struct got_reference *tmp_branch, struct got_repository *repo)
5156 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5157 return got_worktree_rebase_complete(worktree, fileindex,
5158 new_base_branch, tmp_branch, branch, repo);
5161 static const struct got_error *
5162 rebase_commit(struct got_pathlist_head *merged_paths,
5163 struct got_worktree *worktree, struct got_fileindex *fileindex,
5164 struct got_reference *tmp_branch,
5165 struct got_object_id *commit_id, struct got_repository *repo)
5167 const struct got_error *error;
5168 struct got_commit_object *commit;
5169 struct got_object_id *new_commit_id;
5171 error = got_object_open_as_commit(&commit, repo, commit_id);
5172 if (error)
5173 return error;
5175 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5176 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5177 if (error) {
5178 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5179 goto done;
5180 error = show_rebase_progress(commit, commit_id, NULL);
5181 } else {
5182 error = show_rebase_progress(commit, commit_id, new_commit_id);
5183 free(new_commit_id);
5185 done:
5186 got_object_commit_close(commit);
5187 return error;
5190 struct check_path_prefix_arg {
5191 const char *path_prefix;
5192 size_t len;
5193 int errcode;
5196 static const struct got_error *
5197 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5198 struct got_blob_object *blob2, struct got_object_id *id1,
5199 struct got_object_id *id2, const char *path1, const char *path2,
5200 mode_t mode1, mode_t mode2, struct got_repository *repo)
5202 struct check_path_prefix_arg *a = arg;
5204 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5205 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5206 return got_error(a->errcode);
5208 return NULL;
5211 static const struct got_error *
5212 check_path_prefix(struct got_object_id *parent_id,
5213 struct got_object_id *commit_id, const char *path_prefix,
5214 int errcode, struct got_repository *repo)
5216 const struct got_error *err;
5217 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5218 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5219 struct check_path_prefix_arg cpp_arg;
5221 if (got_path_is_root_dir(path_prefix))
5222 return NULL;
5224 err = got_object_open_as_commit(&commit, repo, commit_id);
5225 if (err)
5226 goto done;
5228 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5229 if (err)
5230 goto done;
5232 err = got_object_open_as_tree(&tree1, repo,
5233 got_object_commit_get_tree_id(parent_commit));
5234 if (err)
5235 goto done;
5237 err = got_object_open_as_tree(&tree2, repo,
5238 got_object_commit_get_tree_id(commit));
5239 if (err)
5240 goto done;
5242 cpp_arg.path_prefix = path_prefix;
5243 while (cpp_arg.path_prefix[0] == '/')
5244 cpp_arg.path_prefix++;
5245 cpp_arg.len = strlen(cpp_arg.path_prefix);
5246 cpp_arg.errcode = errcode;
5247 err = got_diff_tree(tree1, tree2, "", "", repo,
5248 check_path_prefix_in_diff, &cpp_arg, 0);
5249 done:
5250 if (tree1)
5251 got_object_tree_close(tree1);
5252 if (tree2)
5253 got_object_tree_close(tree2);
5254 if (commit)
5255 got_object_commit_close(commit);
5256 if (parent_commit)
5257 got_object_commit_close(parent_commit);
5258 return err;
5261 static const struct got_error *
5262 collect_commits(struct got_object_id_queue *commits,
5263 struct got_object_id *initial_commit_id,
5264 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5265 const char *path_prefix, int path_prefix_errcode,
5266 struct got_repository *repo)
5268 const struct got_error *err = NULL;
5269 struct got_commit_graph *graph = NULL;
5270 struct got_object_id *parent_id = NULL;
5271 struct got_object_qid *qid;
5272 struct got_object_id *commit_id = initial_commit_id;
5274 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5275 if (err)
5276 return err;
5278 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5279 check_cancelled, NULL);
5280 if (err)
5281 goto done;
5282 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5283 err = got_commit_graph_iter_next(&parent_id, graph);
5284 if (err) {
5285 if (err->code == GOT_ERR_ITER_COMPLETED) {
5286 err = got_error_msg(GOT_ERR_ANCESTRY,
5287 "ran out of commits to rebase before "
5288 "youngest common ancestor commit has "
5289 "been reached?!?");
5290 goto done;
5291 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5292 goto done;
5293 err = got_commit_graph_fetch_commits(graph, 1, repo,
5294 check_cancelled, NULL);
5295 if (err)
5296 goto done;
5297 } else {
5298 err = check_path_prefix(parent_id, commit_id,
5299 path_prefix, path_prefix_errcode, repo);
5300 if (err)
5301 goto done;
5303 err = got_object_qid_alloc(&qid, commit_id);
5304 if (err)
5305 goto done;
5306 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5307 commit_id = parent_id;
5310 done:
5311 got_commit_graph_close(graph);
5312 return err;
5315 static const struct got_error *
5316 cmd_rebase(int argc, char *argv[])
5318 const struct got_error *error = NULL;
5319 struct got_worktree *worktree = NULL;
5320 struct got_repository *repo = NULL;
5321 struct got_fileindex *fileindex = NULL;
5322 char *cwd = NULL;
5323 struct got_reference *branch = NULL;
5324 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5325 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5326 struct got_object_id *resume_commit_id = NULL;
5327 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5328 struct got_commit_object *commit = NULL;
5329 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5330 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5331 struct got_object_id_queue commits;
5332 struct got_pathlist_head merged_paths;
5333 const struct got_object_id_queue *parent_ids;
5334 struct got_object_qid *qid, *pid;
5336 SIMPLEQ_INIT(&commits);
5337 TAILQ_INIT(&merged_paths);
5339 while ((ch = getopt(argc, argv, "ac")) != -1) {
5340 switch (ch) {
5341 case 'a':
5342 abort_rebase = 1;
5343 break;
5344 case 'c':
5345 continue_rebase = 1;
5346 break;
5347 default:
5348 usage_rebase();
5349 /* NOTREACHED */
5353 argc -= optind;
5354 argv += optind;
5356 #ifndef PROFILE
5357 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5358 "unveil", NULL) == -1)
5359 err(1, "pledge");
5360 #endif
5361 if (abort_rebase && continue_rebase)
5362 usage_rebase();
5363 else if (abort_rebase || continue_rebase) {
5364 if (argc != 0)
5365 usage_rebase();
5366 } else if (argc != 1)
5367 usage_rebase();
5369 cwd = getcwd(NULL, 0);
5370 if (cwd == NULL) {
5371 error = got_error_from_errno("getcwd");
5372 goto done;
5374 error = got_worktree_open(&worktree, cwd);
5375 if (error)
5376 goto done;
5378 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5379 NULL);
5380 if (error != NULL)
5381 goto done;
5383 error = apply_unveil(got_repo_get_path(repo), 0,
5384 got_worktree_get_root_path(worktree));
5385 if (error)
5386 goto done;
5388 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5389 if (error)
5390 goto done;
5392 if (abort_rebase) {
5393 int did_something;
5394 if (!rebase_in_progress) {
5395 error = got_error(GOT_ERR_NOT_REBASING);
5396 goto done;
5398 error = got_worktree_rebase_continue(&resume_commit_id,
5399 &new_base_branch, &tmp_branch, &branch, &fileindex,
5400 worktree, repo);
5401 if (error)
5402 goto done;
5403 printf("Switching work tree to %s\n",
5404 got_ref_get_symref_target(new_base_branch));
5405 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5406 new_base_branch, update_progress, &did_something);
5407 if (error)
5408 goto done;
5409 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5410 goto done; /* nothing else to do */
5413 if (continue_rebase) {
5414 if (!rebase_in_progress) {
5415 error = got_error(GOT_ERR_NOT_REBASING);
5416 goto done;
5418 error = got_worktree_rebase_continue(&resume_commit_id,
5419 &new_base_branch, &tmp_branch, &branch, &fileindex,
5420 worktree, repo);
5421 if (error)
5422 goto done;
5424 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5425 resume_commit_id, repo);
5426 if (error)
5427 goto done;
5429 yca_id = got_object_id_dup(resume_commit_id);
5430 if (yca_id == NULL) {
5431 error = got_error_from_errno("got_object_id_dup");
5432 goto done;
5434 } else {
5435 error = got_ref_open(&branch, repo, argv[0], 0);
5436 if (error != NULL)
5437 goto done;
5440 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5441 if (error)
5442 goto done;
5444 if (!continue_rebase) {
5445 struct got_object_id *base_commit_id;
5447 base_commit_id = got_worktree_get_base_commit_id(worktree);
5448 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5449 base_commit_id, branch_head_commit_id, repo,
5450 check_cancelled, NULL);
5451 if (error)
5452 goto done;
5453 if (yca_id == NULL) {
5454 error = got_error_msg(GOT_ERR_ANCESTRY,
5455 "specified branch shares no common ancestry "
5456 "with work tree's branch");
5457 goto done;
5460 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5461 if (error) {
5462 if (error->code != GOT_ERR_ANCESTRY)
5463 goto done;
5464 error = NULL;
5465 } else {
5466 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5467 "specified branch resolves to a commit which "
5468 "is already contained in work tree's branch");
5469 goto done;
5471 error = got_worktree_rebase_prepare(&new_base_branch,
5472 &tmp_branch, &fileindex, worktree, branch, repo);
5473 if (error)
5474 goto done;
5477 commit_id = branch_head_commit_id;
5478 error = got_object_open_as_commit(&commit, repo, commit_id);
5479 if (error)
5480 goto done;
5482 parent_ids = got_object_commit_get_parent_ids(commit);
5483 pid = SIMPLEQ_FIRST(parent_ids);
5484 if (pid == NULL) {
5485 if (!continue_rebase) {
5486 int did_something;
5487 error = got_worktree_rebase_abort(worktree, fileindex,
5488 repo, new_base_branch, update_progress,
5489 &did_something);
5490 if (error)
5491 goto done;
5492 printf("Rebase of %s aborted\n",
5493 got_ref_get_name(branch));
5495 error = got_error(GOT_ERR_EMPTY_REBASE);
5496 goto done;
5498 error = collect_commits(&commits, commit_id, pid->id,
5499 yca_id, got_worktree_get_path_prefix(worktree),
5500 GOT_ERR_REBASE_PATH, repo);
5501 got_object_commit_close(commit);
5502 commit = NULL;
5503 if (error)
5504 goto done;
5506 if (SIMPLEQ_EMPTY(&commits)) {
5507 if (continue_rebase) {
5508 error = rebase_complete(worktree, fileindex,
5509 branch, new_base_branch, tmp_branch, repo);
5510 goto done;
5511 } else {
5512 /* Fast-forward the reference of the branch. */
5513 struct got_object_id *new_head_commit_id;
5514 char *id_str;
5515 error = got_ref_resolve(&new_head_commit_id, repo,
5516 new_base_branch);
5517 if (error)
5518 goto done;
5519 error = got_object_id_str(&id_str, new_head_commit_id);
5520 printf("Forwarding %s to commit %s\n",
5521 got_ref_get_name(branch), id_str);
5522 free(id_str);
5523 error = got_ref_change_ref(branch,
5524 new_head_commit_id);
5525 if (error)
5526 goto done;
5530 pid = NULL;
5531 SIMPLEQ_FOREACH(qid, &commits, entry) {
5532 commit_id = qid->id;
5533 parent_id = pid ? pid->id : yca_id;
5534 pid = qid;
5536 error = got_worktree_rebase_merge_files(&merged_paths,
5537 worktree, fileindex, parent_id, commit_id, repo,
5538 rebase_progress, &rebase_status, check_cancelled, NULL);
5539 if (error)
5540 goto done;
5542 if (rebase_status == GOT_STATUS_CONFLICT) {
5543 got_worktree_rebase_pathlist_free(&merged_paths);
5544 break;
5547 error = rebase_commit(&merged_paths, worktree, fileindex,
5548 tmp_branch, commit_id, repo);
5549 got_worktree_rebase_pathlist_free(&merged_paths);
5550 if (error)
5551 goto done;
5554 if (rebase_status == GOT_STATUS_CONFLICT) {
5555 error = got_worktree_rebase_postpone(worktree, fileindex);
5556 if (error)
5557 goto done;
5558 error = got_error_msg(GOT_ERR_CONFLICTS,
5559 "conflicts must be resolved before rebasing can continue");
5560 } else
5561 error = rebase_complete(worktree, fileindex, branch,
5562 new_base_branch, tmp_branch, repo);
5563 done:
5564 got_object_id_queue_free(&commits);
5565 free(branch_head_commit_id);
5566 free(resume_commit_id);
5567 free(yca_id);
5568 if (commit)
5569 got_object_commit_close(commit);
5570 if (branch)
5571 got_ref_close(branch);
5572 if (new_base_branch)
5573 got_ref_close(new_base_branch);
5574 if (tmp_branch)
5575 got_ref_close(tmp_branch);
5576 if (worktree)
5577 got_worktree_close(worktree);
5578 if (repo)
5579 got_repo_close(repo);
5580 return error;
5583 __dead static void
5584 usage_histedit(void)
5586 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5587 getprogname());
5588 exit(1);
5591 #define GOT_HISTEDIT_PICK 'p'
5592 #define GOT_HISTEDIT_EDIT 'e'
5593 #define GOT_HISTEDIT_FOLD 'f'
5594 #define GOT_HISTEDIT_DROP 'd'
5595 #define GOT_HISTEDIT_MESG 'm'
5597 static struct got_histedit_cmd {
5598 unsigned char code;
5599 const char *name;
5600 const char *desc;
5601 } got_histedit_cmds[] = {
5602 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5603 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5604 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5605 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5606 { GOT_HISTEDIT_MESG, "mesg",
5607 "single-line log message for commit above (open editor if empty)" },
5610 struct got_histedit_list_entry {
5611 TAILQ_ENTRY(got_histedit_list_entry) entry;
5612 struct got_object_id *commit_id;
5613 const struct got_histedit_cmd *cmd;
5614 char *logmsg;
5616 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5618 static const struct got_error *
5619 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5620 FILE *f, struct got_repository *repo)
5622 const struct got_error *err = NULL;
5623 char *logmsg = NULL, *id_str = NULL;
5624 struct got_commit_object *commit = NULL;
5625 int n;
5627 err = got_object_open_as_commit(&commit, repo, commit_id);
5628 if (err)
5629 goto done;
5631 err = get_short_logmsg(&logmsg, 34, commit);
5632 if (err)
5633 goto done;
5635 err = got_object_id_str(&id_str, commit_id);
5636 if (err)
5637 goto done;
5639 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5640 if (n < 0)
5641 err = got_ferror(f, GOT_ERR_IO);
5642 done:
5643 if (commit)
5644 got_object_commit_close(commit);
5645 free(id_str);
5646 free(logmsg);
5647 return err;
5650 static const struct got_error *
5651 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5652 struct got_repository *repo)
5654 const struct got_error *err = NULL;
5655 struct got_object_qid *qid;
5657 if (SIMPLEQ_EMPTY(commits))
5658 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5660 SIMPLEQ_FOREACH(qid, commits, entry) {
5661 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5662 f, repo);
5663 if (err)
5664 break;
5667 return err;
5670 static const struct got_error *
5671 write_cmd_list(FILE *f)
5673 const struct got_error *err = NULL;
5674 int n, i;
5676 n = fprintf(f, "# Available histedit commands:\n");
5677 if (n < 0)
5678 return got_ferror(f, GOT_ERR_IO);
5680 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5681 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5682 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5683 cmd->desc);
5684 if (n < 0) {
5685 err = got_ferror(f, GOT_ERR_IO);
5686 break;
5689 n = fprintf(f, "# Commits will be processed in order from top to "
5690 "bottom of this file.\n");
5691 if (n < 0)
5692 return got_ferror(f, GOT_ERR_IO);
5693 return err;
5696 static const struct got_error *
5697 histedit_syntax_error(int lineno)
5699 static char msg[42];
5700 int ret;
5702 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5703 lineno);
5704 if (ret == -1 || ret >= sizeof(msg))
5705 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5707 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5710 static const struct got_error *
5711 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5712 char *logmsg, struct got_repository *repo)
5714 const struct got_error *err;
5715 struct got_commit_object *folded_commit = NULL;
5716 char *id_str, *folded_logmsg = NULL;
5718 err = got_object_id_str(&id_str, hle->commit_id);
5719 if (err)
5720 return err;
5722 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5723 if (err)
5724 goto done;
5726 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5727 if (err)
5728 goto done;
5729 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5730 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5731 folded_logmsg) == -1) {
5732 err = got_error_from_errno("asprintf");
5733 goto done;
5735 done:
5736 if (folded_commit)
5737 got_object_commit_close(folded_commit);
5738 free(id_str);
5739 free(folded_logmsg);
5740 return err;
5743 static struct got_histedit_list_entry *
5744 get_folded_commits(struct got_histedit_list_entry *hle)
5746 struct got_histedit_list_entry *prev, *folded = NULL;
5748 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5749 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5750 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5751 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5752 folded = prev;
5753 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5756 return folded;
5759 static const struct got_error *
5760 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5761 struct got_repository *repo)
5763 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5764 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5765 const struct got_error *err = NULL;
5766 struct got_commit_object *commit = NULL;
5767 int fd;
5768 struct got_histedit_list_entry *folded = NULL;
5770 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5771 if (err)
5772 return err;
5774 folded = get_folded_commits(hle);
5775 if (folded) {
5776 while (folded != hle) {
5777 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5778 folded = TAILQ_NEXT(folded, entry);
5779 continue;
5781 err = append_folded_commit_msg(&new_msg, folded,
5782 logmsg, repo);
5783 if (err)
5784 goto done;
5785 free(logmsg);
5786 logmsg = new_msg;
5787 folded = TAILQ_NEXT(folded, entry);
5791 err = got_object_id_str(&id_str, hle->commit_id);
5792 if (err)
5793 goto done;
5794 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5795 if (err)
5796 goto done;
5797 if (asprintf(&new_msg,
5798 "%s\n# original log message of commit %s: %s",
5799 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5800 err = got_error_from_errno("asprintf");
5801 goto done;
5803 free(logmsg);
5804 logmsg = new_msg;
5806 err = got_object_id_str(&id_str, hle->commit_id);
5807 if (err)
5808 goto done;
5810 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5811 if (err)
5812 goto done;
5814 dprintf(fd, logmsg);
5815 close(fd);
5817 err = get_editor(&editor);
5818 if (err)
5819 goto done;
5821 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5822 if (err) {
5823 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5824 goto done;
5825 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5827 done:
5828 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5829 err = got_error_from_errno2("unlink", logmsg_path);
5830 free(logmsg_path);
5831 free(logmsg);
5832 free(orig_logmsg);
5833 free(editor);
5834 if (commit)
5835 got_object_commit_close(commit);
5836 return err;
5839 static const struct got_error *
5840 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5841 FILE *f, struct got_repository *repo)
5843 const struct got_error *err = NULL;
5844 char *line = NULL, *p, *end;
5845 size_t size;
5846 ssize_t len;
5847 int lineno = 0, i;
5848 const struct got_histedit_cmd *cmd;
5849 struct got_object_id *commit_id = NULL;
5850 struct got_histedit_list_entry *hle = NULL;
5852 for (;;) {
5853 len = getline(&line, &size, f);
5854 if (len == -1) {
5855 const struct got_error *getline_err;
5856 if (feof(f))
5857 break;
5858 getline_err = got_error_from_errno("getline");
5859 err = got_ferror(f, getline_err->code);
5860 break;
5862 lineno++;
5863 p = line;
5864 while (isspace((unsigned char)p[0]))
5865 p++;
5866 if (p[0] == '#' || p[0] == '\0') {
5867 free(line);
5868 line = NULL;
5869 continue;
5871 cmd = NULL;
5872 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5873 cmd = &got_histedit_cmds[i];
5874 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5875 isspace((unsigned char)p[strlen(cmd->name)])) {
5876 p += strlen(cmd->name);
5877 break;
5879 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5880 p++;
5881 break;
5884 if (i == nitems(got_histedit_cmds)) {
5885 err = histedit_syntax_error(lineno);
5886 break;
5888 while (isspace((unsigned char)p[0]))
5889 p++;
5890 if (cmd->code == GOT_HISTEDIT_MESG) {
5891 if (hle == NULL || hle->logmsg != NULL) {
5892 err = got_error(GOT_ERR_HISTEDIT_CMD);
5893 break;
5895 if (p[0] == '\0') {
5896 err = histedit_edit_logmsg(hle, repo);
5897 if (err)
5898 break;
5899 } else {
5900 hle->logmsg = strdup(p);
5901 if (hle->logmsg == NULL) {
5902 err = got_error_from_errno("strdup");
5903 break;
5906 free(line);
5907 line = NULL;
5908 continue;
5909 } else {
5910 end = p;
5911 while (end[0] && !isspace((unsigned char)end[0]))
5912 end++;
5913 *end = '\0';
5915 err = got_object_resolve_id_str(&commit_id, repo, p);
5916 if (err) {
5917 /* override error code */
5918 err = histedit_syntax_error(lineno);
5919 break;
5922 hle = malloc(sizeof(*hle));
5923 if (hle == NULL) {
5924 err = got_error_from_errno("malloc");
5925 break;
5927 hle->cmd = cmd;
5928 hle->commit_id = commit_id;
5929 hle->logmsg = NULL;
5930 commit_id = NULL;
5931 free(line);
5932 line = NULL;
5933 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5936 free(line);
5937 free(commit_id);
5938 return err;
5941 static const struct got_error *
5942 histedit_check_script(struct got_histedit_list *histedit_cmds,
5943 struct got_object_id_queue *commits, struct got_repository *repo)
5945 const struct got_error *err = NULL;
5946 struct got_object_qid *qid;
5947 struct got_histedit_list_entry *hle;
5948 static char msg[80];
5949 char *id_str;
5951 if (TAILQ_EMPTY(histedit_cmds))
5952 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5953 "histedit script contains no commands");
5954 if (SIMPLEQ_EMPTY(commits))
5955 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5957 SIMPLEQ_FOREACH(qid, commits, entry) {
5958 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5959 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5960 break;
5962 if (hle == NULL) {
5963 err = got_object_id_str(&id_str, qid->id);
5964 if (err)
5965 return err;
5966 snprintf(msg, sizeof(msg),
5967 "commit %s missing from histedit script", id_str);
5968 free(id_str);
5969 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5973 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5974 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5975 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5976 "last commit in histedit script cannot be folded");
5978 return NULL;
5981 static const struct got_error *
5982 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5983 const char *path, struct got_object_id_queue *commits,
5984 struct got_repository *repo)
5986 const struct got_error *err = NULL;
5987 char *editor;
5988 FILE *f = NULL;
5990 err = get_editor(&editor);
5991 if (err)
5992 return err;
5994 if (spawn_editor(editor, path) == -1) {
5995 err = got_error_from_errno("failed spawning editor");
5996 goto done;
5999 f = fopen(path, "r");
6000 if (f == NULL) {
6001 err = got_error_from_errno("fopen");
6002 goto done;
6004 err = histedit_parse_list(histedit_cmds, f, repo);
6005 if (err)
6006 goto done;
6008 err = histedit_check_script(histedit_cmds, commits, repo);
6009 done:
6010 if (f && fclose(f) != 0 && err == NULL)
6011 err = got_error_from_errno("fclose");
6012 free(editor);
6013 return err;
6016 static const struct got_error *
6017 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6018 struct got_object_id_queue *, const char *, struct got_repository *);
6020 static const struct got_error *
6021 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6022 struct got_object_id_queue *commits, struct got_repository *repo)
6024 const struct got_error *err;
6025 FILE *f = NULL;
6026 char *path = NULL;
6028 err = got_opentemp_named(&path, &f, "got-histedit");
6029 if (err)
6030 return err;
6032 err = write_cmd_list(f);
6033 if (err)
6034 goto done;
6036 err = histedit_write_commit_list(commits, f, repo);
6037 if (err)
6038 goto done;
6040 if (fclose(f) != 0) {
6041 err = got_error_from_errno("fclose");
6042 goto done;
6044 f = NULL;
6046 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6047 if (err) {
6048 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6049 err->code != GOT_ERR_HISTEDIT_CMD)
6050 goto done;
6051 err = histedit_edit_list_retry(histedit_cmds, err,
6052 commits, path, repo);
6054 done:
6055 if (f && fclose(f) != 0 && err == NULL)
6056 err = got_error_from_errno("fclose");
6057 if (path && unlink(path) != 0 && err == NULL)
6058 err = got_error_from_errno2("unlink", path);
6059 free(path);
6060 return err;
6063 static const struct got_error *
6064 histedit_save_list(struct got_histedit_list *histedit_cmds,
6065 struct got_worktree *worktree, struct got_repository *repo)
6067 const struct got_error *err = NULL;
6068 char *path = NULL;
6069 FILE *f = NULL;
6070 struct got_histedit_list_entry *hle;
6071 struct got_commit_object *commit = NULL;
6073 err = got_worktree_get_histedit_script_path(&path, worktree);
6074 if (err)
6075 return err;
6077 f = fopen(path, "w");
6078 if (f == NULL) {
6079 err = got_error_from_errno2("fopen", path);
6080 goto done;
6082 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6083 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6084 repo);
6085 if (err)
6086 break;
6088 if (hle->logmsg) {
6089 int n = fprintf(f, "%c %s\n",
6090 GOT_HISTEDIT_MESG, hle->logmsg);
6091 if (n < 0) {
6092 err = got_ferror(f, GOT_ERR_IO);
6093 break;
6097 done:
6098 if (f && fclose(f) != 0 && err == NULL)
6099 err = got_error_from_errno("fclose");
6100 free(path);
6101 if (commit)
6102 got_object_commit_close(commit);
6103 return err;
6106 void
6107 histedit_free_list(struct got_histedit_list *histedit_cmds)
6109 struct got_histedit_list_entry *hle;
6111 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6112 TAILQ_REMOVE(histedit_cmds, hle, entry);
6113 free(hle);
6117 static const struct got_error *
6118 histedit_load_list(struct got_histedit_list *histedit_cmds,
6119 const char *path, struct got_repository *repo)
6121 const struct got_error *err = NULL;
6122 FILE *f = NULL;
6124 f = fopen(path, "r");
6125 if (f == NULL) {
6126 err = got_error_from_errno2("fopen", path);
6127 goto done;
6130 err = histedit_parse_list(histedit_cmds, f, repo);
6131 done:
6132 if (f && fclose(f) != 0 && err == NULL)
6133 err = got_error_from_errno("fclose");
6134 return err;
6137 static const struct got_error *
6138 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6139 const struct got_error *edit_err, struct got_object_id_queue *commits,
6140 const char *path, struct got_repository *repo)
6142 const struct got_error *err = NULL, *prev_err = edit_err;
6143 int resp = ' ';
6145 while (resp != 'c' && resp != 'r' && resp != 'a') {
6146 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6147 "or (a)bort: ", getprogname(), prev_err->msg);
6148 resp = getchar();
6149 if (resp == '\n')
6150 resp = getchar();
6151 if (resp == 'c') {
6152 histedit_free_list(histedit_cmds);
6153 err = histedit_run_editor(histedit_cmds, path, commits,
6154 repo);
6155 if (err) {
6156 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6157 err->code != GOT_ERR_HISTEDIT_CMD)
6158 break;
6159 prev_err = err;
6160 resp = ' ';
6161 continue;
6163 break;
6164 } else if (resp == 'r') {
6165 histedit_free_list(histedit_cmds);
6166 err = histedit_edit_script(histedit_cmds,
6167 commits, repo);
6168 if (err) {
6169 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6170 err->code != GOT_ERR_HISTEDIT_CMD)
6171 break;
6172 prev_err = err;
6173 resp = ' ';
6174 continue;
6176 break;
6177 } else if (resp == 'a') {
6178 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6179 break;
6180 } else
6181 printf("invalid response '%c'\n", resp);
6184 return err;
6187 static const struct got_error *
6188 histedit_complete(struct got_worktree *worktree,
6189 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6190 struct got_reference *branch, struct got_repository *repo)
6192 printf("Switching work tree to %s\n",
6193 got_ref_get_symref_target(branch));
6194 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6195 branch, repo);
6198 static const struct got_error *
6199 show_histedit_progress(struct got_commit_object *commit,
6200 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6202 const struct got_error *err;
6203 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6205 err = got_object_id_str(&old_id_str, hle->commit_id);
6206 if (err)
6207 goto done;
6209 if (new_id) {
6210 err = got_object_id_str(&new_id_str, new_id);
6211 if (err)
6212 goto done;
6215 old_id_str[12] = '\0';
6216 if (new_id_str)
6217 new_id_str[12] = '\0';
6219 if (hle->logmsg) {
6220 logmsg = strdup(hle->logmsg);
6221 if (logmsg == NULL) {
6222 err = got_error_from_errno("strdup");
6223 goto done;
6225 trim_logmsg(logmsg, 42);
6226 } else {
6227 err = get_short_logmsg(&logmsg, 42, commit);
6228 if (err)
6229 goto done;
6232 switch (hle->cmd->code) {
6233 case GOT_HISTEDIT_PICK:
6234 case GOT_HISTEDIT_EDIT:
6235 printf("%s -> %s: %s\n", old_id_str,
6236 new_id_str ? new_id_str : "no-op change", logmsg);
6237 break;
6238 case GOT_HISTEDIT_DROP:
6239 case GOT_HISTEDIT_FOLD:
6240 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6241 logmsg);
6242 break;
6243 default:
6244 break;
6247 done:
6248 free(old_id_str);
6249 free(new_id_str);
6250 return err;
6253 static const struct got_error *
6254 histedit_commit(struct got_pathlist_head *merged_paths,
6255 struct got_worktree *worktree, struct got_fileindex *fileindex,
6256 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6257 struct got_repository *repo)
6259 const struct got_error *err;
6260 struct got_commit_object *commit;
6261 struct got_object_id *new_commit_id;
6263 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6264 && hle->logmsg == NULL) {
6265 err = histedit_edit_logmsg(hle, repo);
6266 if (err)
6267 return err;
6270 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6271 if (err)
6272 return err;
6274 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6275 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6276 hle->logmsg, repo);
6277 if (err) {
6278 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6279 goto done;
6280 err = show_histedit_progress(commit, hle, NULL);
6281 } else {
6282 err = show_histedit_progress(commit, hle, new_commit_id);
6283 free(new_commit_id);
6285 done:
6286 got_object_commit_close(commit);
6287 return err;
6290 static const struct got_error *
6291 histedit_skip_commit(struct got_histedit_list_entry *hle,
6292 struct got_worktree *worktree, struct got_repository *repo)
6294 const struct got_error *error;
6295 struct got_commit_object *commit;
6297 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6298 repo);
6299 if (error)
6300 return error;
6302 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6303 if (error)
6304 return error;
6306 error = show_histedit_progress(commit, hle, NULL);
6307 got_object_commit_close(commit);
6308 return error;
6311 static const struct got_error *
6312 cmd_histedit(int argc, char *argv[])
6314 const struct got_error *error = NULL;
6315 struct got_worktree *worktree = NULL;
6316 struct got_fileindex *fileindex = NULL;
6317 struct got_repository *repo = NULL;
6318 char *cwd = NULL;
6319 struct got_reference *branch = NULL;
6320 struct got_reference *tmp_branch = NULL;
6321 struct got_object_id *resume_commit_id = NULL;
6322 struct got_object_id *base_commit_id = NULL;
6323 struct got_object_id *head_commit_id = NULL;
6324 struct got_commit_object *commit = NULL;
6325 int ch, rebase_in_progress = 0, did_something;
6326 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6327 const char *edit_script_path = NULL;
6328 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6329 struct got_object_id_queue commits;
6330 struct got_pathlist_head merged_paths;
6331 const struct got_object_id_queue *parent_ids;
6332 struct got_object_qid *pid;
6333 struct got_histedit_list histedit_cmds;
6334 struct got_histedit_list_entry *hle;
6336 SIMPLEQ_INIT(&commits);
6337 TAILQ_INIT(&histedit_cmds);
6338 TAILQ_INIT(&merged_paths);
6340 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6341 switch (ch) {
6342 case 'a':
6343 abort_edit = 1;
6344 break;
6345 case 'c':
6346 continue_edit = 1;
6347 break;
6348 case 'F':
6349 edit_script_path = optarg;
6350 break;
6351 default:
6352 usage_histedit();
6353 /* NOTREACHED */
6357 argc -= optind;
6358 argv += optind;
6360 #ifndef PROFILE
6361 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6362 "unveil", NULL) == -1)
6363 err(1, "pledge");
6364 #endif
6365 if (abort_edit && continue_edit)
6366 usage_histedit();
6367 if (argc != 0)
6368 usage_histedit();
6371 * This command cannot apply unveil(2) in all cases because the
6372 * user may choose to run an editor to edit the histedit script
6373 * and to edit individual commit log messages.
6374 * unveil(2) traverses exec(2); if an editor is used we have to
6375 * apply unveil after edit script and log messages have been written.
6376 * XXX TODO: Make use of unveil(2) where possible.
6379 cwd = getcwd(NULL, 0);
6380 if (cwd == NULL) {
6381 error = got_error_from_errno("getcwd");
6382 goto done;
6384 error = got_worktree_open(&worktree, cwd);
6385 if (error)
6386 goto done;
6388 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6389 NULL);
6390 if (error != NULL)
6391 goto done;
6393 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6394 if (error)
6395 goto done;
6396 if (rebase_in_progress) {
6397 error = got_error(GOT_ERR_REBASING);
6398 goto done;
6401 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6402 if (error)
6403 goto done;
6405 if (edit_in_progress && abort_edit) {
6406 error = got_worktree_histedit_continue(&resume_commit_id,
6407 &tmp_branch, &branch, &base_commit_id, &fileindex,
6408 worktree, repo);
6409 if (error)
6410 goto done;
6411 printf("Switching work tree to %s\n",
6412 got_ref_get_symref_target(branch));
6413 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6414 branch, base_commit_id, update_progress, &did_something);
6415 if (error)
6416 goto done;
6417 printf("Histedit of %s aborted\n",
6418 got_ref_get_symref_target(branch));
6419 goto done; /* nothing else to do */
6420 } else if (abort_edit) {
6421 error = got_error(GOT_ERR_NOT_HISTEDIT);
6422 goto done;
6425 if (continue_edit) {
6426 char *path;
6428 if (!edit_in_progress) {
6429 error = got_error(GOT_ERR_NOT_HISTEDIT);
6430 goto done;
6433 error = got_worktree_get_histedit_script_path(&path, worktree);
6434 if (error)
6435 goto done;
6437 error = histedit_load_list(&histedit_cmds, path, repo);
6438 free(path);
6439 if (error)
6440 goto done;
6442 error = got_worktree_histedit_continue(&resume_commit_id,
6443 &tmp_branch, &branch, &base_commit_id, &fileindex,
6444 worktree, repo);
6445 if (error)
6446 goto done;
6448 error = got_ref_resolve(&head_commit_id, repo, branch);
6449 if (error)
6450 goto done;
6452 error = got_object_open_as_commit(&commit, repo,
6453 head_commit_id);
6454 if (error)
6455 goto done;
6456 parent_ids = got_object_commit_get_parent_ids(commit);
6457 pid = SIMPLEQ_FIRST(parent_ids);
6458 if (pid == NULL) {
6459 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6460 goto done;
6462 error = collect_commits(&commits, head_commit_id, pid->id,
6463 base_commit_id, got_worktree_get_path_prefix(worktree),
6464 GOT_ERR_HISTEDIT_PATH, repo);
6465 got_object_commit_close(commit);
6466 commit = NULL;
6467 if (error)
6468 goto done;
6469 } else {
6470 if (edit_in_progress) {
6471 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6472 goto done;
6475 error = got_ref_open(&branch, repo,
6476 got_worktree_get_head_ref_name(worktree), 0);
6477 if (error != NULL)
6478 goto done;
6480 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6481 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6482 "will not edit commit history of a branch outside "
6483 "the \"refs/heads/\" reference namespace");
6484 goto done;
6487 error = got_ref_resolve(&head_commit_id, repo, branch);
6488 got_ref_close(branch);
6489 branch = NULL;
6490 if (error)
6491 goto done;
6493 error = got_object_open_as_commit(&commit, repo,
6494 head_commit_id);
6495 if (error)
6496 goto done;
6497 parent_ids = got_object_commit_get_parent_ids(commit);
6498 pid = SIMPLEQ_FIRST(parent_ids);
6499 if (pid == NULL) {
6500 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6501 goto done;
6503 error = collect_commits(&commits, head_commit_id, pid->id,
6504 got_worktree_get_base_commit_id(worktree),
6505 got_worktree_get_path_prefix(worktree),
6506 GOT_ERR_HISTEDIT_PATH, repo);
6507 got_object_commit_close(commit);
6508 commit = NULL;
6509 if (error)
6510 goto done;
6512 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6513 &base_commit_id, &fileindex, worktree, repo);
6514 if (error)
6515 goto done;
6517 if (edit_script_path) {
6518 error = histedit_load_list(&histedit_cmds,
6519 edit_script_path, repo);
6520 if (error) {
6521 got_worktree_histedit_abort(worktree, fileindex,
6522 repo, branch, base_commit_id,
6523 update_progress, &did_something);
6524 goto done;
6526 } else {
6527 error = histedit_edit_script(&histedit_cmds, &commits,
6528 repo);
6529 if (error) {
6530 got_worktree_histedit_abort(worktree, fileindex,
6531 repo, branch, base_commit_id,
6532 update_progress, &did_something);
6533 goto done;
6538 error = histedit_save_list(&histedit_cmds, worktree,
6539 repo);
6540 if (error) {
6541 got_worktree_histedit_abort(worktree, fileindex,
6542 repo, branch, base_commit_id,
6543 update_progress, &did_something);
6544 goto done;
6549 error = histedit_check_script(&histedit_cmds, &commits, repo);
6550 if (error)
6551 goto done;
6553 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6554 if (resume_commit_id) {
6555 if (got_object_id_cmp(hle->commit_id,
6556 resume_commit_id) != 0)
6557 continue;
6559 resume_commit_id = NULL;
6560 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6561 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6562 error = histedit_skip_commit(hle, worktree,
6563 repo);
6564 } else {
6565 error = histedit_commit(NULL, worktree,
6566 fileindex, tmp_branch, hle, repo);
6568 if (error)
6569 goto done;
6570 continue;
6573 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6574 error = histedit_skip_commit(hle, worktree, repo);
6575 if (error)
6576 goto done;
6577 continue;
6580 error = got_object_open_as_commit(&commit, repo,
6581 hle->commit_id);
6582 if (error)
6583 goto done;
6584 parent_ids = got_object_commit_get_parent_ids(commit);
6585 pid = SIMPLEQ_FIRST(parent_ids);
6587 error = got_worktree_histedit_merge_files(&merged_paths,
6588 worktree, fileindex, pid->id, hle->commit_id, repo,
6589 rebase_progress, &rebase_status, check_cancelled, NULL);
6590 if (error)
6591 goto done;
6592 got_object_commit_close(commit);
6593 commit = NULL;
6595 if (rebase_status == GOT_STATUS_CONFLICT) {
6596 got_worktree_rebase_pathlist_free(&merged_paths);
6597 break;
6600 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6601 char *id_str;
6602 error = got_object_id_str(&id_str, hle->commit_id);
6603 if (error)
6604 goto done;
6605 printf("Stopping histedit for amending commit %s\n",
6606 id_str);
6607 free(id_str);
6608 got_worktree_rebase_pathlist_free(&merged_paths);
6609 error = got_worktree_histedit_postpone(worktree,
6610 fileindex);
6611 goto done;
6614 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6615 error = histedit_skip_commit(hle, worktree, repo);
6616 if (error)
6617 goto done;
6618 continue;
6621 error = histedit_commit(&merged_paths, worktree, fileindex,
6622 tmp_branch, hle, repo);
6623 got_worktree_rebase_pathlist_free(&merged_paths);
6624 if (error)
6625 goto done;
6628 if (rebase_status == GOT_STATUS_CONFLICT) {
6629 error = got_worktree_histedit_postpone(worktree, fileindex);
6630 if (error)
6631 goto done;
6632 error = got_error_msg(GOT_ERR_CONFLICTS,
6633 "conflicts must be resolved before rebasing can continue");
6634 } else
6635 error = histedit_complete(worktree, fileindex, tmp_branch,
6636 branch, repo);
6637 done:
6638 got_object_id_queue_free(&commits);
6639 histedit_free_list(&histedit_cmds);
6640 free(head_commit_id);
6641 free(base_commit_id);
6642 free(resume_commit_id);
6643 if (commit)
6644 got_object_commit_close(commit);
6645 if (branch)
6646 got_ref_close(branch);
6647 if (tmp_branch)
6648 got_ref_close(tmp_branch);
6649 if (worktree)
6650 got_worktree_close(worktree);
6651 if (repo)
6652 got_repo_close(repo);
6653 return error;
6656 __dead static void
6657 usage_integrate(void)
6659 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6660 exit(1);
6663 static const struct got_error *
6664 cmd_integrate(int argc, char *argv[])
6666 const struct got_error *error = NULL;
6667 struct got_repository *repo = NULL;
6668 struct got_worktree *worktree = NULL;
6669 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6670 const char *branch_arg = NULL;
6671 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6672 struct got_fileindex *fileindex = NULL;
6673 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6674 int ch, did_something = 0;
6676 while ((ch = getopt(argc, argv, "")) != -1) {
6677 switch (ch) {
6678 default:
6679 usage_integrate();
6680 /* NOTREACHED */
6684 argc -= optind;
6685 argv += optind;
6687 if (argc != 1)
6688 usage_integrate();
6689 branch_arg = argv[0];
6691 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6692 "unveil", NULL) == -1)
6693 err(1, "pledge");
6695 cwd = getcwd(NULL, 0);
6696 if (cwd == NULL) {
6697 error = got_error_from_errno("getcwd");
6698 goto done;
6701 error = got_worktree_open(&worktree, cwd);
6702 if (error)
6703 goto done;
6705 error = check_rebase_or_histedit_in_progress(worktree);
6706 if (error)
6707 goto done;
6709 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6710 NULL);
6711 if (error != NULL)
6712 goto done;
6714 error = apply_unveil(got_repo_get_path(repo), 0,
6715 got_worktree_get_root_path(worktree));
6716 if (error)
6717 goto done;
6719 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6720 error = got_error_from_errno("asprintf");
6721 goto done;
6724 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6725 &base_branch_ref, worktree, refname, repo);
6726 if (error)
6727 goto done;
6729 refname = strdup(got_ref_get_name(branch_ref));
6730 if (refname == NULL) {
6731 error = got_error_from_errno("strdup");
6732 got_worktree_integrate_abort(worktree, fileindex, repo,
6733 branch_ref, base_branch_ref);
6734 goto done;
6736 base_refname = strdup(got_ref_get_name(base_branch_ref));
6737 if (base_refname == NULL) {
6738 error = got_error_from_errno("strdup");
6739 got_worktree_integrate_abort(worktree, fileindex, repo,
6740 branch_ref, base_branch_ref);
6741 goto done;
6744 error = got_ref_resolve(&commit_id, repo, branch_ref);
6745 if (error)
6746 goto done;
6748 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6749 if (error)
6750 goto done;
6752 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6753 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6754 "specified branch has already been integrated");
6755 got_worktree_integrate_abort(worktree, fileindex, repo,
6756 branch_ref, base_branch_ref);
6757 goto done;
6760 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6761 if (error) {
6762 if (error->code == GOT_ERR_ANCESTRY)
6763 error = got_error(GOT_ERR_REBASE_REQUIRED);
6764 got_worktree_integrate_abort(worktree, fileindex, repo,
6765 branch_ref, base_branch_ref);
6766 goto done;
6769 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6770 branch_ref, base_branch_ref, update_progress, &did_something,
6771 check_cancelled, NULL);
6772 if (error)
6773 goto done;
6775 printf("Integrated %s into %s\n", refname, base_refname);
6776 done:
6777 if (repo)
6778 got_repo_close(repo);
6779 if (worktree)
6780 got_worktree_close(worktree);
6781 free(cwd);
6782 free(base_commit_id);
6783 free(commit_id);
6784 free(refname);
6785 free(base_refname);
6786 return error;
6789 __dead static void
6790 usage_stage(void)
6792 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6793 "[file-path ...]\n",
6794 getprogname());
6795 exit(1);
6798 static const struct got_error *
6799 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6800 const char *path, struct got_object_id *blob_id,
6801 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6803 const struct got_error *err = NULL;
6804 char *id_str = NULL;
6806 if (staged_status != GOT_STATUS_ADD &&
6807 staged_status != GOT_STATUS_MODIFY &&
6808 staged_status != GOT_STATUS_DELETE)
6809 return NULL;
6811 if (staged_status == GOT_STATUS_ADD ||
6812 staged_status == GOT_STATUS_MODIFY)
6813 err = got_object_id_str(&id_str, staged_blob_id);
6814 else
6815 err = got_object_id_str(&id_str, blob_id);
6816 if (err)
6817 return err;
6819 printf("%s %c %s\n", id_str, staged_status, path);
6820 free(id_str);
6821 return NULL;
6824 static const struct got_error *
6825 cmd_stage(int argc, char *argv[])
6827 const struct got_error *error = NULL;
6828 struct got_repository *repo = NULL;
6829 struct got_worktree *worktree = NULL;
6830 char *cwd = NULL;
6831 struct got_pathlist_head paths;
6832 struct got_pathlist_entry *pe;
6833 int ch, list_stage = 0, pflag = 0;
6834 FILE *patch_script_file = NULL;
6835 const char *patch_script_path = NULL;
6836 struct choose_patch_arg cpa;
6838 TAILQ_INIT(&paths);
6840 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6841 switch (ch) {
6842 case 'l':
6843 list_stage = 1;
6844 break;
6845 case 'p':
6846 pflag = 1;
6847 break;
6848 case 'F':
6849 patch_script_path = optarg;
6850 break;
6851 default:
6852 usage_stage();
6853 /* NOTREACHED */
6857 argc -= optind;
6858 argv += optind;
6860 #ifndef PROFILE
6861 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6862 "unveil", NULL) == -1)
6863 err(1, "pledge");
6864 #endif
6865 if (list_stage && (pflag || patch_script_path))
6866 errx(1, "-l option cannot be used with other options");
6867 if (patch_script_path && !pflag)
6868 errx(1, "-F option can only be used together with -p option");
6870 cwd = getcwd(NULL, 0);
6871 if (cwd == NULL) {
6872 error = got_error_from_errno("getcwd");
6873 goto done;
6876 error = got_worktree_open(&worktree, cwd);
6877 if (error)
6878 goto done;
6880 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6881 NULL);
6882 if (error != NULL)
6883 goto done;
6885 if (patch_script_path) {
6886 patch_script_file = fopen(patch_script_path, "r");
6887 if (patch_script_file == NULL) {
6888 error = got_error_from_errno2("fopen",
6889 patch_script_path);
6890 goto done;
6893 error = apply_unveil(got_repo_get_path(repo), 0,
6894 got_worktree_get_root_path(worktree));
6895 if (error)
6896 goto done;
6898 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6899 if (error)
6900 goto done;
6902 if (list_stage)
6903 error = got_worktree_status(worktree, &paths, repo,
6904 print_stage, NULL, check_cancelled, NULL);
6905 else {
6906 cpa.patch_script_file = patch_script_file;
6907 cpa.action = "stage";
6908 error = got_worktree_stage(worktree, &paths,
6909 pflag ? NULL : print_status, NULL,
6910 pflag ? choose_patch : NULL, &cpa, repo);
6912 done:
6913 if (patch_script_file && fclose(patch_script_file) == EOF &&
6914 error == NULL)
6915 error = got_error_from_errno2("fclose", patch_script_path);
6916 if (repo)
6917 got_repo_close(repo);
6918 if (worktree)
6919 got_worktree_close(worktree);
6920 TAILQ_FOREACH(pe, &paths, entry)
6921 free((char *)pe->path);
6922 got_pathlist_free(&paths);
6923 free(cwd);
6924 return error;
6927 __dead static void
6928 usage_unstage(void)
6930 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6931 "[file-path ...]\n",
6932 getprogname());
6933 exit(1);
6937 static const struct got_error *
6938 cmd_unstage(int argc, char *argv[])
6940 const struct got_error *error = NULL;
6941 struct got_repository *repo = NULL;
6942 struct got_worktree *worktree = NULL;
6943 char *cwd = NULL;
6944 struct got_pathlist_head paths;
6945 struct got_pathlist_entry *pe;
6946 int ch, did_something = 0, pflag = 0;
6947 FILE *patch_script_file = NULL;
6948 const char *patch_script_path = NULL;
6949 struct choose_patch_arg cpa;
6951 TAILQ_INIT(&paths);
6953 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6954 switch (ch) {
6955 case 'p':
6956 pflag = 1;
6957 break;
6958 case 'F':
6959 patch_script_path = optarg;
6960 break;
6961 default:
6962 usage_unstage();
6963 /* NOTREACHED */
6967 argc -= optind;
6968 argv += optind;
6970 #ifndef PROFILE
6971 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6972 "unveil", NULL) == -1)
6973 err(1, "pledge");
6974 #endif
6975 if (patch_script_path && !pflag)
6976 errx(1, "-F option can only be used together with -p option");
6978 cwd = getcwd(NULL, 0);
6979 if (cwd == NULL) {
6980 error = got_error_from_errno("getcwd");
6981 goto done;
6984 error = got_worktree_open(&worktree, cwd);
6985 if (error)
6986 goto done;
6988 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6989 NULL);
6990 if (error != NULL)
6991 goto done;
6993 if (patch_script_path) {
6994 patch_script_file = fopen(patch_script_path, "r");
6995 if (patch_script_file == NULL) {
6996 error = got_error_from_errno2("fopen",
6997 patch_script_path);
6998 goto done;
7002 error = apply_unveil(got_repo_get_path(repo), 0,
7003 got_worktree_get_root_path(worktree));
7004 if (error)
7005 goto done;
7007 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7008 if (error)
7009 goto done;
7011 cpa.patch_script_file = patch_script_file;
7012 cpa.action = "unstage";
7013 error = got_worktree_unstage(worktree, &paths, update_progress,
7014 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7015 done:
7016 if (patch_script_file && fclose(patch_script_file) == EOF &&
7017 error == NULL)
7018 error = got_error_from_errno2("fclose", patch_script_path);
7019 if (repo)
7020 got_repo_close(repo);
7021 if (worktree)
7022 got_worktree_close(worktree);
7023 TAILQ_FOREACH(pe, &paths, entry)
7024 free((char *)pe->path);
7025 got_pathlist_free(&paths);
7026 free(cwd);
7027 return error;
7030 __dead static void
7031 usage_cat(void)
7033 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7034 "arg1 [arg2 ...]\n", getprogname());
7035 exit(1);
7038 static const struct got_error *
7039 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7041 const struct got_error *err;
7042 struct got_blob_object *blob;
7044 err = got_object_open_as_blob(&blob, repo, id, 8192);
7045 if (err)
7046 return err;
7048 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7049 got_object_blob_close(blob);
7050 return err;
7053 static const struct got_error *
7054 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7056 const struct got_error *err;
7057 struct got_tree_object *tree;
7058 int nentries, i;
7060 err = got_object_open_as_tree(&tree, repo, id);
7061 if (err)
7062 return err;
7064 nentries = got_object_tree_get_nentries(tree);
7065 for (i = 0; i < nentries; i++) {
7066 struct got_tree_entry *te;
7067 char *id_str;
7068 if (sigint_received || sigpipe_received)
7069 break;
7070 te = got_object_tree_get_entry(tree, i);
7071 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7072 if (err)
7073 break;
7074 fprintf(outfile, "%s %.7o %s\n", id_str,
7075 got_tree_entry_get_mode(te),
7076 got_tree_entry_get_name(te));
7077 free(id_str);
7080 got_object_tree_close(tree);
7081 return err;
7084 static const struct got_error *
7085 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7087 const struct got_error *err;
7088 struct got_commit_object *commit;
7089 const struct got_object_id_queue *parent_ids;
7090 struct got_object_qid *pid;
7091 char *id_str = NULL;
7092 const char *logmsg = NULL;
7094 err = got_object_open_as_commit(&commit, repo, id);
7095 if (err)
7096 return err;
7098 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7099 if (err)
7100 goto done;
7102 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7103 parent_ids = got_object_commit_get_parent_ids(commit);
7104 fprintf(outfile, "numparents %d\n",
7105 got_object_commit_get_nparents(commit));
7106 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7107 char *pid_str;
7108 err = got_object_id_str(&pid_str, pid->id);
7109 if (err)
7110 goto done;
7111 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7112 free(pid_str);
7114 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7115 got_object_commit_get_author(commit),
7116 got_object_commit_get_author_time(commit));
7118 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7119 got_object_commit_get_author(commit),
7120 got_object_commit_get_committer_time(commit));
7122 logmsg = got_object_commit_get_logmsg_raw(commit);
7123 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7124 fprintf(outfile, "%s", logmsg);
7125 done:
7126 free(id_str);
7127 got_object_commit_close(commit);
7128 return err;
7131 static const struct got_error *
7132 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7134 const struct got_error *err;
7135 struct got_tag_object *tag;
7136 char *id_str = NULL;
7137 const char *tagmsg = NULL;
7139 err = got_object_open_as_tag(&tag, repo, id);
7140 if (err)
7141 return err;
7143 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7144 if (err)
7145 goto done;
7147 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7149 switch (got_object_tag_get_object_type(tag)) {
7150 case GOT_OBJ_TYPE_BLOB:
7151 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7152 GOT_OBJ_LABEL_BLOB);
7153 break;
7154 case GOT_OBJ_TYPE_TREE:
7155 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7156 GOT_OBJ_LABEL_TREE);
7157 break;
7158 case GOT_OBJ_TYPE_COMMIT:
7159 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7160 GOT_OBJ_LABEL_COMMIT);
7161 break;
7162 case GOT_OBJ_TYPE_TAG:
7163 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7164 GOT_OBJ_LABEL_TAG);
7165 break;
7166 default:
7167 break;
7170 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7171 got_object_tag_get_name(tag));
7173 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7174 got_object_tag_get_tagger(tag),
7175 got_object_tag_get_tagger_time(tag));
7177 tagmsg = got_object_tag_get_message(tag);
7178 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7179 fprintf(outfile, "%s", tagmsg);
7180 done:
7181 free(id_str);
7182 got_object_tag_close(tag);
7183 return err;
7186 static const struct got_error *
7187 cmd_cat(int argc, char *argv[])
7189 const struct got_error *error;
7190 struct got_repository *repo = NULL;
7191 struct got_worktree *worktree = NULL;
7192 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7193 const char *commit_id_str = NULL;
7194 struct got_object_id *id = NULL, *commit_id = NULL;
7195 int ch, obj_type, i, force_path = 0;
7197 #ifndef PROFILE
7198 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7199 NULL) == -1)
7200 err(1, "pledge");
7201 #endif
7203 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7204 switch (ch) {
7205 case 'c':
7206 commit_id_str = optarg;
7207 break;
7208 case 'r':
7209 repo_path = realpath(optarg, NULL);
7210 if (repo_path == NULL)
7211 return got_error_from_errno2("realpath",
7212 optarg);
7213 got_path_strip_trailing_slashes(repo_path);
7214 break;
7215 case 'P':
7216 force_path = 1;
7217 break;
7218 default:
7219 usage_cat();
7220 /* NOTREACHED */
7224 argc -= optind;
7225 argv += optind;
7227 cwd = getcwd(NULL, 0);
7228 if (cwd == NULL) {
7229 error = got_error_from_errno("getcwd");
7230 goto done;
7232 error = got_worktree_open(&worktree, cwd);
7233 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7234 goto done;
7235 if (worktree) {
7236 if (repo_path == NULL) {
7237 repo_path = strdup(
7238 got_worktree_get_repo_path(worktree));
7239 if (repo_path == NULL) {
7240 error = got_error_from_errno("strdup");
7241 goto done;
7246 if (repo_path == NULL) {
7247 repo_path = getcwd(NULL, 0);
7248 if (repo_path == NULL)
7249 return got_error_from_errno("getcwd");
7252 error = got_repo_open(&repo, repo_path, NULL);
7253 free(repo_path);
7254 if (error != NULL)
7255 goto done;
7257 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7258 if (error)
7259 goto done;
7261 if (commit_id_str == NULL)
7262 commit_id_str = GOT_REF_HEAD;
7263 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7264 if (error)
7265 goto done;
7267 for (i = 0; i < argc; i++) {
7268 if (force_path) {
7269 error = got_object_id_by_path(&id, repo, commit_id,
7270 argv[i]);
7271 if (error)
7272 break;
7273 } else {
7274 error = match_object_id(&id, &label, argv[i],
7275 GOT_OBJ_TYPE_ANY, 0, repo);
7276 if (error) {
7277 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7278 error->code != GOT_ERR_NOT_REF)
7279 break;
7280 error = got_object_id_by_path(&id, repo,
7281 commit_id, argv[i]);
7282 if (error)
7283 break;
7287 error = got_object_get_type(&obj_type, repo, id);
7288 if (error)
7289 break;
7291 switch (obj_type) {
7292 case GOT_OBJ_TYPE_BLOB:
7293 error = cat_blob(id, repo, stdout);
7294 break;
7295 case GOT_OBJ_TYPE_TREE:
7296 error = cat_tree(id, repo, stdout);
7297 break;
7298 case GOT_OBJ_TYPE_COMMIT:
7299 error = cat_commit(id, repo, stdout);
7300 break;
7301 case GOT_OBJ_TYPE_TAG:
7302 error = cat_tag(id, repo, stdout);
7303 break;
7304 default:
7305 error = got_error(GOT_ERR_OBJ_TYPE);
7306 break;
7308 if (error)
7309 break;
7310 free(label);
7311 label = NULL;
7312 free(id);
7313 id = NULL;
7316 done:
7317 free(label);
7318 free(id);
7319 free(commit_id);
7320 if (worktree)
7321 got_worktree_close(worktree);
7322 if (repo) {
7323 const struct got_error *repo_error;
7324 repo_error = got_repo_close(repo);
7325 if (error == NULL)
7326 error = repo_error;
7328 return error;