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 53ccebc2 2019-07-30 stsp #include "got_version.h"
39 f42b1b34 2018-03-12 stsp #include "got_error.h"
40 f42b1b34 2018-03-12 stsp #include "got_object.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 f42b1b34 2018-03-12 stsp #include "got_repository.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 c09a553d 2018-03-12 stsp #include "got_worktree.h"
45 79109fed 2018-03-27 stsp #include "got_diff.h"
46 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
47 404c43c4 2018-06-21 stsp #include "got_blame.h"
48 63219cd2 2019-01-04 stsp #include "got_privsep.h"
49 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
51 5c860e29 2018-03-12 stsp #ifndef nitems
52 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
59 99437157 2018-11-11 stsp catch_sigint(int signo)
61 99437157 2018-11-11 stsp sigint_received = 1;
65 99437157 2018-11-11 stsp catch_sigpipe(int signo)
67 99437157 2018-11-11 stsp sigpipe_received = 1;
71 8cfb4057 2019-07-09 stsp struct got_cmd {
72 5c860e29 2018-03-12 stsp const char *cmd_name;
73 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
74 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
75 97b3a7be 2019-07-09 stsp const char *cmd_alias;
78 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
79 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
80 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
82 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
85 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
86 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
87 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
88 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
89 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
90 d00136be 2019-03-26 stsp __dead static void usage_add(void);
91 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
92 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
93 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
94 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
95 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
96 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
97 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
98 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
100 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
101 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
102 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
103 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
104 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
105 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
106 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
107 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
108 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
109 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
110 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
111 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
112 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
113 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
114 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
115 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
116 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
117 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
118 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
119 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
121 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
122 97b3a7be 2019-07-09 stsp { "init", cmd_init, usage_init, "" },
123 3ce1b845 2019-07-15 stsp { "import", cmd_import, usage_import, "" },
124 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
125 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
126 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
127 97b3a7be 2019-07-09 stsp { "diff", cmd_diff, usage_diff, "" },
128 97b3a7be 2019-07-09 stsp { "blame", cmd_blame, usage_blame, "" },
129 97b3a7be 2019-07-09 stsp { "tree", cmd_tree, usage_tree, "" },
130 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
131 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
132 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
133 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
134 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
135 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
136 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
137 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
138 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
139 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
140 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
141 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
144 ce5b7c56 2019-07-09 stsp static void
145 ce5b7c56 2019-07-09 stsp list_commands(void)
149 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
150 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
151 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
152 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
154 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
158 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
160 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
161 5c860e29 2018-03-12 stsp unsigned int i;
163 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
165 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
167 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
168 5c860e29 2018-03-12 stsp switch (ch) {
176 ce5b7c56 2019-07-09 stsp usage(hflag);
177 5c860e29 2018-03-12 stsp /* NOTREACHED */
181 5c860e29 2018-03-12 stsp argc -= optind;
182 5c860e29 2018-03-12 stsp argv += optind;
183 1e70621d 2018-03-27 stsp optind = 0;
185 53ccebc2 2019-07-30 stsp if (Vflag) {
186 53ccebc2 2019-07-30 stsp got_version_print_str();
190 5c860e29 2018-03-12 stsp if (argc <= 0)
191 ce5b7c56 2019-07-09 stsp usage(hflag);
193 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
194 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
196 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
197 d7d4f210 2018-03-12 stsp const struct got_error *error;
199 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
201 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
202 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
206 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
208 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
209 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
210 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
217 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
218 ce5b7c56 2019-07-09 stsp list_commands();
222 4ed7e80c 2018-05-20 stsp __dead static void
223 ce5b7c56 2019-07-09 stsp usage(int hflag)
225 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
226 53ccebc2 2019-07-30 stsp getprogname());
228 ce5b7c56 2019-07-09 stsp list_commands();
232 0266afb7 2019-01-04 stsp static const struct got_error *
233 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
235 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
236 e2ba3d07 2019-05-13 stsp const char *editor;
238 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
239 e2ba3d07 2019-05-13 stsp if (editor == NULL)
240 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
242 0ee7065d 2019-05-13 stsp if (editor) {
243 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
245 0ee7065d 2019-05-13 stsp return err;
248 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
249 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
250 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
251 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
254 e2ba3d07 2019-05-13 stsp return NULL;
257 e2ba3d07 2019-05-13 stsp static const struct got_error *
258 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
259 c530dc23 2019-07-23 stsp const char *worktree_path)
261 163ce85a 2019-05-13 stsp const struct got_error *err;
263 37c06ea4 2019-07-15 stsp #ifdef PROFILE
264 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
265 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
267 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
268 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
270 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
271 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
273 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
274 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
276 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
277 163ce85a 2019-05-13 stsp if (err != NULL)
278 163ce85a 2019-05-13 stsp return err;
280 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
281 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
283 0266afb7 2019-01-04 stsp return NULL;
286 2c7829a4 2019-06-17 stsp __dead static void
287 2c7829a4 2019-06-17 stsp usage_init(void)
289 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
293 2c7829a4 2019-06-17 stsp static const struct got_error *
294 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
296 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
297 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
300 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
301 2c7829a4 2019-06-17 stsp switch (ch) {
303 2c7829a4 2019-06-17 stsp usage_init();
304 2c7829a4 2019-06-17 stsp /* NOTREACHED */
308 2c7829a4 2019-06-17 stsp argc -= optind;
309 2c7829a4 2019-06-17 stsp argv += optind;
311 2c7829a4 2019-06-17 stsp #ifndef PROFILE
312 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
313 2c7829a4 2019-06-17 stsp err(1, "pledge");
315 2c7829a4 2019-06-17 stsp if (argc != 1)
316 bc20e173 2019-06-17 stsp usage_init();
318 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
319 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
320 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
322 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
324 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
325 2c7829a4 2019-06-17 stsp if (error &&
326 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
329 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
333 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
334 2c7829a4 2019-06-17 stsp if (error != NULL)
338 3ce1b845 2019-07-15 stsp free(repo_path);
339 3ce1b845 2019-07-15 stsp return error;
342 3ce1b845 2019-07-15 stsp __dead static void
343 3ce1b845 2019-07-15 stsp usage_import(void)
345 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
346 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
351 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
354 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
355 3ce1b845 2019-07-15 stsp int st = -1;
357 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
358 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
359 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
361 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
363 3ce1b845 2019-07-15 stsp goto doneediting;
365 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
366 3ce1b845 2019-07-15 stsp _exit(127);
369 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
370 3ce1b845 2019-07-15 stsp if (errno != EINTR)
373 3ce1b845 2019-07-15 stsp doneediting:
374 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
375 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
376 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
378 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
379 3ce1b845 2019-07-15 stsp errno = EINTR;
383 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
386 3ce1b845 2019-07-15 stsp static const struct got_error *
387 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
388 3ce1b845 2019-07-15 stsp const char *initial_content)
390 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
391 3ce1b845 2019-07-15 stsp char buf[1024];
392 3ce1b845 2019-07-15 stsp struct stat st, st2;
394 3ce1b845 2019-07-15 stsp int content_changed = 0;
395 3ce1b845 2019-07-15 stsp size_t len;
397 3ce1b845 2019-07-15 stsp *logmsg = NULL;
399 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
400 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
402 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
403 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
405 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
406 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
408 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
409 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
410 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
412 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
413 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
414 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
415 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
418 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
419 3ce1b845 2019-07-15 stsp if (fp == NULL) {
420 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
423 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
424 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
425 3ce1b845 2019-07-15 stsp content_changed = 1;
426 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
427 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
428 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
430 3ce1b845 2019-07-15 stsp fclose(fp);
432 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
433 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
437 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
438 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
442 3ce1b845 2019-07-15 stsp free(*logmsg);
443 3ce1b845 2019-07-15 stsp *logmsg = NULL;
445 3ce1b845 2019-07-15 stsp return err;
448 3ce1b845 2019-07-15 stsp static const struct got_error *
449 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
450 3ce1b845 2019-07-15 stsp const char *branch_name)
452 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
453 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
456 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
457 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
458 3ce1b845 2019-07-15 stsp branch_name) == -1)
459 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
461 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
465 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
468 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
470 3ce1b845 2019-07-15 stsp free(initial_content);
471 3ce1b845 2019-07-15 stsp free(logmsg_path);
472 3ce1b845 2019-07-15 stsp return err;
475 3ce1b845 2019-07-15 stsp static const struct got_error *
476 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
478 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
479 3ce1b845 2019-07-15 stsp return NULL;
482 3ce1b845 2019-07-15 stsp static const struct got_error *
483 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
485 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
486 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
487 3ce1b845 2019-07-15 stsp char *editor = NULL;
488 3ce1b845 2019-07-15 stsp const char *got_author = getenv("GOT_AUTHOR");
489 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
490 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
491 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
492 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
493 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
495 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
496 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
498 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
500 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
501 3ce1b845 2019-07-15 stsp switch (ch) {
503 3ce1b845 2019-07-15 stsp branch_name = optarg;
506 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
507 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
508 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
513 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
514 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
515 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
520 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
522 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
528 3ce1b845 2019-07-15 stsp usage_init();
529 3ce1b845 2019-07-15 stsp /* NOTREACHED */
533 3ce1b845 2019-07-15 stsp argc -= optind;
534 3ce1b845 2019-07-15 stsp argv += optind;
536 3ce1b845 2019-07-15 stsp #ifndef PROFILE
537 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
538 3ce1b845 2019-07-15 stsp NULL) == -1)
539 3ce1b845 2019-07-15 stsp err(1, "pledge");
541 3ce1b845 2019-07-15 stsp if (argc != 1)
542 3ce1b845 2019-07-15 stsp usage_import();
544 3ce1b845 2019-07-15 stsp if (got_author == NULL) {
545 3ce1b845 2019-07-15 stsp /* TODO: Look current user up in password database */
546 3ce1b845 2019-07-15 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
550 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
551 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
552 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
553 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
555 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
556 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
560 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
561 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
565 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
566 3ce1b845 2019-07-15 stsp if (error) {
567 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
570 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
571 3ce1b845 2019-07-15 stsp "import target branch already exists");
575 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
576 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
577 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
580 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
583 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
584 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
586 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
587 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
590 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
595 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
596 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
598 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
602 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
603 3ce1b845 2019-07-15 stsp got_author, &ignores, repo, import_progress, NULL);
607 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
611 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
615 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
619 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
620 3ce1b845 2019-07-15 stsp if (error) {
621 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
624 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
625 3ce1b845 2019-07-15 stsp branch_ref);
629 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
634 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
635 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
637 2c7829a4 2019-06-17 stsp free(repo_path);
638 3ce1b845 2019-07-15 stsp free(editor);
639 3ce1b845 2019-07-15 stsp free(refname);
640 3ce1b845 2019-07-15 stsp free(new_commit_id);
641 3ce1b845 2019-07-15 stsp free(id_str);
642 3ce1b845 2019-07-15 stsp if (branch_ref)
643 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
644 3ce1b845 2019-07-15 stsp if (head_ref)
645 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
646 2c7829a4 2019-06-17 stsp return error;
649 4ed7e80c 2018-05-20 stsp __dead static void
650 c09a553d 2018-03-12 stsp usage_checkout(void)
652 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
653 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
657 1ee397ad 2019-07-12 stsp static const struct got_error *
658 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
660 92a684f4 2018-03-12 stsp char *worktree_path = arg;
662 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
663 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
664 1ee397ad 2019-07-12 stsp return NULL;
666 92a684f4 2018-03-12 stsp while (path[0] == '/')
669 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
670 1ee397ad 2019-07-12 stsp return NULL;
673 99437157 2018-11-11 stsp static const struct got_error *
674 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
676 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
677 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
678 99437157 2018-11-11 stsp return NULL;
681 8069f636 2019-01-12 stsp static const struct got_error *
682 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
683 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
685 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
686 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
688 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
689 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
691 36a38700 2019-05-10 stsp return err;
693 d5bea539 2019-05-13 stsp if (yca_id == NULL)
694 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
697 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
698 d5bea539 2019-05-13 stsp * and the work tree's base commit.
700 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
702 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
710 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
711 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
712 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
714 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
715 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
716 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
718 d5bea539 2019-05-13 stsp free(yca_id);
719 d5bea539 2019-05-13 stsp return NULL;
722 a367ff0f 2019-05-14 stsp static const struct got_error *
723 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
724 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
725 a51a74b3 2019-07-27 stsp struct got_repository *repo)
727 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
728 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
729 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
730 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
732 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
736 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
737 a51a74b3 2019-07-27 stsp is_same_branch = 1;
740 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
741 a51a74b3 2019-07-27 stsp is_same_branch = 1;
745 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
749 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
754 a367ff0f 2019-05-14 stsp struct got_object_id *id;
755 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
757 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
758 a367ff0f 2019-05-14 stsp err = NULL;
760 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
762 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
769 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
771 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
772 a367ff0f 2019-05-14 stsp is_same_branch = 1;
779 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
780 a367ff0f 2019-05-14 stsp free(head_commit_id);
781 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
782 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
783 04f57cb3 2019-07-25 stsp return err;
786 04f57cb3 2019-07-25 stsp static const struct got_error *
787 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
788 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
790 04f57cb3 2019-07-25 stsp const struct got_error *err;
791 04f57cb3 2019-07-25 stsp struct got_reference *ref;
793 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
794 04f57cb3 2019-07-25 stsp if (err == NULL) {
795 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
796 04f57cb3 2019-07-25 stsp got_ref_close(ref);
798 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
799 04f57cb3 2019-07-25 stsp return err;
800 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
801 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
803 a367ff0f 2019-05-14 stsp return err;
806 4ed7e80c 2018-05-20 stsp static const struct got_error *
807 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
809 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
810 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
811 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
812 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
813 c09a553d 2018-03-12 stsp char *repo_path = NULL;
814 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
815 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
816 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
817 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
818 72151b04 2019-05-11 stsp int ch, same_path_prefix;
819 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
821 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
823 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
824 0bb8a95e 2018-03-12 stsp switch (ch) {
826 08573d5b 2019-05-14 stsp branch_name = optarg;
829 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
830 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
831 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
834 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
837 2deda0b9 2019-03-07 stsp usage_checkout();
838 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
842 0bb8a95e 2018-03-12 stsp argc -= optind;
843 0bb8a95e 2018-03-12 stsp argv += optind;
845 6715a751 2018-03-16 stsp #ifndef PROFILE
846 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
847 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
848 c09a553d 2018-03-12 stsp err(1, "pledge");
850 0bb8a95e 2018-03-12 stsp if (argc == 1) {
851 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
852 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
853 76089277 2018-04-01 stsp if (repo_path == NULL)
854 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
855 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
856 76089277 2018-04-01 stsp if (cwd == NULL) {
857 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
860 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
861 230a42bd 2019-05-11 jcs base = basename(path_prefix);
862 230a42bd 2019-05-11 jcs if (base == NULL) {
863 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
864 230a42bd 2019-05-11 jcs path_prefix);
868 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
869 230a42bd 2019-05-11 jcs if (base == NULL) {
870 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
875 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
876 c09a553d 2018-03-12 stsp if (dotgit)
877 c09a553d 2018-03-12 stsp *dotgit = '\0';
878 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
879 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
884 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
885 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
886 76089277 2018-04-01 stsp if (repo_path == NULL) {
887 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
890 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
891 76089277 2018-04-01 stsp if (worktree_path == NULL) {
892 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
893 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
897 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
898 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
899 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
904 c09a553d 2018-03-12 stsp usage_checkout();
906 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
907 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
909 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
910 c09a553d 2018-03-12 stsp if (error != NULL)
913 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
914 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
915 c530dc23 2019-07-23 stsp if (error) {
916 c530dc23 2019-07-23 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
918 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
919 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
920 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
925 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
929 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
930 c09a553d 2018-03-12 stsp if (error != NULL)
933 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
934 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
937 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
938 c09a553d 2018-03-12 stsp if (error != NULL)
941 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
942 e5dc7198 2018-12-29 stsp path_prefix);
943 e5dc7198 2018-12-29 stsp if (error != NULL)
945 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
946 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
950 8069f636 2019-01-12 stsp if (commit_id_str) {
951 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
952 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
955 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
956 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
957 8069f636 2019-01-12 stsp if (error != NULL) {
958 8069f636 2019-01-12 stsp free(commit_id);
961 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
964 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
965 8069f636 2019-01-12 stsp commit_id);
966 8069f636 2019-01-12 stsp free(commit_id);
971 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
974 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
975 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
976 c09a553d 2018-03-12 stsp if (error != NULL)
979 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
982 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
983 8069f636 2019-01-12 stsp free(commit_id_str);
984 76089277 2018-04-01 stsp free(repo_path);
985 507dc3bb 2018-12-29 stsp free(worktree_path);
986 507dc3bb 2018-12-29 stsp return error;
989 507dc3bb 2018-12-29 stsp __dead static void
990 507dc3bb 2018-12-29 stsp usage_update(void)
992 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
993 507dc3bb 2018-12-29 stsp getprogname());
997 1ee397ad 2019-07-12 stsp static const struct got_error *
998 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1000 784955db 2019-01-12 stsp int *did_something = arg;
1002 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1003 1ee397ad 2019-07-12 stsp return NULL;
1005 1545c615 2019-02-10 stsp *did_something = 1;
1007 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1008 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1009 1ee397ad 2019-07-12 stsp return NULL;
1011 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1013 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1014 1ee397ad 2019-07-12 stsp return NULL;
1017 be7061eb 2018-12-30 stsp static const struct got_error *
1018 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1019 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1020 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1022 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1023 a1fb16d8 2019-05-24 stsp char *base_id_str;
1024 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1026 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1027 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1028 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1029 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1030 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1031 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1032 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1035 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1036 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1037 a1fb16d8 2019-05-24 stsp if (err) {
1038 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1039 a1fb16d8 2019-05-24 stsp return err;
1040 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1042 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1043 a1fb16d8 2019-05-24 stsp return NULL;
1045 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1046 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1047 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1049 a1fb16d8 2019-05-24 stsp return err;
1050 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1051 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1052 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1053 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1054 0ebf8283 2019-07-24 stsp return NULL;
1057 0ebf8283 2019-07-24 stsp static const struct got_error *
1058 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1060 0ebf8283 2019-07-24 stsp const struct got_error *err;
1061 0ebf8283 2019-07-24 stsp int in_progress;
1063 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1065 0ebf8283 2019-07-24 stsp return err;
1066 0ebf8283 2019-07-24 stsp if (in_progress)
1067 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1069 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1071 0ebf8283 2019-07-24 stsp return err;
1072 0ebf8283 2019-07-24 stsp if (in_progress)
1073 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1075 a1fb16d8 2019-05-24 stsp return NULL;
1078 a5edda0a 2019-07-27 stsp static const struct got_error *
1079 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1080 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1082 a5edda0a 2019-07-27 stsp const struct got_error *err;
1083 a5edda0a 2019-07-27 stsp char *path;
1086 a5edda0a 2019-07-27 stsp if (argc == 0) {
1087 a5edda0a 2019-07-27 stsp path = strdup("");
1088 a5edda0a 2019-07-27 stsp if (path == NULL)
1089 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1090 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1093 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1094 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1097 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1098 a5edda0a 2019-07-27 stsp if (err) {
1099 a5edda0a 2019-07-27 stsp free(path);
1104 a5edda0a 2019-07-27 stsp return err;
1107 a1fb16d8 2019-05-24 stsp static const struct got_error *
1108 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1110 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1111 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1112 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1113 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1114 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1115 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1116 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1117 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1118 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1119 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1120 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1122 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1124 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1125 507dc3bb 2018-12-29 stsp switch (ch) {
1127 024e9686 2019-05-14 stsp branch_name = optarg;
1130 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1131 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1132 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1135 2deda0b9 2019-03-07 stsp usage_update();
1136 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1140 507dc3bb 2018-12-29 stsp argc -= optind;
1141 507dc3bb 2018-12-29 stsp argv += optind;
1143 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1144 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1145 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1146 507dc3bb 2018-12-29 stsp err(1, "pledge");
1148 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1149 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1150 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1151 c4cdcb68 2019-04-03 stsp goto done;
1153 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1154 7d5807f4 2019-07-11 stsp if (error)
1155 7d5807f4 2019-07-11 stsp goto done;
1157 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1158 c4cdcb68 2019-04-03 stsp if (error)
1159 7d5807f4 2019-07-11 stsp goto done;
1161 f2ea84fa 2019-07-27 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1162 f2ea84fa 2019-07-27 stsp if (error)
1163 f2ea84fa 2019-07-27 stsp goto done;
1165 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1166 507dc3bb 2018-12-29 stsp if (error != NULL)
1167 507dc3bb 2018-12-29 stsp goto done;
1169 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1170 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1171 0266afb7 2019-01-04 stsp if (error)
1172 0266afb7 2019-01-04 stsp goto done;
1174 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1175 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1176 024e9686 2019-05-14 stsp if (error != NULL)
1177 024e9686 2019-05-14 stsp goto done;
1178 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1179 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1180 9c4b8182 2019-01-02 stsp if (error != NULL)
1181 9c4b8182 2019-01-02 stsp goto done;
1182 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1183 507dc3bb 2018-12-29 stsp if (error != NULL)
1184 507dc3bb 2018-12-29 stsp goto done;
1186 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1187 04f57cb3 2019-07-25 stsp free(commit_id_str);
1188 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1189 30837e32 2019-07-25 stsp if (error)
1190 a297e751 2019-07-11 stsp goto done;
1191 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1192 a297e751 2019-07-11 stsp if (error)
1193 507dc3bb 2018-12-29 stsp goto done;
1196 a1fb16d8 2019-05-24 stsp if (branch_name) {
1197 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1198 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1199 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1201 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1202 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1203 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1204 024e9686 2019-05-14 stsp goto done;
1206 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1207 024e9686 2019-05-14 stsp if (error)
1208 024e9686 2019-05-14 stsp goto done;
1209 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1210 a367ff0f 2019-05-14 stsp free(head_commit_id);
1211 024e9686 2019-05-14 stsp if (error != NULL)
1212 024e9686 2019-05-14 stsp goto done;
1213 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1214 a367ff0f 2019-05-14 stsp if (error)
1215 a367ff0f 2019-05-14 stsp goto done;
1216 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1217 024e9686 2019-05-14 stsp if (error)
1218 024e9686 2019-05-14 stsp goto done;
1220 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1221 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1222 a367ff0f 2019-05-14 stsp if (error != NULL) {
1223 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1224 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1225 024e9686 2019-05-14 stsp goto done;
1227 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1228 a367ff0f 2019-05-14 stsp if (error)
1229 a367ff0f 2019-05-14 stsp goto done;
1232 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1233 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1234 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1235 507dc3bb 2018-12-29 stsp commit_id);
1236 507dc3bb 2018-12-29 stsp if (error)
1237 507dc3bb 2018-12-29 stsp goto done;
1240 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1241 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1242 507dc3bb 2018-12-29 stsp if (error != NULL)
1243 507dc3bb 2018-12-29 stsp goto done;
1245 784955db 2019-01-12 stsp if (did_something)
1246 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1248 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1250 c09a553d 2018-03-12 stsp free(worktree_path);
1251 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1252 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1253 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1254 507dc3bb 2018-12-29 stsp free(commit_id);
1255 9c4b8182 2019-01-02 stsp free(commit_id_str);
1256 c09a553d 2018-03-12 stsp return error;
1259 f42b1b34 2018-03-12 stsp static const struct got_error *
1260 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1261 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1263 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1264 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1265 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1266 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1267 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1269 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1270 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1272 79109fed 2018-03-27 stsp return err;
1274 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1275 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1276 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1278 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1280 79109fed 2018-03-27 stsp return err;
1282 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1283 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1284 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1286 0f2b3dca 2018-12-22 stsp return err;
1288 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1290 79109fed 2018-03-27 stsp return err;
1293 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1295 0f2b3dca 2018-12-22 stsp goto done;
1297 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1298 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1299 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1300 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1301 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1303 79109fed 2018-03-27 stsp if (tree1)
1304 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1305 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1306 0f2b3dca 2018-12-22 stsp free(id_str1);
1307 0f2b3dca 2018-12-22 stsp free(id_str2);
1308 79109fed 2018-03-27 stsp return err;
1311 4bb494d5 2018-06-16 stsp static char *
1312 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1314 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
1315 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1317 6c281f94 2018-06-11 stsp *p = '\0';
1321 79109fed 2018-03-27 stsp static const struct got_error *
1322 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1323 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1324 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1326 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1327 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1328 6c281f94 2018-06-11 stsp char datebuf[26];
1329 45d799e2 2018-12-23 stsp time_t committer_time;
1330 45d799e2 2018-12-23 stsp const char *author, *committer;
1331 199a4027 2019-02-02 stsp char *refs_str = NULL;
1332 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1334 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1336 199a4027 2019-02-02 stsp const char *name;
1337 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
1339 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1340 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1342 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1343 199a4027 2019-02-02 stsp name += 5;
1344 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1346 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1347 e34f9ed6 2019-02-02 stsp name += 6;
1348 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1349 141c2bff 2019-02-04 stsp name += 8;
1350 199a4027 2019-02-02 stsp s = refs_str;
1351 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1352 199a4027 2019-02-02 stsp name) == -1) {
1353 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1359 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1361 8bf5b3c9 2018-03-17 stsp return err;
1363 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
1364 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1365 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1366 832c249c 2018-06-10 stsp free(id_str);
1367 d3d493d7 2019-02-21 stsp id_str = NULL;
1368 d3d493d7 2019-02-21 stsp free(refs_str);
1369 d3d493d7 2019-02-21 stsp refs_str = NULL;
1370 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1371 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1372 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1373 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
1374 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1375 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1376 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1377 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1378 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1379 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1380 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1381 3fe1abad 2018-06-10 stsp int n = 1;
1382 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1383 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1384 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1386 a0603db2 2018-06-10 stsp return err;
1387 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1388 a0603db2 2018-06-10 stsp free(id_str);
1392 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1393 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
1394 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1396 621015ac 2018-07-23 stsp logmsg = logmsg0;
1398 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1400 832c249c 2018-06-10 stsp printf(" %s\n", line);
1401 832c249c 2018-06-10 stsp } while (line);
1402 621015ac 2018-07-23 stsp free(logmsg0);
1404 971751ac 2018-03-27 stsp if (show_patch) {
1405 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1406 971751ac 2018-03-27 stsp if (err == 0)
1407 971751ac 2018-03-27 stsp printf("\n");
1410 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1411 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1412 07862c20 2018-09-15 stsp return err;
1415 f42b1b34 2018-03-12 stsp static const struct got_error *
1416 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1417 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1418 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1420 f42b1b34 2018-03-12 stsp const struct got_error *err;
1421 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1423 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1424 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1426 f42b1b34 2018-03-12 stsp return err;
1427 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1429 fcc85cad 2018-07-22 stsp goto done;
1431 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1432 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1434 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1437 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1438 372ccdbb 2018-06-10 stsp if (err) {
1439 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1440 9ba79e04 2018-06-11 stsp err = NULL;
1443 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1445 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1451 b43fbaa0 2018-06-11 stsp if (id == NULL)
1454 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1457 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1459 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1460 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1464 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1465 f42b1b34 2018-03-12 stsp return err;
1468 4ed7e80c 2018-05-20 stsp __dead static void
1469 6f3d1eb0 2018-03-12 stsp usage_log(void)
1471 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1472 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1476 4ed7e80c 2018-05-20 stsp static const struct got_error *
1477 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1479 f42b1b34 2018-03-12 stsp const struct got_error *error;
1480 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1481 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1482 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1483 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1484 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1485 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1486 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1487 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1488 64a96a6d 2018-04-01 stsp const char *errstr;
1489 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1491 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1493 6715a751 2018-03-16 stsp #ifndef PROFILE
1494 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1497 f42b1b34 2018-03-12 stsp err(1, "pledge");
1500 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1501 79109fed 2018-03-27 stsp switch (ch) {
1503 79109fed 2018-03-27 stsp show_patch = 1;
1506 d142fc45 2018-04-01 stsp start_commit = optarg;
1509 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1511 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1512 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1515 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1516 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1517 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1520 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1523 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1524 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1525 04ca23f4 2018-07-16 stsp err(1, "-r option");
1526 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1529 2deda0b9 2019-03-07 stsp usage_log();
1530 79109fed 2018-03-27 stsp /* NOTREACHED */
1534 79109fed 2018-03-27 stsp argc -= optind;
1535 79109fed 2018-03-27 stsp argv += optind;
1537 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1538 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1539 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1540 04ca23f4 2018-07-16 stsp goto done;
1543 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1544 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1545 cffc0aa4 2019-02-05 stsp goto done;
1546 cffc0aa4 2019-02-05 stsp error = NULL;
1548 e7301579 2019-03-18 stsp if (argc == 0) {
1549 cbd1af7a 2019-03-18 stsp path = strdup("");
1550 e7301579 2019-03-18 stsp if (path == NULL) {
1551 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1552 cbd1af7a 2019-03-18 stsp goto done;
1554 e7301579 2019-03-18 stsp } else if (argc == 1) {
1555 e7301579 2019-03-18 stsp if (worktree) {
1556 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1558 e7301579 2019-03-18 stsp if (error)
1559 e7301579 2019-03-18 stsp goto done;
1561 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1562 e7301579 2019-03-18 stsp if (path == NULL) {
1563 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1564 e7301579 2019-03-18 stsp goto done;
1568 cbd1af7a 2019-03-18 stsp usage_log();
1570 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1571 5486daa2 2019-05-11 stsp repo_path = worktree ?
1572 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1574 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1575 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1576 cffc0aa4 2019-02-05 stsp goto done;
1579 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1580 d7d4f210 2018-03-12 stsp if (error != NULL)
1581 04ca23f4 2018-07-16 stsp goto done;
1583 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1584 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1585 c02c541e 2019-03-29 stsp if (error)
1586 c02c541e 2019-03-29 stsp goto done;
1588 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1589 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1590 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1591 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1592 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1593 3235492e 2018-04-01 stsp if (error != NULL)
1594 3235492e 2018-04-01 stsp return error;
1595 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1596 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1597 3235492e 2018-04-01 stsp if (error != NULL)
1598 3235492e 2018-04-01 stsp return error;
1599 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1601 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1602 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1603 3235492e 2018-04-01 stsp if (error == NULL) {
1604 1caad61b 2019-02-01 stsp int obj_type;
1605 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1606 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1607 d1f2edc9 2018-06-13 stsp if (error != NULL)
1608 1caad61b 2019-02-01 stsp goto done;
1609 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1610 1caad61b 2019-02-01 stsp if (error != NULL)
1611 1caad61b 2019-02-01 stsp goto done;
1612 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1613 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1614 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1615 1caad61b 2019-02-01 stsp if (error != NULL)
1616 1caad61b 2019-02-01 stsp goto done;
1617 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1618 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1619 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1620 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1621 1caad61b 2019-02-01 stsp goto done;
1624 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1625 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1626 1caad61b 2019-02-01 stsp if (id == NULL)
1627 638f9024 2019-05-13 stsp error = got_error_from_errno(
1628 230a42bd 2019-05-11 jcs "got_object_id_dup");
1629 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1630 1caad61b 2019-02-01 stsp if (error)
1631 1caad61b 2019-02-01 stsp goto done;
1632 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1633 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1634 1caad61b 2019-02-01 stsp goto done;
1636 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1637 d1f2edc9 2018-06-13 stsp if (error != NULL)
1638 1caad61b 2019-02-01 stsp goto done;
1640 15a94983 2018-12-23 stsp if (commit == NULL) {
1641 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1642 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1643 d1f2edc9 2018-06-13 stsp if (error != NULL)
1644 d1f2edc9 2018-06-13 stsp return error;
1647 d7d4f210 2018-03-12 stsp if (error != NULL)
1648 04ca23f4 2018-07-16 stsp goto done;
1650 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1651 04ca23f4 2018-07-16 stsp if (error != NULL)
1652 04ca23f4 2018-07-16 stsp goto done;
1653 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1654 04ca23f4 2018-07-16 stsp free(path);
1655 04ca23f4 2018-07-16 stsp path = in_repo_path;
1658 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1659 199a4027 2019-02-02 stsp if (error)
1660 199a4027 2019-02-02 stsp goto done;
1662 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1663 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1665 04ca23f4 2018-07-16 stsp free(path);
1666 04ca23f4 2018-07-16 stsp free(repo_path);
1667 04ca23f4 2018-07-16 stsp free(cwd);
1669 cffc0aa4 2019-02-05 stsp if (worktree)
1670 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1671 ad242220 2018-09-08 stsp if (repo) {
1672 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1673 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1674 ad242220 2018-09-08 stsp if (error == NULL)
1675 ad242220 2018-09-08 stsp error = repo_error;
1677 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1678 8bf5b3c9 2018-03-17 stsp return error;
1681 4ed7e80c 2018-05-20 stsp __dead static void
1682 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1684 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1685 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1689 b72f483a 2019-02-05 stsp struct print_diff_arg {
1690 b72f483a 2019-02-05 stsp struct got_repository *repo;
1691 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1692 b72f483a 2019-02-05 stsp int diff_context;
1693 3fc0c068 2019-02-10 stsp const char *id_str;
1694 3fc0c068 2019-02-10 stsp int header_shown;
1695 98eaaa12 2019-08-03 stsp int diff_staged;
1698 4ed7e80c 2018-05-20 stsp static const struct got_error *
1699 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
1700 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
1701 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1703 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1704 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1705 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1706 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1707 b72f483a 2019-02-05 stsp char *abspath = NULL;
1708 b72f483a 2019-02-05 stsp struct stat sb;
1710 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1711 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
1712 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
1713 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
1714 98eaaa12 2019-08-03 stsp return NULL;
1716 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
1717 98eaaa12 2019-08-03 stsp return NULL;
1718 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
1719 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
1720 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
1721 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
1722 98eaaa12 2019-08-03 stsp return NULL;
1725 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1726 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
1727 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
1728 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
1729 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1732 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1733 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
1734 98eaaa12 2019-08-03 stsp switch (staged_status) {
1735 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
1736 98eaaa12 2019-08-03 stsp label1 = path;
1737 98eaaa12 2019-08-03 stsp label2 = path;
1739 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
1740 98eaaa12 2019-08-03 stsp label2 = path;
1742 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
1743 98eaaa12 2019-08-03 stsp label1 = path;
1746 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
1748 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1749 98eaaa12 2019-08-03 stsp label1, label2, a->diff_context, a->repo, stdout);
1752 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
1753 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
1754 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1756 408b4ebc 2019-08-03 stsp else if (status != GOT_STATUS_ADD)
1757 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1759 408b4ebc 2019-08-03 stsp goto done;
1761 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1762 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1763 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1764 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1765 2ec1f75b 2019-03-26 stsp goto done;
1768 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1769 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1770 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1771 2ec1f75b 2019-03-26 stsp goto done;
1773 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1774 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1775 2ec1f75b 2019-03-26 stsp goto done;
1778 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1780 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1783 b72f483a 2019-02-05 stsp if (blob1)
1784 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1785 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1786 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1787 b72f483a 2019-02-05 stsp free(abspath);
1788 927df6b7 2019-02-10 stsp return err;
1791 927df6b7 2019-02-10 stsp static const struct got_error *
1792 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1794 b00d56cd 2018-04-01 stsp const struct got_error *error;
1795 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1796 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1797 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1798 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1799 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1800 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1801 15a94983 2018-12-23 stsp int type1, type2;
1802 98eaaa12 2019-08-03 stsp int diff_context = 3, diff_staged = 0, ch;
1803 c0cc5c62 2018-10-18 stsp const char *errstr;
1804 927df6b7 2019-02-10 stsp char *path = NULL;
1806 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1807 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1808 25eccc22 2019-01-04 stsp NULL) == -1)
1809 b00d56cd 2018-04-01 stsp err(1, "pledge");
1812 98eaaa12 2019-08-03 stsp while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1813 b00d56cd 2018-04-01 stsp switch (ch) {
1815 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1816 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1817 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1820 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1821 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1822 b72f483a 2019-02-05 stsp err(1, "-r option");
1823 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1826 98eaaa12 2019-08-03 stsp diff_staged = 1;
1829 2deda0b9 2019-03-07 stsp usage_diff();
1830 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1834 b00d56cd 2018-04-01 stsp argc -= optind;
1835 b00d56cd 2018-04-01 stsp argv += optind;
1837 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1838 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1839 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1840 b72f483a 2019-02-05 stsp goto done;
1842 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1843 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1844 927df6b7 2019-02-10 stsp goto done;
1845 927df6b7 2019-02-10 stsp if (argc <= 1) {
1846 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1847 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1848 927df6b7 2019-02-10 stsp goto done;
1850 b72f483a 2019-02-05 stsp if (repo_path)
1852 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1853 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1854 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1855 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1856 927df6b7 2019-02-10 stsp goto done;
1858 927df6b7 2019-02-10 stsp if (argc == 1) {
1859 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1861 927df6b7 2019-02-10 stsp if (error)
1862 927df6b7 2019-02-10 stsp goto done;
1864 927df6b7 2019-02-10 stsp path = strdup("");
1865 927df6b7 2019-02-10 stsp if (path == NULL) {
1866 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1867 927df6b7 2019-02-10 stsp goto done;
1870 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1871 98eaaa12 2019-08-03 stsp if (diff_staged)
1872 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
1873 98eaaa12 2019-08-03 stsp "objects in repository");
1874 15a94983 2018-12-23 stsp id_str1 = argv[0];
1875 15a94983 2018-12-23 stsp id_str2 = argv[1];
1876 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1877 30db809c 2019-06-05 stsp repo_path =
1878 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1879 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1880 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1881 30db809c 2019-06-05 stsp goto done;
1885 b00d56cd 2018-04-01 stsp usage_diff();
1887 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1888 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1889 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1890 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1893 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1894 76089277 2018-04-01 stsp free(repo_path);
1895 b00d56cd 2018-04-01 stsp if (error != NULL)
1896 b00d56cd 2018-04-01 stsp goto done;
1898 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1899 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1900 c02c541e 2019-03-29 stsp if (error)
1901 c02c541e 2019-03-29 stsp goto done;
1903 30db809c 2019-06-05 stsp if (argc <= 1) {
1904 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1905 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
1906 b72f483a 2019-02-05 stsp char *id_str;
1908 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
1910 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1911 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1912 b72f483a 2019-02-05 stsp if (error)
1913 b72f483a 2019-02-05 stsp goto done;
1914 b72f483a 2019-02-05 stsp arg.repo = repo;
1915 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1916 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1917 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1918 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1919 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
1921 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
1922 72ea6654 2019-07-27 stsp if (error)
1923 72ea6654 2019-07-27 stsp goto done;
1925 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
1926 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1927 b72f483a 2019-02-05 stsp free(id_str);
1928 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
1929 b72f483a 2019-02-05 stsp goto done;
1932 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1933 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1934 e02e74af 2019-05-28 stsp if (error) {
1935 e02e74af 2019-05-28 stsp struct got_reference *ref;
1936 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1937 e02e74af 2019-05-28 stsp goto done;
1938 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1939 e02e74af 2019-05-28 stsp if (error != NULL)
1940 e02e74af 2019-05-28 stsp goto done;
1941 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1942 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1943 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1944 5e70831e 2019-05-28 stsp goto done;
1946 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1947 e02e74af 2019-05-28 stsp got_ref_close(ref);
1948 e02e74af 2019-05-28 stsp if (error != NULL)
1949 5e70831e 2019-05-28 stsp goto done;
1951 a297e751 2019-07-11 stsp error = got_object_id_str(&label1, id1);
1952 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1953 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1954 e02e74af 2019-05-28 stsp goto done;
1958 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1959 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1960 e02e74af 2019-05-28 stsp if (error) {
1961 e02e74af 2019-05-28 stsp struct got_reference *ref;
1962 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1963 e02e74af 2019-05-28 stsp goto done;
1964 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1965 e02e74af 2019-05-28 stsp if (error != NULL)
1966 5e70831e 2019-05-28 stsp goto done;
1967 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1968 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1969 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1970 e02e74af 2019-05-28 stsp goto done;
1972 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1973 e02e74af 2019-05-28 stsp got_ref_close(ref);
1974 e02e74af 2019-05-28 stsp if (error != NULL)
1975 e02e74af 2019-05-28 stsp goto done;
1977 a297e751 2019-07-11 stsp error = got_object_id_str(&label2, id2);
1978 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1979 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1980 5e70831e 2019-05-28 stsp goto done;
1984 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1985 15a94983 2018-12-23 stsp if (error)
1986 15a94983 2018-12-23 stsp goto done;
1988 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1989 15a94983 2018-12-23 stsp if (error)
1990 15a94983 2018-12-23 stsp goto done;
1992 15a94983 2018-12-23 stsp if (type1 != type2) {
1993 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1994 b00d56cd 2018-04-01 stsp goto done;
1997 15a94983 2018-12-23 stsp switch (type1) {
1998 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1999 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2000 54156555 2018-12-24 stsp diff_context, repo, stdout);
2002 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2003 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2004 54156555 2018-12-24 stsp diff_context, repo, stdout);
2006 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2007 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2008 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2009 c0cc5c62 2018-10-18 stsp repo, stdout);
2012 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2016 5e70831e 2019-05-28 stsp free(label1);
2017 5e70831e 2019-05-28 stsp free(label2);
2018 15a94983 2018-12-23 stsp free(id1);
2019 15a94983 2018-12-23 stsp free(id2);
2020 927df6b7 2019-02-10 stsp free(path);
2021 b72f483a 2019-02-05 stsp if (worktree)
2022 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2023 ad242220 2018-09-08 stsp if (repo) {
2024 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2025 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2026 ad242220 2018-09-08 stsp if (error == NULL)
2027 ad242220 2018-09-08 stsp error = repo_error;
2029 b00d56cd 2018-04-01 stsp return error;
2032 404c43c4 2018-06-21 stsp __dead static void
2033 404c43c4 2018-06-21 stsp usage_blame(void)
2035 9270e621 2019-02-05 stsp fprintf(stderr,
2036 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2037 404c43c4 2018-06-21 stsp getprogname());
2041 404c43c4 2018-06-21 stsp static const struct got_error *
2042 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2044 404c43c4 2018-06-21 stsp const struct got_error *error;
2045 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2046 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2047 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2048 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2049 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2052 404c43c4 2018-06-21 stsp #ifndef PROFILE
2053 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2054 36e2fb66 2019-01-04 stsp NULL) == -1)
2055 404c43c4 2018-06-21 stsp err(1, "pledge");
2058 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2059 404c43c4 2018-06-21 stsp switch (ch) {
2061 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2064 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2065 66bea077 2018-08-02 stsp if (repo_path == NULL)
2066 66bea077 2018-08-02 stsp err(1, "-r option");
2067 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2070 2deda0b9 2019-03-07 stsp usage_blame();
2071 404c43c4 2018-06-21 stsp /* NOTREACHED */
2075 404c43c4 2018-06-21 stsp argc -= optind;
2076 404c43c4 2018-06-21 stsp argv += optind;
2078 a39318fd 2018-08-02 stsp if (argc == 1)
2079 404c43c4 2018-06-21 stsp path = argv[0];
2081 404c43c4 2018-06-21 stsp usage_blame();
2083 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2084 66bea077 2018-08-02 stsp if (cwd == NULL) {
2085 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2086 66bea077 2018-08-02 stsp goto done;
2088 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2089 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2090 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2091 66bea077 2018-08-02 stsp goto done;
2093 0c06baac 2019-02-05 stsp error = NULL;
2094 0c06baac 2019-02-05 stsp if (worktree) {
2095 0c06baac 2019-02-05 stsp repo_path =
2096 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2097 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2098 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2099 0c06baac 2019-02-05 stsp if (error)
2100 0c06baac 2019-02-05 stsp goto done;
2102 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2103 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2104 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2105 0c06baac 2019-02-05 stsp goto done;
2110 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2111 c02c541e 2019-03-29 stsp if (error != NULL)
2112 36e2fb66 2019-01-04 stsp goto done;
2114 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2115 c02c541e 2019-03-29 stsp if (error)
2116 404c43c4 2018-06-21 stsp goto done;
2118 0c06baac 2019-02-05 stsp if (worktree) {
2119 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2120 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2121 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2122 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2123 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2124 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2125 6efaaa2d 2019-02-05 stsp path) == -1) {
2126 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2127 0c06baac 2019-02-05 stsp goto done;
2129 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2132 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2134 0c06baac 2019-02-05 stsp if (error)
2135 66bea077 2018-08-02 stsp goto done;
2137 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2138 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2139 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2140 404c43c4 2018-06-21 stsp if (error != NULL)
2141 66bea077 2018-08-02 stsp goto done;
2142 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2143 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2144 404c43c4 2018-06-21 stsp if (error != NULL)
2145 66bea077 2018-08-02 stsp goto done;
2147 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2148 30837e32 2019-07-25 stsp if (error)
2149 66bea077 2018-08-02 stsp goto done;
2152 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
2154 66bea077 2018-08-02 stsp free(in_repo_path);
2155 66bea077 2018-08-02 stsp free(repo_path);
2156 66bea077 2018-08-02 stsp free(cwd);
2157 404c43c4 2018-06-21 stsp free(commit_id);
2158 0c06baac 2019-02-05 stsp if (worktree)
2159 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2160 ad242220 2018-09-08 stsp if (repo) {
2161 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2162 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2163 ad242220 2018-09-08 stsp if (error == NULL)
2164 ad242220 2018-09-08 stsp error = repo_error;
2166 404c43c4 2018-06-21 stsp return error;
2169 5de5890b 2018-10-18 stsp __dead static void
2170 5de5890b 2018-10-18 stsp usage_tree(void)
2172 c1669e2e 2019-01-09 stsp fprintf(stderr,
2173 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2174 5de5890b 2018-10-18 stsp getprogname());
2178 c1669e2e 2019-01-09 stsp static void
2179 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2180 c1669e2e 2019-01-09 stsp const char *root_path)
2182 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2184 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2185 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2188 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2189 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
2190 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2193 5de5890b 2018-10-18 stsp static const struct got_error *
2194 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2195 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2196 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2198 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2199 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2200 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2201 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2202 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2204 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2206 5de5890b 2018-10-18 stsp goto done;
2208 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2210 5de5890b 2018-10-18 stsp goto done;
2211 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2212 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2213 5de5890b 2018-10-18 stsp while (te) {
2214 5de5890b 2018-10-18 stsp char *id = NULL;
2216 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2219 5de5890b 2018-10-18 stsp if (show_ids) {
2220 5de5890b 2018-10-18 stsp char *id_str;
2221 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2223 5de5890b 2018-10-18 stsp goto done;
2224 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2225 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2226 5de5890b 2018-10-18 stsp free(id_str);
2227 5de5890b 2018-10-18 stsp goto done;
2229 5de5890b 2018-10-18 stsp free(id_str);
2231 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2234 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2235 c1669e2e 2019-01-09 stsp char *child_path;
2236 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2237 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2238 c1669e2e 2019-01-09 stsp te->name) == -1) {
2239 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2240 c1669e2e 2019-01-09 stsp goto done;
2242 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2243 c1669e2e 2019-01-09 stsp root_path, repo);
2244 c1669e2e 2019-01-09 stsp free(child_path);
2246 c1669e2e 2019-01-09 stsp goto done;
2249 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2253 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2254 5de5890b 2018-10-18 stsp free(tree_id);
2255 5de5890b 2018-10-18 stsp return err;
2258 5de5890b 2018-10-18 stsp static const struct got_error *
2259 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2261 5de5890b 2018-10-18 stsp const struct got_error *error;
2262 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2263 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2264 7a2c19d6 2019-02-05 stsp const char *path;
2265 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2266 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2267 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2268 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2271 5de5890b 2018-10-18 stsp #ifndef PROFILE
2272 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2273 0f8d269b 2019-01-04 stsp NULL) == -1)
2274 5de5890b 2018-10-18 stsp err(1, "pledge");
2277 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2278 5de5890b 2018-10-18 stsp switch (ch) {
2280 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2283 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2284 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2285 5de5890b 2018-10-18 stsp err(1, "-r option");
2286 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2289 5de5890b 2018-10-18 stsp show_ids = 1;
2292 c1669e2e 2019-01-09 stsp recurse = 1;
2295 2deda0b9 2019-03-07 stsp usage_tree();
2296 5de5890b 2018-10-18 stsp /* NOTREACHED */
2300 5de5890b 2018-10-18 stsp argc -= optind;
2301 5de5890b 2018-10-18 stsp argv += optind;
2303 5de5890b 2018-10-18 stsp if (argc == 1)
2304 5de5890b 2018-10-18 stsp path = argv[0];
2305 5de5890b 2018-10-18 stsp else if (argc > 1)
2306 5de5890b 2018-10-18 stsp usage_tree();
2308 7a2c19d6 2019-02-05 stsp path = NULL;
2310 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2311 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2312 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2313 9bf7a39b 2019-02-05 stsp goto done;
2315 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2316 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2317 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2318 7a2c19d6 2019-02-05 stsp goto done;
2320 7a2c19d6 2019-02-05 stsp error = NULL;
2321 7a2c19d6 2019-02-05 stsp if (worktree) {
2322 7a2c19d6 2019-02-05 stsp repo_path =
2323 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2324 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2325 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2326 7a2c19d6 2019-02-05 stsp if (error)
2327 7a2c19d6 2019-02-05 stsp goto done;
2329 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2330 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2331 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2332 7a2c19d6 2019-02-05 stsp goto done;
2337 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2338 5de5890b 2018-10-18 stsp if (error != NULL)
2339 c02c541e 2019-03-29 stsp goto done;
2341 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2342 c02c541e 2019-03-29 stsp if (error)
2343 5de5890b 2018-10-18 stsp goto done;
2345 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2346 9bf7a39b 2019-02-05 stsp if (worktree) {
2347 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2348 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2349 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2350 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2351 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2352 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2353 9bf7a39b 2019-02-05 stsp goto done;
2355 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2357 9bf7a39b 2019-02-05 stsp if (error)
2358 9bf7a39b 2019-02-05 stsp goto done;
2360 9bf7a39b 2019-02-05 stsp path = "/";
2362 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2363 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2364 9bf7a39b 2019-02-05 stsp if (error != NULL)
2365 9bf7a39b 2019-02-05 stsp goto done;
2368 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2369 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2370 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2371 5de5890b 2018-10-18 stsp if (error != NULL)
2372 5de5890b 2018-10-18 stsp goto done;
2373 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2374 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2375 5de5890b 2018-10-18 stsp if (error != NULL)
2376 5de5890b 2018-10-18 stsp goto done;
2378 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2379 30837e32 2019-07-25 stsp if (error)
2380 5de5890b 2018-10-18 stsp goto done;
2383 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2384 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2386 5de5890b 2018-10-18 stsp free(in_repo_path);
2387 5de5890b 2018-10-18 stsp free(repo_path);
2388 5de5890b 2018-10-18 stsp free(cwd);
2389 5de5890b 2018-10-18 stsp free(commit_id);
2390 7a2c19d6 2019-02-05 stsp if (worktree)
2391 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2392 5de5890b 2018-10-18 stsp if (repo) {
2393 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2394 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2395 5de5890b 2018-10-18 stsp if (error == NULL)
2396 5de5890b 2018-10-18 stsp error = repo_error;
2398 5de5890b 2018-10-18 stsp return error;
2401 6bad629b 2019-02-04 stsp __dead static void
2402 6bad629b 2019-02-04 stsp usage_status(void)
2404 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2408 b72f483a 2019-02-05 stsp static const struct got_error *
2409 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2410 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2411 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2413 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2414 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2415 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2416 b72f483a 2019-02-05 stsp return NULL;
2419 6bad629b 2019-02-04 stsp static const struct got_error *
2420 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2422 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2423 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2424 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2425 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2426 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2427 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2430 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2432 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2433 6bad629b 2019-02-04 stsp switch (ch) {
2435 2deda0b9 2019-03-07 stsp usage_status();
2436 6bad629b 2019-02-04 stsp /* NOTREACHED */
2440 6bad629b 2019-02-04 stsp argc -= optind;
2441 6bad629b 2019-02-04 stsp argv += optind;
2443 6bad629b 2019-02-04 stsp #ifndef PROFILE
2444 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2445 6bad629b 2019-02-04 stsp NULL) == -1)
2446 6bad629b 2019-02-04 stsp err(1, "pledge");
2448 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2449 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2450 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2451 927df6b7 2019-02-10 stsp goto done;
2454 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2455 927df6b7 2019-02-10 stsp if (error != NULL)
2456 927df6b7 2019-02-10 stsp goto done;
2458 a5edda0a 2019-07-27 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2459 a5edda0a 2019-07-27 stsp if (error)
2460 a5edda0a 2019-07-27 stsp goto done;
2462 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2463 6bad629b 2019-02-04 stsp if (error != NULL)
2464 6bad629b 2019-02-04 stsp goto done;
2466 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2467 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2468 6bad629b 2019-02-04 stsp if (error)
2469 6bad629b 2019-02-04 stsp goto done;
2471 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2472 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2474 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2475 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2476 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2477 927df6b7 2019-02-10 stsp free(cwd);
2478 d0eebce4 2019-03-11 stsp return error;
2481 d0eebce4 2019-03-11 stsp __dead static void
2482 d0eebce4 2019-03-11 stsp usage_ref(void)
2484 d0eebce4 2019-03-11 stsp fprintf(stderr,
2485 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2486 d0eebce4 2019-03-11 stsp getprogname());
2490 d0eebce4 2019-03-11 stsp static const struct got_error *
2491 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2493 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2494 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2495 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2497 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2498 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2500 d0eebce4 2019-03-11 stsp return err;
2502 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2503 d0eebce4 2019-03-11 stsp char *refstr;
2504 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2505 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2506 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2507 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2508 d0eebce4 2019-03-11 stsp free(refstr);
2511 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2512 d0eebce4 2019-03-11 stsp return NULL;
2515 d0eebce4 2019-03-11 stsp static const struct got_error *
2516 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2518 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2519 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2521 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2523 d0eebce4 2019-03-11 stsp return err;
2525 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2526 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2527 d0eebce4 2019-03-11 stsp return err;
2530 d0eebce4 2019-03-11 stsp static const struct got_error *
2531 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2533 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2534 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2535 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2538 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2539 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2540 d1644381 2019-07-14 stsp * an unintended typo.
2542 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2543 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2545 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2547 d83d9d5c 2019-05-13 stsp if (err) {
2548 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2550 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2551 d83d9d5c 2019-05-13 stsp return err;
2552 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2554 d83d9d5c 2019-05-13 stsp return err;
2555 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2556 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2558 d83d9d5c 2019-05-13 stsp return err;
2561 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2563 d0eebce4 2019-03-11 stsp goto done;
2565 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2568 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2570 d0eebce4 2019-03-11 stsp return err;
2573 d0eebce4 2019-03-11 stsp static const struct got_error *
2574 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2576 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2577 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2578 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2579 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2580 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2581 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2583 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2584 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2585 d0eebce4 2019-03-11 stsp switch (ch) {
2587 d0eebce4 2019-03-11 stsp delref = optarg;
2590 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2591 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2592 d0eebce4 2019-03-11 stsp err(1, "-r option");
2593 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2596 d0eebce4 2019-03-11 stsp do_list = 1;
2599 d0eebce4 2019-03-11 stsp usage_ref();
2600 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2604 d0eebce4 2019-03-11 stsp if (do_list && delref)
2605 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2607 d0eebce4 2019-03-11 stsp argc -= optind;
2608 d0eebce4 2019-03-11 stsp argv += optind;
2610 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2611 d0eebce4 2019-03-11 stsp if (argc > 0)
2612 d0eebce4 2019-03-11 stsp usage_ref();
2613 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2614 d0eebce4 2019-03-11 stsp usage_ref();
2616 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2617 e0b57350 2019-03-12 stsp if (do_list) {
2618 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2619 e0b57350 2019-03-12 stsp NULL) == -1)
2620 e0b57350 2019-03-12 stsp err(1, "pledge");
2622 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2623 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2624 e0b57350 2019-03-12 stsp err(1, "pledge");
2627 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2628 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2629 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2630 d0eebce4 2019-03-11 stsp goto done;
2633 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2634 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2635 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2636 d0eebce4 2019-03-11 stsp goto done;
2638 d0eebce4 2019-03-11 stsp error = NULL;
2639 d0eebce4 2019-03-11 stsp if (worktree) {
2640 d0eebce4 2019-03-11 stsp repo_path =
2641 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2642 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2643 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2644 d0eebce4 2019-03-11 stsp if (error)
2645 d0eebce4 2019-03-11 stsp goto done;
2647 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2648 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2649 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2650 d0eebce4 2019-03-11 stsp goto done;
2655 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2656 d0eebce4 2019-03-11 stsp if (error != NULL)
2657 d0eebce4 2019-03-11 stsp goto done;
2659 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2660 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2661 c02c541e 2019-03-29 stsp if (error)
2662 c02c541e 2019-03-29 stsp goto done;
2664 d0eebce4 2019-03-11 stsp if (do_list)
2665 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2666 d0eebce4 2019-03-11 stsp else if (delref)
2667 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2669 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2672 4e759de4 2019-06-26 stsp got_repo_close(repo);
2673 4e759de4 2019-06-26 stsp if (worktree)
2674 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2675 4e759de4 2019-06-26 stsp free(cwd);
2676 4e759de4 2019-06-26 stsp free(repo_path);
2677 4e759de4 2019-06-26 stsp return error;
2680 4e759de4 2019-06-26 stsp __dead static void
2681 4e759de4 2019-06-26 stsp usage_branch(void)
2683 4e759de4 2019-06-26 stsp fprintf(stderr,
2684 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2685 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2689 4e759de4 2019-06-26 stsp static const struct got_error *
2690 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
2692 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2693 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2694 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2696 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2698 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2700 4e759de4 2019-06-26 stsp return err;
2702 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2703 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
2704 4e759de4 2019-06-26 stsp char *refstr;
2705 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2706 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2708 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
2709 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
2710 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
2711 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
2713 ba882ee3 2019-07-11 stsp return err;
2714 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
2715 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
2716 ba882ee3 2019-07-11 stsp marker = "* ";
2718 ba882ee3 2019-07-11 stsp marker = "~ ";
2721 4e759de4 2019-06-26 stsp refname += 11;
2722 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2723 4e759de4 2019-06-26 stsp if (refstr == NULL)
2724 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2725 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
2726 4e759de4 2019-06-26 stsp free(refstr);
2729 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2730 4e759de4 2019-06-26 stsp return NULL;
2733 4e759de4 2019-06-26 stsp static const struct got_error *
2734 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2736 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2737 4e759de4 2019-06-26 stsp struct got_reference *ref;
2738 4e759de4 2019-06-26 stsp char *refname;
2740 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2741 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2743 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2745 4e759de4 2019-06-26 stsp goto done;
2747 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2748 4e759de4 2019-06-26 stsp got_ref_close(ref);
2750 4e759de4 2019-06-26 stsp free(refname);
2751 4e759de4 2019-06-26 stsp return err;
2754 4e759de4 2019-06-26 stsp static const struct got_error *
2755 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2756 4e759de4 2019-06-26 stsp const char *base_branch)
2758 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2759 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2760 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2761 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2762 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2765 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
2766 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
2767 d3f84d51 2019-07-11 stsp * an unintended typo.
2769 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
2770 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2772 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2773 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2774 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2775 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2776 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2777 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2779 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2781 4e759de4 2019-06-26 stsp goto done;
2782 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2783 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2785 4e759de4 2019-06-26 stsp goto done;
2787 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2788 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2789 4e759de4 2019-06-26 stsp goto done;
2792 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2793 4e759de4 2019-06-26 stsp if (err == NULL) {
2794 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2795 4e759de4 2019-06-26 stsp goto done;
2796 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2797 4e759de4 2019-06-26 stsp goto done;
2799 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2801 4e759de4 2019-06-26 stsp goto done;
2803 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2806 4e759de4 2019-06-26 stsp got_ref_close(ref);
2808 4e759de4 2019-06-26 stsp free(base_refname);
2809 4e759de4 2019-06-26 stsp free(refname);
2810 4e759de4 2019-06-26 stsp return err;
2813 4e759de4 2019-06-26 stsp static const struct got_error *
2814 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2816 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2817 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2818 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2819 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2820 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2821 4e759de4 2019-06-26 stsp const char *delref = NULL;
2823 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2824 4e759de4 2019-06-26 stsp switch (ch) {
2826 4e759de4 2019-06-26 stsp delref = optarg;
2829 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2830 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2831 4e759de4 2019-06-26 stsp err(1, "-r option");
2832 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2835 4e759de4 2019-06-26 stsp do_list = 1;
2838 4e759de4 2019-06-26 stsp usage_branch();
2839 4e759de4 2019-06-26 stsp /* NOTREACHED */
2843 4e759de4 2019-06-26 stsp if (do_list && delref)
2844 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2846 4e759de4 2019-06-26 stsp argc -= optind;
2847 4e759de4 2019-06-26 stsp argv += optind;
2849 4e759de4 2019-06-26 stsp if (do_list || delref) {
2850 4e759de4 2019-06-26 stsp if (argc > 0)
2851 4e759de4 2019-06-26 stsp usage_branch();
2852 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2853 4e759de4 2019-06-26 stsp usage_branch();
2855 4e759de4 2019-06-26 stsp #ifndef PROFILE
2856 4e759de4 2019-06-26 stsp if (do_list) {
2857 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2858 4e759de4 2019-06-26 stsp NULL) == -1)
2859 4e759de4 2019-06-26 stsp err(1, "pledge");
2861 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2862 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2863 4e759de4 2019-06-26 stsp err(1, "pledge");
2866 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2867 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2868 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2869 4e759de4 2019-06-26 stsp goto done;
2872 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2873 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2874 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2875 4e759de4 2019-06-26 stsp goto done;
2877 4e759de4 2019-06-26 stsp error = NULL;
2878 4e759de4 2019-06-26 stsp if (worktree) {
2879 4e759de4 2019-06-26 stsp repo_path =
2880 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2881 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2882 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2883 4e759de4 2019-06-26 stsp if (error)
2884 4e759de4 2019-06-26 stsp goto done;
2886 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2887 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2888 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2889 4e759de4 2019-06-26 stsp goto done;
2894 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2895 4e759de4 2019-06-26 stsp if (error != NULL)
2896 4e759de4 2019-06-26 stsp goto done;
2898 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2899 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2900 4e759de4 2019-06-26 stsp if (error)
2901 4e759de4 2019-06-26 stsp goto done;
2903 4e759de4 2019-06-26 stsp if (do_list)
2904 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
2905 4e759de4 2019-06-26 stsp else if (delref)
2906 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2908 4e759de4 2019-06-26 stsp const char *base_branch;
2909 4e759de4 2019-06-26 stsp if (argc == 1) {
2910 4e759de4 2019-06-26 stsp base_branch = worktree ?
2911 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2912 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2913 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
2914 dc5351b4 2019-07-30 stsp base_branch += 11;
2916 4e759de4 2019-06-26 stsp base_branch = argv[1];
2917 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2921 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2922 d0eebce4 2019-03-11 stsp if (worktree)
2923 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2924 d0eebce4 2019-03-11 stsp free(cwd);
2925 d0eebce4 2019-03-11 stsp free(repo_path);
2926 d00136be 2019-03-26 stsp return error;
2929 d00136be 2019-03-26 stsp __dead static void
2930 d00136be 2019-03-26 stsp usage_add(void)
2932 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2936 d00136be 2019-03-26 stsp static const struct got_error *
2937 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2939 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2940 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2941 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2942 1dd54920 2019-05-11 stsp char *cwd = NULL;
2943 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2944 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2947 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2949 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2950 d00136be 2019-03-26 stsp switch (ch) {
2952 d00136be 2019-03-26 stsp usage_add();
2953 d00136be 2019-03-26 stsp /* NOTREACHED */
2957 d00136be 2019-03-26 stsp argc -= optind;
2958 d00136be 2019-03-26 stsp argv += optind;
2960 43012d58 2019-07-14 stsp #ifndef PROFILE
2961 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2962 43012d58 2019-07-14 stsp NULL) == -1)
2963 43012d58 2019-07-14 stsp err(1, "pledge");
2965 723c305c 2019-05-11 jcs if (argc < 1)
2966 d00136be 2019-03-26 stsp usage_add();
2968 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2969 d00136be 2019-03-26 stsp if (cwd == NULL) {
2970 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2971 d00136be 2019-03-26 stsp goto done;
2974 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2975 d00136be 2019-03-26 stsp if (error)
2976 d00136be 2019-03-26 stsp goto done;
2978 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2979 031a5338 2019-03-26 stsp if (error != NULL)
2980 031a5338 2019-03-26 stsp goto done;
2982 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2983 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2984 d00136be 2019-03-26 stsp if (error)
2985 d00136be 2019-03-26 stsp goto done;
2987 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2988 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2989 723c305c 2019-05-11 jcs if (path == NULL) {
2990 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2994 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2995 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2996 1dd54920 2019-05-11 stsp if (error) {
2997 1dd54920 2019-05-11 stsp free(path);
3001 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
3002 1dd54920 2019-05-11 stsp NULL, repo);
3005 031a5338 2019-03-26 stsp got_repo_close(repo);
3006 d00136be 2019-03-26 stsp if (worktree)
3007 d00136be 2019-03-26 stsp got_worktree_close(worktree);
3008 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
3009 1dd54920 2019-05-11 stsp free((char *)pe->path);
3010 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
3011 2ec1f75b 2019-03-26 stsp free(cwd);
3012 2ec1f75b 2019-03-26 stsp return error;
3015 2ec1f75b 2019-03-26 stsp __dead static void
3016 648e4ef7 2019-07-09 stsp usage_remove(void)
3018 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3022 2ec1f75b 2019-03-26 stsp static const struct got_error *
3023 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
3025 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
3026 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
3027 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
3028 17ed4618 2019-06-02 stsp char *cwd = NULL;
3029 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
3030 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3031 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
3033 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
3035 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
3036 2ec1f75b 2019-03-26 stsp switch (ch) {
3038 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
3041 2ec1f75b 2019-03-26 stsp usage_add();
3042 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
3046 2ec1f75b 2019-03-26 stsp argc -= optind;
3047 2ec1f75b 2019-03-26 stsp argv += optind;
3049 43012d58 2019-07-14 stsp #ifndef PROFILE
3050 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3051 43012d58 2019-07-14 stsp NULL) == -1)
3052 43012d58 2019-07-14 stsp err(1, "pledge");
3054 17ed4618 2019-06-02 stsp if (argc < 1)
3055 648e4ef7 2019-07-09 stsp usage_remove();
3057 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
3058 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
3059 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3060 2ec1f75b 2019-03-26 stsp goto done;
3062 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3063 2ec1f75b 2019-03-26 stsp if (error)
3064 2ec1f75b 2019-03-26 stsp goto done;
3066 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3068 2ec1f75b 2019-03-26 stsp goto done;
3070 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3071 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3072 2ec1f75b 2019-03-26 stsp if (error)
3073 2ec1f75b 2019-03-26 stsp goto done;
3075 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
3076 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
3077 17ed4618 2019-06-02 stsp if (path == NULL) {
3078 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
3079 17ed4618 2019-06-02 stsp goto done;
3082 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
3083 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3084 17ed4618 2019-06-02 stsp if (error) {
3085 17ed4618 2019-06-02 stsp free(path);
3086 17ed4618 2019-06-02 stsp goto done;
3089 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
3090 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
3091 a129376b 2019-03-28 stsp if (error)
3092 a129376b 2019-03-28 stsp goto done;
3095 a129376b 2019-03-28 stsp got_repo_close(repo);
3096 a129376b 2019-03-28 stsp if (worktree)
3097 a129376b 2019-03-28 stsp got_worktree_close(worktree);
3098 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
3099 17ed4618 2019-06-02 stsp free((char *)pe->path);
3100 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
3101 a129376b 2019-03-28 stsp free(cwd);
3102 a129376b 2019-03-28 stsp return error;
3105 a129376b 2019-03-28 stsp __dead static void
3106 a129376b 2019-03-28 stsp usage_revert(void)
3108 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3112 1ee397ad 2019-07-12 stsp static const struct got_error *
3113 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3115 a129376b 2019-03-28 stsp while (path[0] == '/')
3117 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3118 1ee397ad 2019-07-12 stsp return NULL;
3121 a129376b 2019-03-28 stsp static const struct got_error *
3122 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3124 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3125 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3126 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3127 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3128 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3129 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3130 00db391e 2019-08-03 stsp const char *worktree_path;
3131 e20a8b6f 2019-06-04 stsp int ch, i;
3133 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3135 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3136 a129376b 2019-03-28 stsp switch (ch) {
3138 a129376b 2019-03-28 stsp usage_revert();
3139 a129376b 2019-03-28 stsp /* NOTREACHED */
3143 a129376b 2019-03-28 stsp argc -= optind;
3144 a129376b 2019-03-28 stsp argv += optind;
3146 43012d58 2019-07-14 stsp #ifndef PROFILE
3147 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3148 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3149 43012d58 2019-07-14 stsp err(1, "pledge");
3151 e20a8b6f 2019-06-04 stsp if (argc < 1)
3152 a129376b 2019-03-28 stsp usage_revert();
3154 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3155 a129376b 2019-03-28 stsp if (cwd == NULL) {
3156 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3157 a129376b 2019-03-28 stsp goto done;
3159 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3160 2ec1f75b 2019-03-26 stsp if (error)
3161 2ec1f75b 2019-03-26 stsp goto done;
3163 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3164 a129376b 2019-03-28 stsp if (error != NULL)
3165 a129376b 2019-03-28 stsp goto done;
3167 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3168 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3169 a129376b 2019-03-28 stsp if (error)
3170 a129376b 2019-03-28 stsp goto done;
3172 00db391e 2019-08-03 stsp worktree_path = got_worktree_get_root_path(worktree);
3173 00db391e 2019-08-03 stsp for (i = 0; i < argc; i++) {
3174 00db391e 2019-08-03 stsp char *path = realpath(argv[i], NULL);
3175 00db391e 2019-08-03 stsp if (path == NULL) {
3176 00db391e 2019-08-03 stsp if (errno != ENOENT) {
3177 00db391e 2019-08-03 stsp error = got_error_from_errno2("realpath",
3179 00db391e 2019-08-03 stsp goto done;
3181 00db391e 2019-08-03 stsp if (got_path_is_child(argv[i], worktree_path,
3182 00db391e 2019-08-03 stsp strlen(worktree_path))) {
3183 00db391e 2019-08-03 stsp path = strdup(argv[i]);
3184 00db391e 2019-08-03 stsp if (path == NULL) {
3185 00db391e 2019-08-03 stsp error = got_error_from_errno("strdup");
3186 00db391e 2019-08-03 stsp goto done;
3189 00db391e 2019-08-03 stsp } else if (asprintf(&path, "%s/%s",
3190 00db391e 2019-08-03 stsp got_worktree_get_root_path(worktree),
3191 00db391e 2019-08-03 stsp argv[i]) == -1) {
3192 00db391e 2019-08-03 stsp error = got_error_from_errno("asprintf");
3193 00db391e 2019-08-03 stsp goto done;
3197 00db391e 2019-08-03 stsp got_path_strip_trailing_slashes(path);
3198 00db391e 2019-08-03 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3199 00db391e 2019-08-03 stsp if (error) {
3200 00db391e 2019-08-03 stsp free(path);
3201 00db391e 2019-08-03 stsp goto done;
3205 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
3206 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
3207 a129376b 2019-03-28 stsp if (error)
3208 a129376b 2019-03-28 stsp goto done;
3211 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3212 2ec1f75b 2019-03-26 stsp if (worktree)
3213 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3214 2ec1f75b 2019-03-26 stsp free(path);
3215 d00136be 2019-03-26 stsp free(cwd);
3216 6bad629b 2019-02-04 stsp return error;
3219 c4296144 2019-05-09 stsp __dead static void
3220 c4296144 2019-05-09 stsp usage_commit(void)
3222 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3223 5c1e53bc 2019-07-28 stsp getprogname());
3227 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3228 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3229 e2ba3d07 2019-05-13 stsp const char *editor;
3230 e0870e44 2019-05-13 stsp const char *worktree_path;
3231 76d98825 2019-06-03 stsp const char *branch_name;
3232 314a6357 2019-05-13 stsp const char *repo_path;
3233 e0870e44 2019-05-13 stsp char *logmsg_path;
3237 33ad4cbe 2019-05-12 jcs static const struct got_error *
3238 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3241 76d98825 2019-06-03 stsp char *initial_content = NULL;
3242 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3243 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3244 e0870e44 2019-05-13 stsp char *template = NULL;
3245 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3247 33ad4cbe 2019-05-12 jcs size_t len;
3249 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3250 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3251 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3252 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3253 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3254 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3255 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3256 33ad4cbe 2019-05-12 jcs return NULL;
3259 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3260 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3262 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3263 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3264 76d98825 2019-06-03 stsp a->branch_name) == -1)
3265 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3267 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3269 e0870e44 2019-05-13 stsp goto done;
3271 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3273 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3274 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3275 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3276 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3277 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3281 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3283 3ce1b845 2019-07-15 stsp unlink(a->logmsg_path);
3284 3ce1b845 2019-07-15 stsp free(a->logmsg_path);
3285 76d98825 2019-06-03 stsp free(initial_content);
3286 e0870e44 2019-05-13 stsp free(template);
3288 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3289 3ce1b845 2019-07-15 stsp if (err == NULL) {
3290 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3291 3ce1b845 2019-07-15 stsp if (err) {
3292 3ce1b845 2019-07-15 stsp free(*logmsg);
3293 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3296 33ad4cbe 2019-05-12 jcs return err;
3299 c4296144 2019-05-09 stsp static const struct got_error *
3300 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3302 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3303 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3304 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3305 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
3306 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3307 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3308 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
3309 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3310 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3311 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
3312 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
3314 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
3316 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3317 c4296144 2019-05-09 stsp switch (ch) {
3319 c4296144 2019-05-09 stsp logmsg = optarg;
3322 c4296144 2019-05-09 stsp usage_commit();
3323 c4296144 2019-05-09 stsp /* NOTREACHED */
3327 c4296144 2019-05-09 stsp argc -= optind;
3328 c4296144 2019-05-09 stsp argv += optind;
3330 43012d58 2019-07-14 stsp #ifndef PROFILE
3331 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3332 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3333 43012d58 2019-07-14 stsp err(1, "pledge");
3335 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
3336 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
3337 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3338 35bd8fed 2019-05-09 stsp goto done;
3341 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3342 c4296144 2019-05-09 stsp if (cwd == NULL) {
3343 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3344 c4296144 2019-05-09 stsp goto done;
3346 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3347 7d5807f4 2019-07-11 stsp if (error)
3348 7d5807f4 2019-07-11 stsp goto done;
3350 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3351 c4296144 2019-05-09 stsp if (error)
3352 a698f62e 2019-07-25 stsp goto done;
3353 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
3354 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
3355 c4296144 2019-05-09 stsp goto done;
3358 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
3359 916f288c 2019-07-30 stsp worktree);
3360 916f288c 2019-07-30 stsp if (error)
3361 916f288c 2019-07-30 stsp goto done;
3363 5c1e53bc 2019-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3364 5c1e53bc 2019-07-28 stsp if (error)
3365 5c1e53bc 2019-07-28 stsp goto done;
3367 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3368 c4296144 2019-05-09 stsp if (error != NULL)
3369 c4296144 2019-05-09 stsp goto done;
3372 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3373 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3375 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3376 314a6357 2019-05-13 stsp error = get_editor(&editor);
3378 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3379 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3380 c4296144 2019-05-09 stsp if (error)
3381 c4296144 2019-05-09 stsp goto done;
3383 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3384 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3385 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3386 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3387 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
3388 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3389 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
3390 916f288c 2019-07-30 stsp goto done;
3392 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
3394 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3395 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
3396 5c1e53bc 2019-07-28 stsp error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3397 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3398 e0870e44 2019-05-13 stsp if (error) {
3399 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3400 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3401 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3402 c4296144 2019-05-09 stsp goto done;
3405 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3406 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3408 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3409 c4296144 2019-05-09 stsp if (error)
3410 c4296144 2019-05-09 stsp goto done;
3411 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3414 c4296144 2019-05-09 stsp got_repo_close(repo);
3415 c4296144 2019-05-09 stsp if (worktree)
3416 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3417 c4296144 2019-05-09 stsp free(cwd);
3418 c4296144 2019-05-09 stsp free(id_str);
3419 e2ba3d07 2019-05-13 stsp free(editor);
3420 234035bc 2019-06-01 stsp return error;
3423 234035bc 2019-06-01 stsp __dead static void
3424 234035bc 2019-06-01 stsp usage_cherrypick(void)
3426 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3430 234035bc 2019-06-01 stsp static const struct got_error *
3431 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3433 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3434 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3435 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3436 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3437 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3438 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3439 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3440 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3441 234035bc 2019-06-01 stsp int ch, did_something = 0;
3443 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3444 234035bc 2019-06-01 stsp switch (ch) {
3446 234035bc 2019-06-01 stsp usage_cherrypick();
3447 234035bc 2019-06-01 stsp /* NOTREACHED */
3451 234035bc 2019-06-01 stsp argc -= optind;
3452 234035bc 2019-06-01 stsp argv += optind;
3454 43012d58 2019-07-14 stsp #ifndef PROFILE
3455 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3456 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3457 43012d58 2019-07-14 stsp err(1, "pledge");
3459 234035bc 2019-06-01 stsp if (argc != 1)
3460 234035bc 2019-06-01 stsp usage_cherrypick();
3462 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3463 234035bc 2019-06-01 stsp if (cwd == NULL) {
3464 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3465 234035bc 2019-06-01 stsp goto done;
3467 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3468 234035bc 2019-06-01 stsp if (error)
3469 234035bc 2019-06-01 stsp goto done;
3471 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3472 234035bc 2019-06-01 stsp if (error != NULL)
3473 234035bc 2019-06-01 stsp goto done;
3475 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3476 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3477 234035bc 2019-06-01 stsp if (error)
3478 234035bc 2019-06-01 stsp goto done;
3480 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3481 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3482 234035bc 2019-06-01 stsp if (error != NULL) {
3483 234035bc 2019-06-01 stsp struct got_reference *ref;
3484 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3485 234035bc 2019-06-01 stsp goto done;
3486 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3487 234035bc 2019-06-01 stsp if (error != NULL)
3488 234035bc 2019-06-01 stsp goto done;
3489 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3490 234035bc 2019-06-01 stsp got_ref_close(ref);
3491 234035bc 2019-06-01 stsp if (error != NULL)
3492 234035bc 2019-06-01 stsp goto done;
3494 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3495 234035bc 2019-06-01 stsp if (error)
3496 234035bc 2019-06-01 stsp goto done;
3498 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3499 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3500 234035bc 2019-06-01 stsp if (error != NULL)
3501 234035bc 2019-06-01 stsp goto done;
3503 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3504 234035bc 2019-06-01 stsp if (error) {
3505 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3506 234035bc 2019-06-01 stsp goto done;
3507 234035bc 2019-06-01 stsp error = NULL;
3509 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3510 234035bc 2019-06-01 stsp goto done;
3513 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3514 234035bc 2019-06-01 stsp if (error)
3515 234035bc 2019-06-01 stsp goto done;
3516 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3517 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3518 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3520 234035bc 2019-06-01 stsp if (error != NULL)
3521 234035bc 2019-06-01 stsp goto done;
3523 234035bc 2019-06-01 stsp if (did_something)
3524 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3526 234035bc 2019-06-01 stsp if (commit)
3527 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3528 234035bc 2019-06-01 stsp free(commit_id_str);
3529 234035bc 2019-06-01 stsp if (head_ref)
3530 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3531 234035bc 2019-06-01 stsp if (worktree)
3532 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3534 234035bc 2019-06-01 stsp got_repo_close(repo);
3535 c4296144 2019-05-09 stsp return error;
3538 5ef14e63 2019-06-02 stsp __dead static void
3539 5ef14e63 2019-06-02 stsp usage_backout(void)
3541 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3545 5ef14e63 2019-06-02 stsp static const struct got_error *
3546 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3548 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3549 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3550 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3551 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3552 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3553 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3554 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3555 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3556 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3558 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3559 5ef14e63 2019-06-02 stsp switch (ch) {
3561 5ef14e63 2019-06-02 stsp usage_backout();
3562 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3566 5ef14e63 2019-06-02 stsp argc -= optind;
3567 5ef14e63 2019-06-02 stsp argv += optind;
3569 43012d58 2019-07-14 stsp #ifndef PROFILE
3570 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3571 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3572 43012d58 2019-07-14 stsp err(1, "pledge");
3574 5ef14e63 2019-06-02 stsp if (argc != 1)
3575 5ef14e63 2019-06-02 stsp usage_backout();
3577 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3578 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3579 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3580 5ef14e63 2019-06-02 stsp goto done;
3582 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3583 5ef14e63 2019-06-02 stsp if (error)
3584 5ef14e63 2019-06-02 stsp goto done;
3586 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3587 5ef14e63 2019-06-02 stsp if (error != NULL)
3588 5ef14e63 2019-06-02 stsp goto done;
3590 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3591 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3592 5ef14e63 2019-06-02 stsp if (error)
3593 5ef14e63 2019-06-02 stsp goto done;
3595 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3596 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3597 5ef14e63 2019-06-02 stsp if (error != NULL) {
3598 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3599 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3600 5ef14e63 2019-06-02 stsp goto done;
3601 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3602 5ef14e63 2019-06-02 stsp if (error != NULL)
3603 5ef14e63 2019-06-02 stsp goto done;
3604 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3605 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3606 5ef14e63 2019-06-02 stsp if (error != NULL)
3607 5ef14e63 2019-06-02 stsp goto done;
3609 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3610 5ef14e63 2019-06-02 stsp if (error)
3611 5ef14e63 2019-06-02 stsp goto done;
3613 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3614 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3615 5ef14e63 2019-06-02 stsp if (error != NULL)
3616 5ef14e63 2019-06-02 stsp goto done;
3618 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3619 5ef14e63 2019-06-02 stsp if (error)
3620 5ef14e63 2019-06-02 stsp goto done;
3622 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3623 5ef14e63 2019-06-02 stsp if (error)
3624 5ef14e63 2019-06-02 stsp goto done;
3625 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3626 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3627 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3628 5ef14e63 2019-06-02 stsp goto done;
3631 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3632 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3633 5ef14e63 2019-06-02 stsp if (error != NULL)
3634 5ef14e63 2019-06-02 stsp goto done;
3636 5ef14e63 2019-06-02 stsp if (did_something)
3637 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3639 5ef14e63 2019-06-02 stsp if (commit)
3640 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3641 5ef14e63 2019-06-02 stsp free(commit_id_str);
3642 5ef14e63 2019-06-02 stsp if (head_ref)
3643 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3644 818c7501 2019-07-11 stsp if (worktree)
3645 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3647 818c7501 2019-07-11 stsp got_repo_close(repo);
3648 818c7501 2019-07-11 stsp return error;
3651 818c7501 2019-07-11 stsp __dead static void
3652 818c7501 2019-07-11 stsp usage_rebase(void)
3654 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3655 af54c8f8 2019-07-11 stsp getprogname());
3660 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
3663 0ebf8283 2019-07-24 stsp size_t len;
3665 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
3666 0ebf8283 2019-07-24 stsp if (len > limit)
3667 0ebf8283 2019-07-24 stsp len = limit;
3668 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
3669 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
3671 0ebf8283 2019-07-24 stsp *nl = '\0';
3674 0ebf8283 2019-07-24 stsp static const struct got_error *
3675 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3677 0ebf8283 2019-07-24 stsp const char *logmsg0 = NULL;
3679 0ebf8283 2019-07-24 stsp logmsg0 = got_object_commit_get_logmsg(commit);
3681 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)logmsg0[0]))
3682 0ebf8283 2019-07-24 stsp logmsg0++;
3684 0ebf8283 2019-07-24 stsp *logmsg = strdup(logmsg0);
3685 0ebf8283 2019-07-24 stsp if (*logmsg == NULL)
3686 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
3688 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
3689 0ebf8283 2019-07-24 stsp return NULL;
3692 818c7501 2019-07-11 stsp static const struct got_error *
3693 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3694 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3696 818c7501 2019-07-11 stsp const struct got_error *err;
3697 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3699 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3701 818c7501 2019-07-11 stsp goto done;
3703 ff0d2220 2019-07-11 stsp if (new_id) {
3704 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3706 ff0d2220 2019-07-11 stsp goto done;
3709 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3710 ff0d2220 2019-07-11 stsp if (new_id_str)
3711 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3713 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
3715 0ebf8283 2019-07-24 stsp goto done;
3717 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3718 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3720 818c7501 2019-07-11 stsp free(old_id_str);
3721 818c7501 2019-07-11 stsp free(new_id_str);
3722 818c7501 2019-07-11 stsp return err;
3725 1ee397ad 2019-07-12 stsp static const struct got_error *
3726 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
3728 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
3730 818c7501 2019-07-11 stsp while (path[0] == '/')
3732 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
3734 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
3735 1ee397ad 2019-07-12 stsp return NULL;
3736 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3737 818c7501 2019-07-11 stsp *rebase_status = status;
3738 1ee397ad 2019-07-12 stsp return NULL;
3741 818c7501 2019-07-11 stsp static const struct got_error *
3742 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3743 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
3744 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
3746 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
3747 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
3748 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
3751 818c7501 2019-07-11 stsp static const struct got_error *
3752 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
3753 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
3754 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
3755 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
3757 ff0d2220 2019-07-11 stsp const struct got_error *error;
3758 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
3759 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
3761 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3762 ff0d2220 2019-07-11 stsp if (error)
3763 ff0d2220 2019-07-11 stsp return error;
3765 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3766 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
3767 ff0d2220 2019-07-11 stsp if (error) {
3768 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3769 ff0d2220 2019-07-11 stsp goto done;
3770 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
3772 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
3773 ff0d2220 2019-07-11 stsp free(new_commit_id);
3776 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
3777 ff0d2220 2019-07-11 stsp return error;
3780 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
3781 64c6d990 2019-07-11 stsp const char *path_prefix;
3782 64c6d990 2019-07-11 stsp size_t len;
3783 8ca9bd68 2019-07-25 stsp int errcode;
3786 64c6d990 2019-07-11 stsp static const struct got_error *
3787 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3788 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3789 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
3790 64c6d990 2019-07-11 stsp struct got_repository *repo)
3792 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
3794 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3795 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3796 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
3798 64c6d990 2019-07-11 stsp return NULL;
3801 64c6d990 2019-07-11 stsp static const struct got_error *
3802 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
3803 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
3804 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
3806 64c6d990 2019-07-11 stsp const struct got_error *err;
3807 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3808 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
3809 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
3811 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
3812 64c6d990 2019-07-11 stsp return NULL;
3814 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3816 64c6d990 2019-07-11 stsp goto done;
3818 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3820 64c6d990 2019-07-11 stsp goto done;
3822 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
3823 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
3825 64c6d990 2019-07-11 stsp goto done;
3827 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
3828 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
3830 64c6d990 2019-07-11 stsp goto done;
3832 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
3833 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
3834 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
3835 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
3836 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
3837 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3838 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
3840 64c6d990 2019-07-11 stsp if (tree1)
3841 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
3842 64c6d990 2019-07-11 stsp if (tree2)
3843 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
3844 64c6d990 2019-07-11 stsp if (commit)
3845 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
3846 64c6d990 2019-07-11 stsp if (parent_commit)
3847 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
3848 64c6d990 2019-07-11 stsp return err;
3851 ff0d2220 2019-07-11 stsp static const struct got_error *
3852 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
3853 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
3854 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3855 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
3856 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
3858 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
3859 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
3860 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
3861 af61c510 2019-07-19 stsp struct got_object_qid *qid;
3862 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
3864 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3866 af61c510 2019-07-19 stsp return err;
3868 af61c510 2019-07-19 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3870 af61c510 2019-07-19 stsp goto done;
3871 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3872 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
3873 af61c510 2019-07-19 stsp if (err) {
3874 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
3875 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
3876 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
3877 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
3878 af61c510 2019-07-19 stsp "been reached?!?");
3879 af61c510 2019-07-19 stsp goto done;
3880 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3881 af61c510 2019-07-19 stsp goto done;
3882 af61c510 2019-07-19 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
3884 af61c510 2019-07-19 stsp goto done;
3886 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
3887 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
3889 af61c510 2019-07-19 stsp goto done;
3891 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
3893 af61c510 2019-07-19 stsp goto done;
3894 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3895 af61c510 2019-07-19 stsp commit_id = parent_id;
3899 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
3900 af61c510 2019-07-19 stsp return err;
3903 af61c510 2019-07-19 stsp static const struct got_error *
3904 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
3906 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
3907 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
3908 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
3909 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
3910 818c7501 2019-07-11 stsp char *cwd = NULL;
3911 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
3912 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3913 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
3914 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
3915 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3916 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
3917 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3918 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3919 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
3920 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
3921 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
3922 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
3924 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
3925 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
3927 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
3928 818c7501 2019-07-11 stsp switch (ch) {
3930 818c7501 2019-07-11 stsp abort_rebase = 1;
3933 818c7501 2019-07-11 stsp continue_rebase = 1;
3936 818c7501 2019-07-11 stsp usage_rebase();
3937 818c7501 2019-07-11 stsp /* NOTREACHED */
3941 818c7501 2019-07-11 stsp argc -= optind;
3942 818c7501 2019-07-11 stsp argv += optind;
3944 43012d58 2019-07-14 stsp #ifndef PROFILE
3945 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3946 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3947 43012d58 2019-07-14 stsp err(1, "pledge");
3949 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
3950 818c7501 2019-07-11 stsp usage_rebase();
3951 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
3952 818c7501 2019-07-11 stsp if (argc != 0)
3953 818c7501 2019-07-11 stsp usage_rebase();
3954 818c7501 2019-07-11 stsp } else if (argc != 1)
3955 818c7501 2019-07-11 stsp usage_rebase();
3957 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
3958 818c7501 2019-07-11 stsp if (cwd == NULL) {
3959 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
3960 818c7501 2019-07-11 stsp goto done;
3962 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
3963 818c7501 2019-07-11 stsp if (error)
3964 818c7501 2019-07-11 stsp goto done;
3966 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3967 818c7501 2019-07-11 stsp if (error != NULL)
3968 818c7501 2019-07-11 stsp goto done;
3970 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3971 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3972 818c7501 2019-07-11 stsp if (error)
3973 818c7501 2019-07-11 stsp goto done;
3975 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3976 818c7501 2019-07-11 stsp if (error)
3977 818c7501 2019-07-11 stsp goto done;
3979 f6794adc 2019-07-23 stsp if (abort_rebase) {
3980 818c7501 2019-07-11 stsp int did_something;
3981 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
3982 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
3983 f6794adc 2019-07-23 stsp goto done;
3985 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3986 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
3987 3e3a69f1 2019-07-25 stsp worktree, repo);
3988 818c7501 2019-07-11 stsp if (error)
3989 818c7501 2019-07-11 stsp goto done;
3990 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
3991 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
3992 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
3993 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
3994 818c7501 2019-07-11 stsp if (error)
3995 818c7501 2019-07-11 stsp goto done;
3996 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3997 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
4000 818c7501 2019-07-11 stsp if (continue_rebase) {
4001 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4002 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4003 f6794adc 2019-07-23 stsp goto done;
4005 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4006 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4007 3e3a69f1 2019-07-25 stsp worktree, repo);
4008 818c7501 2019-07-11 stsp if (error)
4009 818c7501 2019-07-11 stsp goto done;
4011 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4012 01757395 2019-07-12 stsp resume_commit_id, repo);
4013 818c7501 2019-07-11 stsp if (error)
4014 818c7501 2019-07-11 stsp goto done;
4016 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
4017 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
4018 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
4019 818c7501 2019-07-11 stsp goto done;
4022 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
4023 818c7501 2019-07-11 stsp if (error != NULL)
4024 818c7501 2019-07-11 stsp goto done;
4027 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4028 ff0d2220 2019-07-11 stsp if (error)
4029 ff0d2220 2019-07-11 stsp goto done;
4031 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
4032 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
4034 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
4035 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4036 a51a74b3 2019-07-27 stsp base_commit_id, branch_head_commit_id, repo);
4037 818c7501 2019-07-11 stsp if (error)
4038 818c7501 2019-07-11 stsp goto done;
4039 818c7501 2019-07-11 stsp if (yca_id == NULL) {
4040 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
4041 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
4042 818c7501 2019-07-11 stsp "with work tree's branch");
4043 818c7501 2019-07-11 stsp goto done;
4046 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
4047 a51a74b3 2019-07-27 stsp if (error) {
4048 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
4049 a51a74b3 2019-07-27 stsp goto done;
4050 a51a74b3 2019-07-27 stsp error = NULL;
4052 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
4053 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
4054 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
4055 a51a74b3 2019-07-27 stsp goto done;
4057 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
4058 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
4059 818c7501 2019-07-11 stsp if (error)
4060 818c7501 2019-07-11 stsp goto done;
4063 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
4064 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4065 818c7501 2019-07-11 stsp if (error)
4066 818c7501 2019-07-11 stsp goto done;
4068 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4069 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
4070 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
4071 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
4072 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
4073 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4074 818c7501 2019-07-11 stsp commit = NULL;
4075 818c7501 2019-07-11 stsp if (error)
4076 818c7501 2019-07-11 stsp goto done;
4078 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
4079 ff0d2220 2019-07-11 stsp if (continue_rebase)
4080 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
4081 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
4083 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4084 818c7501 2019-07-11 stsp goto done;
4087 818c7501 2019-07-11 stsp pid = NULL;
4088 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
4089 818c7501 2019-07-11 stsp commit_id = qid->id;
4090 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
4091 818c7501 2019-07-11 stsp pid = qid;
4093 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
4094 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
4095 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4096 818c7501 2019-07-11 stsp if (error)
4097 818c7501 2019-07-11 stsp goto done;
4099 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4100 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4104 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
4105 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
4106 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4107 818c7501 2019-07-11 stsp if (error)
4108 818c7501 2019-07-11 stsp goto done;
4111 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4112 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
4113 818c7501 2019-07-11 stsp if (error)
4114 818c7501 2019-07-11 stsp goto done;
4115 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4116 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
4118 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
4119 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
4121 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
4122 818c7501 2019-07-11 stsp free(branch_head_commit_id);
4123 818c7501 2019-07-11 stsp free(resume_commit_id);
4124 818c7501 2019-07-11 stsp free(yca_id);
4125 818c7501 2019-07-11 stsp if (commit)
4126 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4127 818c7501 2019-07-11 stsp if (branch)
4128 818c7501 2019-07-11 stsp got_ref_close(branch);
4129 818c7501 2019-07-11 stsp if (new_base_branch)
4130 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
4131 818c7501 2019-07-11 stsp if (tmp_branch)
4132 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
4133 5ef14e63 2019-06-02 stsp if (worktree)
4134 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
4136 5ef14e63 2019-06-02 stsp got_repo_close(repo);
4137 5ef14e63 2019-06-02 stsp return error;
4140 0ebf8283 2019-07-24 stsp __dead static void
4141 0ebf8283 2019-07-24 stsp usage_histedit(void)
4143 0ebf8283 2019-07-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4144 0ebf8283 2019-07-24 stsp getprogname());
4148 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
4149 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
4150 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
4151 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
4152 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
4154 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
4155 0ebf8283 2019-07-24 stsp unsigned char code;
4156 0ebf8283 2019-07-24 stsp const char *name;
4157 0ebf8283 2019-07-24 stsp const char *desc;
4158 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
4159 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
4160 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4161 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4162 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4163 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
4164 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4167 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4168 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4169 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4170 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4171 0ebf8283 2019-07-24 stsp char *logmsg;
4173 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4175 0ebf8283 2019-07-24 stsp static const struct got_error *
4176 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4177 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4179 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4180 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4181 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4184 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4186 0ebf8283 2019-07-24 stsp goto done;
4188 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4190 0ebf8283 2019-07-24 stsp goto done;
4192 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4194 0ebf8283 2019-07-24 stsp goto done;
4196 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4197 0ebf8283 2019-07-24 stsp if (n < 0)
4198 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4200 0ebf8283 2019-07-24 stsp if (commit)
4201 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4202 0ebf8283 2019-07-24 stsp free(id_str);
4203 0ebf8283 2019-07-24 stsp free(logmsg);
4204 0ebf8283 2019-07-24 stsp return err;
4207 0ebf8283 2019-07-24 stsp static const struct got_error *
4208 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4209 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4211 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4212 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4214 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4215 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4217 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4218 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4224 0ebf8283 2019-07-24 stsp return err;
4227 0ebf8283 2019-07-24 stsp static const struct got_error *
4228 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4230 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4233 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4234 0ebf8283 2019-07-24 stsp if (n < 0)
4235 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4237 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4238 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4239 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4240 0ebf8283 2019-07-24 stsp cmd->desc);
4241 0ebf8283 2019-07-24 stsp if (n < 0) {
4242 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4246 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4247 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4248 0ebf8283 2019-07-24 stsp if (n < 0)
4249 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4250 0ebf8283 2019-07-24 stsp return err;
4253 0ebf8283 2019-07-24 stsp static const struct got_error *
4254 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4256 0ebf8283 2019-07-24 stsp static char msg[42];
4259 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4261 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4262 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4264 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4267 0ebf8283 2019-07-24 stsp static const struct got_error *
4268 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4269 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4271 0ebf8283 2019-07-24 stsp const struct got_error *err;
4272 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4273 0ebf8283 2019-07-24 stsp char *id_str;
4275 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4277 0ebf8283 2019-07-24 stsp return err;
4279 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4281 0ebf8283 2019-07-24 stsp goto done;
4283 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4284 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4285 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(folded_commit)) == -1) {
4286 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4287 0ebf8283 2019-07-24 stsp goto done;
4290 0ebf8283 2019-07-24 stsp if (folded_commit)
4291 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4292 0ebf8283 2019-07-24 stsp free(id_str);
4293 0ebf8283 2019-07-24 stsp return err;
4296 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4297 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4299 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4301 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4302 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4303 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
4304 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4305 3f9de99f 2019-07-24 stsp folded = prev;
4306 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4309 0ebf8283 2019-07-24 stsp return folded;
4312 0ebf8283 2019-07-24 stsp static const struct got_error *
4313 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4314 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4316 0ebf8283 2019-07-24 stsp char *logmsg_path = NULL, *id_str = NULL;
4317 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4318 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4319 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4321 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4323 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4325 0ebf8283 2019-07-24 stsp return err;
4327 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4328 0ebf8283 2019-07-24 stsp if (folded) {
4329 0ebf8283 2019-07-24 stsp while (folded != hle) {
4330 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4331 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4334 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4335 0ebf8283 2019-07-24 stsp logmsg, repo);
4337 0ebf8283 2019-07-24 stsp goto done;
4338 0ebf8283 2019-07-24 stsp free(logmsg);
4339 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4340 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4344 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4346 0ebf8283 2019-07-24 stsp goto done;
4347 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4348 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4349 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", id_str,
4350 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(commit)) == -1) {
4351 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4352 0ebf8283 2019-07-24 stsp goto done;
4354 0ebf8283 2019-07-24 stsp free(logmsg);
4355 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4357 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4359 0ebf8283 2019-07-24 stsp goto done;
4361 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4363 0ebf8283 2019-07-24 stsp goto done;
4365 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4366 0ebf8283 2019-07-24 stsp close(fd);
4368 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4370 0ebf8283 2019-07-24 stsp goto done;
4372 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4373 0ebf8283 2019-07-24 stsp if (err) {
4374 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4375 0ebf8283 2019-07-24 stsp goto done;
4376 0ebf8283 2019-07-24 stsp err = NULL;
4377 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4378 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL)
4379 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4382 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4383 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4384 0ebf8283 2019-07-24 stsp free(logmsg_path);
4385 0ebf8283 2019-07-24 stsp free(logmsg);
4386 0ebf8283 2019-07-24 stsp free(editor);
4387 0ebf8283 2019-07-24 stsp if (commit)
4388 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4389 0ebf8283 2019-07-24 stsp return err;
4392 0ebf8283 2019-07-24 stsp static const struct got_error *
4393 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4394 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4396 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4397 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4398 0ebf8283 2019-07-24 stsp size_t size;
4399 0ebf8283 2019-07-24 stsp ssize_t len;
4400 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4401 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4402 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4403 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4405 0ebf8283 2019-07-24 stsp for (;;) {
4406 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4407 0ebf8283 2019-07-24 stsp if (len == -1) {
4408 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4409 0ebf8283 2019-07-24 stsp if (feof(f))
4411 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4412 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4417 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4419 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4420 0ebf8283 2019-07-24 stsp free(line);
4421 0ebf8283 2019-07-24 stsp line = NULL;
4424 0ebf8283 2019-07-24 stsp cmd = NULL;
4425 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4426 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4427 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4428 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4429 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4432 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4437 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
4438 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4441 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4443 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4444 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4445 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4448 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4449 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4453 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4454 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4455 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4459 0ebf8283 2019-07-24 stsp free(line);
4460 0ebf8283 2019-07-24 stsp line = NULL;
4464 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4466 0ebf8283 2019-07-24 stsp *end = '\0';
4468 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4469 0ebf8283 2019-07-24 stsp if (err) {
4470 0ebf8283 2019-07-24 stsp /* override error code */
4471 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4475 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4476 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4477 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4480 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
4481 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
4482 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
4483 0ebf8283 2019-07-24 stsp commit_id = NULL;
4484 0ebf8283 2019-07-24 stsp free(line);
4485 0ebf8283 2019-07-24 stsp line = NULL;
4486 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4489 0ebf8283 2019-07-24 stsp free(line);
4490 0ebf8283 2019-07-24 stsp free(commit_id);
4491 0ebf8283 2019-07-24 stsp return err;
4494 0ebf8283 2019-07-24 stsp static const struct got_error *
4495 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
4496 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4498 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
4499 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
4500 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4501 bfce7f83 2019-07-27 stsp static char msg[80];
4502 bfce7f83 2019-07-27 stsp char *id_str;
4504 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
4505 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4506 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
4508 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4509 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4510 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4513 bfce7f83 2019-07-27 stsp if (hle == NULL) {
4514 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
4516 bfce7f83 2019-07-27 stsp return err;
4517 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
4518 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
4519 bfce7f83 2019-07-27 stsp free(id_str);
4520 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4524 bfce7f83 2019-07-27 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4525 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4526 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
4528 bfce7f83 2019-07-27 stsp return NULL;
4531 bfce7f83 2019-07-27 stsp static const struct got_error *
4532 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
4533 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
4534 bfce7f83 2019-07-27 stsp struct got_repository *repo)
4536 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4537 0ebf8283 2019-07-24 stsp char *editor;
4538 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4540 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4542 0ebf8283 2019-07-24 stsp return err;
4544 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
4545 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
4546 0ebf8283 2019-07-24 stsp goto done;
4549 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4550 0ebf8283 2019-07-24 stsp if (f == NULL) {
4551 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
4552 0ebf8283 2019-07-24 stsp goto done;
4554 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4556 bfce7f83 2019-07-27 stsp goto done;
4558 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
4560 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4561 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4562 0ebf8283 2019-07-24 stsp free(editor);
4563 0ebf8283 2019-07-24 stsp return err;
4566 0ebf8283 2019-07-24 stsp static const struct got_error *
4567 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4568 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
4570 0ebf8283 2019-07-24 stsp static const struct got_error *
4571 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
4572 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4574 0ebf8283 2019-07-24 stsp const struct got_error *err;
4575 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4576 0ebf8283 2019-07-24 stsp char *path = NULL;
4578 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
4580 0ebf8283 2019-07-24 stsp return err;
4582 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
4584 0ebf8283 2019-07-24 stsp goto done;
4586 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
4588 0ebf8283 2019-07-24 stsp goto done;
4590 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
4591 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4592 0ebf8283 2019-07-24 stsp goto done;
4596 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
4597 0ebf8283 2019-07-24 stsp if (err) {
4598 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4599 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4600 0ebf8283 2019-07-24 stsp goto done;
4601 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
4602 0ebf8283 2019-07-24 stsp commits, path, repo);
4605 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4606 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4607 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
4608 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
4609 0ebf8283 2019-07-24 stsp free(path);
4610 0ebf8283 2019-07-24 stsp return err;
4613 0ebf8283 2019-07-24 stsp static const struct got_error *
4614 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
4615 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4617 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4618 0ebf8283 2019-07-24 stsp char *path = NULL;
4619 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4620 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4621 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4623 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
4625 0ebf8283 2019-07-24 stsp return err;
4627 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
4628 0ebf8283 2019-07-24 stsp if (f == NULL) {
4629 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4630 0ebf8283 2019-07-24 stsp goto done;
4632 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4633 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4638 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4639 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
4640 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
4641 0ebf8283 2019-07-24 stsp if (n < 0) {
4642 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4648 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4649 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4650 0ebf8283 2019-07-24 stsp free(path);
4651 0ebf8283 2019-07-24 stsp if (commit)
4652 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4653 0ebf8283 2019-07-24 stsp return err;
4657 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
4659 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4661 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
4662 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
4663 bfce7f83 2019-07-27 stsp free(hle);
4667 0ebf8283 2019-07-24 stsp static const struct got_error *
4668 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
4669 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4671 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4672 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4674 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4675 0ebf8283 2019-07-24 stsp if (f == NULL) {
4676 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4677 0ebf8283 2019-07-24 stsp goto done;
4680 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4682 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4683 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4684 0ebf8283 2019-07-24 stsp return err;
4687 0ebf8283 2019-07-24 stsp static const struct got_error *
4688 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4689 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
4690 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4692 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
4693 0ebf8283 2019-07-24 stsp int resp = ' ';
4695 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
4696 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4697 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
4698 0ebf8283 2019-07-24 stsp resp = getchar();
4699 426ebf2e 2019-07-25 stsp if (resp == 'c') {
4700 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
4701 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
4703 426ebf2e 2019-07-25 stsp if (err) {
4704 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4705 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4707 bfce7f83 2019-07-27 stsp prev_err = err;
4708 426ebf2e 2019-07-25 stsp resp = ' ';
4712 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
4713 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
4714 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
4715 0ebf8283 2019-07-24 stsp commits, repo);
4716 426ebf2e 2019-07-25 stsp if (err) {
4717 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4718 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4720 bfce7f83 2019-07-27 stsp prev_err = err;
4721 426ebf2e 2019-07-25 stsp resp = ' ';
4725 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
4726 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4729 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
4732 0ebf8283 2019-07-24 stsp return err;
4735 0ebf8283 2019-07-24 stsp static const struct got_error *
4736 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
4737 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4738 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
4740 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4741 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4742 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4743 3e3a69f1 2019-07-25 stsp branch, repo);
4746 0ebf8283 2019-07-24 stsp static const struct got_error *
4747 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
4748 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4750 0ebf8283 2019-07-24 stsp const struct got_error *err;
4751 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4753 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
4755 0ebf8283 2019-07-24 stsp goto done;
4757 0ebf8283 2019-07-24 stsp if (new_id) {
4758 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
4760 0ebf8283 2019-07-24 stsp goto done;
4763 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
4764 0ebf8283 2019-07-24 stsp if (new_id_str)
4765 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
4767 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4768 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
4769 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
4770 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4771 0ebf8283 2019-07-24 stsp goto done;
4773 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
4775 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4777 0ebf8283 2019-07-24 stsp goto done;
4780 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
4781 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
4782 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
4783 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
4784 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4786 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
4787 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
4788 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4796 0ebf8283 2019-07-24 stsp free(old_id_str);
4797 0ebf8283 2019-07-24 stsp free(new_id_str);
4798 0ebf8283 2019-07-24 stsp return err;
4801 0ebf8283 2019-07-24 stsp static const struct got_error *
4802 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
4803 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4804 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4805 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4807 0ebf8283 2019-07-24 stsp const struct got_error *err;
4808 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4809 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
4811 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4812 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
4813 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4815 0ebf8283 2019-07-24 stsp return err;
4818 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4820 0ebf8283 2019-07-24 stsp return err;
4822 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4823 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
4824 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
4825 0ebf8283 2019-07-24 stsp if (err) {
4826 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4827 0ebf8283 2019-07-24 stsp goto done;
4828 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
4830 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
4831 0ebf8283 2019-07-24 stsp free(new_commit_id);
4834 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4835 0ebf8283 2019-07-24 stsp return err;
4838 0ebf8283 2019-07-24 stsp static const struct got_error *
4839 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
4840 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4842 0ebf8283 2019-07-24 stsp const struct got_error *error;
4843 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4845 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4847 0ebf8283 2019-07-24 stsp if (error)
4848 0ebf8283 2019-07-24 stsp return error;
4850 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4851 0ebf8283 2019-07-24 stsp if (error)
4852 0ebf8283 2019-07-24 stsp return error;
4854 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
4855 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4856 0ebf8283 2019-07-24 stsp return error;
4859 0ebf8283 2019-07-24 stsp static const struct got_error *
4860 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
4862 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
4863 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
4864 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4865 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
4866 0ebf8283 2019-07-24 stsp char *cwd = NULL;
4867 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
4868 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
4869 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
4870 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
4871 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
4872 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4873 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
4874 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4875 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
4876 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4877 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
4878 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
4879 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
4880 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
4881 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
4882 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4884 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
4885 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
4886 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
4888 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
4889 0ebf8283 2019-07-24 stsp switch (ch) {
4891 0ebf8283 2019-07-24 stsp abort_edit = 1;
4894 0ebf8283 2019-07-24 stsp continue_edit = 1;
4897 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
4900 0ebf8283 2019-07-24 stsp usage_histedit();
4901 0ebf8283 2019-07-24 stsp /* NOTREACHED */
4905 0ebf8283 2019-07-24 stsp argc -= optind;
4906 0ebf8283 2019-07-24 stsp argv += optind;
4908 0ebf8283 2019-07-24 stsp #ifndef PROFILE
4909 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4910 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
4911 0ebf8283 2019-07-24 stsp err(1, "pledge");
4913 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
4914 0ebf8283 2019-07-24 stsp usage_histedit();
4915 0ebf8283 2019-07-24 stsp if (argc != 0)
4916 0ebf8283 2019-07-24 stsp usage_histedit();
4919 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
4920 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
4921 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
4922 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
4923 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
4924 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
4927 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
4928 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
4929 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
4930 0ebf8283 2019-07-24 stsp goto done;
4932 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
4933 0ebf8283 2019-07-24 stsp if (error)
4934 0ebf8283 2019-07-24 stsp goto done;
4936 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4937 0ebf8283 2019-07-24 stsp if (error != NULL)
4938 0ebf8283 2019-07-24 stsp goto done;
4940 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4941 0ebf8283 2019-07-24 stsp if (error)
4942 0ebf8283 2019-07-24 stsp goto done;
4943 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
4944 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
4945 0ebf8283 2019-07-24 stsp goto done;
4948 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4949 0ebf8283 2019-07-24 stsp if (error)
4950 0ebf8283 2019-07-24 stsp goto done;
4952 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
4953 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4954 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
4955 3e3a69f1 2019-07-25 stsp worktree, repo);
4956 0ebf8283 2019-07-24 stsp if (error)
4957 0ebf8283 2019-07-24 stsp goto done;
4958 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4959 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4960 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
4961 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
4962 0ebf8283 2019-07-24 stsp if (error)
4963 0ebf8283 2019-07-24 stsp goto done;
4964 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
4965 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4966 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
4967 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
4968 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4969 0ebf8283 2019-07-24 stsp goto done;
4972 0ebf8283 2019-07-24 stsp if (continue_edit) {
4973 0ebf8283 2019-07-24 stsp char *path;
4975 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
4976 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4977 0ebf8283 2019-07-24 stsp goto done;
4980 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
4981 0ebf8283 2019-07-24 stsp if (error)
4982 0ebf8283 2019-07-24 stsp goto done;
4984 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
4985 0ebf8283 2019-07-24 stsp free(path);
4986 0ebf8283 2019-07-24 stsp if (error)
4987 0ebf8283 2019-07-24 stsp goto done;
4989 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4990 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
4991 3e3a69f1 2019-07-25 stsp worktree, repo);
4992 0ebf8283 2019-07-24 stsp if (error)
4993 0ebf8283 2019-07-24 stsp goto done;
4995 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
4996 0ebf8283 2019-07-24 stsp if (error)
4997 0ebf8283 2019-07-24 stsp goto done;
4999 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5000 0ebf8283 2019-07-24 stsp head_commit_id);
5001 0ebf8283 2019-07-24 stsp if (error)
5002 0ebf8283 2019-07-24 stsp goto done;
5003 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5004 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5005 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5006 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5007 3aac7cf7 2019-07-25 stsp goto done;
5009 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5010 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
5011 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5012 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5013 0ebf8283 2019-07-24 stsp commit = NULL;
5014 0ebf8283 2019-07-24 stsp if (error)
5015 0ebf8283 2019-07-24 stsp goto done;
5017 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
5018 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5019 0ebf8283 2019-07-24 stsp goto done;
5022 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
5023 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
5024 0ebf8283 2019-07-24 stsp if (error != NULL)
5025 0ebf8283 2019-07-24 stsp goto done;
5027 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5028 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5029 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
5030 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
5031 c7d20a3f 2019-07-30 stsp goto done;
5034 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5035 bfce7f83 2019-07-27 stsp got_ref_close(branch);
5036 bfce7f83 2019-07-27 stsp branch = NULL;
5037 0ebf8283 2019-07-24 stsp if (error)
5038 0ebf8283 2019-07-24 stsp goto done;
5040 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5041 0ebf8283 2019-07-24 stsp head_commit_id);
5042 0ebf8283 2019-07-24 stsp if (error)
5043 0ebf8283 2019-07-24 stsp goto done;
5044 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5045 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5046 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5047 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5048 3aac7cf7 2019-07-25 stsp goto done;
5050 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5051 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
5052 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
5053 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5054 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5055 0ebf8283 2019-07-24 stsp commit = NULL;
5056 0ebf8283 2019-07-24 stsp if (error)
5057 0ebf8283 2019-07-24 stsp goto done;
5059 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5060 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
5061 bfce7f83 2019-07-27 stsp if (error)
5062 bfce7f83 2019-07-27 stsp goto done;
5064 0ebf8283 2019-07-24 stsp if (edit_script_path) {
5065 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
5066 0ebf8283 2019-07-24 stsp edit_script_path, repo);
5067 6ba2bea2 2019-07-27 stsp if (error) {
5068 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5069 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5070 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5071 0ebf8283 2019-07-24 stsp goto done;
5074 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
5076 6ba2bea2 2019-07-27 stsp if (error) {
5077 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5078 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5079 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5080 0ebf8283 2019-07-24 stsp goto done;
5085 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
5087 6ba2bea2 2019-07-27 stsp if (error) {
5088 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5089 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5090 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5091 0ebf8283 2019-07-24 stsp goto done;
5096 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
5097 0ebf8283 2019-07-24 stsp if (error)
5098 0ebf8283 2019-07-24 stsp goto done;
5100 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5101 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
5102 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
5103 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
5106 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
5107 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5108 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
5109 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
5112 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
5113 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
5115 0ebf8283 2019-07-24 stsp if (error)
5116 0ebf8283 2019-07-24 stsp goto done;
5120 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5121 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5122 0ebf8283 2019-07-24 stsp if (error)
5123 0ebf8283 2019-07-24 stsp goto done;
5127 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5128 0ebf8283 2019-07-24 stsp hle->commit_id);
5129 0ebf8283 2019-07-24 stsp if (error)
5130 0ebf8283 2019-07-24 stsp goto done;
5131 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5132 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5134 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
5135 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
5136 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5137 0ebf8283 2019-07-24 stsp if (error)
5138 0ebf8283 2019-07-24 stsp goto done;
5139 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5140 0ebf8283 2019-07-24 stsp commit = NULL;
5142 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5143 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5147 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5148 0ebf8283 2019-07-24 stsp char *id_str;
5149 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
5150 0ebf8283 2019-07-24 stsp if (error)
5151 0ebf8283 2019-07-24 stsp goto done;
5152 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
5154 0ebf8283 2019-07-24 stsp free(id_str);
5155 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5156 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
5157 3e3a69f1 2019-07-25 stsp fileindex);
5158 0ebf8283 2019-07-24 stsp goto done;
5161 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5162 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5163 0ebf8283 2019-07-24 stsp if (error)
5164 0ebf8283 2019-07-24 stsp goto done;
5168 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
5169 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
5170 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5171 0ebf8283 2019-07-24 stsp if (error)
5172 0ebf8283 2019-07-24 stsp goto done;
5175 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5176 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
5177 0ebf8283 2019-07-24 stsp if (error)
5178 0ebf8283 2019-07-24 stsp goto done;
5179 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5180 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
5182 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
5183 3e3a69f1 2019-07-25 stsp branch, repo);
5185 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
5186 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
5187 0ebf8283 2019-07-24 stsp free(head_commit_id);
5188 0ebf8283 2019-07-24 stsp free(base_commit_id);
5189 0ebf8283 2019-07-24 stsp free(resume_commit_id);
5190 0ebf8283 2019-07-24 stsp if (commit)
5191 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5192 0ebf8283 2019-07-24 stsp if (branch)
5193 0ebf8283 2019-07-24 stsp got_ref_close(branch);
5194 0ebf8283 2019-07-24 stsp if (tmp_branch)
5195 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
5196 0ebf8283 2019-07-24 stsp if (worktree)
5197 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
5199 715dc77e 2019-08-03 stsp got_repo_close(repo);
5200 715dc77e 2019-08-03 stsp return error;
5203 715dc77e 2019-08-03 stsp __dead static void
5204 715dc77e 2019-08-03 stsp usage_stage(void)
5206 408b4ebc 2019-08-03 stsp fprintf(stderr, "usage: %s stage [-l] | file-path ...\n",
5207 715dc77e 2019-08-03 stsp getprogname());
5211 715dc77e 2019-08-03 stsp static const struct got_error *
5212 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
5213 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5214 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5216 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
5217 408b4ebc 2019-08-03 stsp char *id_str = NULL;
5219 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
5220 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
5221 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5222 408b4ebc 2019-08-03 stsp return NULL;
5224 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5225 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
5226 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
5228 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
5230 408b4ebc 2019-08-03 stsp return err;
5232 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
5233 408b4ebc 2019-08-03 stsp free(id_str);
5234 408b4ebc 2019-08-03 stsp return NULL;
5237 408b4ebc 2019-08-03 stsp static const struct got_error *
5238 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
5240 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
5241 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
5242 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
5243 715dc77e 2019-08-03 stsp char *cwd = NULL;
5244 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
5245 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
5246 715dc77e 2019-08-03 stsp const char *worktree_path;
5247 408b4ebc 2019-08-03 stsp int ch, x, list_stage = 0;
5249 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
5251 408b4ebc 2019-08-03 stsp while ((ch = getopt(argc, argv, "l")) != -1) {
5252 715dc77e 2019-08-03 stsp switch (ch) {
5254 408b4ebc 2019-08-03 stsp list_stage = 1;
5257 715dc77e 2019-08-03 stsp usage_stage();
5258 715dc77e 2019-08-03 stsp /* NOTREACHED */
5262 715dc77e 2019-08-03 stsp argc -= optind;
5263 715dc77e 2019-08-03 stsp argv += optind;
5265 715dc77e 2019-08-03 stsp #ifndef PROFILE
5266 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5267 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
5268 715dc77e 2019-08-03 stsp err(1, "pledge");
5270 408b4ebc 2019-08-03 stsp if (!list_stage && argc < 1)
5271 715dc77e 2019-08-03 stsp usage_stage();
5273 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
5274 715dc77e 2019-08-03 stsp if (cwd == NULL) {
5275 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
5276 715dc77e 2019-08-03 stsp goto done;
5279 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5280 715dc77e 2019-08-03 stsp if (error)
5281 715dc77e 2019-08-03 stsp goto done;
5283 715dc77e 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5284 715dc77e 2019-08-03 stsp if (error != NULL)
5285 715dc77e 2019-08-03 stsp goto done;
5287 715dc77e 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5288 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
5289 715dc77e 2019-08-03 stsp if (error)
5290 715dc77e 2019-08-03 stsp goto done;
5292 715dc77e 2019-08-03 stsp worktree_path = got_worktree_get_root_path(worktree);
5293 715dc77e 2019-08-03 stsp for (x = 0; x < argc; x++) {
5294 408b4ebc 2019-08-03 stsp char *path;
5295 408b4ebc 2019-08-03 stsp if (list_stage) {
5296 408b4ebc 2019-08-03 stsp error = got_worktree_resolve_path(&path, worktree,
5298 408b4ebc 2019-08-03 stsp if (error)
5301 408b4ebc 2019-08-03 stsp path = realpath(argv[x], NULL);
5302 715dc77e 2019-08-03 stsp if (path == NULL) {
5303 715dc77e 2019-08-03 stsp if (errno != ENOENT) {
5304 715dc77e 2019-08-03 stsp error = got_error_from_errno2("realpath",
5306 715dc77e 2019-08-03 stsp goto done;
5308 715dc77e 2019-08-03 stsp if (got_path_is_child(argv[x], worktree_path,
5309 715dc77e 2019-08-03 stsp strlen(worktree_path))) {
5310 715dc77e 2019-08-03 stsp path = strdup(argv[x]);
5311 715dc77e 2019-08-03 stsp if (path == NULL) {
5312 715dc77e 2019-08-03 stsp error = got_error_from_errno("strdup");
5313 715dc77e 2019-08-03 stsp goto done;
5316 715dc77e 2019-08-03 stsp } else if (asprintf(&path, "%s/%s",
5317 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree),
5318 715dc77e 2019-08-03 stsp argv[x]) == -1) {
5319 715dc77e 2019-08-03 stsp error = got_error_from_errno("asprintf");
5320 715dc77e 2019-08-03 stsp goto done;
5324 715dc77e 2019-08-03 stsp got_path_strip_trailing_slashes(path);
5325 715dc77e 2019-08-03 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
5326 715dc77e 2019-08-03 stsp if (error) {
5327 715dc77e 2019-08-03 stsp free(path);
5328 715dc77e 2019-08-03 stsp goto done;
5332 408b4ebc 2019-08-03 stsp if (list_stage) {
5333 408b4ebc 2019-08-03 stsp if (TAILQ_EMPTY(&paths)) {
5334 408b4ebc 2019-08-03 stsp char *s = strdup("");
5335 408b4ebc 2019-08-03 stsp if (s == NULL) {
5336 408b4ebc 2019-08-03 stsp error = got_error_from_errno("strdup");
5337 408b4ebc 2019-08-03 stsp goto done;
5339 408b4ebc 2019-08-03 stsp error = got_pathlist_append(&paths, s, NULL);
5340 408b4ebc 2019-08-03 stsp if (error)
5341 408b4ebc 2019-08-03 stsp goto done;
5343 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
5344 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
5346 408b4ebc 2019-08-03 stsp error = got_worktree_stage(worktree, &paths, print_status,
5347 408b4ebc 2019-08-03 stsp NULL, repo);
5350 0ebf8283 2019-07-24 stsp got_repo_close(repo);
5351 715dc77e 2019-08-03 stsp if (worktree)
5352 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
5353 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5354 715dc77e 2019-08-03 stsp free((char *)pe->path);
5355 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
5356 715dc77e 2019-08-03 stsp free(cwd);
5357 0ebf8283 2019-07-24 stsp return error;