2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 f42b1b34 2018-03-12 stsp #include "got_error.h"
39 f42b1b34 2018-03-12 stsp #include "got_object.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 f42b1b34 2018-03-12 stsp #include "got_repository.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 c09a553d 2018-03-12 stsp #include "got_worktree.h"
44 79109fed 2018-03-27 stsp #include "got_diff.h"
45 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
46 404c43c4 2018-06-21 stsp #include "got_blame.h"
47 63219cd2 2019-01-04 stsp #include "got_privsep.h"
48 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
50 5c860e29 2018-03-12 stsp #ifndef nitems
51 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
55 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
58 99437157 2018-11-11 stsp catch_sigint(int signo)
60 99437157 2018-11-11 stsp sigint_received = 1;
64 99437157 2018-11-11 stsp catch_sigpipe(int signo)
66 99437157 2018-11-11 stsp sigpipe_received = 1;
70 8cfb4057 2019-07-09 stsp struct got_cmd {
71 5c860e29 2018-03-12 stsp const char *cmd_name;
72 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
73 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
74 97b3a7be 2019-07-09 stsp const char *cmd_alias;
77 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
78 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
79 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
81 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
84 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
85 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
86 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
87 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
88 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
89 d00136be 2019-03-26 stsp __dead static void usage_add(void);
90 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
91 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
92 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
93 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
94 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
95 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
97 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
98 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
99 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
100 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
101 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
102 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
103 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
104 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
105 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
106 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
107 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
108 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
109 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
110 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
111 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
112 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
113 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
114 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
116 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
117 97b3a7be 2019-07-09 stsp { "init", cmd_init, usage_init, "" },
118 3ce1b845 2019-07-15 stsp { "import", cmd_import, usage_import, "" },
119 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
120 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
121 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
122 97b3a7be 2019-07-09 stsp { "diff", cmd_diff, usage_diff, "" },
123 97b3a7be 2019-07-09 stsp { "blame", cmd_blame, usage_blame, "" },
124 97b3a7be 2019-07-09 stsp { "tree", cmd_tree, usage_tree, "" },
125 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
126 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
127 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
128 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
129 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
130 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
131 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
132 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
133 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
134 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
137 ce5b7c56 2019-07-09 stsp static void
138 ce5b7c56 2019-07-09 stsp list_commands(void)
142 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
143 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
144 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
145 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
147 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
151 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
153 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
154 5c860e29 2018-03-12 stsp unsigned int i;
156 1b6b95a8 2018-03-12 stsp int hflag = 0;
158 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
160 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
161 5c860e29 2018-03-12 stsp switch (ch) {
166 ce5b7c56 2019-07-09 stsp usage(hflag);
167 5c860e29 2018-03-12 stsp /* NOTREACHED */
171 5c860e29 2018-03-12 stsp argc -= optind;
172 5c860e29 2018-03-12 stsp argv += optind;
173 1e70621d 2018-03-27 stsp optind = 0;
175 5c860e29 2018-03-12 stsp if (argc <= 0)
176 ce5b7c56 2019-07-09 stsp usage(hflag);
178 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
179 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
181 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
182 d7d4f210 2018-03-12 stsp const struct got_error *error;
184 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
186 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
187 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
191 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
193 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
194 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
195 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
202 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
203 ce5b7c56 2019-07-09 stsp list_commands();
207 4ed7e80c 2018-05-20 stsp __dead static void
208 ce5b7c56 2019-07-09 stsp usage(int hflag)
210 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
212 ce5b7c56 2019-07-09 stsp list_commands();
216 0266afb7 2019-01-04 stsp static const struct got_error *
217 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
219 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
220 e2ba3d07 2019-05-13 stsp const char *editor;
222 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
223 e2ba3d07 2019-05-13 stsp if (editor == NULL)
224 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
226 0ee7065d 2019-05-13 stsp if (editor) {
227 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
229 0ee7065d 2019-05-13 stsp return err;
232 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
233 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
234 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
235 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
238 e2ba3d07 2019-05-13 stsp return NULL;
241 e2ba3d07 2019-05-13 stsp static const struct got_error *
242 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
243 314a6357 2019-05-13 stsp const char *worktree_path, int create_worktree)
245 163ce85a 2019-05-13 stsp const struct got_error *err;
247 a0937847 2019-05-11 stsp if (create_worktree) {
248 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
249 163ce85a 2019-05-13 stsp err = got_path_mkdir(worktree_path);
251 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
252 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
254 163ce85a 2019-05-13 stsp err = NULL;
256 df056ada 2019-05-15 stsp err = got_error_path(worktree_path,
257 df056ada 2019-05-15 stsp GOT_ERR_DIR_NOT_EMPTY);
261 163ce85a 2019-05-13 stsp if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
262 163ce85a 2019-05-13 stsp return err;
265 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
266 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
268 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
269 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
271 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
272 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
274 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
275 163ce85a 2019-05-13 stsp if (err != NULL)
276 163ce85a 2019-05-13 stsp return err;
278 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
279 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
281 0266afb7 2019-01-04 stsp return NULL;
284 2c7829a4 2019-06-17 stsp __dead static void
285 2c7829a4 2019-06-17 stsp usage_init(void)
287 2c7829a4 2019-06-17 stsp fprintf(stderr, "usage: %s init path\n", getprogname());
291 2c7829a4 2019-06-17 stsp static const struct got_error *
292 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
294 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
295 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
298 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
299 2c7829a4 2019-06-17 stsp switch (ch) {
301 2c7829a4 2019-06-17 stsp usage_init();
302 2c7829a4 2019-06-17 stsp /* NOTREACHED */
306 2c7829a4 2019-06-17 stsp argc -= optind;
307 2c7829a4 2019-06-17 stsp argv += optind;
309 2c7829a4 2019-06-17 stsp #ifndef PROFILE
310 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
311 2c7829a4 2019-06-17 stsp err(1, "pledge");
313 2c7829a4 2019-06-17 stsp if (argc != 1)
314 bc20e173 2019-06-17 stsp usage_init();
316 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
317 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
318 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
320 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
322 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
323 2c7829a4 2019-06-17 stsp if (error &&
324 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
327 2c7829a4 2019-06-17 stsp error = apply_unveil(repo_path, 0, NULL, 0);
331 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
332 2c7829a4 2019-06-17 stsp if (error != NULL)
336 3ce1b845 2019-07-15 stsp free(repo_path);
337 3ce1b845 2019-07-15 stsp return error;
340 3ce1b845 2019-07-15 stsp __dead static void
341 3ce1b845 2019-07-15 stsp usage_import(void)
343 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
344 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
349 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
352 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
353 3ce1b845 2019-07-15 stsp int st = -1;
355 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
356 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
357 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
359 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
361 3ce1b845 2019-07-15 stsp goto doneediting;
363 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
364 3ce1b845 2019-07-15 stsp _exit(127);
367 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
368 3ce1b845 2019-07-15 stsp if (errno != EINTR)
371 3ce1b845 2019-07-15 stsp doneediting:
372 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
373 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
374 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
376 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
377 3ce1b845 2019-07-15 stsp errno = EINTR;
381 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
384 3ce1b845 2019-07-15 stsp static const struct got_error *
385 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
386 3ce1b845 2019-07-15 stsp const char *initial_content)
388 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
389 3ce1b845 2019-07-15 stsp char buf[1024];
390 3ce1b845 2019-07-15 stsp struct stat st, st2;
392 3ce1b845 2019-07-15 stsp int content_changed = 0;
393 3ce1b845 2019-07-15 stsp size_t len;
395 3ce1b845 2019-07-15 stsp *logmsg = NULL;
397 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
398 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
400 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
401 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
403 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
404 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
406 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
407 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
408 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
410 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
411 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
412 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
413 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
416 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
417 3ce1b845 2019-07-15 stsp if (fp == NULL) {
418 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
421 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
422 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
423 3ce1b845 2019-07-15 stsp content_changed = 1;
424 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
425 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
426 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
428 3ce1b845 2019-07-15 stsp fclose(fp);
430 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
431 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
435 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
436 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
437 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
440 3ce1b845 2019-07-15 stsp free(*logmsg);
441 3ce1b845 2019-07-15 stsp *logmsg = NULL;
443 3ce1b845 2019-07-15 stsp return err;
446 3ce1b845 2019-07-15 stsp static const struct got_error *
447 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
448 3ce1b845 2019-07-15 stsp const char *branch_name)
450 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
451 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
454 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
455 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
456 3ce1b845 2019-07-15 stsp branch_name) == -1)
457 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
459 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
463 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
466 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
468 3ce1b845 2019-07-15 stsp free(initial_content);
469 3ce1b845 2019-07-15 stsp free(logmsg_path);
470 3ce1b845 2019-07-15 stsp return err;
473 3ce1b845 2019-07-15 stsp static const struct got_error *
474 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
476 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
477 3ce1b845 2019-07-15 stsp return NULL;
480 3ce1b845 2019-07-15 stsp static const struct got_error *
481 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
483 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
484 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
485 3ce1b845 2019-07-15 stsp char *editor = NULL;
486 3ce1b845 2019-07-15 stsp const char *got_author = getenv("GOT_AUTHOR");
487 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
488 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
489 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
490 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
491 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
493 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
494 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
496 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
498 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
499 3ce1b845 2019-07-15 stsp switch (ch) {
501 3ce1b845 2019-07-15 stsp branch_name = optarg;
504 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
505 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
506 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
511 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
512 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
513 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
518 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
520 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
526 3ce1b845 2019-07-15 stsp usage_init();
527 3ce1b845 2019-07-15 stsp /* NOTREACHED */
531 3ce1b845 2019-07-15 stsp argc -= optind;
532 3ce1b845 2019-07-15 stsp argv += optind;
534 3ce1b845 2019-07-15 stsp #ifndef PROFILE
535 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
536 3ce1b845 2019-07-15 stsp NULL) == -1)
537 3ce1b845 2019-07-15 stsp err(1, "pledge");
539 3ce1b845 2019-07-15 stsp if (argc != 1)
540 3ce1b845 2019-07-15 stsp usage_import();
542 3ce1b845 2019-07-15 stsp if (got_author == NULL) {
543 3ce1b845 2019-07-15 stsp /* TODO: Look current user up in password database */
544 3ce1b845 2019-07-15 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
548 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
549 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
550 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
551 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
553 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
554 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
558 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
559 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
563 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
564 3ce1b845 2019-07-15 stsp if (error) {
565 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
568 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
569 3ce1b845 2019-07-15 stsp "import target branch already exists");
573 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
574 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
575 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
578 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
581 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
582 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
584 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
585 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
588 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
593 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
594 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
596 3ce1b845 2019-07-15 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL, 0);
600 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
601 3ce1b845 2019-07-15 stsp got_author, &ignores, repo, import_progress, NULL);
605 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
609 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
613 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
617 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
618 3ce1b845 2019-07-15 stsp if (error) {
619 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
622 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
623 3ce1b845 2019-07-15 stsp branch_ref);
627 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
632 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
633 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
635 2c7829a4 2019-06-17 stsp free(repo_path);
636 3ce1b845 2019-07-15 stsp free(editor);
637 3ce1b845 2019-07-15 stsp free(refname);
638 3ce1b845 2019-07-15 stsp free(new_commit_id);
639 3ce1b845 2019-07-15 stsp free(id_str);
640 3ce1b845 2019-07-15 stsp if (branch_ref)
641 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
642 3ce1b845 2019-07-15 stsp if (head_ref)
643 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
644 2c7829a4 2019-06-17 stsp return error;
647 4ed7e80c 2018-05-20 stsp __dead static void
648 c09a553d 2018-03-12 stsp usage_checkout(void)
650 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
651 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
655 1ee397ad 2019-07-12 stsp static const struct got_error *
656 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
658 92a684f4 2018-03-12 stsp char *worktree_path = arg;
660 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
661 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
662 1ee397ad 2019-07-12 stsp return NULL;
664 92a684f4 2018-03-12 stsp while (path[0] == '/')
667 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
668 1ee397ad 2019-07-12 stsp return NULL;
671 99437157 2018-11-11 stsp static const struct got_error *
672 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
674 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
675 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
676 99437157 2018-11-11 stsp return NULL;
679 8069f636 2019-01-12 stsp static const struct got_error *
680 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
681 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
683 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
684 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
686 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
687 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
689 36a38700 2019-05-10 stsp return err;
691 d5bea539 2019-05-13 stsp if (yca_id == NULL)
692 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
695 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
696 d5bea539 2019-05-13 stsp * and the work tree's base commit.
698 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
700 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
708 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
709 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
710 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
712 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
713 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
714 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
716 d5bea539 2019-05-13 stsp free(yca_id);
717 d5bea539 2019-05-13 stsp return NULL;
720 a367ff0f 2019-05-14 stsp static const struct got_error *
721 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
722 a367ff0f 2019-05-14 stsp struct got_reference *head_ref, struct got_repository *repo)
724 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
725 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
726 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
727 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
729 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
733 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
737 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
742 a367ff0f 2019-05-14 stsp struct got_object_id *id;
743 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
745 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
746 a367ff0f 2019-05-14 stsp err = NULL;
749 a367ff0f 2019-05-14 stsp else if (err->code != GOT_ERR_ITER_NEED_MORE)
751 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
758 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
759 a367ff0f 2019-05-14 stsp is_same_branch = 1;
766 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
767 a367ff0f 2019-05-14 stsp free(head_commit_id);
768 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
769 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
770 a367ff0f 2019-05-14 stsp return err;
773 4ed7e80c 2018-05-20 stsp static const struct got_error *
774 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
776 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
777 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
778 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
779 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
780 c09a553d 2018-03-12 stsp char *repo_path = NULL;
781 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
782 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
783 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
784 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
785 72151b04 2019-05-11 stsp int ch, same_path_prefix;
787 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
788 0bb8a95e 2018-03-12 stsp switch (ch) {
790 08573d5b 2019-05-14 stsp branch_name = optarg;
793 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
794 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
795 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
798 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
801 2deda0b9 2019-03-07 stsp usage_checkout();
802 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
806 0bb8a95e 2018-03-12 stsp argc -= optind;
807 0bb8a95e 2018-03-12 stsp argv += optind;
809 6715a751 2018-03-16 stsp #ifndef PROFILE
810 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
811 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
812 c09a553d 2018-03-12 stsp err(1, "pledge");
814 0bb8a95e 2018-03-12 stsp if (argc == 1) {
815 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
816 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
817 76089277 2018-04-01 stsp if (repo_path == NULL)
818 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
819 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
820 76089277 2018-04-01 stsp if (cwd == NULL) {
821 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
824 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
825 230a42bd 2019-05-11 jcs base = basename(path_prefix);
826 230a42bd 2019-05-11 jcs if (base == NULL) {
827 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
828 230a42bd 2019-05-11 jcs path_prefix);
832 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
833 230a42bd 2019-05-11 jcs if (base == NULL) {
834 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
839 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
840 c09a553d 2018-03-12 stsp if (dotgit)
841 c09a553d 2018-03-12 stsp *dotgit = '\0';
842 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
843 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
848 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
849 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
850 76089277 2018-04-01 stsp if (repo_path == NULL) {
851 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
854 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
855 76089277 2018-04-01 stsp if (worktree_path == NULL) {
856 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[1]);
860 c09a553d 2018-03-12 stsp usage_checkout();
862 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
863 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
865 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
866 c09a553d 2018-03-12 stsp if (error != NULL)
869 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
873 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
874 c09a553d 2018-03-12 stsp if (error != NULL)
877 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
878 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
881 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
882 c09a553d 2018-03-12 stsp if (error != NULL)
885 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
886 e5dc7198 2018-12-29 stsp path_prefix);
887 e5dc7198 2018-12-29 stsp if (error != NULL)
889 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
890 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
894 8069f636 2019-01-12 stsp if (commit_id_str) {
895 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
896 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
897 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
898 8069f636 2019-01-12 stsp if (error != NULL)
900 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
901 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
902 8069f636 2019-01-12 stsp if (error != NULL) {
903 8069f636 2019-01-12 stsp free(commit_id);
906 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
909 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
910 8069f636 2019-01-12 stsp commit_id);
911 8069f636 2019-01-12 stsp free(commit_id);
916 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
917 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
918 c09a553d 2018-03-12 stsp if (error != NULL)
921 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
924 8069f636 2019-01-12 stsp free(commit_id_str);
925 76089277 2018-04-01 stsp free(repo_path);
926 507dc3bb 2018-12-29 stsp free(worktree_path);
927 507dc3bb 2018-12-29 stsp return error;
930 507dc3bb 2018-12-29 stsp __dead static void
931 507dc3bb 2018-12-29 stsp usage_update(void)
933 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
934 507dc3bb 2018-12-29 stsp getprogname());
938 1ee397ad 2019-07-12 stsp static const struct got_error *
939 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
941 784955db 2019-01-12 stsp int *did_something = arg;
943 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
944 1ee397ad 2019-07-12 stsp return NULL;
946 1545c615 2019-02-10 stsp *did_something = 1;
948 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
949 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
950 1ee397ad 2019-07-12 stsp return NULL;
952 507dc3bb 2018-12-29 stsp while (path[0] == '/')
954 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
955 1ee397ad 2019-07-12 stsp return NULL;
958 be7061eb 2018-12-30 stsp static const struct got_error *
959 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
960 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
961 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
963 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
964 a1fb16d8 2019-05-24 stsp char *base_id_str;
965 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
967 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
968 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
969 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
970 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
971 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
972 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
973 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
976 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
977 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
979 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
980 a1fb16d8 2019-05-24 stsp return err;
981 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
983 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
984 a1fb16d8 2019-05-24 stsp return NULL;
986 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
987 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
988 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
990 a1fb16d8 2019-05-24 stsp return err;
991 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
992 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
993 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
994 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
995 a1fb16d8 2019-05-24 stsp return NULL;
998 a1fb16d8 2019-05-24 stsp static const struct got_error *
999 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1001 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1002 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1003 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1004 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
1005 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1006 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1007 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1008 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1009 7d5807f4 2019-07-11 stsp int ch, did_something = 0, rebase_in_progress;
1011 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1012 507dc3bb 2018-12-29 stsp switch (ch) {
1014 024e9686 2019-05-14 stsp branch_name = optarg;
1017 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1018 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1019 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1022 2deda0b9 2019-03-07 stsp usage_update();
1023 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1027 507dc3bb 2018-12-29 stsp argc -= optind;
1028 507dc3bb 2018-12-29 stsp argv += optind;
1030 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1031 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1032 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1033 507dc3bb 2018-12-29 stsp err(1, "pledge");
1035 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1036 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1037 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1038 c4cdcb68 2019-04-03 stsp goto done;
1040 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1041 7d5807f4 2019-07-11 stsp if (error)
1042 7d5807f4 2019-07-11 stsp goto done;
1044 7d5807f4 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1045 c4cdcb68 2019-04-03 stsp if (error)
1046 c4cdcb68 2019-04-03 stsp goto done;
1047 7d5807f4 2019-07-11 stsp if (rebase_in_progress) {
1048 7d5807f4 2019-07-11 stsp error = got_error(GOT_ERR_REBASING);
1049 7d5807f4 2019-07-11 stsp goto done;
1052 507dc3bb 2018-12-29 stsp if (argc == 0) {
1053 c4cdcb68 2019-04-03 stsp path = strdup("");
1054 c4cdcb68 2019-04-03 stsp if (path == NULL) {
1055 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1056 507dc3bb 2018-12-29 stsp goto done;
1058 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
1059 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1060 c4cdcb68 2019-04-03 stsp if (error)
1061 507dc3bb 2018-12-29 stsp goto done;
1063 507dc3bb 2018-12-29 stsp usage_update();
1065 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1066 507dc3bb 2018-12-29 stsp if (error != NULL)
1067 507dc3bb 2018-12-29 stsp goto done;
1069 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1070 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
1071 0266afb7 2019-01-04 stsp if (error)
1072 0266afb7 2019-01-04 stsp goto done;
1074 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1075 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1076 024e9686 2019-05-14 stsp if (error != NULL)
1077 024e9686 2019-05-14 stsp goto done;
1078 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1079 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1080 9c4b8182 2019-01-02 stsp if (error != NULL)
1081 9c4b8182 2019-01-02 stsp goto done;
1082 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1083 507dc3bb 2018-12-29 stsp if (error != NULL)
1084 507dc3bb 2018-12-29 stsp goto done;
1086 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1087 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1088 507dc3bb 2018-12-29 stsp if (error != NULL)
1089 a297e751 2019-07-11 stsp goto done;
1090 a297e751 2019-07-11 stsp free(commit_id_str);
1091 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1092 a297e751 2019-07-11 stsp if (error)
1093 507dc3bb 2018-12-29 stsp goto done;
1096 a1fb16d8 2019-05-24 stsp if (branch_name) {
1097 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1098 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
1099 a1fb16d8 2019-05-24 stsp fprintf(stderr, "%s: switching between branches "
1100 a1fb16d8 2019-05-24 stsp "requires that the entire work tree "
1101 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
1102 024e9686 2019-05-14 stsp getprogname(), path);
1103 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
1104 024e9686 2019-05-14 stsp goto done;
1106 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1107 024e9686 2019-05-14 stsp if (error)
1108 024e9686 2019-05-14 stsp goto done;
1109 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1110 a367ff0f 2019-05-14 stsp free(head_commit_id);
1111 024e9686 2019-05-14 stsp if (error != NULL)
1112 024e9686 2019-05-14 stsp goto done;
1113 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
1114 a367ff0f 2019-05-14 stsp if (error)
1115 a367ff0f 2019-05-14 stsp goto done;
1116 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1117 024e9686 2019-05-14 stsp if (error)
1118 024e9686 2019-05-14 stsp goto done;
1120 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1121 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1122 a367ff0f 2019-05-14 stsp if (error != NULL) {
1123 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1124 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1125 024e9686 2019-05-14 stsp goto done;
1127 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
1128 a367ff0f 2019-05-14 stsp if (error)
1129 a367ff0f 2019-05-14 stsp goto done;
1132 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1133 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1134 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1135 507dc3bb 2018-12-29 stsp commit_id);
1136 507dc3bb 2018-12-29 stsp if (error)
1137 507dc3bb 2018-12-29 stsp goto done;
1140 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
1141 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1142 507dc3bb 2018-12-29 stsp if (error != NULL)
1143 507dc3bb 2018-12-29 stsp goto done;
1145 784955db 2019-01-12 stsp if (did_something)
1146 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1148 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1150 c09a553d 2018-03-12 stsp free(worktree_path);
1151 c4cdcb68 2019-04-03 stsp free(path);
1152 507dc3bb 2018-12-29 stsp free(commit_id);
1153 9c4b8182 2019-01-02 stsp free(commit_id_str);
1154 c09a553d 2018-03-12 stsp return error;
1157 f42b1b34 2018-03-12 stsp static const struct got_error *
1158 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1159 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1161 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1162 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1163 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1164 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1165 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1167 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1168 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1170 79109fed 2018-03-27 stsp return err;
1172 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1173 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1174 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1176 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1178 79109fed 2018-03-27 stsp return err;
1180 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1181 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1182 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1184 0f2b3dca 2018-12-22 stsp return err;
1186 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1188 79109fed 2018-03-27 stsp return err;
1191 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1193 0f2b3dca 2018-12-22 stsp goto done;
1195 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1196 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1197 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1198 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1199 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff, &arg);
1201 79109fed 2018-03-27 stsp if (tree1)
1202 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1203 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1204 0f2b3dca 2018-12-22 stsp free(id_str1);
1205 0f2b3dca 2018-12-22 stsp free(id_str2);
1206 79109fed 2018-03-27 stsp return err;
1209 4bb494d5 2018-06-16 stsp static char *
1210 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1212 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
1213 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1215 6c281f94 2018-06-11 stsp *p = '\0';
1219 79109fed 2018-03-27 stsp static const struct got_error *
1220 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1221 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1222 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1224 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1225 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1226 6c281f94 2018-06-11 stsp char datebuf[26];
1227 45d799e2 2018-12-23 stsp time_t committer_time;
1228 45d799e2 2018-12-23 stsp const char *author, *committer;
1229 199a4027 2019-02-02 stsp char *refs_str = NULL;
1230 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1232 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1234 199a4027 2019-02-02 stsp const char *name;
1235 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
1237 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1238 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1240 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1241 199a4027 2019-02-02 stsp name += 5;
1242 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1244 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1245 e34f9ed6 2019-02-02 stsp name += 6;
1246 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1247 141c2bff 2019-02-04 stsp name += 8;
1248 199a4027 2019-02-02 stsp s = refs_str;
1249 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1250 199a4027 2019-02-02 stsp name) == -1) {
1251 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1257 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1259 8bf5b3c9 2018-03-17 stsp return err;
1261 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
1262 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1263 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1264 832c249c 2018-06-10 stsp free(id_str);
1265 d3d493d7 2019-02-21 stsp id_str = NULL;
1266 d3d493d7 2019-02-21 stsp free(refs_str);
1267 d3d493d7 2019-02-21 stsp refs_str = NULL;
1268 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1269 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1270 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1271 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
1272 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1273 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1274 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1275 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1276 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1277 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1278 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1279 3fe1abad 2018-06-10 stsp int n = 1;
1280 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1281 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1282 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1284 a0603db2 2018-06-10 stsp return err;
1285 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1286 a0603db2 2018-06-10 stsp free(id_str);
1290 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1291 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
1292 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1294 621015ac 2018-07-23 stsp logmsg = logmsg0;
1296 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1298 832c249c 2018-06-10 stsp printf(" %s\n", line);
1299 832c249c 2018-06-10 stsp } while (line);
1300 621015ac 2018-07-23 stsp free(logmsg0);
1302 971751ac 2018-03-27 stsp if (show_patch) {
1303 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1304 971751ac 2018-03-27 stsp if (err == 0)
1305 971751ac 2018-03-27 stsp printf("\n");
1308 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1309 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1310 07862c20 2018-09-15 stsp return err;
1313 f42b1b34 2018-03-12 stsp static const struct got_error *
1314 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1315 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1316 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1318 f42b1b34 2018-03-12 stsp const struct got_error *err;
1319 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1321 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1322 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1324 f42b1b34 2018-03-12 stsp return err;
1325 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1327 fcc85cad 2018-07-22 stsp goto done;
1329 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1330 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1332 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1335 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1336 372ccdbb 2018-06-10 stsp if (err) {
1337 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1338 9ba79e04 2018-06-11 stsp err = NULL;
1341 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1343 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1349 b43fbaa0 2018-06-11 stsp if (id == NULL)
1352 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1355 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1357 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1358 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1362 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1363 f42b1b34 2018-03-12 stsp return err;
1366 4ed7e80c 2018-05-20 stsp __dead static void
1367 6f3d1eb0 2018-03-12 stsp usage_log(void)
1369 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1370 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1374 4ed7e80c 2018-05-20 stsp static const struct got_error *
1375 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1377 f42b1b34 2018-03-12 stsp const struct got_error *error;
1378 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1379 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1380 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1381 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1382 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1383 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1384 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1385 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1386 64a96a6d 2018-04-01 stsp const char *errstr;
1387 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1389 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1391 6715a751 2018-03-16 stsp #ifndef PROFILE
1392 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1395 f42b1b34 2018-03-12 stsp err(1, "pledge");
1398 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1399 79109fed 2018-03-27 stsp switch (ch) {
1401 79109fed 2018-03-27 stsp show_patch = 1;
1404 d142fc45 2018-04-01 stsp start_commit = optarg;
1407 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1409 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1410 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1413 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1414 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1415 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1418 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1421 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1422 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1423 04ca23f4 2018-07-16 stsp err(1, "-r option");
1424 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1427 2deda0b9 2019-03-07 stsp usage_log();
1428 79109fed 2018-03-27 stsp /* NOTREACHED */
1432 79109fed 2018-03-27 stsp argc -= optind;
1433 79109fed 2018-03-27 stsp argv += optind;
1435 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1436 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1437 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1438 04ca23f4 2018-07-16 stsp goto done;
1441 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1442 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1443 cffc0aa4 2019-02-05 stsp goto done;
1444 cffc0aa4 2019-02-05 stsp error = NULL;
1446 e7301579 2019-03-18 stsp if (argc == 0) {
1447 cbd1af7a 2019-03-18 stsp path = strdup("");
1448 e7301579 2019-03-18 stsp if (path == NULL) {
1449 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1450 cbd1af7a 2019-03-18 stsp goto done;
1452 e7301579 2019-03-18 stsp } else if (argc == 1) {
1453 e7301579 2019-03-18 stsp if (worktree) {
1454 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1456 e7301579 2019-03-18 stsp if (error)
1457 e7301579 2019-03-18 stsp goto done;
1459 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1460 e7301579 2019-03-18 stsp if (path == NULL) {
1461 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1462 e7301579 2019-03-18 stsp goto done;
1466 cbd1af7a 2019-03-18 stsp usage_log();
1468 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1469 5486daa2 2019-05-11 stsp repo_path = worktree ?
1470 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1472 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1473 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1474 cffc0aa4 2019-02-05 stsp goto done;
1477 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1478 d7d4f210 2018-03-12 stsp if (error != NULL)
1479 04ca23f4 2018-07-16 stsp goto done;
1481 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1482 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1483 c02c541e 2019-03-29 stsp if (error)
1484 c02c541e 2019-03-29 stsp goto done;
1486 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1487 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1488 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1489 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1490 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1491 3235492e 2018-04-01 stsp if (error != NULL)
1492 3235492e 2018-04-01 stsp return error;
1493 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1494 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1495 3235492e 2018-04-01 stsp if (error != NULL)
1496 3235492e 2018-04-01 stsp return error;
1497 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1499 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1500 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1501 3235492e 2018-04-01 stsp if (error == NULL) {
1502 1caad61b 2019-02-01 stsp int obj_type;
1503 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1504 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1505 d1f2edc9 2018-06-13 stsp if (error != NULL)
1506 1caad61b 2019-02-01 stsp goto done;
1507 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1508 1caad61b 2019-02-01 stsp if (error != NULL)
1509 1caad61b 2019-02-01 stsp goto done;
1510 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1511 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1512 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1513 1caad61b 2019-02-01 stsp if (error != NULL)
1514 1caad61b 2019-02-01 stsp goto done;
1515 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1516 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1517 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1518 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1519 1caad61b 2019-02-01 stsp goto done;
1522 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1523 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1524 1caad61b 2019-02-01 stsp if (id == NULL)
1525 638f9024 2019-05-13 stsp error = got_error_from_errno(
1526 230a42bd 2019-05-11 jcs "got_object_id_dup");
1527 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1528 1caad61b 2019-02-01 stsp if (error)
1529 1caad61b 2019-02-01 stsp goto done;
1530 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1531 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1532 1caad61b 2019-02-01 stsp goto done;
1534 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1535 d1f2edc9 2018-06-13 stsp if (error != NULL)
1536 1caad61b 2019-02-01 stsp goto done;
1538 15a94983 2018-12-23 stsp if (commit == NULL) {
1539 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1540 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1541 d1f2edc9 2018-06-13 stsp if (error != NULL)
1542 d1f2edc9 2018-06-13 stsp return error;
1545 d7d4f210 2018-03-12 stsp if (error != NULL)
1546 04ca23f4 2018-07-16 stsp goto done;
1548 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1549 04ca23f4 2018-07-16 stsp if (error != NULL)
1550 04ca23f4 2018-07-16 stsp goto done;
1551 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1552 04ca23f4 2018-07-16 stsp free(path);
1553 04ca23f4 2018-07-16 stsp path = in_repo_path;
1556 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1557 199a4027 2019-02-02 stsp if (error)
1558 199a4027 2019-02-02 stsp goto done;
1560 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1561 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1563 04ca23f4 2018-07-16 stsp free(path);
1564 04ca23f4 2018-07-16 stsp free(repo_path);
1565 04ca23f4 2018-07-16 stsp free(cwd);
1567 cffc0aa4 2019-02-05 stsp if (worktree)
1568 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1569 ad242220 2018-09-08 stsp if (repo) {
1570 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1571 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1572 ad242220 2018-09-08 stsp if (error == NULL)
1573 ad242220 2018-09-08 stsp error = repo_error;
1575 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1576 8bf5b3c9 2018-03-17 stsp return error;
1579 4ed7e80c 2018-05-20 stsp __dead static void
1580 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1582 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1583 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1587 b72f483a 2019-02-05 stsp struct print_diff_arg {
1588 b72f483a 2019-02-05 stsp struct got_repository *repo;
1589 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1590 b72f483a 2019-02-05 stsp int diff_context;
1591 3fc0c068 2019-02-10 stsp const char *id_str;
1592 3fc0c068 2019-02-10 stsp int header_shown;
1595 4ed7e80c 2018-05-20 stsp static const struct got_error *
1596 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1597 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1599 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1600 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1601 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1602 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1603 b72f483a 2019-02-05 stsp char *abspath = NULL;
1604 b72f483a 2019-02-05 stsp struct stat sb;
1606 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1607 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1608 b72f483a 2019-02-05 stsp return NULL;
1610 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1611 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1612 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1613 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1616 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1617 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1619 d00136be 2019-03-26 stsp goto done;
1623 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1624 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1625 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1626 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1627 2ec1f75b 2019-03-26 stsp goto done;
1630 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1631 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1632 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1633 2ec1f75b 2019-03-26 stsp goto done;
1635 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1636 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1637 2ec1f75b 2019-03-26 stsp goto done;
1640 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1642 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1645 b72f483a 2019-02-05 stsp if (blob1)
1646 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1647 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1648 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1649 b72f483a 2019-02-05 stsp free(abspath);
1650 927df6b7 2019-02-10 stsp return err;
1653 927df6b7 2019-02-10 stsp static const struct got_error *
1654 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1656 b00d56cd 2018-04-01 stsp const struct got_error *error;
1657 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1658 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1659 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1660 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1661 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1662 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1663 15a94983 2018-12-23 stsp int type1, type2;
1664 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1665 c0cc5c62 2018-10-18 stsp const char *errstr;
1666 927df6b7 2019-02-10 stsp char *path = NULL;
1668 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1669 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1670 25eccc22 2019-01-04 stsp NULL) == -1)
1671 b00d56cd 2018-04-01 stsp err(1, "pledge");
1674 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1675 b00d56cd 2018-04-01 stsp switch (ch) {
1677 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1678 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1679 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1682 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1683 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1684 b72f483a 2019-02-05 stsp err(1, "-r option");
1685 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1688 2deda0b9 2019-03-07 stsp usage_diff();
1689 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1693 b00d56cd 2018-04-01 stsp argc -= optind;
1694 b00d56cd 2018-04-01 stsp argv += optind;
1696 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1697 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1698 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1699 b72f483a 2019-02-05 stsp goto done;
1701 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1702 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1703 927df6b7 2019-02-10 stsp goto done;
1704 927df6b7 2019-02-10 stsp if (argc <= 1) {
1705 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1706 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1707 927df6b7 2019-02-10 stsp goto done;
1709 b72f483a 2019-02-05 stsp if (repo_path)
1711 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1712 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1713 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1714 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1715 927df6b7 2019-02-10 stsp goto done;
1717 927df6b7 2019-02-10 stsp if (argc == 1) {
1718 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1720 927df6b7 2019-02-10 stsp if (error)
1721 927df6b7 2019-02-10 stsp goto done;
1723 927df6b7 2019-02-10 stsp path = strdup("");
1724 927df6b7 2019-02-10 stsp if (path == NULL) {
1725 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1726 927df6b7 2019-02-10 stsp goto done;
1729 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1730 15a94983 2018-12-23 stsp id_str1 = argv[0];
1731 15a94983 2018-12-23 stsp id_str2 = argv[1];
1732 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1733 30db809c 2019-06-05 stsp repo_path =
1734 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1735 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1736 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1737 30db809c 2019-06-05 stsp goto done;
1741 b00d56cd 2018-04-01 stsp usage_diff();
1743 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1744 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1745 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1746 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1749 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1750 76089277 2018-04-01 stsp free(repo_path);
1751 b00d56cd 2018-04-01 stsp if (error != NULL)
1752 b00d56cd 2018-04-01 stsp goto done;
1754 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1755 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1756 c02c541e 2019-03-29 stsp if (error)
1757 c02c541e 2019-03-29 stsp goto done;
1759 30db809c 2019-06-05 stsp if (argc <= 1) {
1760 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1761 b72f483a 2019-02-05 stsp char *id_str;
1762 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1763 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1764 b72f483a 2019-02-05 stsp if (error)
1765 b72f483a 2019-02-05 stsp goto done;
1766 b72f483a 2019-02-05 stsp arg.repo = repo;
1767 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1768 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1769 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1770 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1772 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1773 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1774 b72f483a 2019-02-05 stsp free(id_str);
1775 b72f483a 2019-02-05 stsp goto done;
1778 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1779 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1780 e02e74af 2019-05-28 stsp if (error) {
1781 e02e74af 2019-05-28 stsp struct got_reference *ref;
1782 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1783 e02e74af 2019-05-28 stsp goto done;
1784 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1785 e02e74af 2019-05-28 stsp if (error != NULL)
1786 e02e74af 2019-05-28 stsp goto done;
1787 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1788 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1789 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1790 5e70831e 2019-05-28 stsp goto done;
1792 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1793 e02e74af 2019-05-28 stsp got_ref_close(ref);
1794 e02e74af 2019-05-28 stsp if (error != NULL)
1795 5e70831e 2019-05-28 stsp goto done;
1797 a297e751 2019-07-11 stsp error = got_object_id_str(&label1, id1);
1798 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1799 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1800 e02e74af 2019-05-28 stsp goto done;
1804 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1805 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1806 e02e74af 2019-05-28 stsp if (error) {
1807 e02e74af 2019-05-28 stsp struct got_reference *ref;
1808 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1809 e02e74af 2019-05-28 stsp goto done;
1810 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1811 e02e74af 2019-05-28 stsp if (error != NULL)
1812 5e70831e 2019-05-28 stsp goto done;
1813 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1814 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1815 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1816 e02e74af 2019-05-28 stsp goto done;
1818 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1819 e02e74af 2019-05-28 stsp got_ref_close(ref);
1820 e02e74af 2019-05-28 stsp if (error != NULL)
1821 e02e74af 2019-05-28 stsp goto done;
1823 a297e751 2019-07-11 stsp error = got_object_id_str(&label2, id2);
1824 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1825 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1826 5e70831e 2019-05-28 stsp goto done;
1830 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1831 15a94983 2018-12-23 stsp if (error)
1832 15a94983 2018-12-23 stsp goto done;
1834 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1835 15a94983 2018-12-23 stsp if (error)
1836 15a94983 2018-12-23 stsp goto done;
1838 15a94983 2018-12-23 stsp if (type1 != type2) {
1839 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1840 b00d56cd 2018-04-01 stsp goto done;
1843 15a94983 2018-12-23 stsp switch (type1) {
1844 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1845 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1846 54156555 2018-12-24 stsp diff_context, repo, stdout);
1848 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1849 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1850 54156555 2018-12-24 stsp diff_context, repo, stdout);
1852 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1853 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1854 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1855 c0cc5c62 2018-10-18 stsp repo, stdout);
1858 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1862 5e70831e 2019-05-28 stsp free(label1);
1863 5e70831e 2019-05-28 stsp free(label2);
1864 15a94983 2018-12-23 stsp free(id1);
1865 15a94983 2018-12-23 stsp free(id2);
1866 927df6b7 2019-02-10 stsp free(path);
1867 b72f483a 2019-02-05 stsp if (worktree)
1868 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1869 ad242220 2018-09-08 stsp if (repo) {
1870 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1871 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1872 ad242220 2018-09-08 stsp if (error == NULL)
1873 ad242220 2018-09-08 stsp error = repo_error;
1875 b00d56cd 2018-04-01 stsp return error;
1878 404c43c4 2018-06-21 stsp __dead static void
1879 404c43c4 2018-06-21 stsp usage_blame(void)
1881 9270e621 2019-02-05 stsp fprintf(stderr,
1882 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1883 404c43c4 2018-06-21 stsp getprogname());
1887 404c43c4 2018-06-21 stsp static const struct got_error *
1888 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1890 404c43c4 2018-06-21 stsp const struct got_error *error;
1891 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1892 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1893 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1894 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1895 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1898 404c43c4 2018-06-21 stsp #ifndef PROFILE
1899 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1900 36e2fb66 2019-01-04 stsp NULL) == -1)
1901 404c43c4 2018-06-21 stsp err(1, "pledge");
1904 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1905 404c43c4 2018-06-21 stsp switch (ch) {
1907 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1910 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1911 66bea077 2018-08-02 stsp if (repo_path == NULL)
1912 66bea077 2018-08-02 stsp err(1, "-r option");
1913 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1916 2deda0b9 2019-03-07 stsp usage_blame();
1917 404c43c4 2018-06-21 stsp /* NOTREACHED */
1921 404c43c4 2018-06-21 stsp argc -= optind;
1922 404c43c4 2018-06-21 stsp argv += optind;
1924 a39318fd 2018-08-02 stsp if (argc == 1)
1925 404c43c4 2018-06-21 stsp path = argv[0];
1927 404c43c4 2018-06-21 stsp usage_blame();
1929 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1930 66bea077 2018-08-02 stsp if (cwd == NULL) {
1931 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1932 66bea077 2018-08-02 stsp goto done;
1934 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1935 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1936 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1937 66bea077 2018-08-02 stsp goto done;
1939 0c06baac 2019-02-05 stsp error = NULL;
1940 0c06baac 2019-02-05 stsp if (worktree) {
1941 0c06baac 2019-02-05 stsp repo_path =
1942 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1943 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1944 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1945 0c06baac 2019-02-05 stsp if (error)
1946 0c06baac 2019-02-05 stsp goto done;
1948 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1949 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1950 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1951 0c06baac 2019-02-05 stsp goto done;
1956 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1957 c02c541e 2019-03-29 stsp if (error != NULL)
1958 36e2fb66 2019-01-04 stsp goto done;
1960 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1961 c02c541e 2019-03-29 stsp if (error)
1962 404c43c4 2018-06-21 stsp goto done;
1964 0c06baac 2019-02-05 stsp if (worktree) {
1965 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1966 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1967 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1968 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1969 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1970 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1971 6efaaa2d 2019-02-05 stsp path) == -1) {
1972 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1973 0c06baac 2019-02-05 stsp goto done;
1975 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1978 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1980 0c06baac 2019-02-05 stsp if (error)
1981 66bea077 2018-08-02 stsp goto done;
1983 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1984 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1985 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1986 404c43c4 2018-06-21 stsp if (error != NULL)
1987 66bea077 2018-08-02 stsp goto done;
1988 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1989 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1990 404c43c4 2018-06-21 stsp if (error != NULL)
1991 66bea077 2018-08-02 stsp goto done;
1993 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1994 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1995 404c43c4 2018-06-21 stsp if (error != NULL)
1996 66bea077 2018-08-02 stsp goto done;
1999 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
2001 66bea077 2018-08-02 stsp free(in_repo_path);
2002 66bea077 2018-08-02 stsp free(repo_path);
2003 66bea077 2018-08-02 stsp free(cwd);
2004 404c43c4 2018-06-21 stsp free(commit_id);
2005 0c06baac 2019-02-05 stsp if (worktree)
2006 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2007 ad242220 2018-09-08 stsp if (repo) {
2008 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2009 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2010 ad242220 2018-09-08 stsp if (error == NULL)
2011 ad242220 2018-09-08 stsp error = repo_error;
2013 404c43c4 2018-06-21 stsp return error;
2016 5de5890b 2018-10-18 stsp __dead static void
2017 5de5890b 2018-10-18 stsp usage_tree(void)
2019 c1669e2e 2019-01-09 stsp fprintf(stderr,
2020 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2021 5de5890b 2018-10-18 stsp getprogname());
2025 c1669e2e 2019-01-09 stsp static void
2026 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2027 c1669e2e 2019-01-09 stsp const char *root_path)
2029 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2031 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2032 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2035 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2036 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
2037 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2040 5de5890b 2018-10-18 stsp static const struct got_error *
2041 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2042 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2043 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2045 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2046 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2047 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2048 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2049 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2051 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2053 5de5890b 2018-10-18 stsp goto done;
2055 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2057 5de5890b 2018-10-18 stsp goto done;
2058 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2059 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2060 5de5890b 2018-10-18 stsp while (te) {
2061 5de5890b 2018-10-18 stsp char *id = NULL;
2063 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2066 5de5890b 2018-10-18 stsp if (show_ids) {
2067 5de5890b 2018-10-18 stsp char *id_str;
2068 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2070 5de5890b 2018-10-18 stsp goto done;
2071 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2072 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2073 5de5890b 2018-10-18 stsp free(id_str);
2074 5de5890b 2018-10-18 stsp goto done;
2076 5de5890b 2018-10-18 stsp free(id_str);
2078 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2081 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2082 c1669e2e 2019-01-09 stsp char *child_path;
2083 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2084 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2085 c1669e2e 2019-01-09 stsp te->name) == -1) {
2086 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2087 c1669e2e 2019-01-09 stsp goto done;
2089 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2090 c1669e2e 2019-01-09 stsp root_path, repo);
2091 c1669e2e 2019-01-09 stsp free(child_path);
2093 c1669e2e 2019-01-09 stsp goto done;
2096 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2100 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2101 5de5890b 2018-10-18 stsp free(tree_id);
2102 5de5890b 2018-10-18 stsp return err;
2105 5de5890b 2018-10-18 stsp static const struct got_error *
2106 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2108 5de5890b 2018-10-18 stsp const struct got_error *error;
2109 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2110 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2111 7a2c19d6 2019-02-05 stsp const char *path;
2112 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2113 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2114 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2115 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2118 5de5890b 2018-10-18 stsp #ifndef PROFILE
2119 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2120 0f8d269b 2019-01-04 stsp NULL) == -1)
2121 5de5890b 2018-10-18 stsp err(1, "pledge");
2124 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2125 5de5890b 2018-10-18 stsp switch (ch) {
2127 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2130 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2131 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2132 5de5890b 2018-10-18 stsp err(1, "-r option");
2133 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2136 5de5890b 2018-10-18 stsp show_ids = 1;
2139 c1669e2e 2019-01-09 stsp recurse = 1;
2142 2deda0b9 2019-03-07 stsp usage_tree();
2143 5de5890b 2018-10-18 stsp /* NOTREACHED */
2147 5de5890b 2018-10-18 stsp argc -= optind;
2148 5de5890b 2018-10-18 stsp argv += optind;
2150 5de5890b 2018-10-18 stsp if (argc == 1)
2151 5de5890b 2018-10-18 stsp path = argv[0];
2152 5de5890b 2018-10-18 stsp else if (argc > 1)
2153 5de5890b 2018-10-18 stsp usage_tree();
2155 7a2c19d6 2019-02-05 stsp path = NULL;
2157 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2158 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2159 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2160 9bf7a39b 2019-02-05 stsp goto done;
2162 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2163 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2164 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2165 7a2c19d6 2019-02-05 stsp goto done;
2167 7a2c19d6 2019-02-05 stsp error = NULL;
2168 7a2c19d6 2019-02-05 stsp if (worktree) {
2169 7a2c19d6 2019-02-05 stsp repo_path =
2170 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2171 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2172 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2173 7a2c19d6 2019-02-05 stsp if (error)
2174 7a2c19d6 2019-02-05 stsp goto done;
2176 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2177 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2178 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2179 7a2c19d6 2019-02-05 stsp goto done;
2184 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2185 5de5890b 2018-10-18 stsp if (error != NULL)
2186 c02c541e 2019-03-29 stsp goto done;
2188 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2189 c02c541e 2019-03-29 stsp if (error)
2190 5de5890b 2018-10-18 stsp goto done;
2192 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2193 9bf7a39b 2019-02-05 stsp if (worktree) {
2194 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2195 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2196 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2197 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2198 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2199 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2200 9bf7a39b 2019-02-05 stsp goto done;
2202 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2204 9bf7a39b 2019-02-05 stsp if (error)
2205 9bf7a39b 2019-02-05 stsp goto done;
2207 9bf7a39b 2019-02-05 stsp path = "/";
2209 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2210 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2211 9bf7a39b 2019-02-05 stsp if (error != NULL)
2212 9bf7a39b 2019-02-05 stsp goto done;
2215 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2216 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2217 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2218 5de5890b 2018-10-18 stsp if (error != NULL)
2219 5de5890b 2018-10-18 stsp goto done;
2220 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2221 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2222 5de5890b 2018-10-18 stsp if (error != NULL)
2223 5de5890b 2018-10-18 stsp goto done;
2225 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
2226 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2227 5de5890b 2018-10-18 stsp if (error != NULL)
2228 5de5890b 2018-10-18 stsp goto done;
2231 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2232 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2234 5de5890b 2018-10-18 stsp free(in_repo_path);
2235 5de5890b 2018-10-18 stsp free(repo_path);
2236 5de5890b 2018-10-18 stsp free(cwd);
2237 5de5890b 2018-10-18 stsp free(commit_id);
2238 7a2c19d6 2019-02-05 stsp if (worktree)
2239 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2240 5de5890b 2018-10-18 stsp if (repo) {
2241 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2242 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2243 5de5890b 2018-10-18 stsp if (error == NULL)
2244 5de5890b 2018-10-18 stsp error = repo_error;
2246 5de5890b 2018-10-18 stsp return error;
2249 6bad629b 2019-02-04 stsp __dead static void
2250 6bad629b 2019-02-04 stsp usage_status(void)
2252 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
2256 b72f483a 2019-02-05 stsp static const struct got_error *
2257 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
2258 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2260 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
2261 b72f483a 2019-02-05 stsp return NULL;
2264 6bad629b 2019-02-04 stsp static const struct got_error *
2265 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2267 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2268 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2269 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2270 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
2273 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2274 6bad629b 2019-02-04 stsp switch (ch) {
2276 2deda0b9 2019-03-07 stsp usage_status();
2277 6bad629b 2019-02-04 stsp /* NOTREACHED */
2281 6bad629b 2019-02-04 stsp argc -= optind;
2282 6bad629b 2019-02-04 stsp argv += optind;
2284 6bad629b 2019-02-04 stsp #ifndef PROFILE
2285 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2286 6bad629b 2019-02-04 stsp NULL) == -1)
2287 6bad629b 2019-02-04 stsp err(1, "pledge");
2289 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2290 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2291 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2292 927df6b7 2019-02-10 stsp goto done;
2295 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2296 927df6b7 2019-02-10 stsp if (error != NULL)
2297 927df6b7 2019-02-10 stsp goto done;
2299 6bad629b 2019-02-04 stsp if (argc == 0) {
2300 927df6b7 2019-02-10 stsp path = strdup("");
2301 927df6b7 2019-02-10 stsp if (path == NULL) {
2302 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2303 6bad629b 2019-02-04 stsp goto done;
2305 6bad629b 2019-02-04 stsp } else if (argc == 1) {
2306 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
2307 927df6b7 2019-02-10 stsp if (error)
2308 6bad629b 2019-02-04 stsp goto done;
2310 6bad629b 2019-02-04 stsp usage_status();
2312 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2313 6bad629b 2019-02-04 stsp if (error != NULL)
2314 6bad629b 2019-02-04 stsp goto done;
2316 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2317 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2318 6bad629b 2019-02-04 stsp if (error)
2319 6bad629b 2019-02-04 stsp goto done;
2321 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
2322 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2324 927df6b7 2019-02-10 stsp free(cwd);
2325 927df6b7 2019-02-10 stsp free(path);
2326 d0eebce4 2019-03-11 stsp return error;
2329 d0eebce4 2019-03-11 stsp __dead static void
2330 d0eebce4 2019-03-11 stsp usage_ref(void)
2332 d0eebce4 2019-03-11 stsp fprintf(stderr,
2333 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2334 d0eebce4 2019-03-11 stsp getprogname());
2338 d0eebce4 2019-03-11 stsp static const struct got_error *
2339 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2341 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2342 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2343 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2345 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2346 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2348 d0eebce4 2019-03-11 stsp return err;
2350 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2351 d0eebce4 2019-03-11 stsp char *refstr;
2352 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2353 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2354 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2355 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2356 d0eebce4 2019-03-11 stsp free(refstr);
2359 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2360 d0eebce4 2019-03-11 stsp return NULL;
2363 d0eebce4 2019-03-11 stsp static const struct got_error *
2364 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2366 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2367 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2369 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2371 d0eebce4 2019-03-11 stsp return err;
2373 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2374 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2375 d0eebce4 2019-03-11 stsp return err;
2378 d0eebce4 2019-03-11 stsp static const struct got_error *
2379 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2381 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2382 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2383 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2386 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2387 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2388 d1644381 2019-07-14 stsp * an unintended typo.
2390 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2391 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2393 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2395 d83d9d5c 2019-05-13 stsp if (err) {
2396 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2398 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2399 d83d9d5c 2019-05-13 stsp return err;
2400 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2402 d83d9d5c 2019-05-13 stsp return err;
2403 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2404 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2406 d83d9d5c 2019-05-13 stsp return err;
2409 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2411 d0eebce4 2019-03-11 stsp goto done;
2413 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2416 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2418 d0eebce4 2019-03-11 stsp return err;
2421 d0eebce4 2019-03-11 stsp static const struct got_error *
2422 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2424 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2425 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2426 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2427 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2428 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2429 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2431 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2432 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2433 d0eebce4 2019-03-11 stsp switch (ch) {
2435 d0eebce4 2019-03-11 stsp delref = optarg;
2438 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2439 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2440 d0eebce4 2019-03-11 stsp err(1, "-r option");
2441 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2444 d0eebce4 2019-03-11 stsp do_list = 1;
2447 d0eebce4 2019-03-11 stsp usage_ref();
2448 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2452 d0eebce4 2019-03-11 stsp if (do_list && delref)
2453 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2455 d0eebce4 2019-03-11 stsp argc -= optind;
2456 d0eebce4 2019-03-11 stsp argv += optind;
2458 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2459 d0eebce4 2019-03-11 stsp if (argc > 0)
2460 d0eebce4 2019-03-11 stsp usage_ref();
2461 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2462 d0eebce4 2019-03-11 stsp usage_ref();
2464 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2465 e0b57350 2019-03-12 stsp if (do_list) {
2466 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2467 e0b57350 2019-03-12 stsp NULL) == -1)
2468 e0b57350 2019-03-12 stsp err(1, "pledge");
2470 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2471 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2472 e0b57350 2019-03-12 stsp err(1, "pledge");
2475 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2476 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2477 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2478 d0eebce4 2019-03-11 stsp goto done;
2481 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2482 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2483 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2484 d0eebce4 2019-03-11 stsp goto done;
2486 d0eebce4 2019-03-11 stsp error = NULL;
2487 d0eebce4 2019-03-11 stsp if (worktree) {
2488 d0eebce4 2019-03-11 stsp repo_path =
2489 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2490 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2491 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2492 d0eebce4 2019-03-11 stsp if (error)
2493 d0eebce4 2019-03-11 stsp goto done;
2495 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2496 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2497 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2498 d0eebce4 2019-03-11 stsp goto done;
2503 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2504 d0eebce4 2019-03-11 stsp if (error != NULL)
2505 d0eebce4 2019-03-11 stsp goto done;
2507 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2508 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2509 c02c541e 2019-03-29 stsp if (error)
2510 c02c541e 2019-03-29 stsp goto done;
2512 d0eebce4 2019-03-11 stsp if (do_list)
2513 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2514 d0eebce4 2019-03-11 stsp else if (delref)
2515 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2517 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2520 4e759de4 2019-06-26 stsp got_repo_close(repo);
2521 4e759de4 2019-06-26 stsp if (worktree)
2522 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2523 4e759de4 2019-06-26 stsp free(cwd);
2524 4e759de4 2019-06-26 stsp free(repo_path);
2525 4e759de4 2019-06-26 stsp return error;
2528 4e759de4 2019-06-26 stsp __dead static void
2529 4e759de4 2019-06-26 stsp usage_branch(void)
2531 4e759de4 2019-06-26 stsp fprintf(stderr,
2532 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2533 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2537 4e759de4 2019-06-26 stsp static const struct got_error *
2538 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
2540 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2541 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2542 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2544 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2546 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2548 4e759de4 2019-06-26 stsp return err;
2550 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2551 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
2552 4e759de4 2019-06-26 stsp char *refstr;
2553 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2554 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2556 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
2557 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
2558 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
2559 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
2561 ba882ee3 2019-07-11 stsp return err;
2562 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
2563 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
2564 ba882ee3 2019-07-11 stsp marker = "* ";
2566 ba882ee3 2019-07-11 stsp marker = "~ ";
2569 4e759de4 2019-06-26 stsp refname += 11;
2570 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2571 4e759de4 2019-06-26 stsp if (refstr == NULL)
2572 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2573 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
2574 4e759de4 2019-06-26 stsp free(refstr);
2577 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2578 4e759de4 2019-06-26 stsp return NULL;
2581 4e759de4 2019-06-26 stsp static const struct got_error *
2582 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2584 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2585 4e759de4 2019-06-26 stsp struct got_reference *ref;
2586 4e759de4 2019-06-26 stsp char *refname;
2588 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2589 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2591 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2593 4e759de4 2019-06-26 stsp goto done;
2595 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2596 4e759de4 2019-06-26 stsp got_ref_close(ref);
2598 4e759de4 2019-06-26 stsp free(refname);
2599 4e759de4 2019-06-26 stsp return err;
2602 4e759de4 2019-06-26 stsp static const struct got_error *
2603 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2604 4e759de4 2019-06-26 stsp const char *base_branch)
2606 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2607 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2608 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2609 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2610 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2613 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
2614 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
2615 d3f84d51 2019-07-11 stsp * an unintended typo.
2617 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
2618 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2620 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2621 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2622 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2623 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2624 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2625 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2627 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2629 4e759de4 2019-06-26 stsp goto done;
2630 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2631 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2633 4e759de4 2019-06-26 stsp goto done;
2635 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2636 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2637 4e759de4 2019-06-26 stsp goto done;
2640 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2641 4e759de4 2019-06-26 stsp if (err == NULL) {
2642 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2643 4e759de4 2019-06-26 stsp goto done;
2644 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2645 4e759de4 2019-06-26 stsp goto done;
2647 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2649 4e759de4 2019-06-26 stsp goto done;
2651 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2654 4e759de4 2019-06-26 stsp got_ref_close(ref);
2656 4e759de4 2019-06-26 stsp free(base_refname);
2657 4e759de4 2019-06-26 stsp free(refname);
2658 4e759de4 2019-06-26 stsp return err;
2661 4e759de4 2019-06-26 stsp static const struct got_error *
2662 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2664 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2665 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2666 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2667 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2668 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2669 4e759de4 2019-06-26 stsp const char *delref = NULL;
2671 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2672 4e759de4 2019-06-26 stsp switch (ch) {
2674 4e759de4 2019-06-26 stsp delref = optarg;
2677 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2678 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2679 4e759de4 2019-06-26 stsp err(1, "-r option");
2680 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2683 4e759de4 2019-06-26 stsp do_list = 1;
2686 4e759de4 2019-06-26 stsp usage_branch();
2687 4e759de4 2019-06-26 stsp /* NOTREACHED */
2691 4e759de4 2019-06-26 stsp if (do_list && delref)
2692 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2694 4e759de4 2019-06-26 stsp argc -= optind;
2695 4e759de4 2019-06-26 stsp argv += optind;
2697 4e759de4 2019-06-26 stsp if (do_list || delref) {
2698 4e759de4 2019-06-26 stsp if (argc > 0)
2699 4e759de4 2019-06-26 stsp usage_branch();
2700 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2701 4e759de4 2019-06-26 stsp usage_branch();
2703 4e759de4 2019-06-26 stsp #ifndef PROFILE
2704 4e759de4 2019-06-26 stsp if (do_list) {
2705 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2706 4e759de4 2019-06-26 stsp NULL) == -1)
2707 4e759de4 2019-06-26 stsp err(1, "pledge");
2709 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2710 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2711 4e759de4 2019-06-26 stsp err(1, "pledge");
2714 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2715 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2716 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2717 4e759de4 2019-06-26 stsp goto done;
2720 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2721 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2722 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2723 4e759de4 2019-06-26 stsp goto done;
2725 4e759de4 2019-06-26 stsp error = NULL;
2726 4e759de4 2019-06-26 stsp if (worktree) {
2727 4e759de4 2019-06-26 stsp repo_path =
2728 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2729 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2730 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2731 4e759de4 2019-06-26 stsp if (error)
2732 4e759de4 2019-06-26 stsp goto done;
2734 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2735 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2736 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2737 4e759de4 2019-06-26 stsp goto done;
2742 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2743 4e759de4 2019-06-26 stsp if (error != NULL)
2744 4e759de4 2019-06-26 stsp goto done;
2746 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2747 4e759de4 2019-06-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2748 4e759de4 2019-06-26 stsp if (error)
2749 4e759de4 2019-06-26 stsp goto done;
2751 4e759de4 2019-06-26 stsp if (do_list)
2752 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
2753 4e759de4 2019-06-26 stsp else if (delref)
2754 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2756 4e759de4 2019-06-26 stsp const char *base_branch;
2757 4e759de4 2019-06-26 stsp if (argc == 1) {
2758 4e759de4 2019-06-26 stsp base_branch = worktree ?
2759 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2760 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2762 4e759de4 2019-06-26 stsp base_branch = argv[1];
2763 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2767 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2768 d0eebce4 2019-03-11 stsp if (worktree)
2769 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2770 d0eebce4 2019-03-11 stsp free(cwd);
2771 d0eebce4 2019-03-11 stsp free(repo_path);
2772 d00136be 2019-03-26 stsp return error;
2775 d00136be 2019-03-26 stsp __dead static void
2776 d00136be 2019-03-26 stsp usage_add(void)
2778 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2782 d00136be 2019-03-26 stsp static const struct got_error *
2783 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2785 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2786 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2787 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2788 1dd54920 2019-05-11 stsp char *cwd = NULL;
2789 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2790 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2793 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2795 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2796 d00136be 2019-03-26 stsp switch (ch) {
2798 d00136be 2019-03-26 stsp usage_add();
2799 d00136be 2019-03-26 stsp /* NOTREACHED */
2803 d00136be 2019-03-26 stsp argc -= optind;
2804 d00136be 2019-03-26 stsp argv += optind;
2806 43012d58 2019-07-14 stsp #ifndef PROFILE
2807 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2808 43012d58 2019-07-14 stsp NULL) == -1)
2809 43012d58 2019-07-14 stsp err(1, "pledge");
2811 723c305c 2019-05-11 jcs if (argc < 1)
2812 d00136be 2019-03-26 stsp usage_add();
2814 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
2815 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2816 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2817 723c305c 2019-05-11 jcs if (path == NULL) {
2818 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2821 1dd54920 2019-05-11 stsp free(path);
2824 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2825 d00136be 2019-03-26 stsp if (cwd == NULL) {
2826 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2827 d00136be 2019-03-26 stsp goto done;
2830 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2831 d00136be 2019-03-26 stsp if (error)
2832 d00136be 2019-03-26 stsp goto done;
2834 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2835 031a5338 2019-03-26 stsp if (error != NULL)
2836 031a5338 2019-03-26 stsp goto done;
2838 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2839 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2840 d00136be 2019-03-26 stsp if (error)
2841 d00136be 2019-03-26 stsp goto done;
2843 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2844 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2845 723c305c 2019-05-11 jcs if (path == NULL) {
2846 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2850 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2851 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2852 1dd54920 2019-05-11 stsp if (error) {
2853 1dd54920 2019-05-11 stsp free(path);
2857 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2858 1dd54920 2019-05-11 stsp NULL, repo);
2861 031a5338 2019-03-26 stsp got_repo_close(repo);
2862 d00136be 2019-03-26 stsp if (worktree)
2863 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2864 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2865 1dd54920 2019-05-11 stsp free((char *)pe->path);
2866 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2867 2ec1f75b 2019-03-26 stsp free(cwd);
2868 2ec1f75b 2019-03-26 stsp return error;
2871 2ec1f75b 2019-03-26 stsp __dead static void
2872 648e4ef7 2019-07-09 stsp usage_remove(void)
2874 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2878 2ec1f75b 2019-03-26 stsp static const struct got_error *
2879 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
2881 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2882 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2883 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2884 17ed4618 2019-06-02 stsp char *cwd = NULL;
2885 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2886 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2887 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2889 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2891 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2892 2ec1f75b 2019-03-26 stsp switch (ch) {
2894 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2897 2ec1f75b 2019-03-26 stsp usage_add();
2898 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2902 2ec1f75b 2019-03-26 stsp argc -= optind;
2903 2ec1f75b 2019-03-26 stsp argv += optind;
2905 43012d58 2019-07-14 stsp #ifndef PROFILE
2906 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2907 43012d58 2019-07-14 stsp NULL) == -1)
2908 43012d58 2019-07-14 stsp err(1, "pledge");
2910 17ed4618 2019-06-02 stsp if (argc < 1)
2911 648e4ef7 2019-07-09 stsp usage_remove();
2913 17ed4618 2019-06-02 stsp /* make sure each file exists before doing anything halfway */
2914 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2915 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2916 17ed4618 2019-06-02 stsp if (path == NULL) {
2917 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2918 17ed4618 2019-06-02 stsp goto done;
2920 17ed4618 2019-06-02 stsp free(path);
2923 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2924 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2925 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2926 2ec1f75b 2019-03-26 stsp goto done;
2928 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2929 2ec1f75b 2019-03-26 stsp if (error)
2930 2ec1f75b 2019-03-26 stsp goto done;
2932 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2934 2ec1f75b 2019-03-26 stsp goto done;
2936 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2937 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2938 2ec1f75b 2019-03-26 stsp if (error)
2939 2ec1f75b 2019-03-26 stsp goto done;
2941 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2942 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2943 17ed4618 2019-06-02 stsp if (path == NULL) {
2944 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2945 17ed4618 2019-06-02 stsp goto done;
2948 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
2949 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2950 17ed4618 2019-06-02 stsp if (error) {
2951 17ed4618 2019-06-02 stsp free(path);
2952 17ed4618 2019-06-02 stsp goto done;
2955 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
2956 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
2957 a129376b 2019-03-28 stsp if (error)
2958 a129376b 2019-03-28 stsp goto done;
2961 a129376b 2019-03-28 stsp got_repo_close(repo);
2962 a129376b 2019-03-28 stsp if (worktree)
2963 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2964 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
2965 17ed4618 2019-06-02 stsp free((char *)pe->path);
2966 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
2967 a129376b 2019-03-28 stsp free(cwd);
2968 a129376b 2019-03-28 stsp return error;
2971 a129376b 2019-03-28 stsp __dead static void
2972 a129376b 2019-03-28 stsp usage_revert(void)
2974 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2978 1ee397ad 2019-07-12 stsp static const struct got_error *
2979 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2981 a129376b 2019-03-28 stsp while (path[0] == '/')
2983 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2984 1ee397ad 2019-07-12 stsp return NULL;
2987 a129376b 2019-03-28 stsp static const struct got_error *
2988 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2990 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2991 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2992 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2993 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2994 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
2995 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2996 e20a8b6f 2019-06-04 stsp int ch, i;
2998 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3000 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3001 a129376b 2019-03-28 stsp switch (ch) {
3003 a129376b 2019-03-28 stsp usage_revert();
3004 a129376b 2019-03-28 stsp /* NOTREACHED */
3008 a129376b 2019-03-28 stsp argc -= optind;
3009 a129376b 2019-03-28 stsp argv += optind;
3011 43012d58 2019-07-14 stsp #ifndef PROFILE
3012 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3013 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3014 43012d58 2019-07-14 stsp err(1, "pledge");
3016 e20a8b6f 2019-06-04 stsp if (argc < 1)
3017 a129376b 2019-03-28 stsp usage_revert();
3019 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
3020 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
3021 e20a8b6f 2019-06-04 stsp if (path == NULL) {
3022 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
3023 e20a8b6f 2019-06-04 stsp goto done;
3026 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
3027 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3028 e20a8b6f 2019-06-04 stsp if (error) {
3029 e20a8b6f 2019-06-04 stsp free(path);
3030 e20a8b6f 2019-06-04 stsp goto done;
3034 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3035 a129376b 2019-03-28 stsp if (cwd == NULL) {
3036 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3037 a129376b 2019-03-28 stsp goto done;
3039 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3040 2ec1f75b 2019-03-26 stsp if (error)
3041 2ec1f75b 2019-03-26 stsp goto done;
3043 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3044 a129376b 2019-03-28 stsp if (error != NULL)
3045 a129376b 2019-03-28 stsp goto done;
3047 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3048 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
3049 a129376b 2019-03-28 stsp if (error)
3050 a129376b 2019-03-28 stsp goto done;
3052 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
3053 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
3054 a129376b 2019-03-28 stsp if (error)
3055 a129376b 2019-03-28 stsp goto done;
3058 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3059 2ec1f75b 2019-03-26 stsp if (worktree)
3060 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3061 2ec1f75b 2019-03-26 stsp free(path);
3062 d00136be 2019-03-26 stsp free(cwd);
3063 6bad629b 2019-02-04 stsp return error;
3066 c4296144 2019-05-09 stsp __dead static void
3067 c4296144 2019-05-09 stsp usage_commit(void)
3069 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3073 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3074 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3075 e2ba3d07 2019-05-13 stsp const char *editor;
3076 e0870e44 2019-05-13 stsp const char *worktree_path;
3077 76d98825 2019-06-03 stsp const char *branch_name;
3078 314a6357 2019-05-13 stsp const char *repo_path;
3079 e0870e44 2019-05-13 stsp char *logmsg_path;
3083 33ad4cbe 2019-05-12 jcs static const struct got_error *
3084 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3087 76d98825 2019-06-03 stsp char *initial_content = NULL;
3088 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3089 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3090 e0870e44 2019-05-13 stsp char *template = NULL;
3091 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3093 33ad4cbe 2019-05-12 jcs size_t len;
3095 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3096 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3097 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3098 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3099 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3100 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3101 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3102 33ad4cbe 2019-05-12 jcs return NULL;
3105 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3106 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3108 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3109 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3110 76d98825 2019-06-03 stsp a->branch_name) == -1)
3111 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3113 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3115 e0870e44 2019-05-13 stsp goto done;
3117 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3119 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3120 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3121 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3122 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3123 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3127 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3129 3ce1b845 2019-07-15 stsp unlink(a->logmsg_path);
3130 3ce1b845 2019-07-15 stsp free(a->logmsg_path);
3131 76d98825 2019-06-03 stsp free(initial_content);
3132 e0870e44 2019-05-13 stsp free(template);
3134 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3135 3ce1b845 2019-07-15 stsp if (err == NULL) {
3136 314a6357 2019-05-13 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3137 3ce1b845 2019-07-15 stsp if (err) {
3138 3ce1b845 2019-07-15 stsp free(*logmsg);
3139 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3142 33ad4cbe 2019-05-12 jcs return err;
3145 c4296144 2019-05-09 stsp static const struct got_error *
3146 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3148 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3149 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3150 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3151 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
3152 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3153 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3154 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
3155 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3156 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3157 7d5807f4 2019-07-11 stsp int ch, rebase_in_progress;
3159 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3160 c4296144 2019-05-09 stsp switch (ch) {
3162 c4296144 2019-05-09 stsp logmsg = optarg;
3165 c4296144 2019-05-09 stsp usage_commit();
3166 c4296144 2019-05-09 stsp /* NOTREACHED */
3170 c4296144 2019-05-09 stsp argc -= optind;
3171 c4296144 2019-05-09 stsp argv += optind;
3173 43012d58 2019-07-14 stsp #ifndef PROFILE
3174 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3175 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3176 43012d58 2019-07-14 stsp err(1, "pledge");
3178 c4296144 2019-05-09 stsp if (argc == 1) {
3179 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
3180 c4296144 2019-05-09 stsp if (path == NULL) {
3181 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
3182 c4296144 2019-05-09 stsp goto done;
3184 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
3185 c4296144 2019-05-09 stsp } else if (argc != 0)
3186 c4296144 2019-05-09 stsp usage_commit();
3188 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
3189 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
3190 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3191 35bd8fed 2019-05-09 stsp goto done;
3194 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3195 c4296144 2019-05-09 stsp if (cwd == NULL) {
3196 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3197 c4296144 2019-05-09 stsp goto done;
3199 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3200 7d5807f4 2019-07-11 stsp if (error)
3201 7d5807f4 2019-07-11 stsp goto done;
3203 7d5807f4 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3204 c4296144 2019-05-09 stsp if (error)
3205 7d5807f4 2019-07-11 stsp goto done;
3206 7d5807f4 2019-07-11 stsp if (rebase_in_progress) {
3207 7d5807f4 2019-07-11 stsp error = got_error(GOT_ERR_REBASING);
3208 c4296144 2019-05-09 stsp goto done;
3211 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3212 c4296144 2019-05-09 stsp if (error != NULL)
3213 c4296144 2019-05-09 stsp goto done;
3216 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3217 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3219 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3220 314a6357 2019-05-13 stsp error = get_editor(&editor);
3222 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3223 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
3224 c4296144 2019-05-09 stsp if (error)
3225 c4296144 2019-05-09 stsp goto done;
3227 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3228 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3229 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3230 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3231 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3232 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
3233 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3234 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
3235 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3236 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
3237 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3238 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3239 e0870e44 2019-05-13 stsp if (error) {
3240 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3241 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3242 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3243 c4296144 2019-05-09 stsp goto done;
3246 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3247 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3249 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3250 c4296144 2019-05-09 stsp if (error)
3251 c4296144 2019-05-09 stsp goto done;
3252 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3255 c4296144 2019-05-09 stsp got_repo_close(repo);
3256 c4296144 2019-05-09 stsp if (worktree)
3257 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3258 c4296144 2019-05-09 stsp free(path);
3259 c4296144 2019-05-09 stsp free(cwd);
3260 c4296144 2019-05-09 stsp free(id_str);
3261 e2ba3d07 2019-05-13 stsp free(editor);
3262 234035bc 2019-06-01 stsp return error;
3265 234035bc 2019-06-01 stsp __dead static void
3266 234035bc 2019-06-01 stsp usage_cherrypick(void)
3268 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3272 234035bc 2019-06-01 stsp static const struct got_error *
3273 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3275 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3276 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3277 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3278 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3279 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3280 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3281 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3282 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3283 234035bc 2019-06-01 stsp int ch, did_something = 0;
3285 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3286 234035bc 2019-06-01 stsp switch (ch) {
3288 234035bc 2019-06-01 stsp usage_cherrypick();
3289 234035bc 2019-06-01 stsp /* NOTREACHED */
3293 234035bc 2019-06-01 stsp argc -= optind;
3294 234035bc 2019-06-01 stsp argv += optind;
3296 43012d58 2019-07-14 stsp #ifndef PROFILE
3297 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3298 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3299 43012d58 2019-07-14 stsp err(1, "pledge");
3301 234035bc 2019-06-01 stsp if (argc != 1)
3302 234035bc 2019-06-01 stsp usage_cherrypick();
3304 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3305 234035bc 2019-06-01 stsp if (cwd == NULL) {
3306 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3307 234035bc 2019-06-01 stsp goto done;
3309 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3310 234035bc 2019-06-01 stsp if (error)
3311 234035bc 2019-06-01 stsp goto done;
3313 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3314 234035bc 2019-06-01 stsp if (error != NULL)
3315 234035bc 2019-06-01 stsp goto done;
3317 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3318 234035bc 2019-06-01 stsp got_worktree_get_root_path(worktree), 0);
3319 234035bc 2019-06-01 stsp if (error)
3320 234035bc 2019-06-01 stsp goto done;
3322 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3323 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3324 234035bc 2019-06-01 stsp if (error != NULL) {
3325 234035bc 2019-06-01 stsp struct got_reference *ref;
3326 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3327 234035bc 2019-06-01 stsp goto done;
3328 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3329 234035bc 2019-06-01 stsp if (error != NULL)
3330 234035bc 2019-06-01 stsp goto done;
3331 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3332 234035bc 2019-06-01 stsp got_ref_close(ref);
3333 234035bc 2019-06-01 stsp if (error != NULL)
3334 234035bc 2019-06-01 stsp goto done;
3336 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3337 234035bc 2019-06-01 stsp if (error)
3338 234035bc 2019-06-01 stsp goto done;
3340 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3341 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3342 234035bc 2019-06-01 stsp if (error != NULL)
3343 234035bc 2019-06-01 stsp goto done;
3345 234035bc 2019-06-01 stsp error = check_same_branch(commit_id, head_ref, repo);
3346 234035bc 2019-06-01 stsp if (error) {
3347 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3348 234035bc 2019-06-01 stsp goto done;
3349 234035bc 2019-06-01 stsp error = NULL;
3351 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3352 234035bc 2019-06-01 stsp goto done;
3355 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3356 234035bc 2019-06-01 stsp if (error)
3357 234035bc 2019-06-01 stsp goto done;
3358 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3359 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3360 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3362 234035bc 2019-06-01 stsp if (error != NULL)
3363 234035bc 2019-06-01 stsp goto done;
3365 234035bc 2019-06-01 stsp if (did_something)
3366 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3368 234035bc 2019-06-01 stsp if (commit)
3369 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3370 234035bc 2019-06-01 stsp free(commit_id_str);
3371 234035bc 2019-06-01 stsp if (head_ref)
3372 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3373 234035bc 2019-06-01 stsp if (worktree)
3374 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3376 234035bc 2019-06-01 stsp got_repo_close(repo);
3377 c4296144 2019-05-09 stsp return error;
3380 5ef14e63 2019-06-02 stsp __dead static void
3381 5ef14e63 2019-06-02 stsp usage_backout(void)
3383 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3387 5ef14e63 2019-06-02 stsp static const struct got_error *
3388 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3390 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3391 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3392 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3393 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3394 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3395 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3396 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3397 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3398 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3400 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3401 5ef14e63 2019-06-02 stsp switch (ch) {
3403 5ef14e63 2019-06-02 stsp usage_backout();
3404 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3408 5ef14e63 2019-06-02 stsp argc -= optind;
3409 5ef14e63 2019-06-02 stsp argv += optind;
3411 43012d58 2019-07-14 stsp #ifndef PROFILE
3412 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3413 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3414 43012d58 2019-07-14 stsp err(1, "pledge");
3416 5ef14e63 2019-06-02 stsp if (argc != 1)
3417 5ef14e63 2019-06-02 stsp usage_backout();
3419 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3420 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3421 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3422 5ef14e63 2019-06-02 stsp goto done;
3424 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3425 5ef14e63 2019-06-02 stsp if (error)
3426 5ef14e63 2019-06-02 stsp goto done;
3428 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3429 5ef14e63 2019-06-02 stsp if (error != NULL)
3430 5ef14e63 2019-06-02 stsp goto done;
3432 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3433 5ef14e63 2019-06-02 stsp got_worktree_get_root_path(worktree), 0);
3434 5ef14e63 2019-06-02 stsp if (error)
3435 5ef14e63 2019-06-02 stsp goto done;
3437 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3438 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3439 5ef14e63 2019-06-02 stsp if (error != NULL) {
3440 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3441 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3442 5ef14e63 2019-06-02 stsp goto done;
3443 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3444 5ef14e63 2019-06-02 stsp if (error != NULL)
3445 5ef14e63 2019-06-02 stsp goto done;
3446 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3447 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3448 5ef14e63 2019-06-02 stsp if (error != NULL)
3449 5ef14e63 2019-06-02 stsp goto done;
3451 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3452 5ef14e63 2019-06-02 stsp if (error)
3453 5ef14e63 2019-06-02 stsp goto done;
3455 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3456 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3457 5ef14e63 2019-06-02 stsp if (error != NULL)
3458 5ef14e63 2019-06-02 stsp goto done;
3460 5ef14e63 2019-06-02 stsp error = check_same_branch(commit_id, head_ref, repo);
3461 5ef14e63 2019-06-02 stsp if (error)
3462 5ef14e63 2019-06-02 stsp goto done;
3464 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3465 5ef14e63 2019-06-02 stsp if (error)
3466 5ef14e63 2019-06-02 stsp goto done;
3467 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3468 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3469 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3470 5ef14e63 2019-06-02 stsp goto done;
3473 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3474 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3475 5ef14e63 2019-06-02 stsp if (error != NULL)
3476 5ef14e63 2019-06-02 stsp goto done;
3478 5ef14e63 2019-06-02 stsp if (did_something)
3479 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3481 5ef14e63 2019-06-02 stsp if (commit)
3482 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3483 5ef14e63 2019-06-02 stsp free(commit_id_str);
3484 5ef14e63 2019-06-02 stsp if (head_ref)
3485 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3486 818c7501 2019-07-11 stsp if (worktree)
3487 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3489 818c7501 2019-07-11 stsp got_repo_close(repo);
3490 818c7501 2019-07-11 stsp return error;
3493 818c7501 2019-07-11 stsp __dead static void
3494 818c7501 2019-07-11 stsp usage_rebase(void)
3496 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3497 af54c8f8 2019-07-11 stsp getprogname());
3501 818c7501 2019-07-11 stsp static const struct got_error *
3502 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3503 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3505 818c7501 2019-07-11 stsp const struct got_error *err;
3506 818c7501 2019-07-11 stsp char *old_id_str = NULL, *new_id_str = NULL;
3507 818c7501 2019-07-11 stsp char *logmsg0 = NULL, *logmsg, *nl;
3508 818c7501 2019-07-11 stsp size_t len;
3510 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3512 818c7501 2019-07-11 stsp goto done;
3514 ff0d2220 2019-07-11 stsp if (new_id) {
3515 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3517 ff0d2220 2019-07-11 stsp goto done;
3520 818c7501 2019-07-11 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3521 818c7501 2019-07-11 stsp if (logmsg0 == NULL) {
3522 818c7501 2019-07-11 stsp err = got_error_from_errno("strdup");
3523 818c7501 2019-07-11 stsp goto done;
3525 818c7501 2019-07-11 stsp logmsg = logmsg0;
3527 10796104 2019-07-11 stsp while (isspace((unsigned char)logmsg[0]))
3530 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3531 ff0d2220 2019-07-11 stsp if (new_id_str)
3532 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3533 818c7501 2019-07-11 stsp len = strlen(logmsg);
3534 818c7501 2019-07-11 stsp if (len > 42)
3536 818c7501 2019-07-11 stsp logmsg[len] = '\0';
3537 818c7501 2019-07-11 stsp nl = strchr(logmsg, '\n');
3539 818c7501 2019-07-11 stsp *nl = '\0';
3540 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3541 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3543 818c7501 2019-07-11 stsp free(old_id_str);
3544 818c7501 2019-07-11 stsp free(new_id_str);
3545 818c7501 2019-07-11 stsp free(logmsg0);
3546 818c7501 2019-07-11 stsp return err;
3549 1ee397ad 2019-07-12 stsp static const struct got_error *
3550 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
3552 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
3554 818c7501 2019-07-11 stsp while (path[0] == '/')
3556 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
3558 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
3559 1ee397ad 2019-07-12 stsp return NULL;
3560 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3561 818c7501 2019-07-11 stsp *rebase_status = status;
3562 1ee397ad 2019-07-12 stsp return NULL;
3565 818c7501 2019-07-11 stsp static const struct got_error *
3566 818c7501 2019-07-11 stsp rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3567 818c7501 2019-07-11 stsp struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3568 818c7501 2019-07-11 stsp struct got_repository *repo)
3570 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
3571 818c7501 2019-07-11 stsp return got_worktree_rebase_complete(worktree,
3572 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
3575 818c7501 2019-07-11 stsp static const struct got_error *
3576 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
3577 01757395 2019-07-12 stsp struct got_worktree *worktree, struct got_reference *tmp_branch,
3578 ff0d2220 2019-07-11 stsp struct got_object_id *commit_id, struct got_repository *repo)
3580 ff0d2220 2019-07-11 stsp const struct got_error *error;
3581 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
3582 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
3584 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3585 ff0d2220 2019-07-11 stsp if (error)
3586 ff0d2220 2019-07-11 stsp return error;
3588 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3589 01757395 2019-07-12 stsp worktree, tmp_branch, commit, commit_id, repo);
3590 ff0d2220 2019-07-11 stsp if (error) {
3591 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3592 ff0d2220 2019-07-11 stsp goto done;
3593 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
3595 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
3596 ff0d2220 2019-07-11 stsp free(new_commit_id);
3599 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
3600 ff0d2220 2019-07-11 stsp return error;
3603 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
3604 64c6d990 2019-07-11 stsp const char *path_prefix;
3605 64c6d990 2019-07-11 stsp size_t len;
3608 64c6d990 2019-07-11 stsp static const struct got_error *
3609 64c6d990 2019-07-11 stsp check_path_prefix(void *arg, struct got_blob_object *blob1,
3610 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3611 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
3612 64c6d990 2019-07-11 stsp struct got_repository *repo)
3614 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
3616 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3617 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3618 64c6d990 2019-07-11 stsp return got_error(GOT_ERR_REBASE_PATH);
3620 64c6d990 2019-07-11 stsp return NULL;
3623 64c6d990 2019-07-11 stsp static const struct got_error *
3624 64c6d990 2019-07-11 stsp rebase_check_path_prefix(struct got_object_id *parent_id,
3625 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
3626 64c6d990 2019-07-11 stsp struct got_repository *repo)
3628 64c6d990 2019-07-11 stsp const struct got_error *err;
3629 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3630 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
3631 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
3633 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
3634 64c6d990 2019-07-11 stsp return NULL;
3636 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3638 64c6d990 2019-07-11 stsp goto done;
3640 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3642 64c6d990 2019-07-11 stsp goto done;
3644 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
3645 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
3647 64c6d990 2019-07-11 stsp goto done;
3649 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
3650 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
3652 64c6d990 2019-07-11 stsp goto done;
3654 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
3655 64c6d990 2019-07-11 stsp cpp_arg.len = strlen(path_prefix);
3656 64c6d990 2019-07-11 stsp err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3657 64c6d990 2019-07-11 stsp &cpp_arg);
3659 64c6d990 2019-07-11 stsp if (tree1)
3660 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
3661 64c6d990 2019-07-11 stsp if (tree2)
3662 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
3663 64c6d990 2019-07-11 stsp if (commit)
3664 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
3665 64c6d990 2019-07-11 stsp if (parent_commit)
3666 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
3667 64c6d990 2019-07-11 stsp return err;
3670 ff0d2220 2019-07-11 stsp static const struct got_error *
3671 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
3673 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
3674 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
3675 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
3676 818c7501 2019-07-11 stsp char *cwd = NULL;
3677 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
3678 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3679 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
3680 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
3681 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3682 818c7501 2019-07-11 stsp struct got_commit_graph *graph = NULL;
3683 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
3684 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3685 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3686 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
3687 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
3688 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
3689 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
3691 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
3692 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
3694 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
3695 818c7501 2019-07-11 stsp switch (ch) {
3697 818c7501 2019-07-11 stsp abort_rebase = 1;
3700 818c7501 2019-07-11 stsp continue_rebase = 1;
3703 818c7501 2019-07-11 stsp usage_rebase();
3704 818c7501 2019-07-11 stsp /* NOTREACHED */
3708 818c7501 2019-07-11 stsp argc -= optind;
3709 818c7501 2019-07-11 stsp argv += optind;
3711 43012d58 2019-07-14 stsp #ifndef PROFILE
3712 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3713 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3714 43012d58 2019-07-14 stsp err(1, "pledge");
3716 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
3717 818c7501 2019-07-11 stsp usage_rebase();
3718 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
3719 818c7501 2019-07-11 stsp if (argc != 0)
3720 818c7501 2019-07-11 stsp usage_rebase();
3721 818c7501 2019-07-11 stsp } else if (argc != 1)
3722 818c7501 2019-07-11 stsp usage_rebase();
3724 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
3725 818c7501 2019-07-11 stsp if (cwd == NULL) {
3726 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
3727 818c7501 2019-07-11 stsp goto done;
3729 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
3730 818c7501 2019-07-11 stsp if (error)
3731 818c7501 2019-07-11 stsp goto done;
3733 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3734 818c7501 2019-07-11 stsp if (error != NULL)
3735 818c7501 2019-07-11 stsp goto done;
3737 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3738 818c7501 2019-07-11 stsp got_worktree_get_root_path(worktree), 0);
3739 818c7501 2019-07-11 stsp if (error)
3740 818c7501 2019-07-11 stsp goto done;
3742 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3743 818c7501 2019-07-11 stsp if (error)
3744 818c7501 2019-07-11 stsp goto done;
3746 818c7501 2019-07-11 stsp if (rebase_in_progress && abort_rebase) {
3747 818c7501 2019-07-11 stsp int did_something;
3748 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3749 818c7501 2019-07-11 stsp &new_base_branch, &tmp_branch, &branch, worktree, repo);
3750 818c7501 2019-07-11 stsp if (error)
3751 818c7501 2019-07-11 stsp goto done;
3752 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
3753 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
3754 818c7501 2019-07-11 stsp error = got_worktree_rebase_abort(worktree, repo,
3755 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
3756 818c7501 2019-07-11 stsp if (error)
3757 818c7501 2019-07-11 stsp goto done;
3758 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3759 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
3760 818c7501 2019-07-11 stsp } else if (abort_rebase) {
3761 818c7501 2019-07-11 stsp error = got_error(GOT_ERR_NOT_REBASING);
3762 818c7501 2019-07-11 stsp goto done;
3765 818c7501 2019-07-11 stsp if (continue_rebase) {
3766 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3767 818c7501 2019-07-11 stsp &new_base_branch, &tmp_branch, &branch, worktree, repo);
3768 818c7501 2019-07-11 stsp if (error)
3769 818c7501 2019-07-11 stsp goto done;
3771 01757395 2019-07-12 stsp error = rebase_commit(NULL, worktree, tmp_branch,
3772 01757395 2019-07-12 stsp resume_commit_id, repo);
3773 818c7501 2019-07-11 stsp if (error)
3774 818c7501 2019-07-11 stsp goto done;
3776 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
3777 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
3778 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
3779 818c7501 2019-07-11 stsp goto done;
3782 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
3783 818c7501 2019-07-11 stsp if (error != NULL)
3784 818c7501 2019-07-11 stsp goto done;
3786 818c7501 2019-07-11 stsp error = check_same_branch(
3787 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree), branch, repo);
3788 818c7501 2019-07-11 stsp if (error) {
3789 818c7501 2019-07-11 stsp if (error->code != GOT_ERR_ANCESTRY)
3790 818c7501 2019-07-11 stsp goto done;
3791 818c7501 2019-07-11 stsp error = NULL;
3793 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
3794 818c7501 2019-07-11 stsp "specified branch resolves to a commit which "
3795 818c7501 2019-07-11 stsp "is already contained in work tree's branch");
3796 818c7501 2019-07-11 stsp goto done;
3800 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3801 ff0d2220 2019-07-11 stsp if (error)
3802 ff0d2220 2019-07-11 stsp goto done;
3804 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
3805 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3806 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree),
3807 818c7501 2019-07-11 stsp branch_head_commit_id, repo);
3808 818c7501 2019-07-11 stsp if (error)
3809 818c7501 2019-07-11 stsp goto done;
3810 818c7501 2019-07-11 stsp if (yca_id == NULL) {
3811 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
3812 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
3813 818c7501 2019-07-11 stsp "with work tree's branch");
3814 818c7501 2019-07-11 stsp goto done;
3817 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
3818 818c7501 2019-07-11 stsp &tmp_branch, worktree, branch, repo);
3819 818c7501 2019-07-11 stsp if (error)
3820 818c7501 2019-07-11 stsp goto done;
3823 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
3824 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3825 818c7501 2019-07-11 stsp if (error)
3826 818c7501 2019-07-11 stsp goto done;
3828 818c7501 2019-07-11 stsp error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3829 818c7501 2019-07-11 stsp if (error)
3830 818c7501 2019-07-11 stsp goto done;
3831 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3832 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
3833 818c7501 2019-07-11 stsp error = got_commit_graph_iter_start(graph, pid->id, repo);
3834 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3835 818c7501 2019-07-11 stsp commit = NULL;
3836 818c7501 2019-07-11 stsp if (error)
3837 818c7501 2019-07-11 stsp goto done;
3838 818c7501 2019-07-11 stsp while (got_object_id_cmp(commit_id, yca_id) != 0) {
3839 818c7501 2019-07-11 stsp error = got_commit_graph_iter_next(&parent_id, graph);
3840 818c7501 2019-07-11 stsp if (error) {
3841 818c7501 2019-07-11 stsp if (error->code == GOT_ERR_ITER_COMPLETED) {
3842 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
3843 818c7501 2019-07-11 stsp "ran out of commits to rebase before "
3844 818c7501 2019-07-11 stsp "youngest common ancestor commit has "
3845 818c7501 2019-07-11 stsp "been reached?!?");
3846 818c7501 2019-07-11 stsp goto done;
3847 818c7501 2019-07-11 stsp } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3848 818c7501 2019-07-11 stsp goto done;
3849 818c7501 2019-07-11 stsp error = got_commit_graph_fetch_commits(graph, 1, repo);
3850 818c7501 2019-07-11 stsp if (error)
3851 818c7501 2019-07-11 stsp goto done;
3853 64c6d990 2019-07-11 stsp error = rebase_check_path_prefix(parent_id, commit_id,
3854 64c6d990 2019-07-11 stsp got_worktree_get_path_prefix(worktree), repo);
3855 64c6d990 2019-07-11 stsp if (error)
3856 64c6d990 2019-07-11 stsp goto done;
3858 818c7501 2019-07-11 stsp error = got_object_qid_alloc(&qid, commit_id);
3859 818c7501 2019-07-11 stsp if (error)
3860 818c7501 2019-07-11 stsp goto done;
3861 818c7501 2019-07-11 stsp SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3862 818c7501 2019-07-11 stsp commit_id = parent_id;
3866 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
3867 ff0d2220 2019-07-11 stsp if (continue_rebase)
3868 ff0d2220 2019-07-11 stsp error = rebase_complete(worktree, branch,
3869 ff0d2220 2019-07-11 stsp new_base_branch, tmp_branch, repo);
3871 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
3872 818c7501 2019-07-11 stsp goto done;
3875 818c7501 2019-07-11 stsp pid = NULL;
3876 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
3877 818c7501 2019-07-11 stsp commit_id = qid->id;
3878 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
3879 818c7501 2019-07-11 stsp pid = qid;
3881 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
3882 01757395 2019-07-12 stsp worktree, parent_id, commit_id, repo, rebase_progress,
3883 01757395 2019-07-12 stsp &rebase_status, check_cancelled, NULL);
3884 818c7501 2019-07-11 stsp if (error)
3885 818c7501 2019-07-11 stsp goto done;
3887 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
3888 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
3892 01757395 2019-07-12 stsp error = rebase_commit(&merged_paths, worktree, tmp_branch,
3893 01757395 2019-07-12 stsp commit_id, repo);
3894 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
3895 818c7501 2019-07-11 stsp if (error)
3896 818c7501 2019-07-11 stsp goto done;
3899 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
3900 818c7501 2019-07-11 stsp error = got_worktree_rebase_postpone(worktree);
3901 818c7501 2019-07-11 stsp if (error)
3902 818c7501 2019-07-11 stsp goto done;
3903 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
3904 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
3906 818c7501 2019-07-11 stsp error = rebase_complete(worktree, branch, new_base_branch,
3907 818c7501 2019-07-11 stsp tmp_branch, repo);
3909 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
3910 818c7501 2019-07-11 stsp free(branch_head_commit_id);
3911 818c7501 2019-07-11 stsp free(resume_commit_id);
3912 818c7501 2019-07-11 stsp free(yca_id);
3913 818c7501 2019-07-11 stsp if (graph)
3914 818c7501 2019-07-11 stsp got_commit_graph_close(graph);
3915 818c7501 2019-07-11 stsp if (commit)
3916 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3917 818c7501 2019-07-11 stsp if (branch)
3918 818c7501 2019-07-11 stsp got_ref_close(branch);
3919 818c7501 2019-07-11 stsp if (new_base_branch)
3920 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
3921 818c7501 2019-07-11 stsp if (tmp_branch)
3922 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
3923 5ef14e63 2019-06-02 stsp if (worktree)
3924 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
3926 5ef14e63 2019-06-02 stsp got_repo_close(repo);
3927 5ef14e63 2019-06-02 stsp return error;