Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 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 <fcntl.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <ctype.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <time.h>
37 #include <paths.h>
38 #include <regex.h>
39 #include <getopt.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_diff.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_opentemp.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static volatile sig_atomic_t sigint_received;
60 static volatile sig_atomic_t sigpipe_received;
62 static void
63 catch_sigint(int signo)
64 {
65 sigint_received = 1;
66 }
68 static void
69 catch_sigpipe(int signo)
70 {
71 sigpipe_received = 1;
72 }
75 struct got_cmd {
76 const char *cmd_name;
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
80 };
82 __dead static void usage(int);
83 __dead static void usage_init(void);
84 __dead static void usage_import(void);
85 __dead static void usage_checkout(void);
86 __dead static void usage_update(void);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_status(void);
92 __dead static void usage_ref(void);
93 __dead static void usage_branch(void);
94 __dead static void usage_tag(void);
95 __dead static void usage_add(void);
96 __dead static void usage_remove(void);
97 __dead static void usage_revert(void);
98 __dead static void usage_commit(void);
99 __dead static void usage_cherrypick(void);
100 __dead static void usage_backout(void);
101 __dead static void usage_rebase(void);
102 __dead static void usage_histedit(void);
103 __dead static void usage_integrate(void);
104 __dead static void usage_stage(void);
105 __dead static void usage_unstage(void);
106 __dead static void usage_cat(void);
108 static const struct got_error* cmd_init(int, char *[]);
109 static const struct got_error* cmd_import(int, char *[]);
110 static const struct got_error* cmd_checkout(int, char *[]);
111 static const struct got_error* cmd_update(int, char *[]);
112 static const struct got_error* cmd_log(int, char *[]);
113 static const struct got_error* cmd_diff(int, char *[]);
114 static const struct got_error* cmd_blame(int, char *[]);
115 static const struct got_error* cmd_tree(int, char *[]);
116 static const struct got_error* cmd_status(int, char *[]);
117 static const struct got_error* cmd_ref(int, char *[]);
118 static const struct got_error* cmd_branch(int, char *[]);
119 static const struct got_error* cmd_tag(int, char *[]);
120 static const struct got_error* cmd_add(int, char *[]);
121 static const struct got_error* cmd_remove(int, char *[]);
122 static const struct got_error* cmd_revert(int, char *[]);
123 static const struct got_error* cmd_commit(int, char *[]);
124 static const struct got_error* cmd_cherrypick(int, char *[]);
125 static const struct got_error* cmd_backout(int, char *[]);
126 static const struct got_error* cmd_rebase(int, char *[]);
127 static const struct got_error* cmd_histedit(int, char *[]);
128 static const struct got_error* cmd_integrate(int, char *[]);
129 static const struct got_error* cmd_stage(int, char *[]);
130 static const struct got_error* cmd_unstage(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
133 static struct got_cmd got_commands[] = {
134 { "init", cmd_init, usage_init, "in" },
135 { "import", cmd_import, usage_import, "im" },
136 { "checkout", cmd_checkout, usage_checkout, "co" },
137 { "update", cmd_update, usage_update, "up" },
138 { "log", cmd_log, usage_log, "" },
139 { "diff", cmd_diff, usage_diff, "di" },
140 { "blame", cmd_blame, usage_blame, "bl" },
141 { "tree", cmd_tree, usage_tree, "tr" },
142 { "status", cmd_status, usage_status, "st" },
143 { "ref", cmd_ref, usage_ref, "" },
144 { "branch", cmd_branch, usage_branch, "br" },
145 { "tag", cmd_tag, usage_tag, "" },
146 { "add", cmd_add, usage_add, "" },
147 { "remove", cmd_remove, usage_remove, "rm" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "rebase", cmd_rebase, usage_rebase, "rb" },
153 { "histedit", cmd_histedit, usage_histedit, "he" },
154 { "integrate", cmd_integrate, usage_integrate,"ig" },
155 { "stage", cmd_stage, usage_stage, "sg" },
156 { "unstage", cmd_unstage, usage_unstage, "ug" },
157 { "cat", cmd_cat, usage_cat, "" },
158 };
160 static void
161 list_commands(void)
163 int i;
165 fprintf(stderr, "commands:");
166 for (i = 0; i < nitems(got_commands); i++) {
167 struct got_cmd *cmd = &got_commands[i];
168 fprintf(stderr, " %s", cmd->cmd_name);
170 fputc('\n', stderr);
173 int
174 main(int argc, char *argv[])
176 struct got_cmd *cmd;
177 unsigned int i;
178 int ch;
179 int hflag = 0, Vflag = 0;
180 static struct option longopts[] = {
181 { "version", no_argument, NULL, 'V' },
182 { NULL, 0, NULL, 0}
183 };
185 setlocale(LC_CTYPE, "");
187 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
188 switch (ch) {
189 case 'h':
190 hflag = 1;
191 break;
192 case 'V':
193 Vflag = 1;
194 break;
195 default:
196 usage(hflag);
197 /* NOTREACHED */
201 argc -= optind;
202 argv += optind;
203 optind = 0;
205 if (Vflag) {
206 got_version_print_str();
207 return 1;
210 if (argc <= 0)
211 usage(hflag);
213 signal(SIGINT, catch_sigint);
214 signal(SIGPIPE, catch_sigpipe);
216 for (i = 0; i < nitems(got_commands); i++) {
217 const struct got_error *error;
219 cmd = &got_commands[i];
221 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 strcmp(cmd->cmd_alias, argv[0]) != 0)
223 continue;
225 if (hflag)
226 got_commands[i].cmd_usage();
228 error = got_commands[i].cmd_main(argc, argv);
229 if (error && error->code != GOT_ERR_CANCELLED &&
230 error->code != GOT_ERR_PRIVSEP_EXIT &&
231 !(sigpipe_received &&
232 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
233 !(sigint_received &&
234 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
236 return 1;
239 return 0;
242 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 list_commands();
244 return 1;
247 __dead static void
248 usage(int hflag)
250 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
251 getprogname());
252 if (hflag)
253 list_commands();
254 exit(1);
257 static const struct got_error *
258 get_editor(char **abspath)
260 const struct got_error *err = NULL;
261 const char *editor;
263 *abspath = NULL;
265 editor = getenv("VISUAL");
266 if (editor == NULL)
267 editor = getenv("EDITOR");
269 if (editor) {
270 err = got_path_find_prog(abspath, editor);
271 if (err)
272 return err;
275 if (*abspath == NULL) {
276 *abspath = strdup("/bin/ed");
277 if (*abspath == NULL)
278 return got_error_from_errno("strdup");
281 return NULL;
284 static const struct got_error *
285 apply_unveil(const char *repo_path, int repo_read_only,
286 const char *worktree_path)
288 const struct got_error *err;
290 #ifdef PROFILE
291 if (unveil("gmon.out", "rwc") != 0)
292 return got_error_from_errno2("unveil", "gmon.out");
293 #endif
294 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 return got_error_from_errno2("unveil", repo_path);
297 if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 return got_error_from_errno2("unveil", worktree_path);
300 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
301 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
303 err = got_privsep_unveil_exec_helpers();
304 if (err != NULL)
305 return err;
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
310 return NULL;
313 __dead static void
314 usage_init(void)
316 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
317 exit(1);
320 static const struct got_error *
321 cmd_init(int argc, char *argv[])
323 const struct got_error *error = NULL;
324 char *repo_path = NULL;
325 int ch;
327 while ((ch = getopt(argc, argv, "")) != -1) {
328 switch (ch) {
329 default:
330 usage_init();
331 /* NOTREACHED */
335 argc -= optind;
336 argv += optind;
338 #ifndef PROFILE
339 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 err(1, "pledge");
341 #endif
342 if (argc != 1)
343 usage_init();
345 repo_path = strdup(argv[0]);
346 if (repo_path == NULL)
347 return got_error_from_errno("strdup");
349 got_path_strip_trailing_slashes(repo_path);
351 error = got_path_mkdir(repo_path);
352 if (error &&
353 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
354 goto done;
356 error = apply_unveil(repo_path, 0, NULL);
357 if (error)
358 goto done;
360 error = got_repo_init(repo_path);
361 done:
362 free(repo_path);
363 return error;
366 __dead static void
367 usage_import(void)
369 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 "[-r repository-path] [-I pattern] path\n", getprogname());
371 exit(1);
374 int
375 spawn_editor(const char *editor, const char *file)
377 pid_t pid;
378 sig_t sighup, sigint, sigquit;
379 int st = -1;
381 sighup = signal(SIGHUP, SIG_IGN);
382 sigint = signal(SIGINT, SIG_IGN);
383 sigquit = signal(SIGQUIT, SIG_IGN);
385 switch (pid = fork()) {
386 case -1:
387 goto doneediting;
388 case 0:
389 execl(editor, editor, file, (char *)NULL);
390 _exit(127);
393 while (waitpid(pid, &st, 0) == -1)
394 if (errno != EINTR)
395 break;
397 doneediting:
398 (void)signal(SIGHUP, sighup);
399 (void)signal(SIGINT, sigint);
400 (void)signal(SIGQUIT, sigquit);
402 if (!WIFEXITED(st)) {
403 errno = EINTR;
404 return -1;
407 return WEXITSTATUS(st);
410 static const struct got_error *
411 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 const char *initial_content)
414 const struct got_error *err = NULL;
415 char buf[1024];
416 struct stat st, st2;
417 FILE *fp;
418 int content_changed = 0;
419 size_t len;
421 *logmsg = NULL;
423 if (stat(logmsg_path, &st) == -1)
424 return got_error_from_errno2("stat", logmsg_path);
426 if (spawn_editor(editor, logmsg_path) == -1)
427 return got_error_from_errno("failed spawning editor");
429 if (stat(logmsg_path, &st2) == -1)
430 return got_error_from_errno("stat");
432 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 "no changes made to commit message, aborting");
436 *logmsg = malloc(st2.st_size + 1);
437 if (*logmsg == NULL)
438 return got_error_from_errno("malloc");
439 (*logmsg)[0] = '\0';
440 len = 0;
442 fp = fopen(logmsg_path, "r");
443 if (fp == NULL) {
444 err = got_error_from_errno("fopen");
445 goto done;
447 while (fgets(buf, sizeof(buf), fp) != NULL) {
448 if (!content_changed && strcmp(buf, initial_content) != 0)
449 content_changed = 1;
450 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 continue; /* remove comments and leading empty lines */
452 len = strlcat(*logmsg, buf, st2.st_size);
454 fclose(fp);
456 while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 (*logmsg)[len - 1] = '\0';
458 len--;
461 if (len == 0 || !content_changed)
462 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 "commit message cannot be empty, aborting");
464 done:
465 if (err) {
466 free(*logmsg);
467 *logmsg = NULL;
469 return err;
472 static const struct got_error *
473 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 const char *path_dir, const char *branch_name)
476 char *initial_content = NULL;
477 const struct got_error *err = NULL;
478 int fd;
480 if (asprintf(&initial_content,
481 "\n# %s to be imported to branch %s\n", path_dir,
482 branch_name) == -1)
483 return got_error_from_errno("asprintf");
485 err = got_opentemp_named_fd(logmsg_path, &fd,
486 GOT_TMPDIR_STR "/got-importmsg");
487 if (err)
488 goto done;
490 dprintf(fd, initial_content);
491 close(fd);
493 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
494 done:
495 free(initial_content);
496 return err;
499 static const struct got_error *
500 import_progress(void *arg, const char *path)
502 printf("A %s\n", path);
503 return NULL;
506 static const struct got_error *
507 get_author(char **author, struct got_repository *repo)
509 const struct got_error *err = NULL;
510 const char *got_author, *name, *email;
512 *author = NULL;
514 name = got_repo_get_gitconfig_author_name(repo);
515 email = got_repo_get_gitconfig_author_email(repo);
516 if (name && email) {
517 if (asprintf(author, "%s <%s>", name, email) == -1)
518 return got_error_from_errno("asprintf");
519 return NULL;
522 got_author = getenv("GOT_AUTHOR");
523 if (got_author == NULL) {
524 name = got_repo_get_global_gitconfig_author_name(repo);
525 email = got_repo_get_global_gitconfig_author_email(repo);
526 if (name && email) {
527 if (asprintf(author, "%s <%s>", name, email) == -1)
528 return got_error_from_errno("asprintf");
529 return NULL;
531 /* TODO: Look up user in password database? */
532 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
535 *author = strdup(got_author);
536 if (*author == NULL)
537 return got_error_from_errno("strdup");
539 /*
540 * Really dumb email address check; we're only doing this to
541 * avoid git's object parser breaking on commits we create.
542 */
543 while (*got_author && *got_author != '<')
544 got_author++;
545 if (*got_author != '<') {
546 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
547 goto done;
549 while (*got_author && *got_author != '@')
550 got_author++;
551 if (*got_author != '@') {
552 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 goto done;
555 while (*got_author && *got_author != '>')
556 got_author++;
557 if (*got_author != '>')
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 done:
560 if (err) {
561 free(*author);
562 *author = NULL;
564 return err;
567 static const struct got_error *
568 get_gitconfig_path(char **gitconfig_path)
570 const char *homedir = getenv("HOME");
572 *gitconfig_path = NULL;
573 if (homedir) {
574 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
575 return got_error_from_errno("asprintf");
578 return NULL;
581 static const struct got_error *
582 cmd_import(int argc, char *argv[])
584 const struct got_error *error = NULL;
585 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
586 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
587 const char *branch_name = "main";
588 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
589 struct got_repository *repo = NULL;
590 struct got_reference *branch_ref = NULL, *head_ref = NULL;
591 struct got_object_id *new_commit_id = NULL;
592 int ch;
593 struct got_pathlist_head ignores;
594 struct got_pathlist_entry *pe;
595 int preserve_logmsg = 0;
597 TAILQ_INIT(&ignores);
599 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
600 switch (ch) {
601 case 'b':
602 branch_name = optarg;
603 break;
604 case 'm':
605 logmsg = strdup(optarg);
606 if (logmsg == NULL) {
607 error = got_error_from_errno("strdup");
608 goto done;
610 break;
611 case 'r':
612 repo_path = realpath(optarg, NULL);
613 if (repo_path == NULL) {
614 error = got_error_from_errno2("realpath",
615 optarg);
616 goto done;
618 break;
619 case 'I':
620 if (optarg[0] == '\0')
621 break;
622 error = got_pathlist_insert(&pe, &ignores, optarg,
623 NULL);
624 if (error)
625 goto done;
626 break;
627 default:
628 usage_import();
629 /* NOTREACHED */
633 argc -= optind;
634 argv += optind;
636 #ifndef PROFILE
637 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
638 "unveil",
639 NULL) == -1)
640 err(1, "pledge");
641 #endif
642 if (argc != 1)
643 usage_import();
645 if (repo_path == NULL) {
646 repo_path = getcwd(NULL, 0);
647 if (repo_path == NULL)
648 return got_error_from_errno("getcwd");
650 got_path_strip_trailing_slashes(repo_path);
651 error = get_gitconfig_path(&gitconfig_path);
652 if (error)
653 goto done;
654 error = got_repo_open(&repo, repo_path, gitconfig_path);
655 if (error)
656 goto done;
658 error = get_author(&author, repo);
659 if (error)
660 return error;
662 /*
663 * Don't let the user create a branch name with a leading '-'.
664 * While technically a valid reference name, this case is usually
665 * an unintended typo.
666 */
667 if (branch_name[0] == '-')
668 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
670 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
671 error = got_error_from_errno("asprintf");
672 goto done;
675 error = got_ref_open(&branch_ref, repo, refname, 0);
676 if (error) {
677 if (error->code != GOT_ERR_NOT_REF)
678 goto done;
679 } else {
680 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
681 "import target branch already exists");
682 goto done;
685 path_dir = realpath(argv[0], NULL);
686 if (path_dir == NULL) {
687 error = got_error_from_errno2("realpath", argv[0]);
688 goto done;
690 got_path_strip_trailing_slashes(path_dir);
692 /*
693 * unveil(2) traverses exec(2); if an editor is used we have
694 * to apply unveil after the log message has been written.
695 */
696 if (logmsg == NULL || strlen(logmsg) == 0) {
697 error = get_editor(&editor);
698 if (error)
699 goto done;
700 free(logmsg);
701 error = collect_import_msg(&logmsg, &logmsg_path, editor,
702 path_dir, refname);
703 if (error) {
704 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
705 logmsg_path != NULL)
706 preserve_logmsg = 1;
707 goto done;
711 if (unveil(path_dir, "r") != 0) {
712 error = got_error_from_errno2("unveil", path_dir);
713 if (logmsg_path)
714 preserve_logmsg = 1;
715 goto done;
718 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
719 if (error) {
720 if (logmsg_path)
721 preserve_logmsg = 1;
722 goto done;
725 error = got_repo_import(&new_commit_id, path_dir, logmsg,
726 author, &ignores, repo, import_progress, NULL);
727 if (error) {
728 if (logmsg_path)
729 preserve_logmsg = 1;
730 goto done;
733 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
734 if (error) {
735 if (logmsg_path)
736 preserve_logmsg = 1;
737 goto done;
740 error = got_ref_write(branch_ref, repo);
741 if (error) {
742 if (logmsg_path)
743 preserve_logmsg = 1;
744 goto done;
747 error = got_object_id_str(&id_str, new_commit_id);
748 if (error) {
749 if (logmsg_path)
750 preserve_logmsg = 1;
751 goto done;
754 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
755 if (error) {
756 if (error->code != GOT_ERR_NOT_REF) {
757 if (logmsg_path)
758 preserve_logmsg = 1;
759 goto done;
762 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
763 branch_ref);
764 if (error) {
765 if (logmsg_path)
766 preserve_logmsg = 1;
767 goto done;
770 error = got_ref_write(head_ref, repo);
771 if (error) {
772 if (logmsg_path)
773 preserve_logmsg = 1;
774 goto done;
778 printf("Created branch %s with commit %s\n",
779 got_ref_get_name(branch_ref), id_str);
780 done:
781 if (preserve_logmsg) {
782 fprintf(stderr, "%s: log message preserved in %s\n",
783 getprogname(), logmsg_path);
784 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
785 error = got_error_from_errno2("unlink", logmsg_path);
786 free(logmsg);
787 free(logmsg_path);
788 free(repo_path);
789 free(editor);
790 free(refname);
791 free(new_commit_id);
792 free(id_str);
793 free(author);
794 free(gitconfig_path);
795 if (branch_ref)
796 got_ref_close(branch_ref);
797 if (head_ref)
798 got_ref_close(head_ref);
799 return error;
802 __dead static void
803 usage_checkout(void)
805 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
806 "[-p prefix] repository-path [worktree-path]\n", getprogname());
807 exit(1);
810 static void
811 show_worktree_base_ref_warning(void)
813 fprintf(stderr, "%s: warning: could not create a reference "
814 "to the work tree's base commit; the commit could be "
815 "garbage-collected by Git; making the repository "
816 "writable and running 'got update' will prevent this\n",
817 getprogname());
820 struct got_checkout_progress_arg {
821 const char *worktree_path;
822 int had_base_commit_ref_error;
823 };
825 static const struct got_error *
826 checkout_progress(void *arg, unsigned char status, const char *path)
828 struct got_checkout_progress_arg *a = arg;
830 /* Base commit bump happens silently. */
831 if (status == GOT_STATUS_BUMP_BASE)
832 return NULL;
834 if (status == GOT_STATUS_BASE_REF_ERR) {
835 a->had_base_commit_ref_error = 1;
836 return NULL;
839 while (path[0] == '/')
840 path++;
842 printf("%c %s/%s\n", status, a->worktree_path, path);
843 return NULL;
846 static const struct got_error *
847 check_cancelled(void *arg)
849 if (sigint_received || sigpipe_received)
850 return got_error(GOT_ERR_CANCELLED);
851 return NULL;
854 static const struct got_error *
855 check_linear_ancestry(struct got_object_id *commit_id,
856 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
857 struct got_repository *repo)
859 const struct got_error *err = NULL;
860 struct got_object_id *yca_id;
862 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
863 commit_id, base_commit_id, repo, check_cancelled, NULL);
864 if (err)
865 return err;
867 if (yca_id == NULL)
868 return got_error(GOT_ERR_ANCESTRY);
870 /*
871 * Require a straight line of history between the target commit
872 * and the work tree's base commit.
874 * Non-linear situations such as this require a rebase:
876 * (commit) D F (base_commit)
877 * \ /
878 * C E
879 * \ /
880 * B (yca)
881 * |
882 * A
884 * 'got update' only handles linear cases:
885 * Update forwards in time: A (base/yca) - B - C - D (commit)
886 * Update backwards in time: D (base) - C - B - A (commit/yca)
887 */
888 if (allow_forwards_in_time_only) {
889 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
890 return got_error(GOT_ERR_ANCESTRY);
891 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
892 got_object_id_cmp(base_commit_id, yca_id) != 0)
893 return got_error(GOT_ERR_ANCESTRY);
895 free(yca_id);
896 return NULL;
899 static const struct got_error *
900 check_same_branch(struct got_object_id *commit_id,
901 struct got_reference *head_ref, struct got_object_id *yca_id,
902 struct got_repository *repo)
904 const struct got_error *err = NULL;
905 struct got_commit_graph *graph = NULL;
906 struct got_object_id *head_commit_id = NULL;
907 int is_same_branch = 0;
909 err = got_ref_resolve(&head_commit_id, repo, head_ref);
910 if (err)
911 goto done;
913 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
914 is_same_branch = 1;
915 goto done;
917 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
918 is_same_branch = 1;
919 goto done;
922 err = got_commit_graph_open(&graph, "/", 1);
923 if (err)
924 goto done;
926 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
927 check_cancelled, NULL);
928 if (err)
929 goto done;
931 for (;;) {
932 struct got_object_id *id;
933 err = got_commit_graph_iter_next(&id, graph, repo,
934 check_cancelled, NULL);
935 if (err) {
936 if (err->code == GOT_ERR_ITER_COMPLETED)
937 err = NULL;
938 break;
941 if (id) {
942 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
943 break;
944 if (got_object_id_cmp(id, commit_id) == 0) {
945 is_same_branch = 1;
946 break;
950 done:
951 if (graph)
952 got_commit_graph_close(graph);
953 free(head_commit_id);
954 if (!err && !is_same_branch)
955 err = got_error(GOT_ERR_ANCESTRY);
956 return err;
959 static const struct got_error *
960 cmd_checkout(int argc, char *argv[])
962 const struct got_error *error = NULL;
963 struct got_repository *repo = NULL;
964 struct got_reference *head_ref = NULL;
965 struct got_worktree *worktree = NULL;
966 char *repo_path = NULL;
967 char *worktree_path = NULL;
968 const char *path_prefix = "";
969 const char *branch_name = GOT_REF_HEAD;
970 char *commit_id_str = NULL;
971 int ch, same_path_prefix, allow_nonempty = 0;
972 struct got_pathlist_head paths;
973 struct got_checkout_progress_arg cpa;
975 TAILQ_INIT(&paths);
977 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
978 switch (ch) {
979 case 'b':
980 branch_name = optarg;
981 break;
982 case 'c':
983 commit_id_str = strdup(optarg);
984 if (commit_id_str == NULL)
985 return got_error_from_errno("strdup");
986 break;
987 case 'E':
988 allow_nonempty = 1;
989 break;
990 case 'p':
991 path_prefix = optarg;
992 break;
993 default:
994 usage_checkout();
995 /* NOTREACHED */
999 argc -= optind;
1000 argv += optind;
1002 #ifndef PROFILE
1003 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1004 "unveil", NULL) == -1)
1005 err(1, "pledge");
1006 #endif
1007 if (argc == 1) {
1008 char *cwd, *base, *dotgit;
1009 repo_path = realpath(argv[0], NULL);
1010 if (repo_path == NULL)
1011 return got_error_from_errno2("realpath", argv[0]);
1012 cwd = getcwd(NULL, 0);
1013 if (cwd == NULL) {
1014 error = got_error_from_errno("getcwd");
1015 goto done;
1017 if (path_prefix[0]) {
1018 base = basename(path_prefix);
1019 if (base == NULL) {
1020 error = got_error_from_errno2("basename",
1021 path_prefix);
1022 goto done;
1024 } else {
1025 base = basename(repo_path);
1026 if (base == NULL) {
1027 error = got_error_from_errno2("basename",
1028 repo_path);
1029 goto done;
1032 dotgit = strstr(base, ".git");
1033 if (dotgit)
1034 *dotgit = '\0';
1035 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1036 error = got_error_from_errno("asprintf");
1037 free(cwd);
1038 goto done;
1040 free(cwd);
1041 } else if (argc == 2) {
1042 repo_path = realpath(argv[0], NULL);
1043 if (repo_path == NULL) {
1044 error = got_error_from_errno2("realpath", argv[0]);
1045 goto done;
1047 worktree_path = realpath(argv[1], NULL);
1048 if (worktree_path == NULL) {
1049 if (errno != ENOENT) {
1050 error = got_error_from_errno2("realpath",
1051 argv[1]);
1052 goto done;
1054 worktree_path = strdup(argv[1]);
1055 if (worktree_path == NULL) {
1056 error = got_error_from_errno("strdup");
1057 goto done;
1060 } else
1061 usage_checkout();
1063 got_path_strip_trailing_slashes(repo_path);
1064 got_path_strip_trailing_slashes(worktree_path);
1066 error = got_repo_open(&repo, repo_path, NULL);
1067 if (error != NULL)
1068 goto done;
1070 /* Pre-create work tree path for unveil(2) */
1071 error = got_path_mkdir(worktree_path);
1072 if (error) {
1073 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1074 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1075 goto done;
1076 if (!allow_nonempty &&
1077 !got_path_dir_is_empty(worktree_path)) {
1078 error = got_error_path(worktree_path,
1079 GOT_ERR_DIR_NOT_EMPTY);
1080 goto done;
1084 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1085 if (error)
1086 goto done;
1088 error = got_ref_open(&head_ref, repo, branch_name, 0);
1089 if (error != NULL)
1090 goto done;
1092 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1093 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1094 goto done;
1096 error = got_worktree_open(&worktree, worktree_path);
1097 if (error != NULL)
1098 goto done;
1100 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1101 path_prefix);
1102 if (error != NULL)
1103 goto done;
1104 if (!same_path_prefix) {
1105 error = got_error(GOT_ERR_PATH_PREFIX);
1106 goto done;
1109 if (commit_id_str) {
1110 struct got_object_id *commit_id;
1111 error = got_repo_match_object_id(&commit_id, NULL,
1112 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1113 if (error)
1114 goto done;
1115 error = check_linear_ancestry(commit_id,
1116 got_worktree_get_base_commit_id(worktree), 0, repo);
1117 if (error != NULL) {
1118 free(commit_id);
1119 goto done;
1121 error = check_same_branch(commit_id, head_ref, NULL, repo);
1122 if (error)
1123 goto done;
1124 error = got_worktree_set_base_commit_id(worktree, repo,
1125 commit_id);
1126 free(commit_id);
1127 if (error)
1128 goto done;
1131 error = got_pathlist_append(&paths, "", NULL);
1132 if (error)
1133 goto done;
1134 cpa.worktree_path = worktree_path;
1135 cpa.had_base_commit_ref_error = 0;
1136 error = got_worktree_checkout_files(worktree, &paths, repo,
1137 checkout_progress, &cpa, check_cancelled, NULL);
1138 if (error != NULL)
1139 goto done;
1141 printf("Now shut up and hack\n");
1142 if (cpa.had_base_commit_ref_error)
1143 show_worktree_base_ref_warning();
1144 done:
1145 got_pathlist_free(&paths);
1146 free(commit_id_str);
1147 free(repo_path);
1148 free(worktree_path);
1149 return error;
1152 __dead static void
1153 usage_update(void)
1155 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1156 getprogname());
1157 exit(1);
1160 static const struct got_error *
1161 update_progress(void *arg, unsigned char status, const char *path)
1163 int *did_something = arg;
1165 if (status == GOT_STATUS_EXISTS ||
1166 status == GOT_STATUS_BASE_REF_ERR)
1167 return NULL;
1169 *did_something = 1;
1171 /* Base commit bump happens silently. */
1172 if (status == GOT_STATUS_BUMP_BASE)
1173 return NULL;
1175 while (path[0] == '/')
1176 path++;
1177 printf("%c %s\n", status, path);
1178 return NULL;
1181 static const struct got_error *
1182 switch_head_ref(struct got_reference *head_ref,
1183 struct got_object_id *commit_id, struct got_worktree *worktree,
1184 struct got_repository *repo)
1186 const struct got_error *err = NULL;
1187 char *base_id_str;
1188 int ref_has_moved = 0;
1190 /* Trivial case: switching between two different references. */
1191 if (strcmp(got_ref_get_name(head_ref),
1192 got_worktree_get_head_ref_name(worktree)) != 0) {
1193 printf("Switching work tree from %s to %s\n",
1194 got_worktree_get_head_ref_name(worktree),
1195 got_ref_get_name(head_ref));
1196 return got_worktree_set_head_ref(worktree, head_ref);
1199 err = check_linear_ancestry(commit_id,
1200 got_worktree_get_base_commit_id(worktree), 0, repo);
1201 if (err) {
1202 if (err->code != GOT_ERR_ANCESTRY)
1203 return err;
1204 ref_has_moved = 1;
1206 if (!ref_has_moved)
1207 return NULL;
1209 /* Switching to a rebased branch with the same reference name. */
1210 err = got_object_id_str(&base_id_str,
1211 got_worktree_get_base_commit_id(worktree));
1212 if (err)
1213 return err;
1214 printf("Reference %s now points at a different branch\n",
1215 got_worktree_get_head_ref_name(worktree));
1216 printf("Switching work tree from %s to %s\n", base_id_str,
1217 got_worktree_get_head_ref_name(worktree));
1218 return NULL;
1221 static const struct got_error *
1222 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1224 const struct got_error *err;
1225 int in_progress;
1227 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1228 if (err)
1229 return err;
1230 if (in_progress)
1231 return got_error(GOT_ERR_REBASING);
1233 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1234 if (err)
1235 return err;
1236 if (in_progress)
1237 return got_error(GOT_ERR_HISTEDIT_BUSY);
1239 return NULL;
1242 static const struct got_error *
1243 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1244 char *argv[], struct got_worktree *worktree)
1246 const struct got_error *err = NULL;
1247 char *path;
1248 int i;
1250 if (argc == 0) {
1251 path = strdup("");
1252 if (path == NULL)
1253 return got_error_from_errno("strdup");
1254 return got_pathlist_append(paths, path, NULL);
1257 for (i = 0; i < argc; i++) {
1258 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1259 if (err)
1260 break;
1261 err = got_pathlist_append(paths, path, NULL);
1262 if (err) {
1263 free(path);
1264 break;
1268 return err;
1271 static const struct got_error *
1272 cmd_update(int argc, char *argv[])
1274 const struct got_error *error = NULL;
1275 struct got_repository *repo = NULL;
1276 struct got_worktree *worktree = NULL;
1277 char *worktree_path = NULL;
1278 struct got_object_id *commit_id = NULL;
1279 char *commit_id_str = NULL;
1280 const char *branch_name = NULL;
1281 struct got_reference *head_ref = NULL;
1282 struct got_pathlist_head paths;
1283 struct got_pathlist_entry *pe;
1284 int ch, did_something = 0;
1286 TAILQ_INIT(&paths);
1288 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1289 switch (ch) {
1290 case 'b':
1291 branch_name = optarg;
1292 break;
1293 case 'c':
1294 commit_id_str = strdup(optarg);
1295 if (commit_id_str == NULL)
1296 return got_error_from_errno("strdup");
1297 break;
1298 default:
1299 usage_update();
1300 /* NOTREACHED */
1304 argc -= optind;
1305 argv += optind;
1307 #ifndef PROFILE
1308 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1309 "unveil", NULL) == -1)
1310 err(1, "pledge");
1311 #endif
1312 worktree_path = getcwd(NULL, 0);
1313 if (worktree_path == NULL) {
1314 error = got_error_from_errno("getcwd");
1315 goto done;
1317 error = got_worktree_open(&worktree, worktree_path);
1318 if (error)
1319 goto done;
1321 error = check_rebase_or_histedit_in_progress(worktree);
1322 if (error)
1323 goto done;
1325 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1326 NULL);
1327 if (error != NULL)
1328 goto done;
1330 error = apply_unveil(got_repo_get_path(repo), 0,
1331 got_worktree_get_root_path(worktree));
1332 if (error)
1333 goto done;
1335 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1336 if (error)
1337 goto done;
1339 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1340 got_worktree_get_head_ref_name(worktree), 0);
1341 if (error != NULL)
1342 goto done;
1343 if (commit_id_str == NULL) {
1344 error = got_ref_resolve(&commit_id, repo, head_ref);
1345 if (error != NULL)
1346 goto done;
1347 error = got_object_id_str(&commit_id_str, commit_id);
1348 if (error != NULL)
1349 goto done;
1350 } else {
1351 error = got_repo_match_object_id(&commit_id, NULL,
1352 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1353 free(commit_id_str);
1354 commit_id_str = NULL;
1355 if (error)
1356 goto done;
1357 error = got_object_id_str(&commit_id_str, commit_id);
1358 if (error)
1359 goto done;
1362 if (branch_name) {
1363 struct got_object_id *head_commit_id;
1364 TAILQ_FOREACH(pe, &paths, entry) {
1365 if (pe->path_len == 0)
1366 continue;
1367 error = got_error_msg(GOT_ERR_BAD_PATH,
1368 "switching between branches requires that "
1369 "the entire work tree gets updated");
1370 goto done;
1372 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1373 if (error)
1374 goto done;
1375 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1376 repo);
1377 free(head_commit_id);
1378 if (error != NULL)
1379 goto done;
1380 error = check_same_branch(commit_id, head_ref, NULL, repo);
1381 if (error)
1382 goto done;
1383 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1384 if (error)
1385 goto done;
1386 } else {
1387 error = check_linear_ancestry(commit_id,
1388 got_worktree_get_base_commit_id(worktree), 0, repo);
1389 if (error != NULL) {
1390 if (error->code == GOT_ERR_ANCESTRY)
1391 error = got_error(GOT_ERR_BRANCH_MOVED);
1392 goto done;
1394 error = check_same_branch(commit_id, head_ref, NULL, repo);
1395 if (error)
1396 goto done;
1399 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1400 commit_id) != 0) {
1401 error = got_worktree_set_base_commit_id(worktree, repo,
1402 commit_id);
1403 if (error)
1404 goto done;
1407 error = got_worktree_checkout_files(worktree, &paths, repo,
1408 update_progress, &did_something, check_cancelled, NULL);
1409 if (error != NULL)
1410 goto done;
1412 if (did_something)
1413 printf("Updated to commit %s\n", commit_id_str);
1414 else
1415 printf("Already up-to-date\n");
1416 done:
1417 free(worktree_path);
1418 TAILQ_FOREACH(pe, &paths, entry)
1419 free((char *)pe->path);
1420 got_pathlist_free(&paths);
1421 free(commit_id);
1422 free(commit_id_str);
1423 return error;
1426 static const struct got_error *
1427 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1428 const char *path, int diff_context, int ignore_whitespace,
1429 struct got_repository *repo)
1431 const struct got_error *err = NULL;
1432 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1434 if (blob_id1) {
1435 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1436 if (err)
1437 goto done;
1440 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1441 if (err)
1442 goto done;
1444 while (path[0] == '/')
1445 path++;
1446 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1447 ignore_whitespace, stdout);
1448 done:
1449 if (blob1)
1450 got_object_blob_close(blob1);
1451 got_object_blob_close(blob2);
1452 return err;
1455 static const struct got_error *
1456 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1457 const char *path, int diff_context, int ignore_whitespace,
1458 struct got_repository *repo)
1460 const struct got_error *err = NULL;
1461 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1462 struct got_diff_blob_output_unidiff_arg arg;
1464 if (tree_id1) {
1465 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1466 if (err)
1467 goto done;
1470 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1471 if (err)
1472 goto done;
1474 arg.diff_context = diff_context;
1475 arg.ignore_whitespace = ignore_whitespace;
1476 arg.outfile = stdout;
1477 while (path[0] == '/')
1478 path++;
1479 err = got_diff_tree(tree1, tree2, path, path, repo,
1480 got_diff_blob_output_unidiff, &arg, 1);
1481 done:
1482 if (tree1)
1483 got_object_tree_close(tree1);
1484 if (tree2)
1485 got_object_tree_close(tree2);
1486 return err;
1489 static const struct got_error *
1490 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1491 const char *path, int diff_context, struct got_repository *repo)
1493 const struct got_error *err = NULL;
1494 struct got_commit_object *pcommit = NULL;
1495 char *id_str1 = NULL, *id_str2 = NULL;
1496 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1497 struct got_object_qid *qid;
1499 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1500 if (qid != NULL) {
1501 err = got_object_open_as_commit(&pcommit, repo,
1502 qid->id);
1503 if (err)
1504 return err;
1507 if (path && path[0] != '\0') {
1508 int obj_type;
1509 err = got_object_id_by_path(&obj_id2, repo, id, path);
1510 if (err)
1511 goto done;
1512 err = got_object_id_str(&id_str2, obj_id2);
1513 if (err) {
1514 free(obj_id2);
1515 goto done;
1517 if (pcommit) {
1518 err = got_object_id_by_path(&obj_id1, repo,
1519 qid->id, path);
1520 if (err) {
1521 free(obj_id2);
1522 goto done;
1524 err = got_object_id_str(&id_str1, obj_id1);
1525 if (err) {
1526 free(obj_id2);
1527 goto done;
1530 err = got_object_get_type(&obj_type, repo, obj_id2);
1531 if (err) {
1532 free(obj_id2);
1533 goto done;
1535 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1536 switch (obj_type) {
1537 case GOT_OBJ_TYPE_BLOB:
1538 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1539 0, repo);
1540 break;
1541 case GOT_OBJ_TYPE_TREE:
1542 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1543 0, repo);
1544 break;
1545 default:
1546 err = got_error(GOT_ERR_OBJ_TYPE);
1547 break;
1549 free(obj_id1);
1550 free(obj_id2);
1551 } else {
1552 obj_id2 = got_object_commit_get_tree_id(commit);
1553 err = got_object_id_str(&id_str2, obj_id2);
1554 if (err)
1555 goto done;
1556 obj_id1 = got_object_commit_get_tree_id(pcommit);
1557 err = got_object_id_str(&id_str1, obj_id1);
1558 if (err)
1559 goto done;
1560 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1561 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1563 done:
1564 free(id_str1);
1565 free(id_str2);
1566 if (pcommit)
1567 got_object_commit_close(pcommit);
1568 return err;
1571 static char *
1572 get_datestr(time_t *time, char *datebuf)
1574 struct tm mytm, *tm;
1575 char *p, *s;
1577 tm = gmtime_r(time, &mytm);
1578 if (tm == NULL)
1579 return NULL;
1580 s = asctime_r(tm, datebuf);
1581 if (s == NULL)
1582 return NULL;
1583 p = strchr(s, '\n');
1584 if (p)
1585 *p = '\0';
1586 return s;
1589 static const struct got_error *
1590 match_logmsg(int *have_match, struct got_object_id *id,
1591 struct got_commit_object *commit, regex_t *regex)
1593 const struct got_error *err = NULL;
1594 regmatch_t regmatch;
1595 char *id_str = NULL, *logmsg = NULL;
1597 *have_match = 0;
1599 err = got_object_id_str(&id_str, id);
1600 if (err)
1601 return err;
1603 err = got_object_commit_get_logmsg(&logmsg, commit);
1604 if (err)
1605 goto done;
1607 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1608 *have_match = 1;
1609 done:
1610 free(id_str);
1611 free(logmsg);
1612 return err;
1615 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1617 static const struct got_error *
1618 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1619 struct got_repository *repo, const char *path, int show_patch,
1620 int diff_context, struct got_reflist_head *refs)
1622 const struct got_error *err = NULL;
1623 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1624 char datebuf[26];
1625 time_t committer_time;
1626 const char *author, *committer;
1627 char *refs_str = NULL;
1628 struct got_reflist_entry *re;
1630 SIMPLEQ_FOREACH(re, refs, entry) {
1631 char *s;
1632 const char *name;
1633 struct got_tag_object *tag = NULL;
1634 int cmp;
1636 name = got_ref_get_name(re->ref);
1637 if (strcmp(name, GOT_REF_HEAD) == 0)
1638 continue;
1639 if (strncmp(name, "refs/", 5) == 0)
1640 name += 5;
1641 if (strncmp(name, "got/", 4) == 0)
1642 continue;
1643 if (strncmp(name, "heads/", 6) == 0)
1644 name += 6;
1645 if (strncmp(name, "remotes/", 8) == 0)
1646 name += 8;
1647 if (strncmp(name, "tags/", 5) == 0) {
1648 err = got_object_open_as_tag(&tag, repo, re->id);
1649 if (err) {
1650 if (err->code != GOT_ERR_OBJ_TYPE)
1651 return err;
1652 /* Ref points at something other than a tag. */
1653 err = NULL;
1654 tag = NULL;
1657 cmp = got_object_id_cmp(tag ?
1658 got_object_tag_get_object_id(tag) : re->id, id);
1659 if (tag)
1660 got_object_tag_close(tag);
1661 if (cmp != 0)
1662 continue;
1663 s = refs_str;
1664 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1665 name) == -1) {
1666 err = got_error_from_errno("asprintf");
1667 free(s);
1668 return err;
1670 free(s);
1672 err = got_object_id_str(&id_str, id);
1673 if (err)
1674 return err;
1676 printf(GOT_COMMIT_SEP_STR);
1677 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1678 refs_str ? refs_str : "", refs_str ? ")" : "");
1679 free(id_str);
1680 id_str = NULL;
1681 free(refs_str);
1682 refs_str = NULL;
1683 printf("from: %s\n", got_object_commit_get_author(commit));
1684 committer_time = got_object_commit_get_committer_time(commit);
1685 datestr = get_datestr(&committer_time, datebuf);
1686 if (datestr)
1687 printf("date: %s UTC\n", datestr);
1688 author = got_object_commit_get_author(commit);
1689 committer = got_object_commit_get_committer(commit);
1690 if (strcmp(author, committer) != 0)
1691 printf("via: %s\n", committer);
1692 if (got_object_commit_get_nparents(commit) > 1) {
1693 const struct got_object_id_queue *parent_ids;
1694 struct got_object_qid *qid;
1695 int n = 1;
1696 parent_ids = got_object_commit_get_parent_ids(commit);
1697 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1698 err = got_object_id_str(&id_str, qid->id);
1699 if (err)
1700 return err;
1701 printf("parent %d: %s\n", n++, id_str);
1702 free(id_str);
1706 err = got_object_commit_get_logmsg(&logmsg0, commit);
1707 if (err)
1708 return err;
1710 logmsg = logmsg0;
1711 do {
1712 line = strsep(&logmsg, "\n");
1713 if (line)
1714 printf(" %s\n", line);
1715 } while (line);
1716 free(logmsg0);
1718 if (show_patch) {
1719 err = print_patch(commit, id, path, diff_context, repo);
1720 if (err == 0)
1721 printf("\n");
1724 if (fflush(stdout) != 0 && err == NULL)
1725 err = got_error_from_errno("fflush");
1726 return err;
1729 static const struct got_error *
1730 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1731 const char *path, int show_patch, const char *search_pattern,
1732 int diff_context, int limit, int log_branches,
1733 struct got_reflist_head *refs)
1735 const struct got_error *err;
1736 struct got_commit_graph *graph;
1737 regex_t regex;
1738 int have_match;
1740 if (search_pattern &&
1741 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1742 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1744 err = got_commit_graph_open(&graph, path, !log_branches);
1745 if (err)
1746 return err;
1747 err = got_commit_graph_iter_start(graph, root_id, repo,
1748 check_cancelled, NULL);
1749 if (err)
1750 goto done;
1751 for (;;) {
1752 struct got_commit_object *commit;
1753 struct got_object_id *id;
1755 if (sigint_received || sigpipe_received)
1756 break;
1758 err = got_commit_graph_iter_next(&id, graph, repo,
1759 check_cancelled, NULL);
1760 if (err) {
1761 if (err->code == GOT_ERR_ITER_COMPLETED)
1762 err = NULL;
1763 break;
1765 if (id == NULL)
1766 break;
1768 err = got_object_open_as_commit(&commit, repo, id);
1769 if (err)
1770 break;
1772 if (search_pattern) {
1773 err = match_logmsg(&have_match, id, commit, &regex);
1774 if (err) {
1775 got_object_commit_close(commit);
1776 break;
1778 if (have_match == 0) {
1779 got_object_commit_close(commit);
1780 continue;
1784 err = print_commit(commit, id, repo, path, show_patch,
1785 diff_context, refs);
1786 got_object_commit_close(commit);
1787 if (err || (limit && --limit == 0))
1788 break;
1790 done:
1791 if (search_pattern)
1792 regfree(&regex);
1793 got_commit_graph_close(graph);
1794 return err;
1797 __dead static void
1798 usage_log(void)
1800 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1801 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1802 exit(1);
1805 static int
1806 get_default_log_limit(void)
1808 const char *got_default_log_limit;
1809 long long n;
1810 const char *errstr;
1812 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1813 if (got_default_log_limit == NULL)
1814 return 0;
1815 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1816 if (errstr != NULL)
1817 return 0;
1818 return n;
1821 static const struct got_error *
1822 cmd_log(int argc, char *argv[])
1824 const struct got_error *error;
1825 struct got_repository *repo = NULL;
1826 struct got_worktree *worktree = NULL;
1827 struct got_commit_object *commit = NULL;
1828 struct got_object_id *id = NULL;
1829 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1830 const char *start_commit = NULL, *search_pattern = NULL;
1831 int diff_context = -1, ch;
1832 int show_patch = 0, limit = 0, log_branches = 0;
1833 const char *errstr;
1834 struct got_reflist_head refs;
1836 SIMPLEQ_INIT(&refs);
1838 #ifndef PROFILE
1839 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1840 NULL)
1841 == -1)
1842 err(1, "pledge");
1843 #endif
1845 limit = get_default_log_limit();
1847 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1848 switch (ch) {
1849 case 'p':
1850 show_patch = 1;
1851 break;
1852 case 'c':
1853 start_commit = optarg;
1854 break;
1855 case 'C':
1856 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1857 &errstr);
1858 if (errstr != NULL)
1859 err(1, "-C option %s", errstr);
1860 break;
1861 case 'l':
1862 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1863 if (errstr != NULL)
1864 err(1, "-l option %s", errstr);
1865 break;
1866 case 'b':
1867 log_branches = 1;
1868 break;
1869 case 'r':
1870 repo_path = realpath(optarg, NULL);
1871 if (repo_path == NULL)
1872 return got_error_from_errno2("realpath",
1873 optarg);
1874 got_path_strip_trailing_slashes(repo_path);
1875 break;
1876 case 's':
1877 search_pattern = optarg;
1878 break;
1879 default:
1880 usage_log();
1881 /* NOTREACHED */
1885 argc -= optind;
1886 argv += optind;
1888 if (diff_context == -1)
1889 diff_context = 3;
1890 else if (!show_patch)
1891 errx(1, "-C reguires -p");
1893 cwd = getcwd(NULL, 0);
1894 if (cwd == NULL) {
1895 error = got_error_from_errno("getcwd");
1896 goto done;
1899 error = got_worktree_open(&worktree, cwd);
1900 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1901 goto done;
1902 error = NULL;
1904 if (argc == 0) {
1905 path = strdup("");
1906 if (path == NULL) {
1907 error = got_error_from_errno("strdup");
1908 goto done;
1910 } else if (argc == 1) {
1911 if (worktree) {
1912 error = got_worktree_resolve_path(&path, worktree,
1913 argv[0]);
1914 if (error)
1915 goto done;
1916 } else {
1917 path = strdup(argv[0]);
1918 if (path == NULL) {
1919 error = got_error_from_errno("strdup");
1920 goto done;
1923 } else
1924 usage_log();
1926 if (repo_path == NULL) {
1927 repo_path = worktree ?
1928 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1930 if (repo_path == NULL) {
1931 error = got_error_from_errno("strdup");
1932 goto done;
1935 error = got_repo_open(&repo, repo_path, NULL);
1936 if (error != NULL)
1937 goto done;
1939 error = apply_unveil(got_repo_get_path(repo), 1,
1940 worktree ? got_worktree_get_root_path(worktree) : NULL);
1941 if (error)
1942 goto done;
1944 if (start_commit == NULL) {
1945 struct got_reference *head_ref;
1946 error = got_ref_open(&head_ref, repo,
1947 worktree ? got_worktree_get_head_ref_name(worktree)
1948 : GOT_REF_HEAD, 0);
1949 if (error != NULL)
1950 return error;
1951 error = got_ref_resolve(&id, repo, head_ref);
1952 got_ref_close(head_ref);
1953 if (error != NULL)
1954 return error;
1955 error = got_object_open_as_commit(&commit, repo, id);
1956 } else {
1957 struct got_reference *ref;
1958 error = got_ref_open(&ref, repo, start_commit, 0);
1959 if (error == NULL) {
1960 int obj_type;
1961 error = got_ref_resolve(&id, repo, ref);
1962 got_ref_close(ref);
1963 if (error != NULL)
1964 goto done;
1965 error = got_object_get_type(&obj_type, repo, id);
1966 if (error != NULL)
1967 goto done;
1968 if (obj_type == GOT_OBJ_TYPE_TAG) {
1969 struct got_tag_object *tag;
1970 error = got_object_open_as_tag(&tag, repo, id);
1971 if (error != NULL)
1972 goto done;
1973 if (got_object_tag_get_object_type(tag) !=
1974 GOT_OBJ_TYPE_COMMIT) {
1975 got_object_tag_close(tag);
1976 error = got_error(GOT_ERR_OBJ_TYPE);
1977 goto done;
1979 free(id);
1980 id = got_object_id_dup(
1981 got_object_tag_get_object_id(tag));
1982 if (id == NULL)
1983 error = got_error_from_errno(
1984 "got_object_id_dup");
1985 got_object_tag_close(tag);
1986 if (error)
1987 goto done;
1988 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1989 error = got_error(GOT_ERR_OBJ_TYPE);
1990 goto done;
1992 error = got_object_open_as_commit(&commit, repo, id);
1993 if (error != NULL)
1994 goto done;
1996 if (commit == NULL) {
1997 error = got_repo_match_object_id_prefix(&id,
1998 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1999 if (error != NULL)
2000 return error;
2003 if (error != NULL)
2004 goto done;
2006 if (worktree) {
2007 const char *prefix = got_worktree_get_path_prefix(worktree);
2008 char *p;
2009 if (asprintf(&p, "%s%s%s", prefix,
2010 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2011 error = got_error_from_errno("asprintf");
2012 goto done;
2014 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2015 free(p);
2016 } else
2017 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2018 if (error != NULL)
2019 goto done;
2020 if (in_repo_path) {
2021 free(path);
2022 path = in_repo_path;
2025 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2026 if (error)
2027 goto done;
2029 error = print_commits(id, repo, path, show_patch, search_pattern,
2030 diff_context, limit, log_branches, &refs);
2031 done:
2032 free(path);
2033 free(repo_path);
2034 free(cwd);
2035 free(id);
2036 if (worktree)
2037 got_worktree_close(worktree);
2038 if (repo) {
2039 const struct got_error *repo_error;
2040 repo_error = got_repo_close(repo);
2041 if (error == NULL)
2042 error = repo_error;
2044 got_ref_list_free(&refs);
2045 return error;
2048 __dead static void
2049 usage_diff(void)
2051 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2052 "[-w] [object1 object2 | path]\n", getprogname());
2053 exit(1);
2056 struct print_diff_arg {
2057 struct got_repository *repo;
2058 struct got_worktree *worktree;
2059 int diff_context;
2060 const char *id_str;
2061 int header_shown;
2062 int diff_staged;
2063 int ignore_whitespace;
2066 static const struct got_error *
2067 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2068 const char *path, struct got_object_id *blob_id,
2069 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2070 int dirfd, const char *de_name)
2072 struct print_diff_arg *a = arg;
2073 const struct got_error *err = NULL;
2074 struct got_blob_object *blob1 = NULL;
2075 int fd = -1;
2076 FILE *f2 = NULL;
2077 char *abspath = NULL, *label1 = NULL;
2078 struct stat sb;
2080 if (a->diff_staged) {
2081 if (staged_status != GOT_STATUS_MODIFY &&
2082 staged_status != GOT_STATUS_ADD &&
2083 staged_status != GOT_STATUS_DELETE)
2084 return NULL;
2085 } else {
2086 if (staged_status == GOT_STATUS_DELETE)
2087 return NULL;
2088 if (status == GOT_STATUS_NONEXISTENT)
2089 return got_error_set_errno(ENOENT, path);
2090 if (status != GOT_STATUS_MODIFY &&
2091 status != GOT_STATUS_ADD &&
2092 status != GOT_STATUS_DELETE &&
2093 status != GOT_STATUS_CONFLICT)
2094 return NULL;
2097 if (!a->header_shown) {
2098 printf("diff %s %s%s\n", a->id_str,
2099 got_worktree_get_root_path(a->worktree),
2100 a->diff_staged ? " (staged changes)" : "");
2101 a->header_shown = 1;
2104 if (a->diff_staged) {
2105 const char *label1 = NULL, *label2 = NULL;
2106 switch (staged_status) {
2107 case GOT_STATUS_MODIFY:
2108 label1 = path;
2109 label2 = path;
2110 break;
2111 case GOT_STATUS_ADD:
2112 label2 = path;
2113 break;
2114 case GOT_STATUS_DELETE:
2115 label1 = path;
2116 break;
2117 default:
2118 return got_error(GOT_ERR_FILE_STATUS);
2120 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2121 label1, label2, a->diff_context, a->ignore_whitespace,
2122 a->repo, stdout);
2125 if (staged_status == GOT_STATUS_ADD ||
2126 staged_status == GOT_STATUS_MODIFY) {
2127 char *id_str;
2128 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2129 8192);
2130 if (err)
2131 goto done;
2132 err = got_object_id_str(&id_str, staged_blob_id);
2133 if (err)
2134 goto done;
2135 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2136 err = got_error_from_errno("asprintf");
2137 free(id_str);
2138 goto done;
2140 free(id_str);
2141 } else if (status != GOT_STATUS_ADD) {
2142 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2143 if (err)
2144 goto done;
2147 if (status != GOT_STATUS_DELETE) {
2148 if (asprintf(&abspath, "%s/%s",
2149 got_worktree_get_root_path(a->worktree), path) == -1) {
2150 err = got_error_from_errno("asprintf");
2151 goto done;
2154 if (dirfd != -1) {
2155 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2156 if (fd == -1) {
2157 err = got_error_from_errno2("openat", abspath);
2158 goto done;
2160 } else {
2161 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2162 if (fd == -1) {
2163 err = got_error_from_errno2("open", abspath);
2164 goto done;
2167 if (fstat(fd, &sb) == -1) {
2168 err = got_error_from_errno2("fstat", abspath);
2169 goto done;
2171 f2 = fdopen(fd, "r");
2172 if (f2 == NULL) {
2173 err = got_error_from_errno2("fdopen", abspath);
2174 goto done;
2176 fd = -1;
2177 } else
2178 sb.st_size = 0;
2180 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2181 a->diff_context, a->ignore_whitespace, stdout);
2182 done:
2183 if (blob1)
2184 got_object_blob_close(blob1);
2185 if (f2 && fclose(f2) == EOF && err == NULL)
2186 err = got_error_from_errno("fclose");
2187 if (fd != -1 && close(fd) == -1 && err == NULL)
2188 err = got_error_from_errno("close");
2189 free(abspath);
2190 return err;
2193 static const struct got_error *
2194 cmd_diff(int argc, char *argv[])
2196 const struct got_error *error;
2197 struct got_repository *repo = NULL;
2198 struct got_worktree *worktree = NULL;
2199 char *cwd = NULL, *repo_path = NULL;
2200 struct got_object_id *id1 = NULL, *id2 = NULL;
2201 const char *id_str1 = NULL, *id_str2 = NULL;
2202 char *label1 = NULL, *label2 = NULL;
2203 int type1, type2;
2204 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2205 const char *errstr;
2206 char *path = NULL;
2208 #ifndef PROFILE
2209 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2210 NULL) == -1)
2211 err(1, "pledge");
2212 #endif
2214 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2215 switch (ch) {
2216 case 'C':
2217 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2218 &errstr);
2219 if (errstr != NULL)
2220 err(1, "-C option %s", errstr);
2221 break;
2222 case 'r':
2223 repo_path = realpath(optarg, NULL);
2224 if (repo_path == NULL)
2225 return got_error_from_errno2("realpath",
2226 optarg);
2227 got_path_strip_trailing_slashes(repo_path);
2228 break;
2229 case 's':
2230 diff_staged = 1;
2231 break;
2232 case 'w':
2233 ignore_whitespace = 1;
2234 break;
2235 default:
2236 usage_diff();
2237 /* NOTREACHED */
2241 argc -= optind;
2242 argv += optind;
2244 cwd = getcwd(NULL, 0);
2245 if (cwd == NULL) {
2246 error = got_error_from_errno("getcwd");
2247 goto done;
2249 error = got_worktree_open(&worktree, cwd);
2250 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2251 goto done;
2252 if (argc <= 1) {
2253 if (worktree == NULL) {
2254 error = got_error(GOT_ERR_NOT_WORKTREE);
2255 goto done;
2257 if (repo_path)
2258 errx(1,
2259 "-r option can't be used when diffing a work tree");
2260 repo_path = strdup(got_worktree_get_repo_path(worktree));
2261 if (repo_path == NULL) {
2262 error = got_error_from_errno("strdup");
2263 goto done;
2265 if (argc == 1) {
2266 error = got_worktree_resolve_path(&path, worktree,
2267 argv[0]);
2268 if (error)
2269 goto done;
2270 } else {
2271 path = strdup("");
2272 if (path == NULL) {
2273 error = got_error_from_errno("strdup");
2274 goto done;
2277 } else if (argc == 2) {
2278 if (diff_staged)
2279 errx(1, "-s option can't be used when diffing "
2280 "objects in repository");
2281 id_str1 = argv[0];
2282 id_str2 = argv[1];
2283 if (worktree && repo_path == NULL) {
2284 repo_path =
2285 strdup(got_worktree_get_repo_path(worktree));
2286 if (repo_path == NULL) {
2287 error = got_error_from_errno("strdup");
2288 goto done;
2291 } else
2292 usage_diff();
2294 if (repo_path == NULL) {
2295 repo_path = getcwd(NULL, 0);
2296 if (repo_path == NULL)
2297 return got_error_from_errno("getcwd");
2300 error = got_repo_open(&repo, repo_path, NULL);
2301 free(repo_path);
2302 if (error != NULL)
2303 goto done;
2305 error = apply_unveil(got_repo_get_path(repo), 1,
2306 worktree ? got_worktree_get_root_path(worktree) : NULL);
2307 if (error)
2308 goto done;
2310 if (argc <= 1) {
2311 struct print_diff_arg arg;
2312 struct got_pathlist_head paths;
2313 char *id_str;
2315 TAILQ_INIT(&paths);
2317 error = got_object_id_str(&id_str,
2318 got_worktree_get_base_commit_id(worktree));
2319 if (error)
2320 goto done;
2321 arg.repo = repo;
2322 arg.worktree = worktree;
2323 arg.diff_context = diff_context;
2324 arg.id_str = id_str;
2325 arg.header_shown = 0;
2326 arg.diff_staged = diff_staged;
2327 arg.ignore_whitespace = ignore_whitespace;
2329 error = got_pathlist_append(&paths, path, NULL);
2330 if (error)
2331 goto done;
2333 error = got_worktree_status(worktree, &paths, repo, print_diff,
2334 &arg, check_cancelled, NULL);
2335 free(id_str);
2336 got_pathlist_free(&paths);
2337 goto done;
2340 error = got_repo_match_object_id(&id1, &label1, id_str1,
2341 GOT_OBJ_TYPE_ANY, 1, repo);
2342 if (error)
2343 goto done;
2345 error = got_repo_match_object_id(&id2, &label2, id_str2,
2346 GOT_OBJ_TYPE_ANY, 1, repo);
2347 if (error)
2348 goto done;
2350 error = got_object_get_type(&type1, repo, id1);
2351 if (error)
2352 goto done;
2354 error = got_object_get_type(&type2, repo, id2);
2355 if (error)
2356 goto done;
2358 if (type1 != type2) {
2359 error = got_error(GOT_ERR_OBJ_TYPE);
2360 goto done;
2363 switch (type1) {
2364 case GOT_OBJ_TYPE_BLOB:
2365 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2366 diff_context, ignore_whitespace, repo, stdout);
2367 break;
2368 case GOT_OBJ_TYPE_TREE:
2369 error = got_diff_objects_as_trees(id1, id2, "", "",
2370 diff_context, ignore_whitespace, repo, stdout);
2371 break;
2372 case GOT_OBJ_TYPE_COMMIT:
2373 printf("diff %s %s\n", label1, label2);
2374 error = got_diff_objects_as_commits(id1, id2, diff_context,
2375 ignore_whitespace, repo, stdout);
2376 break;
2377 default:
2378 error = got_error(GOT_ERR_OBJ_TYPE);
2380 done:
2381 free(label1);
2382 free(label2);
2383 free(id1);
2384 free(id2);
2385 free(path);
2386 if (worktree)
2387 got_worktree_close(worktree);
2388 if (repo) {
2389 const struct got_error *repo_error;
2390 repo_error = got_repo_close(repo);
2391 if (error == NULL)
2392 error = repo_error;
2394 return error;
2397 __dead static void
2398 usage_blame(void)
2400 fprintf(stderr,
2401 "usage: %s blame [-c commit] [-r repository-path] path\n",
2402 getprogname());
2403 exit(1);
2406 struct blame_line {
2407 int annotated;
2408 char *id_str;
2409 char *committer;
2410 char datebuf[11]; /* YYYY-MM-DD + NUL */
2413 struct blame_cb_args {
2414 struct blame_line *lines;
2415 int nlines;
2416 int nlines_prec;
2417 int lineno_cur;
2418 off_t *line_offsets;
2419 FILE *f;
2420 struct got_repository *repo;
2423 static const struct got_error *
2424 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2426 const struct got_error *err = NULL;
2427 struct blame_cb_args *a = arg;
2428 struct blame_line *bline;
2429 char *line = NULL;
2430 size_t linesize = 0;
2431 struct got_commit_object *commit = NULL;
2432 off_t offset;
2433 struct tm tm;
2434 time_t committer_time;
2436 if (nlines != a->nlines ||
2437 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2438 return got_error(GOT_ERR_RANGE);
2440 if (sigint_received)
2441 return got_error(GOT_ERR_ITER_COMPLETED);
2443 if (lineno == -1)
2444 return NULL; /* no change in this commit */
2446 /* Annotate this line. */
2447 bline = &a->lines[lineno - 1];
2448 if (bline->annotated)
2449 return NULL;
2450 err = got_object_id_str(&bline->id_str, id);
2451 if (err)
2452 return err;
2454 err = got_object_open_as_commit(&commit, a->repo, id);
2455 if (err)
2456 goto done;
2458 bline->committer = strdup(got_object_commit_get_committer(commit));
2459 if (bline->committer == NULL) {
2460 err = got_error_from_errno("strdup");
2461 goto done;
2464 committer_time = got_object_commit_get_committer_time(commit);
2465 if (localtime_r(&committer_time, &tm) == NULL)
2466 return got_error_from_errno("localtime_r");
2467 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2468 &tm) >= sizeof(bline->datebuf)) {
2469 err = got_error(GOT_ERR_NO_SPACE);
2470 goto done;
2472 bline->annotated = 1;
2474 /* Print lines annotated so far. */
2475 bline = &a->lines[a->lineno_cur - 1];
2476 if (!bline->annotated)
2477 goto done;
2479 offset = a->line_offsets[a->lineno_cur - 1];
2480 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2481 err = got_error_from_errno("fseeko");
2482 goto done;
2485 while (bline->annotated) {
2486 char *smallerthan, *at, *nl, *committer;
2487 size_t len;
2489 if (getline(&line, &linesize, a->f) == -1) {
2490 if (ferror(a->f))
2491 err = got_error_from_errno("getline");
2492 break;
2495 committer = bline->committer;
2496 smallerthan = strchr(committer, '<');
2497 if (smallerthan && smallerthan[1] != '\0')
2498 committer = smallerthan + 1;
2499 at = strchr(committer, '@');
2500 if (at)
2501 *at = '\0';
2502 len = strlen(committer);
2503 if (len >= 9)
2504 committer[8] = '\0';
2506 nl = strchr(line, '\n');
2507 if (nl)
2508 *nl = '\0';
2509 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2510 bline->id_str, bline->datebuf, committer, line);
2512 a->lineno_cur++;
2513 bline = &a->lines[a->lineno_cur - 1];
2515 done:
2516 if (commit)
2517 got_object_commit_close(commit);
2518 free(line);
2519 return err;
2522 static const struct got_error *
2523 cmd_blame(int argc, char *argv[])
2525 const struct got_error *error;
2526 struct got_repository *repo = NULL;
2527 struct got_worktree *worktree = NULL;
2528 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2529 struct got_object_id *obj_id = NULL;
2530 struct got_object_id *commit_id = NULL;
2531 struct got_blob_object *blob = NULL;
2532 char *commit_id_str = NULL;
2533 struct blame_cb_args bca;
2534 int ch, obj_type, i;
2535 size_t filesize;
2537 memset(&bca, 0, sizeof(bca));
2539 #ifndef PROFILE
2540 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2541 NULL) == -1)
2542 err(1, "pledge");
2543 #endif
2545 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2546 switch (ch) {
2547 case 'c':
2548 commit_id_str = optarg;
2549 break;
2550 case 'r':
2551 repo_path = realpath(optarg, NULL);
2552 if (repo_path == NULL)
2553 return got_error_from_errno2("realpath",
2554 optarg);
2555 got_path_strip_trailing_slashes(repo_path);
2556 break;
2557 default:
2558 usage_blame();
2559 /* NOTREACHED */
2563 argc -= optind;
2564 argv += optind;
2566 if (argc == 1)
2567 path = argv[0];
2568 else
2569 usage_blame();
2571 cwd = getcwd(NULL, 0);
2572 if (cwd == NULL) {
2573 error = got_error_from_errno("getcwd");
2574 goto done;
2576 if (repo_path == NULL) {
2577 error = got_worktree_open(&worktree, cwd);
2578 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2579 goto done;
2580 else
2581 error = NULL;
2582 if (worktree) {
2583 repo_path =
2584 strdup(got_worktree_get_repo_path(worktree));
2585 if (repo_path == NULL)
2586 error = got_error_from_errno("strdup");
2587 if (error)
2588 goto done;
2589 } else {
2590 repo_path = strdup(cwd);
2591 if (repo_path == NULL) {
2592 error = got_error_from_errno("strdup");
2593 goto done;
2598 error = got_repo_open(&repo, repo_path, NULL);
2599 if (error != NULL)
2600 goto done;
2602 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2603 if (error)
2604 goto done;
2606 if (worktree) {
2607 const char *prefix = got_worktree_get_path_prefix(worktree);
2608 char *p, *worktree_subdir = cwd +
2609 strlen(got_worktree_get_root_path(worktree));
2610 if (asprintf(&p, "%s%s%s%s%s",
2611 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2612 worktree_subdir, worktree_subdir[0] ? "/" : "",
2613 path) == -1) {
2614 error = got_error_from_errno("asprintf");
2615 goto done;
2617 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2618 free(p);
2619 } else {
2620 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2622 if (error)
2623 goto done;
2625 if (commit_id_str == NULL) {
2626 struct got_reference *head_ref;
2627 error = got_ref_open(&head_ref, repo, worktree ?
2628 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2629 if (error != NULL)
2630 goto done;
2631 error = got_ref_resolve(&commit_id, repo, head_ref);
2632 got_ref_close(head_ref);
2633 if (error != NULL)
2634 goto done;
2635 } else {
2636 error = got_repo_match_object_id(&commit_id, NULL,
2637 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2638 if (error)
2639 goto done;
2642 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2643 if (error)
2644 goto done;
2646 error = got_object_get_type(&obj_type, repo, obj_id);
2647 if (error)
2648 goto done;
2650 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2651 error = got_error(GOT_ERR_OBJ_TYPE);
2652 goto done;
2655 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2656 if (error)
2657 goto done;
2658 bca.f = got_opentemp();
2659 if (bca.f == NULL) {
2660 error = got_error_from_errno("got_opentemp");
2661 goto done;
2663 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2664 &bca.line_offsets, bca.f, blob);
2665 if (error || bca.nlines == 0)
2666 goto done;
2668 /* Don't include \n at EOF in the blame line count. */
2669 if (bca.line_offsets[bca.nlines - 1] == filesize)
2670 bca.nlines--;
2672 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2673 if (bca.lines == NULL) {
2674 error = got_error_from_errno("calloc");
2675 goto done;
2677 bca.lineno_cur = 1;
2678 bca.nlines_prec = 0;
2679 i = bca.nlines;
2680 while (i > 0) {
2681 i /= 10;
2682 bca.nlines_prec++;
2684 bca.repo = repo;
2686 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2687 check_cancelled, NULL);
2688 done:
2689 free(in_repo_path);
2690 free(repo_path);
2691 free(cwd);
2692 free(commit_id);
2693 free(obj_id);
2694 if (blob)
2695 got_object_blob_close(blob);
2696 if (worktree)
2697 got_worktree_close(worktree);
2698 if (repo) {
2699 const struct got_error *repo_error;
2700 repo_error = got_repo_close(repo);
2701 if (error == NULL)
2702 error = repo_error;
2704 if (bca.lines) {
2705 for (i = 0; i < bca.nlines; i++) {
2706 struct blame_line *bline = &bca.lines[i];
2707 free(bline->id_str);
2708 free(bline->committer);
2710 free(bca.lines);
2712 free(bca.line_offsets);
2713 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2714 error = got_error_from_errno("fclose");
2715 return error;
2718 __dead static void
2719 usage_tree(void)
2721 fprintf(stderr,
2722 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2723 getprogname());
2724 exit(1);
2727 static void
2728 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2729 const char *root_path)
2731 int is_root_path = (strcmp(path, root_path) == 0);
2732 const char *modestr = "";
2733 mode_t mode = got_tree_entry_get_mode(te);
2735 path += strlen(root_path);
2736 while (path[0] == '/')
2737 path++;
2739 if (got_object_tree_entry_is_submodule(te))
2740 modestr = "$";
2741 else if (S_ISLNK(mode))
2742 modestr = "@";
2743 else if (S_ISDIR(mode))
2744 modestr = "/";
2745 else if (mode & S_IXUSR)
2746 modestr = "*";
2748 printf("%s%s%s%s%s\n", id ? id : "", path,
2749 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2752 static const struct got_error *
2753 print_tree(const char *path, struct got_object_id *commit_id,
2754 int show_ids, int recurse, const char *root_path,
2755 struct got_repository *repo)
2757 const struct got_error *err = NULL;
2758 struct got_object_id *tree_id = NULL;
2759 struct got_tree_object *tree = NULL;
2760 int nentries, i;
2762 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2763 if (err)
2764 goto done;
2766 err = got_object_open_as_tree(&tree, repo, tree_id);
2767 if (err)
2768 goto done;
2769 nentries = got_object_tree_get_nentries(tree);
2770 for (i = 0; i < nentries; i++) {
2771 struct got_tree_entry *te;
2772 char *id = NULL;
2774 if (sigint_received || sigpipe_received)
2775 break;
2777 te = got_object_tree_get_entry(tree, i);
2778 if (show_ids) {
2779 char *id_str;
2780 err = got_object_id_str(&id_str,
2781 got_tree_entry_get_id(te));
2782 if (err)
2783 goto done;
2784 if (asprintf(&id, "%s ", id_str) == -1) {
2785 err = got_error_from_errno("asprintf");
2786 free(id_str);
2787 goto done;
2789 free(id_str);
2791 print_entry(te, id, path, root_path);
2792 free(id);
2794 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2795 char *child_path;
2796 if (asprintf(&child_path, "%s%s%s", path,
2797 path[0] == '/' && path[1] == '\0' ? "" : "/",
2798 got_tree_entry_get_name(te)) == -1) {
2799 err = got_error_from_errno("asprintf");
2800 goto done;
2802 err = print_tree(child_path, commit_id, show_ids, 1,
2803 root_path, repo);
2804 free(child_path);
2805 if (err)
2806 goto done;
2809 done:
2810 if (tree)
2811 got_object_tree_close(tree);
2812 free(tree_id);
2813 return err;
2816 static const struct got_error *
2817 cmd_tree(int argc, char *argv[])
2819 const struct got_error *error;
2820 struct got_repository *repo = NULL;
2821 struct got_worktree *worktree = NULL;
2822 const char *path;
2823 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2824 struct got_object_id *commit_id = NULL;
2825 char *commit_id_str = NULL;
2826 int show_ids = 0, recurse = 0;
2827 int ch;
2829 #ifndef PROFILE
2830 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2831 NULL) == -1)
2832 err(1, "pledge");
2833 #endif
2835 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2836 switch (ch) {
2837 case 'c':
2838 commit_id_str = optarg;
2839 break;
2840 case 'r':
2841 repo_path = realpath(optarg, NULL);
2842 if (repo_path == NULL)
2843 return got_error_from_errno2("realpath",
2844 optarg);
2845 got_path_strip_trailing_slashes(repo_path);
2846 break;
2847 case 'i':
2848 show_ids = 1;
2849 break;
2850 case 'R':
2851 recurse = 1;
2852 break;
2853 default:
2854 usage_tree();
2855 /* NOTREACHED */
2859 argc -= optind;
2860 argv += optind;
2862 if (argc == 1)
2863 path = argv[0];
2864 else if (argc > 1)
2865 usage_tree();
2866 else
2867 path = NULL;
2869 cwd = getcwd(NULL, 0);
2870 if (cwd == NULL) {
2871 error = got_error_from_errno("getcwd");
2872 goto done;
2874 if (repo_path == NULL) {
2875 error = got_worktree_open(&worktree, cwd);
2876 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2877 goto done;
2878 else
2879 error = NULL;
2880 if (worktree) {
2881 repo_path =
2882 strdup(got_worktree_get_repo_path(worktree));
2883 if (repo_path == NULL)
2884 error = got_error_from_errno("strdup");
2885 if (error)
2886 goto done;
2887 } else {
2888 repo_path = strdup(cwd);
2889 if (repo_path == NULL) {
2890 error = got_error_from_errno("strdup");
2891 goto done;
2896 error = got_repo_open(&repo, repo_path, NULL);
2897 if (error != NULL)
2898 goto done;
2900 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2901 if (error)
2902 goto done;
2904 if (path == NULL) {
2905 if (worktree) {
2906 char *p, *worktree_subdir = cwd +
2907 strlen(got_worktree_get_root_path(worktree));
2908 if (asprintf(&p, "%s/%s",
2909 got_worktree_get_path_prefix(worktree),
2910 worktree_subdir) == -1) {
2911 error = got_error_from_errno("asprintf");
2912 goto done;
2914 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2915 free(p);
2916 if (error)
2917 goto done;
2918 } else
2919 path = "/";
2921 if (in_repo_path == NULL) {
2922 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2923 if (error != NULL)
2924 goto done;
2927 if (commit_id_str == NULL) {
2928 struct got_reference *head_ref;
2929 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2930 if (error != NULL)
2931 goto done;
2932 error = got_ref_resolve(&commit_id, repo, head_ref);
2933 got_ref_close(head_ref);
2934 if (error != NULL)
2935 goto done;
2936 } else {
2937 error = got_repo_match_object_id(&commit_id, NULL,
2938 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2939 if (error)
2940 goto done;
2943 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2944 in_repo_path, repo);
2945 done:
2946 free(in_repo_path);
2947 free(repo_path);
2948 free(cwd);
2949 free(commit_id);
2950 if (worktree)
2951 got_worktree_close(worktree);
2952 if (repo) {
2953 const struct got_error *repo_error;
2954 repo_error = got_repo_close(repo);
2955 if (error == NULL)
2956 error = repo_error;
2958 return error;
2961 __dead static void
2962 usage_status(void)
2964 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2965 exit(1);
2968 static const struct got_error *
2969 print_status(void *arg, unsigned char status, unsigned char staged_status,
2970 const char *path, struct got_object_id *blob_id,
2971 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2972 int dirfd, const char *de_name)
2974 if (status == staged_status && (status == GOT_STATUS_DELETE))
2975 status = GOT_STATUS_NO_CHANGE;
2976 printf("%c%c %s\n", status, staged_status, path);
2977 return NULL;
2980 static const struct got_error *
2981 cmd_status(int argc, char *argv[])
2983 const struct got_error *error = NULL;
2984 struct got_repository *repo = NULL;
2985 struct got_worktree *worktree = NULL;
2986 char *cwd = NULL;
2987 struct got_pathlist_head paths;
2988 struct got_pathlist_entry *pe;
2989 int ch;
2991 TAILQ_INIT(&paths);
2993 while ((ch = getopt(argc, argv, "")) != -1) {
2994 switch (ch) {
2995 default:
2996 usage_status();
2997 /* NOTREACHED */
3001 argc -= optind;
3002 argv += optind;
3004 #ifndef PROFILE
3005 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3006 NULL) == -1)
3007 err(1, "pledge");
3008 #endif
3009 cwd = getcwd(NULL, 0);
3010 if (cwd == NULL) {
3011 error = got_error_from_errno("getcwd");
3012 goto done;
3015 error = got_worktree_open(&worktree, cwd);
3016 if (error != NULL)
3017 goto done;
3019 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3020 NULL);
3021 if (error != NULL)
3022 goto done;
3024 error = apply_unveil(got_repo_get_path(repo), 1,
3025 got_worktree_get_root_path(worktree));
3026 if (error)
3027 goto done;
3029 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3030 if (error)
3031 goto done;
3033 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3034 check_cancelled, NULL);
3035 done:
3036 TAILQ_FOREACH(pe, &paths, entry)
3037 free((char *)pe->path);
3038 got_pathlist_free(&paths);
3039 free(cwd);
3040 return error;
3043 __dead static void
3044 usage_ref(void)
3046 fprintf(stderr,
3047 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3048 getprogname());
3049 exit(1);
3052 static const struct got_error *
3053 list_refs(struct got_repository *repo)
3055 static const struct got_error *err = NULL;
3056 struct got_reflist_head refs;
3057 struct got_reflist_entry *re;
3059 SIMPLEQ_INIT(&refs);
3060 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3061 if (err)
3062 return err;
3064 SIMPLEQ_FOREACH(re, &refs, entry) {
3065 char *refstr;
3066 refstr = got_ref_to_str(re->ref);
3067 if (refstr == NULL)
3068 return got_error_from_errno("got_ref_to_str");
3069 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3070 free(refstr);
3073 got_ref_list_free(&refs);
3074 return NULL;
3077 static const struct got_error *
3078 delete_ref(struct got_repository *repo, const char *refname)
3080 const struct got_error *err = NULL;
3081 struct got_reference *ref;
3083 err = got_ref_open(&ref, repo, refname, 0);
3084 if (err)
3085 return err;
3087 err = got_ref_delete(ref, repo);
3088 got_ref_close(ref);
3089 return err;
3092 static const struct got_error *
3093 add_ref(struct got_repository *repo, const char *refname, const char *target)
3095 const struct got_error *err = NULL;
3096 struct got_object_id *id;
3097 struct got_reference *ref = NULL;
3100 * Don't let the user create a reference name with a leading '-'.
3101 * While technically a valid reference name, this case is usually
3102 * an unintended typo.
3104 if (refname[0] == '-')
3105 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3107 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3108 repo);
3109 if (err) {
3110 struct got_reference *target_ref;
3112 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3113 return err;
3114 err = got_ref_open(&target_ref, repo, target, 0);
3115 if (err)
3116 return err;
3117 err = got_ref_resolve(&id, repo, target_ref);
3118 got_ref_close(target_ref);
3119 if (err)
3120 return err;
3123 err = got_ref_alloc(&ref, refname, id);
3124 if (err)
3125 goto done;
3127 err = got_ref_write(ref, repo);
3128 done:
3129 if (ref)
3130 got_ref_close(ref);
3131 free(id);
3132 return err;
3135 static const struct got_error *
3136 add_symref(struct got_repository *repo, const char *refname, const char *target)
3138 const struct got_error *err = NULL;
3139 struct got_reference *ref = NULL;
3140 struct got_reference *target_ref = NULL;
3143 * Don't let the user create a reference name with a leading '-'.
3144 * While technically a valid reference name, this case is usually
3145 * an unintended typo.
3147 if (refname[0] == '-')
3148 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3150 err = got_ref_open(&target_ref, repo, target, 0);
3151 if (err)
3152 return err;
3154 err = got_ref_alloc_symref(&ref, refname, target_ref);
3155 if (err)
3156 goto done;
3158 err = got_ref_write(ref, repo);
3159 done:
3160 if (target_ref)
3161 got_ref_close(target_ref);
3162 if (ref)
3163 got_ref_close(ref);
3164 return err;
3167 static const struct got_error *
3168 cmd_ref(int argc, char *argv[])
3170 const struct got_error *error = NULL;
3171 struct got_repository *repo = NULL;
3172 struct got_worktree *worktree = NULL;
3173 char *cwd = NULL, *repo_path = NULL;
3174 int ch, do_list = 0, create_symref = 0;
3175 const char *delref = NULL;
3177 /* TODO: Add -s option for adding symbolic references. */
3178 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3179 switch (ch) {
3180 case 'd':
3181 delref = optarg;
3182 break;
3183 case 'r':
3184 repo_path = realpath(optarg, NULL);
3185 if (repo_path == NULL)
3186 return got_error_from_errno2("realpath",
3187 optarg);
3188 got_path_strip_trailing_slashes(repo_path);
3189 break;
3190 case 'l':
3191 do_list = 1;
3192 break;
3193 case 's':
3194 create_symref = 1;
3195 break;
3196 default:
3197 usage_ref();
3198 /* NOTREACHED */
3202 if (do_list && delref)
3203 errx(1, "-l and -d options are mutually exclusive\n");
3205 argc -= optind;
3206 argv += optind;
3208 if (do_list || delref) {
3209 if (create_symref)
3210 errx(1, "-s option cannot be used together with the "
3211 "-l or -d options");
3212 if (argc > 0)
3213 usage_ref();
3214 } else if (argc != 2)
3215 usage_ref();
3217 #ifndef PROFILE
3218 if (do_list) {
3219 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3220 NULL) == -1)
3221 err(1, "pledge");
3222 } else {
3223 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3224 "sendfd unveil", NULL) == -1)
3225 err(1, "pledge");
3227 #endif
3228 cwd = getcwd(NULL, 0);
3229 if (cwd == NULL) {
3230 error = got_error_from_errno("getcwd");
3231 goto done;
3234 if (repo_path == NULL) {
3235 error = got_worktree_open(&worktree, cwd);
3236 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3237 goto done;
3238 else
3239 error = NULL;
3240 if (worktree) {
3241 repo_path =
3242 strdup(got_worktree_get_repo_path(worktree));
3243 if (repo_path == NULL)
3244 error = got_error_from_errno("strdup");
3245 if (error)
3246 goto done;
3247 } else {
3248 repo_path = strdup(cwd);
3249 if (repo_path == NULL) {
3250 error = got_error_from_errno("strdup");
3251 goto done;
3256 error = got_repo_open(&repo, repo_path, NULL);
3257 if (error != NULL)
3258 goto done;
3260 error = apply_unveil(got_repo_get_path(repo), do_list,
3261 worktree ? got_worktree_get_root_path(worktree) : NULL);
3262 if (error)
3263 goto done;
3265 if (do_list)
3266 error = list_refs(repo);
3267 else if (delref)
3268 error = delete_ref(repo, delref);
3269 else if (create_symref)
3270 error = add_symref(repo, argv[0], argv[1]);
3271 else
3272 error = add_ref(repo, argv[0], argv[1]);
3273 done:
3274 if (repo)
3275 got_repo_close(repo);
3276 if (worktree)
3277 got_worktree_close(worktree);
3278 free(cwd);
3279 free(repo_path);
3280 return error;
3283 __dead static void
3284 usage_branch(void)
3286 fprintf(stderr,
3287 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3288 "[name]\n", getprogname());
3289 exit(1);
3292 static const struct got_error *
3293 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3294 struct got_reference *ref)
3296 const struct got_error *err = NULL;
3297 const char *refname, *marker = " ";
3298 char *refstr;
3300 refname = got_ref_get_name(ref);
3301 if (worktree && strcmp(refname,
3302 got_worktree_get_head_ref_name(worktree)) == 0) {
3303 struct got_object_id *id = NULL;
3305 err = got_ref_resolve(&id, repo, ref);
3306 if (err)
3307 return err;
3308 if (got_object_id_cmp(id,
3309 got_worktree_get_base_commit_id(worktree)) == 0)
3310 marker = "* ";
3311 else
3312 marker = "~ ";
3313 free(id);
3316 if (strncmp(refname, "refs/heads/", 11) == 0)
3317 refname += 11;
3318 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3319 refname += 18;
3321 refstr = got_ref_to_str(ref);
3322 if (refstr == NULL)
3323 return got_error_from_errno("got_ref_to_str");
3325 printf("%s%s: %s\n", marker, refname, refstr);
3326 free(refstr);
3327 return NULL;
3330 static const struct got_error *
3331 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3333 const char *refname;
3335 if (worktree == NULL)
3336 return got_error(GOT_ERR_NOT_WORKTREE);
3338 refname = got_worktree_get_head_ref_name(worktree);
3340 if (strncmp(refname, "refs/heads/", 11) == 0)
3341 refname += 11;
3342 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3343 refname += 18;
3345 printf("%s\n", refname);
3347 return NULL;
3350 static const struct got_error *
3351 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3353 static const struct got_error *err = NULL;
3354 struct got_reflist_head refs;
3355 struct got_reflist_entry *re;
3356 struct got_reference *temp_ref = NULL;
3357 int rebase_in_progress, histedit_in_progress;
3359 SIMPLEQ_INIT(&refs);
3361 if (worktree) {
3362 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3363 worktree);
3364 if (err)
3365 return err;
3367 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3368 worktree);
3369 if (err)
3370 return err;
3372 if (rebase_in_progress || histedit_in_progress) {
3373 err = got_ref_open(&temp_ref, repo,
3374 got_worktree_get_head_ref_name(worktree), 0);
3375 if (err)
3376 return err;
3377 list_branch(repo, worktree, temp_ref);
3378 got_ref_close(temp_ref);
3382 err = got_ref_list(&refs, repo, "refs/heads",
3383 got_ref_cmp_by_name, NULL);
3384 if (err)
3385 return err;
3387 SIMPLEQ_FOREACH(re, &refs, entry)
3388 list_branch(repo, worktree, re->ref);
3390 got_ref_list_free(&refs);
3391 return NULL;
3394 static const struct got_error *
3395 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3396 const char *branch_name)
3398 const struct got_error *err = NULL;
3399 struct got_reference *ref = NULL;
3400 char *refname;
3402 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3403 return got_error_from_errno("asprintf");
3405 err = got_ref_open(&ref, repo, refname, 0);
3406 if (err)
3407 goto done;
3409 if (worktree &&
3410 strcmp(got_worktree_get_head_ref_name(worktree),
3411 got_ref_get_name(ref)) == 0) {
3412 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3413 "will not delete this work tree's current branch");
3414 goto done;
3417 err = got_ref_delete(ref, repo);
3418 done:
3419 if (ref)
3420 got_ref_close(ref);
3421 free(refname);
3422 return err;
3425 static const struct got_error *
3426 add_branch(struct got_repository *repo, const char *branch_name,
3427 struct got_object_id *base_commit_id)
3429 const struct got_error *err = NULL;
3430 struct got_reference *ref = NULL;
3431 char *base_refname = NULL, *refname = NULL;
3434 * Don't let the user create a branch name with a leading '-'.
3435 * While technically a valid reference name, this case is usually
3436 * an unintended typo.
3438 if (branch_name[0] == '-')
3439 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3441 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3442 err = got_error_from_errno("asprintf");
3443 goto done;
3446 err = got_ref_open(&ref, repo, refname, 0);
3447 if (err == NULL) {
3448 err = got_error(GOT_ERR_BRANCH_EXISTS);
3449 goto done;
3450 } else if (err->code != GOT_ERR_NOT_REF)
3451 goto done;
3453 err = got_ref_alloc(&ref, refname, base_commit_id);
3454 if (err)
3455 goto done;
3457 err = got_ref_write(ref, repo);
3458 done:
3459 if (ref)
3460 got_ref_close(ref);
3461 free(base_refname);
3462 free(refname);
3463 return err;
3466 static const struct got_error *
3467 cmd_branch(int argc, char *argv[])
3469 const struct got_error *error = NULL;
3470 struct got_repository *repo = NULL;
3471 struct got_worktree *worktree = NULL;
3472 char *cwd = NULL, *repo_path = NULL;
3473 int ch, do_list = 0, do_show = 0, do_update = 1;
3474 const char *delref = NULL, *commit_id_arg = NULL;
3475 struct got_reference *ref = NULL;
3476 struct got_pathlist_head paths;
3477 struct got_pathlist_entry *pe;
3478 struct got_object_id *commit_id = NULL;
3479 char *commit_id_str = NULL;
3481 TAILQ_INIT(&paths);
3483 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3484 switch (ch) {
3485 case 'c':
3486 commit_id_arg = optarg;
3487 break;
3488 case 'd':
3489 delref = optarg;
3490 break;
3491 case 'r':
3492 repo_path = realpath(optarg, NULL);
3493 if (repo_path == NULL)
3494 return got_error_from_errno2("realpath",
3495 optarg);
3496 got_path_strip_trailing_slashes(repo_path);
3497 break;
3498 case 'l':
3499 do_list = 1;
3500 break;
3501 case 'n':
3502 do_update = 0;
3503 break;
3504 default:
3505 usage_branch();
3506 /* NOTREACHED */
3510 if (do_list && delref)
3511 errx(1, "-l and -d options are mutually exclusive\n");
3513 argc -= optind;
3514 argv += optind;
3516 if (!do_list && !delref && argc == 0)
3517 do_show = 1;
3519 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3520 errx(1, "-c option can only be used when creating a branch");
3522 if (do_list || delref) {
3523 if (argc > 0)
3524 usage_branch();
3525 } else if (!do_show && argc != 1)
3526 usage_branch();
3528 #ifndef PROFILE
3529 if (do_list || do_show) {
3530 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3531 NULL) == -1)
3532 err(1, "pledge");
3533 } else {
3534 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3535 "sendfd unveil", NULL) == -1)
3536 err(1, "pledge");
3538 #endif
3539 cwd = getcwd(NULL, 0);
3540 if (cwd == NULL) {
3541 error = got_error_from_errno("getcwd");
3542 goto done;
3545 if (repo_path == NULL) {
3546 error = got_worktree_open(&worktree, cwd);
3547 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3548 goto done;
3549 else
3550 error = NULL;
3551 if (worktree) {
3552 repo_path =
3553 strdup(got_worktree_get_repo_path(worktree));
3554 if (repo_path == NULL)
3555 error = got_error_from_errno("strdup");
3556 if (error)
3557 goto done;
3558 } else {
3559 repo_path = strdup(cwd);
3560 if (repo_path == NULL) {
3561 error = got_error_from_errno("strdup");
3562 goto done;
3567 error = got_repo_open(&repo, repo_path, NULL);
3568 if (error != NULL)
3569 goto done;
3571 error = apply_unveil(got_repo_get_path(repo), do_list,
3572 worktree ? got_worktree_get_root_path(worktree) : NULL);
3573 if (error)
3574 goto done;
3576 if (do_show)
3577 error = show_current_branch(repo, worktree);
3578 else if (do_list)
3579 error = list_branches(repo, worktree);
3580 else if (delref)
3581 error = delete_branch(repo, worktree, delref);
3582 else {
3583 if (commit_id_arg == NULL)
3584 commit_id_arg = worktree ?
3585 got_worktree_get_head_ref_name(worktree) :
3586 GOT_REF_HEAD;
3587 error = got_repo_match_object_id(&commit_id, NULL,
3588 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3589 if (error)
3590 goto done;
3591 error = add_branch(repo, argv[0], commit_id);
3592 if (error)
3593 goto done;
3594 if (worktree && do_update) {
3595 int did_something = 0;
3596 char *branch_refname = NULL;
3598 error = got_object_id_str(&commit_id_str, commit_id);
3599 if (error)
3600 goto done;
3601 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3602 worktree);
3603 if (error)
3604 goto done;
3605 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3606 == -1) {
3607 error = got_error_from_errno("asprintf");
3608 goto done;
3610 error = got_ref_open(&ref, repo, branch_refname, 0);
3611 free(branch_refname);
3612 if (error)
3613 goto done;
3614 error = switch_head_ref(ref, commit_id, worktree,
3615 repo);
3616 if (error)
3617 goto done;
3618 error = got_worktree_set_base_commit_id(worktree, repo,
3619 commit_id);
3620 if (error)
3621 goto done;
3622 error = got_worktree_checkout_files(worktree, &paths,
3623 repo, update_progress, &did_something,
3624 check_cancelled, NULL);
3625 if (error)
3626 goto done;
3627 if (did_something)
3628 printf("Updated to commit %s\n", commit_id_str);
3631 done:
3632 if (ref)
3633 got_ref_close(ref);
3634 if (repo)
3635 got_repo_close(repo);
3636 if (worktree)
3637 got_worktree_close(worktree);
3638 free(cwd);
3639 free(repo_path);
3640 free(commit_id);
3641 free(commit_id_str);
3642 TAILQ_FOREACH(pe, &paths, entry)
3643 free((char *)pe->path);
3644 got_pathlist_free(&paths);
3645 return error;
3649 __dead static void
3650 usage_tag(void)
3652 fprintf(stderr,
3653 "usage: %s tag [-c commit] [-r repository] [-l] "
3654 "[-m message] name\n", getprogname());
3655 exit(1);
3658 #if 0
3659 static const struct got_error *
3660 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3662 const struct got_error *err = NULL;
3663 struct got_reflist_entry *re, *se, *new;
3664 struct got_object_id *re_id, *se_id;
3665 struct got_tag_object *re_tag, *se_tag;
3666 time_t re_time, se_time;
3668 SIMPLEQ_FOREACH(re, tags, entry) {
3669 se = SIMPLEQ_FIRST(sorted);
3670 if (se == NULL) {
3671 err = got_reflist_entry_dup(&new, re);
3672 if (err)
3673 return err;
3674 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3675 continue;
3676 } else {
3677 err = got_ref_resolve(&re_id, repo, re->ref);
3678 if (err)
3679 break;
3680 err = got_object_open_as_tag(&re_tag, repo, re_id);
3681 free(re_id);
3682 if (err)
3683 break;
3684 re_time = got_object_tag_get_tagger_time(re_tag);
3685 got_object_tag_close(re_tag);
3688 while (se) {
3689 err = got_ref_resolve(&se_id, repo, re->ref);
3690 if (err)
3691 break;
3692 err = got_object_open_as_tag(&se_tag, repo, se_id);
3693 free(se_id);
3694 if (err)
3695 break;
3696 se_time = got_object_tag_get_tagger_time(se_tag);
3697 got_object_tag_close(se_tag);
3699 if (se_time > re_time) {
3700 err = got_reflist_entry_dup(&new, re);
3701 if (err)
3702 return err;
3703 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3704 break;
3706 se = SIMPLEQ_NEXT(se, entry);
3707 continue;
3710 done:
3711 return err;
3713 #endif
3715 static const struct got_error *
3716 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3718 static const struct got_error *err = NULL;
3719 struct got_reflist_head refs;
3720 struct got_reflist_entry *re;
3722 SIMPLEQ_INIT(&refs);
3724 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3725 if (err)
3726 return err;
3728 SIMPLEQ_FOREACH(re, &refs, entry) {
3729 const char *refname;
3730 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3731 char datebuf[26];
3732 const char *tagger;
3733 time_t tagger_time;
3734 struct got_object_id *id;
3735 struct got_tag_object *tag;
3736 struct got_commit_object *commit = NULL;
3738 refname = got_ref_get_name(re->ref);
3739 if (strncmp(refname, "refs/tags/", 10) != 0)
3740 continue;
3741 refname += 10;
3742 refstr = got_ref_to_str(re->ref);
3743 if (refstr == NULL) {
3744 err = got_error_from_errno("got_ref_to_str");
3745 break;
3747 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3748 free(refstr);
3750 err = got_ref_resolve(&id, repo, re->ref);
3751 if (err)
3752 break;
3753 err = got_object_open_as_tag(&tag, repo, id);
3754 if (err) {
3755 if (err->code != GOT_ERR_OBJ_TYPE) {
3756 free(id);
3757 break;
3759 /* "lightweight" tag */
3760 err = got_object_open_as_commit(&commit, repo, id);
3761 if (err) {
3762 free(id);
3763 break;
3765 tagger = got_object_commit_get_committer(commit);
3766 tagger_time =
3767 got_object_commit_get_committer_time(commit);
3768 err = got_object_id_str(&id_str, id);
3769 free(id);
3770 if (err)
3771 break;
3772 } else {
3773 free(id);
3774 tagger = got_object_tag_get_tagger(tag);
3775 tagger_time = got_object_tag_get_tagger_time(tag);
3776 err = got_object_id_str(&id_str,
3777 got_object_tag_get_object_id(tag));
3778 if (err)
3779 break;
3781 printf("from: %s\n", tagger);
3782 datestr = get_datestr(&tagger_time, datebuf);
3783 if (datestr)
3784 printf("date: %s UTC\n", datestr);
3785 if (commit)
3786 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3787 else {
3788 switch (got_object_tag_get_object_type(tag)) {
3789 case GOT_OBJ_TYPE_BLOB:
3790 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3791 id_str);
3792 break;
3793 case GOT_OBJ_TYPE_TREE:
3794 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3795 id_str);
3796 break;
3797 case GOT_OBJ_TYPE_COMMIT:
3798 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3799 id_str);
3800 break;
3801 case GOT_OBJ_TYPE_TAG:
3802 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3803 id_str);
3804 break;
3805 default:
3806 break;
3809 free(id_str);
3810 if (commit) {
3811 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3812 if (err)
3813 break;
3814 got_object_commit_close(commit);
3815 } else {
3816 tagmsg0 = strdup(got_object_tag_get_message(tag));
3817 got_object_tag_close(tag);
3818 if (tagmsg0 == NULL) {
3819 err = got_error_from_errno("strdup");
3820 break;
3824 tagmsg = tagmsg0;
3825 do {
3826 line = strsep(&tagmsg, "\n");
3827 if (line)
3828 printf(" %s\n", line);
3829 } while (line);
3830 free(tagmsg0);
3833 got_ref_list_free(&refs);
3834 return NULL;
3837 static const struct got_error *
3838 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3839 const char *tag_name, const char *repo_path)
3841 const struct got_error *err = NULL;
3842 char *template = NULL, *initial_content = NULL;
3843 char *editor = NULL;
3844 int fd = -1;
3846 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
3847 err = got_error_from_errno("asprintf");
3848 goto done;
3851 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3852 commit_id_str, tag_name) == -1) {
3853 err = got_error_from_errno("asprintf");
3854 goto done;
3857 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3858 if (err)
3859 goto done;
3861 dprintf(fd, initial_content);
3862 close(fd);
3864 err = get_editor(&editor);
3865 if (err)
3866 goto done;
3867 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3868 done:
3869 free(initial_content);
3870 free(template);
3871 free(editor);
3873 /* Editor is done; we can now apply unveil(2) */
3874 if (err == NULL) {
3875 err = apply_unveil(repo_path, 0, NULL);
3876 if (err) {
3877 free(*tagmsg);
3878 *tagmsg = NULL;
3881 return err;
3884 static const struct got_error *
3885 add_tag(struct got_repository *repo, const char *tag_name,
3886 const char *commit_arg, const char *tagmsg_arg)
3888 const struct got_error *err = NULL;
3889 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3890 char *label = NULL, *commit_id_str = NULL;
3891 struct got_reference *ref = NULL;
3892 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3893 char *tagmsg_path = NULL, *tag_id_str = NULL;
3894 int preserve_tagmsg = 0;
3897 * Don't let the user create a tag name with a leading '-'.
3898 * While technically a valid reference name, this case is usually
3899 * an unintended typo.
3901 if (tag_name[0] == '-')
3902 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3904 err = get_author(&tagger, repo);
3905 if (err)
3906 return err;
3908 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3909 GOT_OBJ_TYPE_COMMIT, 1, repo);
3910 if (err)
3911 goto done;
3913 err = got_object_id_str(&commit_id_str, commit_id);
3914 if (err)
3915 goto done;
3917 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3918 refname = strdup(tag_name);
3919 if (refname == NULL) {
3920 err = got_error_from_errno("strdup");
3921 goto done;
3923 tag_name += 10;
3924 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3925 err = got_error_from_errno("asprintf");
3926 goto done;
3929 err = got_ref_open(&ref, repo, refname, 0);
3930 if (err == NULL) {
3931 err = got_error(GOT_ERR_TAG_EXISTS);
3932 goto done;
3933 } else if (err->code != GOT_ERR_NOT_REF)
3934 goto done;
3936 if (tagmsg_arg == NULL) {
3937 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3938 tag_name, got_repo_get_path(repo));
3939 if (err) {
3940 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3941 tagmsg_path != NULL)
3942 preserve_tagmsg = 1;
3943 goto done;
3947 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3948 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3949 if (err) {
3950 if (tagmsg_path)
3951 preserve_tagmsg = 1;
3952 goto done;
3955 err = got_ref_alloc(&ref, refname, tag_id);
3956 if (err) {
3957 if (tagmsg_path)
3958 preserve_tagmsg = 1;
3959 goto done;
3962 err = got_ref_write(ref, repo);
3963 if (err) {
3964 if (tagmsg_path)
3965 preserve_tagmsg = 1;
3966 goto done;
3969 err = got_object_id_str(&tag_id_str, tag_id);
3970 if (err) {
3971 if (tagmsg_path)
3972 preserve_tagmsg = 1;
3973 goto done;
3975 printf("Created tag %s\n", tag_id_str);
3976 done:
3977 if (preserve_tagmsg) {
3978 fprintf(stderr, "%s: tag message preserved in %s\n",
3979 getprogname(), tagmsg_path);
3980 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3981 err = got_error_from_errno2("unlink", tagmsg_path);
3982 free(tag_id_str);
3983 if (ref)
3984 got_ref_close(ref);
3985 free(commit_id);
3986 free(commit_id_str);
3987 free(refname);
3988 free(tagmsg);
3989 free(tagmsg_path);
3990 free(tagger);
3991 return err;
3994 static const struct got_error *
3995 cmd_tag(int argc, char *argv[])
3997 const struct got_error *error = NULL;
3998 struct got_repository *repo = NULL;
3999 struct got_worktree *worktree = NULL;
4000 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4001 char *gitconfig_path = NULL;
4002 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4003 int ch, do_list = 0;
4005 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4006 switch (ch) {
4007 case 'c':
4008 commit_id_arg = optarg;
4009 break;
4010 case 'm':
4011 tagmsg = optarg;
4012 break;
4013 case 'r':
4014 repo_path = realpath(optarg, NULL);
4015 if (repo_path == NULL)
4016 return got_error_from_errno2("realpath",
4017 optarg);
4018 got_path_strip_trailing_slashes(repo_path);
4019 break;
4020 case 'l':
4021 do_list = 1;
4022 break;
4023 default:
4024 usage_tag();
4025 /* NOTREACHED */
4029 argc -= optind;
4030 argv += optind;
4032 if (do_list) {
4033 if (commit_id_arg != NULL)
4034 errx(1, "-c option can only be used when creating a tag");
4035 if (tagmsg)
4036 errx(1, "-l and -m options are mutually exclusive");
4037 if (argc > 0)
4038 usage_tag();
4039 } else if (argc != 1)
4040 usage_tag();
4042 tag_name = argv[0];
4044 #ifndef PROFILE
4045 if (do_list) {
4046 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4047 NULL) == -1)
4048 err(1, "pledge");
4049 } else {
4050 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4051 "sendfd unveil", NULL) == -1)
4052 err(1, "pledge");
4054 #endif
4055 cwd = getcwd(NULL, 0);
4056 if (cwd == NULL) {
4057 error = got_error_from_errno("getcwd");
4058 goto done;
4061 if (repo_path == NULL) {
4062 error = got_worktree_open(&worktree, cwd);
4063 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4064 goto done;
4065 else
4066 error = NULL;
4067 if (worktree) {
4068 repo_path =
4069 strdup(got_worktree_get_repo_path(worktree));
4070 if (repo_path == NULL)
4071 error = got_error_from_errno("strdup");
4072 if (error)
4073 goto done;
4074 } else {
4075 repo_path = strdup(cwd);
4076 if (repo_path == NULL) {
4077 error = got_error_from_errno("strdup");
4078 goto done;
4083 if (do_list) {
4084 error = got_repo_open(&repo, repo_path, NULL);
4085 if (error != NULL)
4086 goto done;
4087 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4088 if (error)
4089 goto done;
4090 error = list_tags(repo, worktree);
4091 } else {
4092 error = get_gitconfig_path(&gitconfig_path);
4093 if (error)
4094 goto done;
4095 error = got_repo_open(&repo, repo_path, gitconfig_path);
4096 if (error != NULL)
4097 goto done;
4099 if (tagmsg) {
4100 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4101 if (error)
4102 goto done;
4105 if (commit_id_arg == NULL) {
4106 struct got_reference *head_ref;
4107 struct got_object_id *commit_id;
4108 error = got_ref_open(&head_ref, repo,
4109 worktree ? got_worktree_get_head_ref_name(worktree)
4110 : GOT_REF_HEAD, 0);
4111 if (error)
4112 goto done;
4113 error = got_ref_resolve(&commit_id, repo, head_ref);
4114 got_ref_close(head_ref);
4115 if (error)
4116 goto done;
4117 error = got_object_id_str(&commit_id_str, commit_id);
4118 free(commit_id);
4119 if (error)
4120 goto done;
4123 error = add_tag(repo, tag_name,
4124 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4126 done:
4127 if (repo)
4128 got_repo_close(repo);
4129 if (worktree)
4130 got_worktree_close(worktree);
4131 free(cwd);
4132 free(repo_path);
4133 free(gitconfig_path);
4134 free(commit_id_str);
4135 return error;
4138 __dead static void
4139 usage_add(void)
4141 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4142 getprogname());
4143 exit(1);
4146 static const struct got_error *
4147 add_progress(void *arg, unsigned char status, const char *path)
4149 while (path[0] == '/')
4150 path++;
4151 printf("%c %s\n", status, path);
4152 return NULL;
4155 static const struct got_error *
4156 cmd_add(int argc, char *argv[])
4158 const struct got_error *error = NULL;
4159 struct got_repository *repo = NULL;
4160 struct got_worktree *worktree = NULL;
4161 char *cwd = NULL;
4162 struct got_pathlist_head paths;
4163 struct got_pathlist_entry *pe;
4164 int ch, can_recurse = 0, no_ignores = 0;
4166 TAILQ_INIT(&paths);
4168 while ((ch = getopt(argc, argv, "IR")) != -1) {
4169 switch (ch) {
4170 case 'I':
4171 no_ignores = 1;
4172 break;
4173 case 'R':
4174 can_recurse = 1;
4175 break;
4176 default:
4177 usage_add();
4178 /* NOTREACHED */
4182 argc -= optind;
4183 argv += optind;
4185 #ifndef PROFILE
4186 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4187 NULL) == -1)
4188 err(1, "pledge");
4189 #endif
4190 if (argc < 1)
4191 usage_add();
4193 cwd = getcwd(NULL, 0);
4194 if (cwd == NULL) {
4195 error = got_error_from_errno("getcwd");
4196 goto done;
4199 error = got_worktree_open(&worktree, cwd);
4200 if (error)
4201 goto done;
4203 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4204 NULL);
4205 if (error != NULL)
4206 goto done;
4208 error = apply_unveil(got_repo_get_path(repo), 1,
4209 got_worktree_get_root_path(worktree));
4210 if (error)
4211 goto done;
4213 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4214 if (error)
4215 goto done;
4217 if (!can_recurse && no_ignores) {
4218 error = got_error_msg(GOT_ERR_BAD_PATH,
4219 "disregarding ignores requires -R option");
4220 goto done;
4224 if (!can_recurse) {
4225 char *ondisk_path;
4226 struct stat sb;
4227 TAILQ_FOREACH(pe, &paths, entry) {
4228 if (asprintf(&ondisk_path, "%s/%s",
4229 got_worktree_get_root_path(worktree),
4230 pe->path) == -1) {
4231 error = got_error_from_errno("asprintf");
4232 goto done;
4234 if (lstat(ondisk_path, &sb) == -1) {
4235 if (errno == ENOENT) {
4236 free(ondisk_path);
4237 continue;
4239 error = got_error_from_errno2("lstat",
4240 ondisk_path);
4241 free(ondisk_path);
4242 goto done;
4244 free(ondisk_path);
4245 if (S_ISDIR(sb.st_mode)) {
4246 error = got_error_msg(GOT_ERR_BAD_PATH,
4247 "adding directories requires -R option");
4248 goto done;
4253 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4254 NULL, repo, no_ignores);
4255 done:
4256 if (repo)
4257 got_repo_close(repo);
4258 if (worktree)
4259 got_worktree_close(worktree);
4260 TAILQ_FOREACH(pe, &paths, entry)
4261 free((char *)pe->path);
4262 got_pathlist_free(&paths);
4263 free(cwd);
4264 return error;
4267 __dead static void
4268 usage_remove(void)
4270 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4271 getprogname());
4272 exit(1);
4275 static const struct got_error *
4276 print_remove_status(void *arg, unsigned char status,
4277 unsigned char staged_status, const char *path)
4279 while (path[0] == '/')
4280 path++;
4281 if (status == GOT_STATUS_NONEXISTENT)
4282 return NULL;
4283 if (status == staged_status && (status == GOT_STATUS_DELETE))
4284 status = GOT_STATUS_NO_CHANGE;
4285 printf("%c%c %s\n", status, staged_status, path);
4286 return NULL;
4289 static const struct got_error *
4290 cmd_remove(int argc, char *argv[])
4292 const struct got_error *error = NULL;
4293 struct got_worktree *worktree = NULL;
4294 struct got_repository *repo = NULL;
4295 char *cwd = NULL;
4296 struct got_pathlist_head paths;
4297 struct got_pathlist_entry *pe;
4298 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4300 TAILQ_INIT(&paths);
4302 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4303 switch (ch) {
4304 case 'f':
4305 delete_local_mods = 1;
4306 break;
4307 case 'k':
4308 keep_on_disk = 1;
4309 break;
4310 case 'R':
4311 can_recurse = 1;
4312 break;
4313 default:
4314 usage_remove();
4315 /* NOTREACHED */
4319 argc -= optind;
4320 argv += optind;
4322 #ifndef PROFILE
4323 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4324 NULL) == -1)
4325 err(1, "pledge");
4326 #endif
4327 if (argc < 1)
4328 usage_remove();
4330 cwd = getcwd(NULL, 0);
4331 if (cwd == NULL) {
4332 error = got_error_from_errno("getcwd");
4333 goto done;
4335 error = got_worktree_open(&worktree, cwd);
4336 if (error)
4337 goto done;
4339 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4340 NULL);
4341 if (error)
4342 goto done;
4344 error = apply_unveil(got_repo_get_path(repo), 1,
4345 got_worktree_get_root_path(worktree));
4346 if (error)
4347 goto done;
4349 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4350 if (error)
4351 goto done;
4353 if (!can_recurse) {
4354 char *ondisk_path;
4355 struct stat sb;
4356 TAILQ_FOREACH(pe, &paths, entry) {
4357 if (asprintf(&ondisk_path, "%s/%s",
4358 got_worktree_get_root_path(worktree),
4359 pe->path) == -1) {
4360 error = got_error_from_errno("asprintf");
4361 goto done;
4363 if (lstat(ondisk_path, &sb) == -1) {
4364 if (errno == ENOENT) {
4365 free(ondisk_path);
4366 continue;
4368 error = got_error_from_errno2("lstat",
4369 ondisk_path);
4370 free(ondisk_path);
4371 goto done;
4373 free(ondisk_path);
4374 if (S_ISDIR(sb.st_mode)) {
4375 error = got_error_msg(GOT_ERR_BAD_PATH,
4376 "removing directories requires -R option");
4377 goto done;
4382 error = got_worktree_schedule_delete(worktree, &paths,
4383 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4384 done:
4385 if (repo)
4386 got_repo_close(repo);
4387 if (worktree)
4388 got_worktree_close(worktree);
4389 TAILQ_FOREACH(pe, &paths, entry)
4390 free((char *)pe->path);
4391 got_pathlist_free(&paths);
4392 free(cwd);
4393 return error;
4396 __dead static void
4397 usage_revert(void)
4399 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4400 "path ...\n", getprogname());
4401 exit(1);
4404 static const struct got_error *
4405 revert_progress(void *arg, unsigned char status, const char *path)
4407 if (status == GOT_STATUS_UNVERSIONED)
4408 return NULL;
4410 while (path[0] == '/')
4411 path++;
4412 printf("%c %s\n", status, path);
4413 return NULL;
4416 struct choose_patch_arg {
4417 FILE *patch_script_file;
4418 const char *action;
4421 static const struct got_error *
4422 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4423 int nchanges, const char *action)
4425 char *line = NULL;
4426 size_t linesize = 0;
4427 ssize_t linelen;
4429 switch (status) {
4430 case GOT_STATUS_ADD:
4431 printf("A %s\n%s this addition? [y/n] ", path, action);
4432 break;
4433 case GOT_STATUS_DELETE:
4434 printf("D %s\n%s this deletion? [y/n] ", path, action);
4435 break;
4436 case GOT_STATUS_MODIFY:
4437 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4438 return got_error_from_errno("fseek");
4439 printf(GOT_COMMIT_SEP_STR);
4440 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4441 printf("%s", line);
4442 if (ferror(patch_file))
4443 return got_error_from_errno("getline");
4444 printf(GOT_COMMIT_SEP_STR);
4445 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4446 path, n, nchanges, action);
4447 break;
4448 default:
4449 return got_error_path(path, GOT_ERR_FILE_STATUS);
4452 return NULL;
4455 static const struct got_error *
4456 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4457 FILE *patch_file, int n, int nchanges)
4459 const struct got_error *err = NULL;
4460 char *line = NULL;
4461 size_t linesize = 0;
4462 ssize_t linelen;
4463 int resp = ' ';
4464 struct choose_patch_arg *a = arg;
4466 *choice = GOT_PATCH_CHOICE_NONE;
4468 if (a->patch_script_file) {
4469 char *nl;
4470 err = show_change(status, path, patch_file, n, nchanges,
4471 a->action);
4472 if (err)
4473 return err;
4474 linelen = getline(&line, &linesize, a->patch_script_file);
4475 if (linelen == -1) {
4476 if (ferror(a->patch_script_file))
4477 return got_error_from_errno("getline");
4478 return NULL;
4480 nl = strchr(line, '\n');
4481 if (nl)
4482 *nl = '\0';
4483 if (strcmp(line, "y") == 0) {
4484 *choice = GOT_PATCH_CHOICE_YES;
4485 printf("y\n");
4486 } else if (strcmp(line, "n") == 0) {
4487 *choice = GOT_PATCH_CHOICE_NO;
4488 printf("n\n");
4489 } else if (strcmp(line, "q") == 0 &&
4490 status == GOT_STATUS_MODIFY) {
4491 *choice = GOT_PATCH_CHOICE_QUIT;
4492 printf("q\n");
4493 } else
4494 printf("invalid response '%s'\n", line);
4495 free(line);
4496 return NULL;
4499 while (resp != 'y' && resp != 'n' && resp != 'q') {
4500 err = show_change(status, path, patch_file, n, nchanges,
4501 a->action);
4502 if (err)
4503 return err;
4504 resp = getchar();
4505 if (resp == '\n')
4506 resp = getchar();
4507 if (status == GOT_STATUS_MODIFY) {
4508 if (resp != 'y' && resp != 'n' && resp != 'q') {
4509 printf("invalid response '%c'\n", resp);
4510 resp = ' ';
4512 } else if (resp != 'y' && resp != 'n') {
4513 printf("invalid response '%c'\n", resp);
4514 resp = ' ';
4518 if (resp == 'y')
4519 *choice = GOT_PATCH_CHOICE_YES;
4520 else if (resp == 'n')
4521 *choice = GOT_PATCH_CHOICE_NO;
4522 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4523 *choice = GOT_PATCH_CHOICE_QUIT;
4525 return NULL;
4529 static const struct got_error *
4530 cmd_revert(int argc, char *argv[])
4532 const struct got_error *error = NULL;
4533 struct got_worktree *worktree = NULL;
4534 struct got_repository *repo = NULL;
4535 char *cwd = NULL, *path = NULL;
4536 struct got_pathlist_head paths;
4537 struct got_pathlist_entry *pe;
4538 int ch, can_recurse = 0, pflag = 0;
4539 FILE *patch_script_file = NULL;
4540 const char *patch_script_path = NULL;
4541 struct choose_patch_arg cpa;
4543 TAILQ_INIT(&paths);
4545 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4546 switch (ch) {
4547 case 'p':
4548 pflag = 1;
4549 break;
4550 case 'F':
4551 patch_script_path = optarg;
4552 break;
4553 case 'R':
4554 can_recurse = 1;
4555 break;
4556 default:
4557 usage_revert();
4558 /* NOTREACHED */
4562 argc -= optind;
4563 argv += optind;
4565 #ifndef PROFILE
4566 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4567 "unveil", NULL) == -1)
4568 err(1, "pledge");
4569 #endif
4570 if (argc < 1)
4571 usage_revert();
4572 if (patch_script_path && !pflag)
4573 errx(1, "-F option can only be used together with -p option");
4575 cwd = getcwd(NULL, 0);
4576 if (cwd == NULL) {
4577 error = got_error_from_errno("getcwd");
4578 goto done;
4580 error = got_worktree_open(&worktree, cwd);
4581 if (error)
4582 goto done;
4584 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4585 NULL);
4586 if (error != NULL)
4587 goto done;
4589 if (patch_script_path) {
4590 patch_script_file = fopen(patch_script_path, "r");
4591 if (patch_script_file == NULL) {
4592 error = got_error_from_errno2("fopen",
4593 patch_script_path);
4594 goto done;
4597 error = apply_unveil(got_repo_get_path(repo), 1,
4598 got_worktree_get_root_path(worktree));
4599 if (error)
4600 goto done;
4602 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4603 if (error)
4604 goto done;
4606 if (!can_recurse) {
4607 char *ondisk_path;
4608 struct stat sb;
4609 TAILQ_FOREACH(pe, &paths, entry) {
4610 if (asprintf(&ondisk_path, "%s/%s",
4611 got_worktree_get_root_path(worktree),
4612 pe->path) == -1) {
4613 error = got_error_from_errno("asprintf");
4614 goto done;
4616 if (lstat(ondisk_path, &sb) == -1) {
4617 if (errno == ENOENT) {
4618 free(ondisk_path);
4619 continue;
4621 error = got_error_from_errno2("lstat",
4622 ondisk_path);
4623 free(ondisk_path);
4624 goto done;
4626 free(ondisk_path);
4627 if (S_ISDIR(sb.st_mode)) {
4628 error = got_error_msg(GOT_ERR_BAD_PATH,
4629 "reverting directories requires -R option");
4630 goto done;
4635 cpa.patch_script_file = patch_script_file;
4636 cpa.action = "revert";
4637 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4638 pflag ? choose_patch : NULL, &cpa, repo);
4639 done:
4640 if (patch_script_file && fclose(patch_script_file) == EOF &&
4641 error == NULL)
4642 error = got_error_from_errno2("fclose", patch_script_path);
4643 if (repo)
4644 got_repo_close(repo);
4645 if (worktree)
4646 got_worktree_close(worktree);
4647 free(path);
4648 free(cwd);
4649 return error;
4652 __dead static void
4653 usage_commit(void)
4655 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4656 getprogname());
4657 exit(1);
4660 struct collect_commit_logmsg_arg {
4661 const char *cmdline_log;
4662 const char *editor;
4663 const char *worktree_path;
4664 const char *branch_name;
4665 const char *repo_path;
4666 char *logmsg_path;
4670 static const struct got_error *
4671 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4672 void *arg)
4674 char *initial_content = NULL;
4675 struct got_pathlist_entry *pe;
4676 const struct got_error *err = NULL;
4677 char *template = NULL;
4678 struct collect_commit_logmsg_arg *a = arg;
4679 int fd;
4680 size_t len;
4682 /* if a message was specified on the command line, just use it */
4683 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4684 len = strlen(a->cmdline_log) + 1;
4685 *logmsg = malloc(len + 1);
4686 if (*logmsg == NULL)
4687 return got_error_from_errno("malloc");
4688 strlcpy(*logmsg, a->cmdline_log, len);
4689 return NULL;
4692 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4693 return got_error_from_errno("asprintf");
4695 if (asprintf(&initial_content,
4696 "\n# changes to be committed on branch %s:\n",
4697 a->branch_name) == -1)
4698 return got_error_from_errno("asprintf");
4700 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4701 if (err)
4702 goto done;
4704 dprintf(fd, initial_content);
4706 TAILQ_FOREACH(pe, commitable_paths, entry) {
4707 struct got_commitable *ct = pe->data;
4708 dprintf(fd, "# %c %s\n",
4709 got_commitable_get_status(ct),
4710 got_commitable_get_path(ct));
4712 close(fd);
4714 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4715 done:
4716 free(initial_content);
4717 free(template);
4719 /* Editor is done; we can now apply unveil(2) */
4720 if (err == NULL) {
4721 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4722 if (err) {
4723 free(*logmsg);
4724 *logmsg = NULL;
4727 return err;
4730 static const struct got_error *
4731 cmd_commit(int argc, char *argv[])
4733 const struct got_error *error = NULL;
4734 struct got_worktree *worktree = NULL;
4735 struct got_repository *repo = NULL;
4736 char *cwd = NULL, *id_str = NULL;
4737 struct got_object_id *id = NULL;
4738 const char *logmsg = NULL;
4739 struct collect_commit_logmsg_arg cl_arg;
4740 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4741 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4742 struct got_pathlist_head paths;
4744 TAILQ_INIT(&paths);
4745 cl_arg.logmsg_path = NULL;
4747 while ((ch = getopt(argc, argv, "m:")) != -1) {
4748 switch (ch) {
4749 case 'm':
4750 logmsg = optarg;
4751 break;
4752 default:
4753 usage_commit();
4754 /* NOTREACHED */
4758 argc -= optind;
4759 argv += optind;
4761 #ifndef PROFILE
4762 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4763 "unveil", NULL) == -1)
4764 err(1, "pledge");
4765 #endif
4766 cwd = getcwd(NULL, 0);
4767 if (cwd == NULL) {
4768 error = got_error_from_errno("getcwd");
4769 goto done;
4771 error = got_worktree_open(&worktree, cwd);
4772 if (error)
4773 goto done;
4775 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4776 if (error)
4777 goto done;
4778 if (rebase_in_progress) {
4779 error = got_error(GOT_ERR_REBASING);
4780 goto done;
4783 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4784 worktree);
4785 if (error)
4786 goto done;
4788 error = get_gitconfig_path(&gitconfig_path);
4789 if (error)
4790 goto done;
4791 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4792 gitconfig_path);
4793 if (error != NULL)
4794 goto done;
4796 error = get_author(&author, repo);
4797 if (error)
4798 return error;
4801 * unveil(2) traverses exec(2); if an editor is used we have
4802 * to apply unveil after the log message has been written.
4804 if (logmsg == NULL || strlen(logmsg) == 0)
4805 error = get_editor(&editor);
4806 else
4807 error = apply_unveil(got_repo_get_path(repo), 0,
4808 got_worktree_get_root_path(worktree));
4809 if (error)
4810 goto done;
4812 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4813 if (error)
4814 goto done;
4816 cl_arg.editor = editor;
4817 cl_arg.cmdline_log = logmsg;
4818 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4819 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4820 if (!histedit_in_progress) {
4821 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4822 error = got_error(GOT_ERR_COMMIT_BRANCH);
4823 goto done;
4825 cl_arg.branch_name += 11;
4827 cl_arg.repo_path = got_repo_get_path(repo);
4828 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4829 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4830 if (error) {
4831 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4832 cl_arg.logmsg_path != NULL)
4833 preserve_logmsg = 1;
4834 goto done;
4837 error = got_object_id_str(&id_str, id);
4838 if (error)
4839 goto done;
4840 printf("Created commit %s\n", id_str);
4841 done:
4842 if (preserve_logmsg) {
4843 fprintf(stderr, "%s: log message preserved in %s\n",
4844 getprogname(), cl_arg.logmsg_path);
4845 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4846 error == NULL)
4847 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4848 free(cl_arg.logmsg_path);
4849 if (repo)
4850 got_repo_close(repo);
4851 if (worktree)
4852 got_worktree_close(worktree);
4853 free(cwd);
4854 free(id_str);
4855 free(gitconfig_path);
4856 free(editor);
4857 free(author);
4858 return error;
4861 __dead static void
4862 usage_cherrypick(void)
4864 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4865 exit(1);
4868 static const struct got_error *
4869 cmd_cherrypick(int argc, char *argv[])
4871 const struct got_error *error = NULL;
4872 struct got_worktree *worktree = NULL;
4873 struct got_repository *repo = NULL;
4874 char *cwd = NULL, *commit_id_str = NULL;
4875 struct got_object_id *commit_id = NULL;
4876 struct got_commit_object *commit = NULL;
4877 struct got_object_qid *pid;
4878 struct got_reference *head_ref = NULL;
4879 int ch, did_something = 0;
4881 while ((ch = getopt(argc, argv, "")) != -1) {
4882 switch (ch) {
4883 default:
4884 usage_cherrypick();
4885 /* NOTREACHED */
4889 argc -= optind;
4890 argv += optind;
4892 #ifndef PROFILE
4893 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4894 "unveil", NULL) == -1)
4895 err(1, "pledge");
4896 #endif
4897 if (argc != 1)
4898 usage_cherrypick();
4900 cwd = getcwd(NULL, 0);
4901 if (cwd == NULL) {
4902 error = got_error_from_errno("getcwd");
4903 goto done;
4905 error = got_worktree_open(&worktree, cwd);
4906 if (error)
4907 goto done;
4909 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4910 NULL);
4911 if (error != NULL)
4912 goto done;
4914 error = apply_unveil(got_repo_get_path(repo), 0,
4915 got_worktree_get_root_path(worktree));
4916 if (error)
4917 goto done;
4919 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4920 GOT_OBJ_TYPE_COMMIT, repo);
4921 if (error != NULL) {
4922 struct got_reference *ref;
4923 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4924 goto done;
4925 error = got_ref_open(&ref, repo, argv[0], 0);
4926 if (error != NULL)
4927 goto done;
4928 error = got_ref_resolve(&commit_id, repo, ref);
4929 got_ref_close(ref);
4930 if (error != NULL)
4931 goto done;
4933 error = got_object_id_str(&commit_id_str, commit_id);
4934 if (error)
4935 goto done;
4937 error = got_ref_open(&head_ref, repo,
4938 got_worktree_get_head_ref_name(worktree), 0);
4939 if (error != NULL)
4940 goto done;
4942 error = check_same_branch(commit_id, head_ref, NULL, repo);
4943 if (error) {
4944 if (error->code != GOT_ERR_ANCESTRY)
4945 goto done;
4946 error = NULL;
4947 } else {
4948 error = got_error(GOT_ERR_SAME_BRANCH);
4949 goto done;
4952 error = got_object_open_as_commit(&commit, repo, commit_id);
4953 if (error)
4954 goto done;
4955 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4956 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4957 commit_id, repo, update_progress, &did_something, check_cancelled,
4958 NULL);
4959 if (error != NULL)
4960 goto done;
4962 if (did_something)
4963 printf("Merged commit %s\n", commit_id_str);
4964 done:
4965 if (commit)
4966 got_object_commit_close(commit);
4967 free(commit_id_str);
4968 if (head_ref)
4969 got_ref_close(head_ref);
4970 if (worktree)
4971 got_worktree_close(worktree);
4972 if (repo)
4973 got_repo_close(repo);
4974 return error;
4977 __dead static void
4978 usage_backout(void)
4980 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4981 exit(1);
4984 static const struct got_error *
4985 cmd_backout(int argc, char *argv[])
4987 const struct got_error *error = NULL;
4988 struct got_worktree *worktree = NULL;
4989 struct got_repository *repo = NULL;
4990 char *cwd = NULL, *commit_id_str = NULL;
4991 struct got_object_id *commit_id = NULL;
4992 struct got_commit_object *commit = NULL;
4993 struct got_object_qid *pid;
4994 struct got_reference *head_ref = NULL;
4995 int ch, did_something = 0;
4997 while ((ch = getopt(argc, argv, "")) != -1) {
4998 switch (ch) {
4999 default:
5000 usage_backout();
5001 /* NOTREACHED */
5005 argc -= optind;
5006 argv += optind;
5008 #ifndef PROFILE
5009 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5010 "unveil", NULL) == -1)
5011 err(1, "pledge");
5012 #endif
5013 if (argc != 1)
5014 usage_backout();
5016 cwd = getcwd(NULL, 0);
5017 if (cwd == NULL) {
5018 error = got_error_from_errno("getcwd");
5019 goto done;
5021 error = got_worktree_open(&worktree, cwd);
5022 if (error)
5023 goto done;
5025 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5026 NULL);
5027 if (error != NULL)
5028 goto done;
5030 error = apply_unveil(got_repo_get_path(repo), 0,
5031 got_worktree_get_root_path(worktree));
5032 if (error)
5033 goto done;
5035 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5036 GOT_OBJ_TYPE_COMMIT, repo);
5037 if (error != NULL) {
5038 struct got_reference *ref;
5039 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5040 goto done;
5041 error = got_ref_open(&ref, repo, argv[0], 0);
5042 if (error != NULL)
5043 goto done;
5044 error = got_ref_resolve(&commit_id, repo, ref);
5045 got_ref_close(ref);
5046 if (error != NULL)
5047 goto done;
5049 error = got_object_id_str(&commit_id_str, commit_id);
5050 if (error)
5051 goto done;
5053 error = got_ref_open(&head_ref, repo,
5054 got_worktree_get_head_ref_name(worktree), 0);
5055 if (error != NULL)
5056 goto done;
5058 error = check_same_branch(commit_id, head_ref, NULL, repo);
5059 if (error)
5060 goto done;
5062 error = got_object_open_as_commit(&commit, repo, commit_id);
5063 if (error)
5064 goto done;
5065 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5066 if (pid == NULL) {
5067 error = got_error(GOT_ERR_ROOT_COMMIT);
5068 goto done;
5071 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5072 update_progress, &did_something, check_cancelled, NULL);
5073 if (error != NULL)
5074 goto done;
5076 if (did_something)
5077 printf("Backed out commit %s\n", commit_id_str);
5078 done:
5079 if (commit)
5080 got_object_commit_close(commit);
5081 free(commit_id_str);
5082 if (head_ref)
5083 got_ref_close(head_ref);
5084 if (worktree)
5085 got_worktree_close(worktree);
5086 if (repo)
5087 got_repo_close(repo);
5088 return error;
5091 __dead static void
5092 usage_rebase(void)
5094 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5095 getprogname());
5096 exit(1);
5099 void
5100 trim_logmsg(char *logmsg, int limit)
5102 char *nl;
5103 size_t len;
5105 len = strlen(logmsg);
5106 if (len > limit)
5107 len = limit;
5108 logmsg[len] = '\0';
5109 nl = strchr(logmsg, '\n');
5110 if (nl)
5111 *nl = '\0';
5114 static const struct got_error *
5115 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5117 const struct got_error *err;
5118 char *logmsg0 = NULL;
5119 const char *s;
5121 err = got_object_commit_get_logmsg(&logmsg0, commit);
5122 if (err)
5123 return err;
5125 s = logmsg0;
5126 while (isspace((unsigned char)s[0]))
5127 s++;
5129 *logmsg = strdup(s);
5130 if (*logmsg == NULL) {
5131 err = got_error_from_errno("strdup");
5132 goto done;
5135 trim_logmsg(*logmsg, limit);
5136 done:
5137 free(logmsg0);
5138 return err;
5141 static const struct got_error *
5142 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5144 const struct got_error *err;
5145 struct got_commit_object *commit = NULL;
5146 char *id_str = NULL, *logmsg = NULL;
5148 err = got_object_open_as_commit(&commit, repo, id);
5149 if (err)
5150 return err;
5152 err = got_object_id_str(&id_str, id);
5153 if (err)
5154 goto done;
5156 id_str[12] = '\0';
5158 err = get_short_logmsg(&logmsg, 42, commit);
5159 if (err)
5160 goto done;
5162 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5163 done:
5164 free(id_str);
5165 got_object_commit_close(commit);
5166 free(logmsg);
5167 return err;
5170 static const struct got_error *
5171 show_rebase_progress(struct got_commit_object *commit,
5172 struct got_object_id *old_id, struct got_object_id *new_id)
5174 const struct got_error *err;
5175 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5177 err = got_object_id_str(&old_id_str, old_id);
5178 if (err)
5179 goto done;
5181 if (new_id) {
5182 err = got_object_id_str(&new_id_str, new_id);
5183 if (err)
5184 goto done;
5187 old_id_str[12] = '\0';
5188 if (new_id_str)
5189 new_id_str[12] = '\0';
5191 err = get_short_logmsg(&logmsg, 42, commit);
5192 if (err)
5193 goto done;
5195 printf("%s -> %s: %s\n", old_id_str,
5196 new_id_str ? new_id_str : "no-op change", logmsg);
5197 done:
5198 free(old_id_str);
5199 free(new_id_str);
5200 free(logmsg);
5201 return err;
5204 static const struct got_error *
5205 rebase_progress(void *arg, unsigned char status, const char *path)
5207 unsigned char *rebase_status = arg;
5209 while (path[0] == '/')
5210 path++;
5211 printf("%c %s\n", status, path);
5213 if (*rebase_status == GOT_STATUS_CONFLICT)
5214 return NULL;
5215 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5216 *rebase_status = status;
5217 return NULL;
5220 static const struct got_error *
5221 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5222 struct got_reference *branch, struct got_reference *new_base_branch,
5223 struct got_reference *tmp_branch, struct got_repository *repo)
5225 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5226 return got_worktree_rebase_complete(worktree, fileindex,
5227 new_base_branch, tmp_branch, branch, repo);
5230 static const struct got_error *
5231 rebase_commit(struct got_pathlist_head *merged_paths,
5232 struct got_worktree *worktree, struct got_fileindex *fileindex,
5233 struct got_reference *tmp_branch,
5234 struct got_object_id *commit_id, struct got_repository *repo)
5236 const struct got_error *error;
5237 struct got_commit_object *commit;
5238 struct got_object_id *new_commit_id;
5240 error = got_object_open_as_commit(&commit, repo, commit_id);
5241 if (error)
5242 return error;
5244 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5245 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5246 if (error) {
5247 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5248 goto done;
5249 error = show_rebase_progress(commit, commit_id, NULL);
5250 } else {
5251 error = show_rebase_progress(commit, commit_id, new_commit_id);
5252 free(new_commit_id);
5254 done:
5255 got_object_commit_close(commit);
5256 return error;
5259 struct check_path_prefix_arg {
5260 const char *path_prefix;
5261 size_t len;
5262 int errcode;
5265 static const struct got_error *
5266 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5267 struct got_blob_object *blob2, struct got_object_id *id1,
5268 struct got_object_id *id2, const char *path1, const char *path2,
5269 mode_t mode1, mode_t mode2, struct got_repository *repo)
5271 struct check_path_prefix_arg *a = arg;
5273 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5274 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5275 return got_error(a->errcode);
5277 return NULL;
5280 static const struct got_error *
5281 check_path_prefix(struct got_object_id *parent_id,
5282 struct got_object_id *commit_id, const char *path_prefix,
5283 int errcode, struct got_repository *repo)
5285 const struct got_error *err;
5286 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5287 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5288 struct check_path_prefix_arg cpp_arg;
5290 if (got_path_is_root_dir(path_prefix))
5291 return NULL;
5293 err = got_object_open_as_commit(&commit, repo, commit_id);
5294 if (err)
5295 goto done;
5297 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5298 if (err)
5299 goto done;
5301 err = got_object_open_as_tree(&tree1, repo,
5302 got_object_commit_get_tree_id(parent_commit));
5303 if (err)
5304 goto done;
5306 err = got_object_open_as_tree(&tree2, repo,
5307 got_object_commit_get_tree_id(commit));
5308 if (err)
5309 goto done;
5311 cpp_arg.path_prefix = path_prefix;
5312 while (cpp_arg.path_prefix[0] == '/')
5313 cpp_arg.path_prefix++;
5314 cpp_arg.len = strlen(cpp_arg.path_prefix);
5315 cpp_arg.errcode = errcode;
5316 err = got_diff_tree(tree1, tree2, "", "", repo,
5317 check_path_prefix_in_diff, &cpp_arg, 0);
5318 done:
5319 if (tree1)
5320 got_object_tree_close(tree1);
5321 if (tree2)
5322 got_object_tree_close(tree2);
5323 if (commit)
5324 got_object_commit_close(commit);
5325 if (parent_commit)
5326 got_object_commit_close(parent_commit);
5327 return err;
5330 static const struct got_error *
5331 collect_commits(struct got_object_id_queue *commits,
5332 struct got_object_id *initial_commit_id,
5333 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5334 const char *path_prefix, int path_prefix_errcode,
5335 struct got_repository *repo)
5337 const struct got_error *err = NULL;
5338 struct got_commit_graph *graph = NULL;
5339 struct got_object_id *parent_id = NULL;
5340 struct got_object_qid *qid;
5341 struct got_object_id *commit_id = initial_commit_id;
5343 err = got_commit_graph_open(&graph, "/", 1);
5344 if (err)
5345 return err;
5347 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5348 check_cancelled, NULL);
5349 if (err)
5350 goto done;
5351 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5352 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5353 check_cancelled, NULL);
5354 if (err) {
5355 if (err->code == GOT_ERR_ITER_COMPLETED) {
5356 err = got_error_msg(GOT_ERR_ANCESTRY,
5357 "ran out of commits to rebase before "
5358 "youngest common ancestor commit has "
5359 "been reached?!?");
5361 goto done;
5362 } else {
5363 err = check_path_prefix(parent_id, commit_id,
5364 path_prefix, path_prefix_errcode, repo);
5365 if (err)
5366 goto done;
5368 err = got_object_qid_alloc(&qid, commit_id);
5369 if (err)
5370 goto done;
5371 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5372 commit_id = parent_id;
5375 done:
5376 got_commit_graph_close(graph);
5377 return err;
5380 static const struct got_error *
5381 cmd_rebase(int argc, char *argv[])
5383 const struct got_error *error = NULL;
5384 struct got_worktree *worktree = NULL;
5385 struct got_repository *repo = NULL;
5386 struct got_fileindex *fileindex = NULL;
5387 char *cwd = NULL;
5388 struct got_reference *branch = NULL;
5389 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5390 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5391 struct got_object_id *resume_commit_id = NULL;
5392 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5393 struct got_commit_object *commit = NULL;
5394 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5395 int histedit_in_progress = 0;
5396 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5397 struct got_object_id_queue commits;
5398 struct got_pathlist_head merged_paths;
5399 const struct got_object_id_queue *parent_ids;
5400 struct got_object_qid *qid, *pid;
5402 SIMPLEQ_INIT(&commits);
5403 TAILQ_INIT(&merged_paths);
5405 while ((ch = getopt(argc, argv, "ac")) != -1) {
5406 switch (ch) {
5407 case 'a':
5408 abort_rebase = 1;
5409 break;
5410 case 'c':
5411 continue_rebase = 1;
5412 break;
5413 default:
5414 usage_rebase();
5415 /* NOTREACHED */
5419 argc -= optind;
5420 argv += optind;
5422 #ifndef PROFILE
5423 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5424 "unveil", NULL) == -1)
5425 err(1, "pledge");
5426 #endif
5427 if (abort_rebase && continue_rebase)
5428 usage_rebase();
5429 else if (abort_rebase || continue_rebase) {
5430 if (argc != 0)
5431 usage_rebase();
5432 } else if (argc != 1)
5433 usage_rebase();
5435 cwd = getcwd(NULL, 0);
5436 if (cwd == NULL) {
5437 error = got_error_from_errno("getcwd");
5438 goto done;
5440 error = got_worktree_open(&worktree, cwd);
5441 if (error)
5442 goto done;
5444 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5445 NULL);
5446 if (error != NULL)
5447 goto done;
5449 error = apply_unveil(got_repo_get_path(repo), 0,
5450 got_worktree_get_root_path(worktree));
5451 if (error)
5452 goto done;
5454 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5455 worktree);
5456 if (error)
5457 goto done;
5458 if (histedit_in_progress) {
5459 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5460 goto done;
5463 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5464 if (error)
5465 goto done;
5467 if (abort_rebase) {
5468 int did_something;
5469 if (!rebase_in_progress) {
5470 error = got_error(GOT_ERR_NOT_REBASING);
5471 goto done;
5473 error = got_worktree_rebase_continue(&resume_commit_id,
5474 &new_base_branch, &tmp_branch, &branch, &fileindex,
5475 worktree, repo);
5476 if (error)
5477 goto done;
5478 printf("Switching work tree to %s\n",
5479 got_ref_get_symref_target(new_base_branch));
5480 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5481 new_base_branch, update_progress, &did_something);
5482 if (error)
5483 goto done;
5484 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5485 goto done; /* nothing else to do */
5488 if (continue_rebase) {
5489 if (!rebase_in_progress) {
5490 error = got_error(GOT_ERR_NOT_REBASING);
5491 goto done;
5493 error = got_worktree_rebase_continue(&resume_commit_id,
5494 &new_base_branch, &tmp_branch, &branch, &fileindex,
5495 worktree, repo);
5496 if (error)
5497 goto done;
5499 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5500 resume_commit_id, repo);
5501 if (error)
5502 goto done;
5504 yca_id = got_object_id_dup(resume_commit_id);
5505 if (yca_id == NULL) {
5506 error = got_error_from_errno("got_object_id_dup");
5507 goto done;
5509 } else {
5510 error = got_ref_open(&branch, repo, argv[0], 0);
5511 if (error != NULL)
5512 goto done;
5515 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5516 if (error)
5517 goto done;
5519 if (!continue_rebase) {
5520 struct got_object_id *base_commit_id;
5522 base_commit_id = got_worktree_get_base_commit_id(worktree);
5523 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5524 base_commit_id, branch_head_commit_id, repo,
5525 check_cancelled, NULL);
5526 if (error)
5527 goto done;
5528 if (yca_id == NULL) {
5529 error = got_error_msg(GOT_ERR_ANCESTRY,
5530 "specified branch shares no common ancestry "
5531 "with work tree's branch");
5532 goto done;
5535 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5536 if (error) {
5537 if (error->code != GOT_ERR_ANCESTRY)
5538 goto done;
5539 error = NULL;
5540 } else {
5541 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5542 "specified branch resolves to a commit which "
5543 "is already contained in work tree's branch");
5544 goto done;
5546 error = got_worktree_rebase_prepare(&new_base_branch,
5547 &tmp_branch, &fileindex, worktree, branch, repo);
5548 if (error)
5549 goto done;
5552 commit_id = branch_head_commit_id;
5553 error = got_object_open_as_commit(&commit, repo, commit_id);
5554 if (error)
5555 goto done;
5557 parent_ids = got_object_commit_get_parent_ids(commit);
5558 pid = SIMPLEQ_FIRST(parent_ids);
5559 if (pid == NULL) {
5560 if (!continue_rebase) {
5561 int did_something;
5562 error = got_worktree_rebase_abort(worktree, fileindex,
5563 repo, new_base_branch, update_progress,
5564 &did_something);
5565 if (error)
5566 goto done;
5567 printf("Rebase of %s aborted\n",
5568 got_ref_get_name(branch));
5570 error = got_error(GOT_ERR_EMPTY_REBASE);
5571 goto done;
5573 error = collect_commits(&commits, commit_id, pid->id,
5574 yca_id, got_worktree_get_path_prefix(worktree),
5575 GOT_ERR_REBASE_PATH, repo);
5576 got_object_commit_close(commit);
5577 commit = NULL;
5578 if (error)
5579 goto done;
5581 if (SIMPLEQ_EMPTY(&commits)) {
5582 if (continue_rebase) {
5583 error = rebase_complete(worktree, fileindex,
5584 branch, new_base_branch, tmp_branch, repo);
5585 goto done;
5586 } else {
5587 /* Fast-forward the reference of the branch. */
5588 struct got_object_id *new_head_commit_id;
5589 char *id_str;
5590 error = got_ref_resolve(&new_head_commit_id, repo,
5591 new_base_branch);
5592 if (error)
5593 goto done;
5594 error = got_object_id_str(&id_str, new_head_commit_id);
5595 printf("Forwarding %s to commit %s\n",
5596 got_ref_get_name(branch), id_str);
5597 free(id_str);
5598 error = got_ref_change_ref(branch,
5599 new_head_commit_id);
5600 if (error)
5601 goto done;
5605 pid = NULL;
5606 SIMPLEQ_FOREACH(qid, &commits, entry) {
5607 commit_id = qid->id;
5608 parent_id = pid ? pid->id : yca_id;
5609 pid = qid;
5611 error = got_worktree_rebase_merge_files(&merged_paths,
5612 worktree, fileindex, parent_id, commit_id, repo,
5613 rebase_progress, &rebase_status, check_cancelled, NULL);
5614 if (error)
5615 goto done;
5617 if (rebase_status == GOT_STATUS_CONFLICT) {
5618 error = show_rebase_merge_conflict(qid->id, repo);
5619 if (error)
5620 goto done;
5621 got_worktree_rebase_pathlist_free(&merged_paths);
5622 break;
5625 error = rebase_commit(&merged_paths, worktree, fileindex,
5626 tmp_branch, commit_id, repo);
5627 got_worktree_rebase_pathlist_free(&merged_paths);
5628 if (error)
5629 goto done;
5632 if (rebase_status == GOT_STATUS_CONFLICT) {
5633 error = got_worktree_rebase_postpone(worktree, fileindex);
5634 if (error)
5635 goto done;
5636 error = got_error_msg(GOT_ERR_CONFLICTS,
5637 "conflicts must be resolved before rebasing can continue");
5638 } else
5639 error = rebase_complete(worktree, fileindex, branch,
5640 new_base_branch, tmp_branch, repo);
5641 done:
5642 got_object_id_queue_free(&commits);
5643 free(branch_head_commit_id);
5644 free(resume_commit_id);
5645 free(yca_id);
5646 if (commit)
5647 got_object_commit_close(commit);
5648 if (branch)
5649 got_ref_close(branch);
5650 if (new_base_branch)
5651 got_ref_close(new_base_branch);
5652 if (tmp_branch)
5653 got_ref_close(tmp_branch);
5654 if (worktree)
5655 got_worktree_close(worktree);
5656 if (repo)
5657 got_repo_close(repo);
5658 return error;
5661 __dead static void
5662 usage_histedit(void)
5664 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5665 getprogname());
5666 exit(1);
5669 #define GOT_HISTEDIT_PICK 'p'
5670 #define GOT_HISTEDIT_EDIT 'e'
5671 #define GOT_HISTEDIT_FOLD 'f'
5672 #define GOT_HISTEDIT_DROP 'd'
5673 #define GOT_HISTEDIT_MESG 'm'
5675 static struct got_histedit_cmd {
5676 unsigned char code;
5677 const char *name;
5678 const char *desc;
5679 } got_histedit_cmds[] = {
5680 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5681 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5682 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5683 "be used" },
5684 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5685 { GOT_HISTEDIT_MESG, "mesg",
5686 "single-line log message for commit above (open editor if empty)" },
5689 struct got_histedit_list_entry {
5690 TAILQ_ENTRY(got_histedit_list_entry) entry;
5691 struct got_object_id *commit_id;
5692 const struct got_histedit_cmd *cmd;
5693 char *logmsg;
5695 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5697 static const struct got_error *
5698 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5699 FILE *f, struct got_repository *repo)
5701 const struct got_error *err = NULL;
5702 char *logmsg = NULL, *id_str = NULL;
5703 struct got_commit_object *commit = NULL;
5704 int n;
5706 err = got_object_open_as_commit(&commit, repo, commit_id);
5707 if (err)
5708 goto done;
5710 err = get_short_logmsg(&logmsg, 34, commit);
5711 if (err)
5712 goto done;
5714 err = got_object_id_str(&id_str, commit_id);
5715 if (err)
5716 goto done;
5718 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5719 if (n < 0)
5720 err = got_ferror(f, GOT_ERR_IO);
5721 done:
5722 if (commit)
5723 got_object_commit_close(commit);
5724 free(id_str);
5725 free(logmsg);
5726 return err;
5729 static const struct got_error *
5730 histedit_write_commit_list(struct got_object_id_queue *commits,
5731 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5733 const struct got_error *err = NULL;
5734 struct got_object_qid *qid;
5736 if (SIMPLEQ_EMPTY(commits))
5737 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5739 SIMPLEQ_FOREACH(qid, commits, entry) {
5740 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5741 f, repo);
5742 if (err)
5743 break;
5744 if (edit_logmsg_only) {
5745 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5746 if (n < 0) {
5747 err = got_ferror(f, GOT_ERR_IO);
5748 break;
5753 return err;
5756 static const struct got_error *
5757 write_cmd_list(FILE *f, const char *branch_name,
5758 struct got_object_id_queue *commits)
5760 const struct got_error *err = NULL;
5761 int n, i;
5762 char *id_str;
5763 struct got_object_qid *qid;
5765 qid = SIMPLEQ_FIRST(commits);
5766 err = got_object_id_str(&id_str, qid->id);
5767 if (err)
5768 return err;
5770 n = fprintf(f,
5771 "# Editing the history of branch '%s' starting at\n"
5772 "# commit %s\n"
5773 "# Commits will be processed in order from top to "
5774 "bottom of this file.\n", branch_name, id_str);
5775 if (n < 0) {
5776 err = got_ferror(f, GOT_ERR_IO);
5777 goto done;
5780 n = fprintf(f, "# Available histedit commands:\n");
5781 if (n < 0) {
5782 err = got_ferror(f, GOT_ERR_IO);
5783 goto done;
5786 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5787 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5788 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5789 cmd->desc);
5790 if (n < 0) {
5791 err = got_ferror(f, GOT_ERR_IO);
5792 break;
5795 done:
5796 free(id_str);
5797 return err;
5800 static const struct got_error *
5801 histedit_syntax_error(int lineno)
5803 static char msg[42];
5804 int ret;
5806 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5807 lineno);
5808 if (ret == -1 || ret >= sizeof(msg))
5809 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5811 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5814 static const struct got_error *
5815 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5816 char *logmsg, struct got_repository *repo)
5818 const struct got_error *err;
5819 struct got_commit_object *folded_commit = NULL;
5820 char *id_str, *folded_logmsg = NULL;
5822 err = got_object_id_str(&id_str, hle->commit_id);
5823 if (err)
5824 return err;
5826 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5827 if (err)
5828 goto done;
5830 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5831 if (err)
5832 goto done;
5833 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5834 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5835 folded_logmsg) == -1) {
5836 err = got_error_from_errno("asprintf");
5838 done:
5839 if (folded_commit)
5840 got_object_commit_close(folded_commit);
5841 free(id_str);
5842 free(folded_logmsg);
5843 return err;
5846 static struct got_histedit_list_entry *
5847 get_folded_commits(struct got_histedit_list_entry *hle)
5849 struct got_histedit_list_entry *prev, *folded = NULL;
5851 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5852 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5853 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5854 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5855 folded = prev;
5856 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5859 return folded;
5862 static const struct got_error *
5863 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5864 struct got_repository *repo)
5866 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5867 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5868 const struct got_error *err = NULL;
5869 struct got_commit_object *commit = NULL;
5870 int fd;
5871 struct got_histedit_list_entry *folded = NULL;
5873 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5874 if (err)
5875 return err;
5877 folded = get_folded_commits(hle);
5878 if (folded) {
5879 while (folded != hle) {
5880 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5881 folded = TAILQ_NEXT(folded, entry);
5882 continue;
5884 err = append_folded_commit_msg(&new_msg, folded,
5885 logmsg, repo);
5886 if (err)
5887 goto done;
5888 free(logmsg);
5889 logmsg = new_msg;
5890 folded = TAILQ_NEXT(folded, entry);
5894 err = got_object_id_str(&id_str, hle->commit_id);
5895 if (err)
5896 goto done;
5897 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5898 if (err)
5899 goto done;
5900 if (asprintf(&new_msg,
5901 "%s\n# original log message of commit %s: %s",
5902 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5903 err = got_error_from_errno("asprintf");
5904 goto done;
5906 free(logmsg);
5907 logmsg = new_msg;
5909 err = got_object_id_str(&id_str, hle->commit_id);
5910 if (err)
5911 goto done;
5913 err = got_opentemp_named_fd(&logmsg_path, &fd,
5914 GOT_TMPDIR_STR "/got-logmsg");
5915 if (err)
5916 goto done;
5918 dprintf(fd, logmsg);
5919 close(fd);
5921 err = get_editor(&editor);
5922 if (err)
5923 goto done;
5925 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5926 if (err) {
5927 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5928 goto done;
5929 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5931 done:
5932 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5933 err = got_error_from_errno2("unlink", logmsg_path);
5934 free(logmsg_path);
5935 free(logmsg);
5936 free(orig_logmsg);
5937 free(editor);
5938 if (commit)
5939 got_object_commit_close(commit);
5940 return err;
5943 static const struct got_error *
5944 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5945 FILE *f, struct got_repository *repo)
5947 const struct got_error *err = NULL;
5948 char *line = NULL, *p, *end;
5949 size_t size;
5950 ssize_t len;
5951 int lineno = 0, i;
5952 const struct got_histedit_cmd *cmd;
5953 struct got_object_id *commit_id = NULL;
5954 struct got_histedit_list_entry *hle = NULL;
5956 for (;;) {
5957 len = getline(&line, &size, f);
5958 if (len == -1) {
5959 const struct got_error *getline_err;
5960 if (feof(f))
5961 break;
5962 getline_err = got_error_from_errno("getline");
5963 err = got_ferror(f, getline_err->code);
5964 break;
5966 lineno++;
5967 p = line;
5968 while (isspace((unsigned char)p[0]))
5969 p++;
5970 if (p[0] == '#' || p[0] == '\0') {
5971 free(line);
5972 line = NULL;
5973 continue;
5975 cmd = NULL;
5976 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5977 cmd = &got_histedit_cmds[i];
5978 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5979 isspace((unsigned char)p[strlen(cmd->name)])) {
5980 p += strlen(cmd->name);
5981 break;
5983 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5984 p++;
5985 break;
5988 if (i == nitems(got_histedit_cmds)) {
5989 err = histedit_syntax_error(lineno);
5990 break;
5992 while (isspace((unsigned char)p[0]))
5993 p++;
5994 if (cmd->code == GOT_HISTEDIT_MESG) {
5995 if (hle == NULL || hle->logmsg != NULL) {
5996 err = got_error(GOT_ERR_HISTEDIT_CMD);
5997 break;
5999 if (p[0] == '\0') {
6000 err = histedit_edit_logmsg(hle, repo);
6001 if (err)
6002 break;
6003 } else {
6004 hle->logmsg = strdup(p);
6005 if (hle->logmsg == NULL) {
6006 err = got_error_from_errno("strdup");
6007 break;
6010 free(line);
6011 line = NULL;
6012 continue;
6013 } else {
6014 end = p;
6015 while (end[0] && !isspace((unsigned char)end[0]))
6016 end++;
6017 *end = '\0';
6019 err = got_object_resolve_id_str(&commit_id, repo, p);
6020 if (err) {
6021 /* override error code */
6022 err = histedit_syntax_error(lineno);
6023 break;
6026 hle = malloc(sizeof(*hle));
6027 if (hle == NULL) {
6028 err = got_error_from_errno("malloc");
6029 break;
6031 hle->cmd = cmd;
6032 hle->commit_id = commit_id;
6033 hle->logmsg = NULL;
6034 commit_id = NULL;
6035 free(line);
6036 line = NULL;
6037 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6040 free(line);
6041 free(commit_id);
6042 return err;
6045 static const struct got_error *
6046 histedit_check_script(struct got_histedit_list *histedit_cmds,
6047 struct got_object_id_queue *commits, struct got_repository *repo)
6049 const struct got_error *err = NULL;
6050 struct got_object_qid *qid;
6051 struct got_histedit_list_entry *hle;
6052 static char msg[80];
6053 char *id_str;
6055 if (TAILQ_EMPTY(histedit_cmds))
6056 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6057 "histedit script contains no commands");
6058 if (SIMPLEQ_EMPTY(commits))
6059 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6061 SIMPLEQ_FOREACH(qid, commits, entry) {
6062 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6063 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6064 break;
6066 if (hle == NULL) {
6067 err = got_object_id_str(&id_str, qid->id);
6068 if (err)
6069 return err;
6070 snprintf(msg, sizeof(msg),
6071 "commit %s missing from histedit script", id_str);
6072 free(id_str);
6073 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6077 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6078 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6079 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6080 "last commit in histedit script cannot be folded");
6082 return NULL;
6085 static const struct got_error *
6086 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6087 const char *path, struct got_object_id_queue *commits,
6088 struct got_repository *repo)
6090 const struct got_error *err = NULL;
6091 char *editor;
6092 FILE *f = NULL;
6094 err = get_editor(&editor);
6095 if (err)
6096 return err;
6098 if (spawn_editor(editor, path) == -1) {
6099 err = got_error_from_errno("failed spawning editor");
6100 goto done;
6103 f = fopen(path, "r");
6104 if (f == NULL) {
6105 err = got_error_from_errno("fopen");
6106 goto done;
6108 err = histedit_parse_list(histedit_cmds, f, repo);
6109 if (err)
6110 goto done;
6112 err = histedit_check_script(histedit_cmds, commits, repo);
6113 done:
6114 if (f && fclose(f) != 0 && err == NULL)
6115 err = got_error_from_errno("fclose");
6116 free(editor);
6117 return err;
6120 static const struct got_error *
6121 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6122 struct got_object_id_queue *, const char *, const char *,
6123 struct got_repository *);
6125 static const struct got_error *
6126 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6127 struct got_object_id_queue *commits, const char *branch_name,
6128 int edit_logmsg_only, struct got_repository *repo)
6130 const struct got_error *err;
6131 FILE *f = NULL;
6132 char *path = NULL;
6134 err = got_opentemp_named(&path, &f, "got-histedit");
6135 if (err)
6136 return err;
6138 err = write_cmd_list(f, branch_name, commits);
6139 if (err)
6140 goto done;
6142 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6143 if (err)
6144 goto done;
6146 if (edit_logmsg_only) {
6147 rewind(f);
6148 err = histedit_parse_list(histedit_cmds, f, repo);
6149 } else {
6150 if (fclose(f) != 0) {
6151 err = got_error_from_errno("fclose");
6152 goto done;
6154 f = NULL;
6155 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6156 if (err) {
6157 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6158 err->code != GOT_ERR_HISTEDIT_CMD)
6159 goto done;
6160 err = histedit_edit_list_retry(histedit_cmds, err,
6161 commits, path, branch_name, repo);
6164 done:
6165 if (f && fclose(f) != 0 && err == NULL)
6166 err = got_error_from_errno("fclose");
6167 if (path && unlink(path) != 0 && err == NULL)
6168 err = got_error_from_errno2("unlink", path);
6169 free(path);
6170 return err;
6173 static const struct got_error *
6174 histedit_save_list(struct got_histedit_list *histedit_cmds,
6175 struct got_worktree *worktree, struct got_repository *repo)
6177 const struct got_error *err = NULL;
6178 char *path = NULL;
6179 FILE *f = NULL;
6180 struct got_histedit_list_entry *hle;
6181 struct got_commit_object *commit = NULL;
6183 err = got_worktree_get_histedit_script_path(&path, worktree);
6184 if (err)
6185 return err;
6187 f = fopen(path, "w");
6188 if (f == NULL) {
6189 err = got_error_from_errno2("fopen", path);
6190 goto done;
6192 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6193 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6194 repo);
6195 if (err)
6196 break;
6198 if (hle->logmsg) {
6199 int n = fprintf(f, "%c %s\n",
6200 GOT_HISTEDIT_MESG, hle->logmsg);
6201 if (n < 0) {
6202 err = got_ferror(f, GOT_ERR_IO);
6203 break;
6207 done:
6208 if (f && fclose(f) != 0 && err == NULL)
6209 err = got_error_from_errno("fclose");
6210 free(path);
6211 if (commit)
6212 got_object_commit_close(commit);
6213 return err;
6216 void
6217 histedit_free_list(struct got_histedit_list *histedit_cmds)
6219 struct got_histedit_list_entry *hle;
6221 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6222 TAILQ_REMOVE(histedit_cmds, hle, entry);
6223 free(hle);
6227 static const struct got_error *
6228 histedit_load_list(struct got_histedit_list *histedit_cmds,
6229 const char *path, struct got_repository *repo)
6231 const struct got_error *err = NULL;
6232 FILE *f = NULL;
6234 f = fopen(path, "r");
6235 if (f == NULL) {
6236 err = got_error_from_errno2("fopen", path);
6237 goto done;
6240 err = histedit_parse_list(histedit_cmds, f, repo);
6241 done:
6242 if (f && fclose(f) != 0 && err == NULL)
6243 err = got_error_from_errno("fclose");
6244 return err;
6247 static const struct got_error *
6248 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6249 const struct got_error *edit_err, struct got_object_id_queue *commits,
6250 const char *path, const char *branch_name, struct got_repository *repo)
6252 const struct got_error *err = NULL, *prev_err = edit_err;
6253 int resp = ' ';
6255 while (resp != 'c' && resp != 'r' && resp != 'a') {
6256 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6257 "or (a)bort: ", getprogname(), prev_err->msg);
6258 resp = getchar();
6259 if (resp == '\n')
6260 resp = getchar();
6261 if (resp == 'c') {
6262 histedit_free_list(histedit_cmds);
6263 err = histedit_run_editor(histedit_cmds, path, commits,
6264 repo);
6265 if (err) {
6266 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6267 err->code != GOT_ERR_HISTEDIT_CMD)
6268 break;
6269 prev_err = err;
6270 resp = ' ';
6271 continue;
6273 break;
6274 } else if (resp == 'r') {
6275 histedit_free_list(histedit_cmds);
6276 err = histedit_edit_script(histedit_cmds,
6277 commits, branch_name, 0, repo);
6278 if (err) {
6279 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6280 err->code != GOT_ERR_HISTEDIT_CMD)
6281 break;
6282 prev_err = err;
6283 resp = ' ';
6284 continue;
6286 break;
6287 } else if (resp == 'a') {
6288 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6289 break;
6290 } else
6291 printf("invalid response '%c'\n", resp);
6294 return err;
6297 static const struct got_error *
6298 histedit_complete(struct got_worktree *worktree,
6299 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6300 struct got_reference *branch, struct got_repository *repo)
6302 printf("Switching work tree to %s\n",
6303 got_ref_get_symref_target(branch));
6304 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6305 branch, repo);
6308 static const struct got_error *
6309 show_histedit_progress(struct got_commit_object *commit,
6310 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6312 const struct got_error *err;
6313 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6315 err = got_object_id_str(&old_id_str, hle->commit_id);
6316 if (err)
6317 goto done;
6319 if (new_id) {
6320 err = got_object_id_str(&new_id_str, new_id);
6321 if (err)
6322 goto done;
6325 old_id_str[12] = '\0';
6326 if (new_id_str)
6327 new_id_str[12] = '\0';
6329 if (hle->logmsg) {
6330 logmsg = strdup(hle->logmsg);
6331 if (logmsg == NULL) {
6332 err = got_error_from_errno("strdup");
6333 goto done;
6335 trim_logmsg(logmsg, 42);
6336 } else {
6337 err = get_short_logmsg(&logmsg, 42, commit);
6338 if (err)
6339 goto done;
6342 switch (hle->cmd->code) {
6343 case GOT_HISTEDIT_PICK:
6344 case GOT_HISTEDIT_EDIT:
6345 printf("%s -> %s: %s\n", old_id_str,
6346 new_id_str ? new_id_str : "no-op change", logmsg);
6347 break;
6348 case GOT_HISTEDIT_DROP:
6349 case GOT_HISTEDIT_FOLD:
6350 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6351 logmsg);
6352 break;
6353 default:
6354 break;
6356 done:
6357 free(old_id_str);
6358 free(new_id_str);
6359 return err;
6362 static const struct got_error *
6363 histedit_commit(struct got_pathlist_head *merged_paths,
6364 struct got_worktree *worktree, struct got_fileindex *fileindex,
6365 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6366 struct got_repository *repo)
6368 const struct got_error *err;
6369 struct got_commit_object *commit;
6370 struct got_object_id *new_commit_id;
6372 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6373 && hle->logmsg == NULL) {
6374 err = histedit_edit_logmsg(hle, repo);
6375 if (err)
6376 return err;
6379 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6380 if (err)
6381 return err;
6383 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6384 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6385 hle->logmsg, repo);
6386 if (err) {
6387 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6388 goto done;
6389 err = show_histedit_progress(commit, hle, NULL);
6390 } else {
6391 err = show_histedit_progress(commit, hle, new_commit_id);
6392 free(new_commit_id);
6394 done:
6395 got_object_commit_close(commit);
6396 return err;
6399 static const struct got_error *
6400 histedit_skip_commit(struct got_histedit_list_entry *hle,
6401 struct got_worktree *worktree, struct got_repository *repo)
6403 const struct got_error *error;
6404 struct got_commit_object *commit;
6406 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6407 repo);
6408 if (error)
6409 return error;
6411 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6412 if (error)
6413 return error;
6415 error = show_histedit_progress(commit, hle, NULL);
6416 got_object_commit_close(commit);
6417 return error;
6420 static const struct got_error *
6421 check_local_changes(void *arg, unsigned char status,
6422 unsigned char staged_status, const char *path,
6423 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6424 struct got_object_id *commit_id, int dirfd, const char *de_name)
6426 int *have_local_changes = arg;
6428 switch (status) {
6429 case GOT_STATUS_ADD:
6430 case GOT_STATUS_DELETE:
6431 case GOT_STATUS_MODIFY:
6432 case GOT_STATUS_CONFLICT:
6433 *have_local_changes = 1;
6434 return got_error(GOT_ERR_CANCELLED);
6435 default:
6436 break;
6439 switch (staged_status) {
6440 case GOT_STATUS_ADD:
6441 case GOT_STATUS_DELETE:
6442 case GOT_STATUS_MODIFY:
6443 *have_local_changes = 1;
6444 return got_error(GOT_ERR_CANCELLED);
6445 default:
6446 break;
6449 return NULL;
6452 static const struct got_error *
6453 cmd_histedit(int argc, char *argv[])
6455 const struct got_error *error = NULL;
6456 struct got_worktree *worktree = NULL;
6457 struct got_fileindex *fileindex = NULL;
6458 struct got_repository *repo = NULL;
6459 char *cwd = NULL;
6460 struct got_reference *branch = NULL;
6461 struct got_reference *tmp_branch = NULL;
6462 struct got_object_id *resume_commit_id = NULL;
6463 struct got_object_id *base_commit_id = NULL;
6464 struct got_object_id *head_commit_id = NULL;
6465 struct got_commit_object *commit = NULL;
6466 int ch, rebase_in_progress = 0, did_something;
6467 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6468 int edit_logmsg_only = 0;
6469 const char *edit_script_path = NULL;
6470 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6471 struct got_object_id_queue commits;
6472 struct got_pathlist_head merged_paths;
6473 const struct got_object_id_queue *parent_ids;
6474 struct got_object_qid *pid;
6475 struct got_histedit_list histedit_cmds;
6476 struct got_histedit_list_entry *hle;
6478 SIMPLEQ_INIT(&commits);
6479 TAILQ_INIT(&histedit_cmds);
6480 TAILQ_INIT(&merged_paths);
6482 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6483 switch (ch) {
6484 case 'a':
6485 abort_edit = 1;
6486 break;
6487 case 'c':
6488 continue_edit = 1;
6489 break;
6490 case 'F':
6491 edit_script_path = optarg;
6492 break;
6493 case 'm':
6494 edit_logmsg_only = 1;
6495 break;
6496 default:
6497 usage_histedit();
6498 /* NOTREACHED */
6502 argc -= optind;
6503 argv += optind;
6505 #ifndef PROFILE
6506 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6507 "unveil", NULL) == -1)
6508 err(1, "pledge");
6509 #endif
6510 if (abort_edit && continue_edit)
6511 errx(1, "histedit's -a and -c options are mutually exclusive");
6512 if (edit_script_path && edit_logmsg_only)
6513 errx(1, "histedit's -F and -m options are mutually exclusive");
6514 if (abort_edit && edit_logmsg_only)
6515 errx(1, "histedit's -a and -m options are mutually exclusive");
6516 if (continue_edit && edit_logmsg_only)
6517 errx(1, "histedit's -c and -m options are mutually exclusive");
6518 if (argc != 0)
6519 usage_histedit();
6522 * This command cannot apply unveil(2) in all cases because the
6523 * user may choose to run an editor to edit the histedit script
6524 * and to edit individual commit log messages.
6525 * unveil(2) traverses exec(2); if an editor is used we have to
6526 * apply unveil after edit script and log messages have been written.
6527 * XXX TODO: Make use of unveil(2) where possible.
6530 cwd = getcwd(NULL, 0);
6531 if (cwd == NULL) {
6532 error = got_error_from_errno("getcwd");
6533 goto done;
6535 error = got_worktree_open(&worktree, cwd);
6536 if (error)
6537 goto done;
6539 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6540 NULL);
6541 if (error != NULL)
6542 goto done;
6544 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6545 if (error)
6546 goto done;
6547 if (rebase_in_progress) {
6548 error = got_error(GOT_ERR_REBASING);
6549 goto done;
6552 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6553 if (error)
6554 goto done;
6556 if (edit_in_progress && edit_logmsg_only) {
6557 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6558 "histedit operation is in progress in this "
6559 "work tree and must be continued or aborted "
6560 "before the -m option can be used");
6561 goto done;
6564 if (edit_in_progress && abort_edit) {
6565 error = got_worktree_histedit_continue(&resume_commit_id,
6566 &tmp_branch, &branch, &base_commit_id, &fileindex,
6567 worktree, repo);
6568 if (error)
6569 goto done;
6570 printf("Switching work tree to %s\n",
6571 got_ref_get_symref_target(branch));
6572 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6573 branch, base_commit_id, update_progress, &did_something);
6574 if (error)
6575 goto done;
6576 printf("Histedit of %s aborted\n",
6577 got_ref_get_symref_target(branch));
6578 goto done; /* nothing else to do */
6579 } else if (abort_edit) {
6580 error = got_error(GOT_ERR_NOT_HISTEDIT);
6581 goto done;
6584 if (continue_edit) {
6585 char *path;
6587 if (!edit_in_progress) {
6588 error = got_error(GOT_ERR_NOT_HISTEDIT);
6589 goto done;
6592 error = got_worktree_get_histedit_script_path(&path, worktree);
6593 if (error)
6594 goto done;
6596 error = histedit_load_list(&histedit_cmds, path, repo);
6597 free(path);
6598 if (error)
6599 goto done;
6601 error = got_worktree_histedit_continue(&resume_commit_id,
6602 &tmp_branch, &branch, &base_commit_id, &fileindex,
6603 worktree, repo);
6604 if (error)
6605 goto done;
6607 error = got_ref_resolve(&head_commit_id, repo, branch);
6608 if (error)
6609 goto done;
6611 error = got_object_open_as_commit(&commit, repo,
6612 head_commit_id);
6613 if (error)
6614 goto done;
6615 parent_ids = got_object_commit_get_parent_ids(commit);
6616 pid = SIMPLEQ_FIRST(parent_ids);
6617 if (pid == NULL) {
6618 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6619 goto done;
6621 error = collect_commits(&commits, head_commit_id, pid->id,
6622 base_commit_id, got_worktree_get_path_prefix(worktree),
6623 GOT_ERR_HISTEDIT_PATH, repo);
6624 got_object_commit_close(commit);
6625 commit = NULL;
6626 if (error)
6627 goto done;
6628 } else {
6629 if (edit_in_progress) {
6630 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6631 goto done;
6634 error = got_ref_open(&branch, repo,
6635 got_worktree_get_head_ref_name(worktree), 0);
6636 if (error != NULL)
6637 goto done;
6639 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6640 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6641 "will not edit commit history of a branch outside "
6642 "the \"refs/heads/\" reference namespace");
6643 goto done;
6646 error = got_ref_resolve(&head_commit_id, repo, branch);
6647 got_ref_close(branch);
6648 branch = NULL;
6649 if (error)
6650 goto done;
6652 error = got_object_open_as_commit(&commit, repo,
6653 head_commit_id);
6654 if (error)
6655 goto done;
6656 parent_ids = got_object_commit_get_parent_ids(commit);
6657 pid = SIMPLEQ_FIRST(parent_ids);
6658 if (pid == NULL) {
6659 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6660 goto done;
6662 error = collect_commits(&commits, head_commit_id, pid->id,
6663 got_worktree_get_base_commit_id(worktree),
6664 got_worktree_get_path_prefix(worktree),
6665 GOT_ERR_HISTEDIT_PATH, repo);
6666 got_object_commit_close(commit);
6667 commit = NULL;
6668 if (error)
6669 goto done;
6671 if (SIMPLEQ_EMPTY(&commits)) {
6672 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6673 goto done;
6676 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6677 &base_commit_id, &fileindex, worktree, repo);
6678 if (error)
6679 goto done;
6681 if (edit_script_path) {
6682 error = histedit_load_list(&histedit_cmds,
6683 edit_script_path, repo);
6684 if (error) {
6685 got_worktree_histedit_abort(worktree, fileindex,
6686 repo, branch, base_commit_id,
6687 update_progress, &did_something);
6688 goto done;
6690 } else {
6691 const char *branch_name;
6692 branch_name = got_ref_get_symref_target(branch);
6693 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6694 branch_name += 11;
6695 error = histedit_edit_script(&histedit_cmds, &commits,
6696 branch_name, edit_logmsg_only, repo);
6697 if (error) {
6698 got_worktree_histedit_abort(worktree, fileindex,
6699 repo, branch, base_commit_id,
6700 update_progress, &did_something);
6701 goto done;
6706 error = histedit_save_list(&histedit_cmds, worktree,
6707 repo);
6708 if (error) {
6709 got_worktree_histedit_abort(worktree, fileindex,
6710 repo, branch, base_commit_id,
6711 update_progress, &did_something);
6712 goto done;
6717 error = histedit_check_script(&histedit_cmds, &commits, repo);
6718 if (error)
6719 goto done;
6721 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6722 if (resume_commit_id) {
6723 if (got_object_id_cmp(hle->commit_id,
6724 resume_commit_id) != 0)
6725 continue;
6727 resume_commit_id = NULL;
6728 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6729 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6730 error = histedit_skip_commit(hle, worktree,
6731 repo);
6732 if (error)
6733 goto done;
6734 } else {
6735 struct got_pathlist_head paths;
6736 int have_changes = 0;
6738 TAILQ_INIT(&paths);
6739 error = got_pathlist_append(&paths, "", NULL);
6740 if (error)
6741 goto done;
6742 error = got_worktree_status(worktree, &paths,
6743 repo, check_local_changes, &have_changes,
6744 check_cancelled, NULL);
6745 got_pathlist_free(&paths);
6746 if (error) {
6747 if (error->code != GOT_ERR_CANCELLED)
6748 goto done;
6749 if (sigint_received || sigpipe_received)
6750 goto done;
6752 if (have_changes) {
6753 error = histedit_commit(NULL, worktree,
6754 fileindex, tmp_branch, hle, repo);
6755 if (error)
6756 goto done;
6757 } else {
6758 error = got_object_open_as_commit(
6759 &commit, repo, hle->commit_id);
6760 if (error)
6761 goto done;
6762 error = show_histedit_progress(commit,
6763 hle, NULL);
6764 got_object_commit_close(commit);
6765 commit = NULL;
6766 if (error)
6767 goto done;
6770 continue;
6773 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6774 error = histedit_skip_commit(hle, worktree, repo);
6775 if (error)
6776 goto done;
6777 continue;
6780 error = got_object_open_as_commit(&commit, repo,
6781 hle->commit_id);
6782 if (error)
6783 goto done;
6784 parent_ids = got_object_commit_get_parent_ids(commit);
6785 pid = SIMPLEQ_FIRST(parent_ids);
6787 error = got_worktree_histedit_merge_files(&merged_paths,
6788 worktree, fileindex, pid->id, hle->commit_id, repo,
6789 rebase_progress, &rebase_status, check_cancelled, NULL);
6790 if (error)
6791 goto done;
6792 got_object_commit_close(commit);
6793 commit = NULL;
6795 if (rebase_status == GOT_STATUS_CONFLICT) {
6796 error = show_rebase_merge_conflict(hle->commit_id,
6797 repo);
6798 if (error)
6799 goto done;
6800 got_worktree_rebase_pathlist_free(&merged_paths);
6801 break;
6804 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6805 char *id_str;
6806 error = got_object_id_str(&id_str, hle->commit_id);
6807 if (error)
6808 goto done;
6809 printf("Stopping histedit for amending commit %s\n",
6810 id_str);
6811 free(id_str);
6812 got_worktree_rebase_pathlist_free(&merged_paths);
6813 error = got_worktree_histedit_postpone(worktree,
6814 fileindex);
6815 goto done;
6818 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6819 error = histedit_skip_commit(hle, worktree, repo);
6820 if (error)
6821 goto done;
6822 continue;
6825 error = histedit_commit(&merged_paths, worktree, fileindex,
6826 tmp_branch, hle, repo);
6827 got_worktree_rebase_pathlist_free(&merged_paths);
6828 if (error)
6829 goto done;
6832 if (rebase_status == GOT_STATUS_CONFLICT) {
6833 error = got_worktree_histedit_postpone(worktree, fileindex);
6834 if (error)
6835 goto done;
6836 error = got_error_msg(GOT_ERR_CONFLICTS,
6837 "conflicts must be resolved before rebasing can continue");
6838 } else
6839 error = histedit_complete(worktree, fileindex, tmp_branch,
6840 branch, repo);
6841 done:
6842 got_object_id_queue_free(&commits);
6843 histedit_free_list(&histedit_cmds);
6844 free(head_commit_id);
6845 free(base_commit_id);
6846 free(resume_commit_id);
6847 if (commit)
6848 got_object_commit_close(commit);
6849 if (branch)
6850 got_ref_close(branch);
6851 if (tmp_branch)
6852 got_ref_close(tmp_branch);
6853 if (worktree)
6854 got_worktree_close(worktree);
6855 if (repo)
6856 got_repo_close(repo);
6857 return error;
6860 __dead static void
6861 usage_integrate(void)
6863 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6864 exit(1);
6867 static const struct got_error *
6868 cmd_integrate(int argc, char *argv[])
6870 const struct got_error *error = NULL;
6871 struct got_repository *repo = NULL;
6872 struct got_worktree *worktree = NULL;
6873 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6874 const char *branch_arg = NULL;
6875 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6876 struct got_fileindex *fileindex = NULL;
6877 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6878 int ch, did_something = 0;
6880 while ((ch = getopt(argc, argv, "")) != -1) {
6881 switch (ch) {
6882 default:
6883 usage_integrate();
6884 /* NOTREACHED */
6888 argc -= optind;
6889 argv += optind;
6891 if (argc != 1)
6892 usage_integrate();
6893 branch_arg = argv[0];
6895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6896 "unveil", NULL) == -1)
6897 err(1, "pledge");
6899 cwd = getcwd(NULL, 0);
6900 if (cwd == NULL) {
6901 error = got_error_from_errno("getcwd");
6902 goto done;
6905 error = got_worktree_open(&worktree, cwd);
6906 if (error)
6907 goto done;
6909 error = check_rebase_or_histedit_in_progress(worktree);
6910 if (error)
6911 goto done;
6913 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6914 NULL);
6915 if (error != NULL)
6916 goto done;
6918 error = apply_unveil(got_repo_get_path(repo), 0,
6919 got_worktree_get_root_path(worktree));
6920 if (error)
6921 goto done;
6923 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6924 error = got_error_from_errno("asprintf");
6925 goto done;
6928 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6929 &base_branch_ref, worktree, refname, repo);
6930 if (error)
6931 goto done;
6933 refname = strdup(got_ref_get_name(branch_ref));
6934 if (refname == NULL) {
6935 error = got_error_from_errno("strdup");
6936 got_worktree_integrate_abort(worktree, fileindex, repo,
6937 branch_ref, base_branch_ref);
6938 goto done;
6940 base_refname = strdup(got_ref_get_name(base_branch_ref));
6941 if (base_refname == NULL) {
6942 error = got_error_from_errno("strdup");
6943 got_worktree_integrate_abort(worktree, fileindex, repo,
6944 branch_ref, base_branch_ref);
6945 goto done;
6948 error = got_ref_resolve(&commit_id, repo, branch_ref);
6949 if (error)
6950 goto done;
6952 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6953 if (error)
6954 goto done;
6956 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6957 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6958 "specified branch has already been integrated");
6959 got_worktree_integrate_abort(worktree, fileindex, repo,
6960 branch_ref, base_branch_ref);
6961 goto done;
6964 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6965 if (error) {
6966 if (error->code == GOT_ERR_ANCESTRY)
6967 error = got_error(GOT_ERR_REBASE_REQUIRED);
6968 got_worktree_integrate_abort(worktree, fileindex, repo,
6969 branch_ref, base_branch_ref);
6970 goto done;
6973 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6974 branch_ref, base_branch_ref, update_progress, &did_something,
6975 check_cancelled, NULL);
6976 if (error)
6977 goto done;
6979 printf("Integrated %s into %s\n", refname, base_refname);
6980 done:
6981 if (repo)
6982 got_repo_close(repo);
6983 if (worktree)
6984 got_worktree_close(worktree);
6985 free(cwd);
6986 free(base_commit_id);
6987 free(commit_id);
6988 free(refname);
6989 free(base_refname);
6990 return error;
6993 __dead static void
6994 usage_stage(void)
6996 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6997 "[file-path ...]\n",
6998 getprogname());
6999 exit(1);
7002 static const struct got_error *
7003 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7004 const char *path, struct got_object_id *blob_id,
7005 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7006 int dirfd, const char *de_name)
7008 const struct got_error *err = NULL;
7009 char *id_str = NULL;
7011 if (staged_status != GOT_STATUS_ADD &&
7012 staged_status != GOT_STATUS_MODIFY &&
7013 staged_status != GOT_STATUS_DELETE)
7014 return NULL;
7016 if (staged_status == GOT_STATUS_ADD ||
7017 staged_status == GOT_STATUS_MODIFY)
7018 err = got_object_id_str(&id_str, staged_blob_id);
7019 else
7020 err = got_object_id_str(&id_str, blob_id);
7021 if (err)
7022 return err;
7024 printf("%s %c %s\n", id_str, staged_status, path);
7025 free(id_str);
7026 return NULL;
7029 static const struct got_error *
7030 cmd_stage(int argc, char *argv[])
7032 const struct got_error *error = NULL;
7033 struct got_repository *repo = NULL;
7034 struct got_worktree *worktree = NULL;
7035 char *cwd = NULL;
7036 struct got_pathlist_head paths;
7037 struct got_pathlist_entry *pe;
7038 int ch, list_stage = 0, pflag = 0;
7039 FILE *patch_script_file = NULL;
7040 const char *patch_script_path = NULL;
7041 struct choose_patch_arg cpa;
7043 TAILQ_INIT(&paths);
7045 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7046 switch (ch) {
7047 case 'l':
7048 list_stage = 1;
7049 break;
7050 case 'p':
7051 pflag = 1;
7052 break;
7053 case 'F':
7054 patch_script_path = optarg;
7055 break;
7056 default:
7057 usage_stage();
7058 /* NOTREACHED */
7062 argc -= optind;
7063 argv += optind;
7065 #ifndef PROFILE
7066 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7067 "unveil", NULL) == -1)
7068 err(1, "pledge");
7069 #endif
7070 if (list_stage && (pflag || patch_script_path))
7071 errx(1, "-l option cannot be used with other options");
7072 if (patch_script_path && !pflag)
7073 errx(1, "-F option can only be used together with -p option");
7075 cwd = getcwd(NULL, 0);
7076 if (cwd == NULL) {
7077 error = got_error_from_errno("getcwd");
7078 goto done;
7081 error = got_worktree_open(&worktree, cwd);
7082 if (error)
7083 goto done;
7085 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7086 NULL);
7087 if (error != NULL)
7088 goto done;
7090 if (patch_script_path) {
7091 patch_script_file = fopen(patch_script_path, "r");
7092 if (patch_script_file == NULL) {
7093 error = got_error_from_errno2("fopen",
7094 patch_script_path);
7095 goto done;
7098 error = apply_unveil(got_repo_get_path(repo), 0,
7099 got_worktree_get_root_path(worktree));
7100 if (error)
7101 goto done;
7103 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7104 if (error)
7105 goto done;
7107 if (list_stage)
7108 error = got_worktree_status(worktree, &paths, repo,
7109 print_stage, NULL, check_cancelled, NULL);
7110 else {
7111 cpa.patch_script_file = patch_script_file;
7112 cpa.action = "stage";
7113 error = got_worktree_stage(worktree, &paths,
7114 pflag ? NULL : print_status, NULL,
7115 pflag ? choose_patch : NULL, &cpa, repo);
7117 done:
7118 if (patch_script_file && fclose(patch_script_file) == EOF &&
7119 error == NULL)
7120 error = got_error_from_errno2("fclose", patch_script_path);
7121 if (repo)
7122 got_repo_close(repo);
7123 if (worktree)
7124 got_worktree_close(worktree);
7125 TAILQ_FOREACH(pe, &paths, entry)
7126 free((char *)pe->path);
7127 got_pathlist_free(&paths);
7128 free(cwd);
7129 return error;
7132 __dead static void
7133 usage_unstage(void)
7135 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7136 "[file-path ...]\n",
7137 getprogname());
7138 exit(1);
7142 static const struct got_error *
7143 cmd_unstage(int argc, char *argv[])
7145 const struct got_error *error = NULL;
7146 struct got_repository *repo = NULL;
7147 struct got_worktree *worktree = NULL;
7148 char *cwd = NULL;
7149 struct got_pathlist_head paths;
7150 struct got_pathlist_entry *pe;
7151 int ch, did_something = 0, pflag = 0;
7152 FILE *patch_script_file = NULL;
7153 const char *patch_script_path = NULL;
7154 struct choose_patch_arg cpa;
7156 TAILQ_INIT(&paths);
7158 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7159 switch (ch) {
7160 case 'p':
7161 pflag = 1;
7162 break;
7163 case 'F':
7164 patch_script_path = optarg;
7165 break;
7166 default:
7167 usage_unstage();
7168 /* NOTREACHED */
7172 argc -= optind;
7173 argv += optind;
7175 #ifndef PROFILE
7176 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7177 "unveil", NULL) == -1)
7178 err(1, "pledge");
7179 #endif
7180 if (patch_script_path && !pflag)
7181 errx(1, "-F option can only be used together with -p option");
7183 cwd = getcwd(NULL, 0);
7184 if (cwd == NULL) {
7185 error = got_error_from_errno("getcwd");
7186 goto done;
7189 error = got_worktree_open(&worktree, cwd);
7190 if (error)
7191 goto done;
7193 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7194 NULL);
7195 if (error != NULL)
7196 goto done;
7198 if (patch_script_path) {
7199 patch_script_file = fopen(patch_script_path, "r");
7200 if (patch_script_file == NULL) {
7201 error = got_error_from_errno2("fopen",
7202 patch_script_path);
7203 goto done;
7207 error = apply_unveil(got_repo_get_path(repo), 0,
7208 got_worktree_get_root_path(worktree));
7209 if (error)
7210 goto done;
7212 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7213 if (error)
7214 goto done;
7216 cpa.patch_script_file = patch_script_file;
7217 cpa.action = "unstage";
7218 error = got_worktree_unstage(worktree, &paths, update_progress,
7219 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7220 done:
7221 if (patch_script_file && fclose(patch_script_file) == EOF &&
7222 error == NULL)
7223 error = got_error_from_errno2("fclose", patch_script_path);
7224 if (repo)
7225 got_repo_close(repo);
7226 if (worktree)
7227 got_worktree_close(worktree);
7228 TAILQ_FOREACH(pe, &paths, entry)
7229 free((char *)pe->path);
7230 got_pathlist_free(&paths);
7231 free(cwd);
7232 return error;
7235 __dead static void
7236 usage_cat(void)
7238 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7239 "arg1 [arg2 ...]\n", getprogname());
7240 exit(1);
7243 static const struct got_error *
7244 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7246 const struct got_error *err;
7247 struct got_blob_object *blob;
7249 err = got_object_open_as_blob(&blob, repo, id, 8192);
7250 if (err)
7251 return err;
7253 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7254 got_object_blob_close(blob);
7255 return err;
7258 static const struct got_error *
7259 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7261 const struct got_error *err;
7262 struct got_tree_object *tree;
7263 int nentries, i;
7265 err = got_object_open_as_tree(&tree, repo, id);
7266 if (err)
7267 return err;
7269 nentries = got_object_tree_get_nentries(tree);
7270 for (i = 0; i < nentries; i++) {
7271 struct got_tree_entry *te;
7272 char *id_str;
7273 if (sigint_received || sigpipe_received)
7274 break;
7275 te = got_object_tree_get_entry(tree, i);
7276 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7277 if (err)
7278 break;
7279 fprintf(outfile, "%s %.7o %s\n", id_str,
7280 got_tree_entry_get_mode(te),
7281 got_tree_entry_get_name(te));
7282 free(id_str);
7285 got_object_tree_close(tree);
7286 return err;
7289 static const struct got_error *
7290 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7292 const struct got_error *err;
7293 struct got_commit_object *commit;
7294 const struct got_object_id_queue *parent_ids;
7295 struct got_object_qid *pid;
7296 char *id_str = NULL;
7297 const char *logmsg = NULL;
7299 err = got_object_open_as_commit(&commit, repo, id);
7300 if (err)
7301 return err;
7303 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7304 if (err)
7305 goto done;
7307 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7308 parent_ids = got_object_commit_get_parent_ids(commit);
7309 fprintf(outfile, "numparents %d\n",
7310 got_object_commit_get_nparents(commit));
7311 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7312 char *pid_str;
7313 err = got_object_id_str(&pid_str, pid->id);
7314 if (err)
7315 goto done;
7316 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7317 free(pid_str);
7319 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7320 got_object_commit_get_author(commit),
7321 got_object_commit_get_author_time(commit));
7323 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7324 got_object_commit_get_author(commit),
7325 got_object_commit_get_committer_time(commit));
7327 logmsg = got_object_commit_get_logmsg_raw(commit);
7328 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7329 fprintf(outfile, "%s", logmsg);
7330 done:
7331 free(id_str);
7332 got_object_commit_close(commit);
7333 return err;
7336 static const struct got_error *
7337 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7339 const struct got_error *err;
7340 struct got_tag_object *tag;
7341 char *id_str = NULL;
7342 const char *tagmsg = NULL;
7344 err = got_object_open_as_tag(&tag, repo, id);
7345 if (err)
7346 return err;
7348 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7349 if (err)
7350 goto done;
7352 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7354 switch (got_object_tag_get_object_type(tag)) {
7355 case GOT_OBJ_TYPE_BLOB:
7356 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7357 GOT_OBJ_LABEL_BLOB);
7358 break;
7359 case GOT_OBJ_TYPE_TREE:
7360 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7361 GOT_OBJ_LABEL_TREE);
7362 break;
7363 case GOT_OBJ_TYPE_COMMIT:
7364 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7365 GOT_OBJ_LABEL_COMMIT);
7366 break;
7367 case GOT_OBJ_TYPE_TAG:
7368 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7369 GOT_OBJ_LABEL_TAG);
7370 break;
7371 default:
7372 break;
7375 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7376 got_object_tag_get_name(tag));
7378 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7379 got_object_tag_get_tagger(tag),
7380 got_object_tag_get_tagger_time(tag));
7382 tagmsg = got_object_tag_get_message(tag);
7383 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7384 fprintf(outfile, "%s", tagmsg);
7385 done:
7386 free(id_str);
7387 got_object_tag_close(tag);
7388 return err;
7391 static const struct got_error *
7392 cmd_cat(int argc, char *argv[])
7394 const struct got_error *error;
7395 struct got_repository *repo = NULL;
7396 struct got_worktree *worktree = NULL;
7397 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7398 const char *commit_id_str = NULL;
7399 struct got_object_id *id = NULL, *commit_id = NULL;
7400 int ch, obj_type, i, force_path = 0;
7402 #ifndef PROFILE
7403 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7404 NULL) == -1)
7405 err(1, "pledge");
7406 #endif
7408 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7409 switch (ch) {
7410 case 'c':
7411 commit_id_str = optarg;
7412 break;
7413 case 'r':
7414 repo_path = realpath(optarg, NULL);
7415 if (repo_path == NULL)
7416 return got_error_from_errno2("realpath",
7417 optarg);
7418 got_path_strip_trailing_slashes(repo_path);
7419 break;
7420 case 'P':
7421 force_path = 1;
7422 break;
7423 default:
7424 usage_cat();
7425 /* NOTREACHED */
7429 argc -= optind;
7430 argv += optind;
7432 cwd = getcwd(NULL, 0);
7433 if (cwd == NULL) {
7434 error = got_error_from_errno("getcwd");
7435 goto done;
7437 error = got_worktree_open(&worktree, cwd);
7438 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7439 goto done;
7440 if (worktree) {
7441 if (repo_path == NULL) {
7442 repo_path = strdup(
7443 got_worktree_get_repo_path(worktree));
7444 if (repo_path == NULL) {
7445 error = got_error_from_errno("strdup");
7446 goto done;
7451 if (repo_path == NULL) {
7452 repo_path = getcwd(NULL, 0);
7453 if (repo_path == NULL)
7454 return got_error_from_errno("getcwd");
7457 error = got_repo_open(&repo, repo_path, NULL);
7458 free(repo_path);
7459 if (error != NULL)
7460 goto done;
7462 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7463 if (error)
7464 goto done;
7466 if (commit_id_str == NULL)
7467 commit_id_str = GOT_REF_HEAD;
7468 error = got_repo_match_object_id(&commit_id, NULL,
7469 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7470 if (error)
7471 goto done;
7473 for (i = 0; i < argc; i++) {
7474 if (force_path) {
7475 error = got_object_id_by_path(&id, repo, commit_id,
7476 argv[i]);
7477 if (error)
7478 break;
7479 } else {
7480 error = got_repo_match_object_id(&id, &label, argv[i],
7481 GOT_OBJ_TYPE_ANY, 0, repo);
7482 if (error) {
7483 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7484 error->code != GOT_ERR_NOT_REF)
7485 break;
7486 error = got_object_id_by_path(&id, repo,
7487 commit_id, argv[i]);
7488 if (error)
7489 break;
7493 error = got_object_get_type(&obj_type, repo, id);
7494 if (error)
7495 break;
7497 switch (obj_type) {
7498 case GOT_OBJ_TYPE_BLOB:
7499 error = cat_blob(id, repo, stdout);
7500 break;
7501 case GOT_OBJ_TYPE_TREE:
7502 error = cat_tree(id, repo, stdout);
7503 break;
7504 case GOT_OBJ_TYPE_COMMIT:
7505 error = cat_commit(id, repo, stdout);
7506 break;
7507 case GOT_OBJ_TYPE_TAG:
7508 error = cat_tag(id, repo, stdout);
7509 break;
7510 default:
7511 error = got_error(GOT_ERR_OBJ_TYPE);
7512 break;
7514 if (error)
7515 break;
7516 free(label);
7517 label = NULL;
7518 free(id);
7519 id = NULL;
7521 done:
7522 free(label);
7523 free(id);
7524 free(commit_id);
7525 if (worktree)
7526 got_worktree_close(worktree);
7527 if (repo) {
7528 const struct got_error *repo_error;
7529 repo_error = got_repo_close(repo);
7530 if (error == NULL)
7531 error = repo_error;
7533 return error;