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 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 3c45a30a 2019-05-12 jcs #include <sys/param.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12ce7a6c 2019-08-12 stsp #include <limits.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);
99 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
101 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
102 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
103 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
104 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
105 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
106 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
107 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
108 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
110 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
111 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
112 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
113 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
114 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
115 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
116 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
117 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
118 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
119 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
120 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
121 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
123 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
124 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
125 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
126 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
127 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
128 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
129 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
130 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
131 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
132 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
133 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
134 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
135 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
136 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
137 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
138 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
139 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
140 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
141 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
142 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
143 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
144 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
147 ce5b7c56 2019-07-09 stsp static void
148 ce5b7c56 2019-07-09 stsp list_commands(void)
152 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
153 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
154 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
155 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
157 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
161 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
163 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
164 5c860e29 2018-03-12 stsp unsigned int i;
166 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
168 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
170 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
171 5c860e29 2018-03-12 stsp switch (ch) {
179 ce5b7c56 2019-07-09 stsp usage(hflag);
180 5c860e29 2018-03-12 stsp /* NOTREACHED */
184 5c860e29 2018-03-12 stsp argc -= optind;
185 5c860e29 2018-03-12 stsp argv += optind;
186 1e70621d 2018-03-27 stsp optind = 0;
188 53ccebc2 2019-07-30 stsp if (Vflag) {
189 53ccebc2 2019-07-30 stsp got_version_print_str();
193 5c860e29 2018-03-12 stsp if (argc <= 0)
194 ce5b7c56 2019-07-09 stsp usage(hflag);
196 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
197 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
199 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
200 d7d4f210 2018-03-12 stsp const struct got_error *error;
202 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
204 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
205 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
209 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
211 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
212 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
213 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
220 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
221 ce5b7c56 2019-07-09 stsp list_commands();
225 4ed7e80c 2018-05-20 stsp __dead static void
226 ce5b7c56 2019-07-09 stsp usage(int hflag)
228 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
229 53ccebc2 2019-07-30 stsp getprogname());
231 ce5b7c56 2019-07-09 stsp list_commands();
235 0266afb7 2019-01-04 stsp static const struct got_error *
236 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
238 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
239 e2ba3d07 2019-05-13 stsp const char *editor;
241 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
242 e2ba3d07 2019-05-13 stsp if (editor == NULL)
243 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
245 0ee7065d 2019-05-13 stsp if (editor) {
246 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
248 0ee7065d 2019-05-13 stsp return err;
251 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
252 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
253 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
254 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
257 e2ba3d07 2019-05-13 stsp return NULL;
260 e2ba3d07 2019-05-13 stsp static const struct got_error *
261 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
262 c530dc23 2019-07-23 stsp const char *worktree_path)
264 163ce85a 2019-05-13 stsp const struct got_error *err;
266 37c06ea4 2019-07-15 stsp #ifdef PROFILE
267 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
268 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
270 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
271 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
273 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
274 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
276 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
277 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
279 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
280 163ce85a 2019-05-13 stsp if (err != NULL)
281 163ce85a 2019-05-13 stsp return err;
283 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
284 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
286 0266afb7 2019-01-04 stsp return NULL;
289 2c7829a4 2019-06-17 stsp __dead static void
290 2c7829a4 2019-06-17 stsp usage_init(void)
292 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
296 2c7829a4 2019-06-17 stsp static const struct got_error *
297 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
299 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
300 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
303 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
304 2c7829a4 2019-06-17 stsp switch (ch) {
306 2c7829a4 2019-06-17 stsp usage_init();
307 2c7829a4 2019-06-17 stsp /* NOTREACHED */
311 2c7829a4 2019-06-17 stsp argc -= optind;
312 2c7829a4 2019-06-17 stsp argv += optind;
314 2c7829a4 2019-06-17 stsp #ifndef PROFILE
315 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
316 2c7829a4 2019-06-17 stsp err(1, "pledge");
318 2c7829a4 2019-06-17 stsp if (argc != 1)
319 bc20e173 2019-06-17 stsp usage_init();
321 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
322 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
323 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
325 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
327 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
328 2c7829a4 2019-06-17 stsp if (error &&
329 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
332 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
336 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
337 2c7829a4 2019-06-17 stsp if (error != NULL)
341 3ce1b845 2019-07-15 stsp free(repo_path);
342 3ce1b845 2019-07-15 stsp return error;
345 3ce1b845 2019-07-15 stsp __dead static void
346 3ce1b845 2019-07-15 stsp usage_import(void)
348 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
349 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
354 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
357 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
358 3ce1b845 2019-07-15 stsp int st = -1;
360 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
361 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
362 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
364 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
366 3ce1b845 2019-07-15 stsp goto doneediting;
368 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
369 3ce1b845 2019-07-15 stsp _exit(127);
372 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
373 3ce1b845 2019-07-15 stsp if (errno != EINTR)
376 3ce1b845 2019-07-15 stsp doneediting:
377 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
378 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
379 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
381 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
382 3ce1b845 2019-07-15 stsp errno = EINTR;
386 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
389 3ce1b845 2019-07-15 stsp static const struct got_error *
390 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
391 3ce1b845 2019-07-15 stsp const char *initial_content)
393 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
394 3ce1b845 2019-07-15 stsp char buf[1024];
395 3ce1b845 2019-07-15 stsp struct stat st, st2;
397 3ce1b845 2019-07-15 stsp int content_changed = 0;
398 3ce1b845 2019-07-15 stsp size_t len;
400 3ce1b845 2019-07-15 stsp *logmsg = NULL;
402 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
403 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
405 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
406 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
408 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
409 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
411 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
412 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
413 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
415 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
416 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
417 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
418 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
421 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
422 3ce1b845 2019-07-15 stsp if (fp == NULL) {
423 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
426 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
427 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
428 3ce1b845 2019-07-15 stsp content_changed = 1;
429 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
430 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
431 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
433 3ce1b845 2019-07-15 stsp fclose(fp);
435 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
436 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
440 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
441 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
442 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
445 3ce1b845 2019-07-15 stsp free(*logmsg);
446 3ce1b845 2019-07-15 stsp *logmsg = NULL;
448 3ce1b845 2019-07-15 stsp return err;
451 3ce1b845 2019-07-15 stsp static const struct got_error *
452 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
453 3ce1b845 2019-07-15 stsp const char *branch_name)
455 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
456 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
459 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
460 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
461 3ce1b845 2019-07-15 stsp branch_name) == -1)
462 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
464 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
468 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
471 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
473 3ce1b845 2019-07-15 stsp free(initial_content);
474 3ce1b845 2019-07-15 stsp free(logmsg_path);
475 3ce1b845 2019-07-15 stsp return err;
478 3ce1b845 2019-07-15 stsp static const struct got_error *
479 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
481 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
482 3ce1b845 2019-07-15 stsp return NULL;
485 3ce1b845 2019-07-15 stsp static const struct got_error *
486 84792843 2019-08-09 stsp get_author(const char **author)
488 84792843 2019-08-09 stsp const char *got_author;
490 84792843 2019-08-09 stsp *author = NULL;
492 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
493 84792843 2019-08-09 stsp if (got_author == NULL) {
494 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
495 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
498 84792843 2019-08-09 stsp *author = got_author;
501 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
502 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
504 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
505 84792843 2019-08-09 stsp got_author++;
506 84792843 2019-08-09 stsp if (*got_author != '<')
507 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
508 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
509 84792843 2019-08-09 stsp got_author++;
510 84792843 2019-08-09 stsp if (*got_author != '@')
511 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
512 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
513 84792843 2019-08-09 stsp got_author++;
514 84792843 2019-08-09 stsp if (*got_author != '>')
515 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
517 84792843 2019-08-09 stsp return NULL;
520 84792843 2019-08-09 stsp static const struct got_error *
521 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
523 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
524 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
525 3ce1b845 2019-07-15 stsp char *editor = NULL;
526 84792843 2019-08-09 stsp const char *author;
527 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
528 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
529 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
530 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
531 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
533 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
534 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
536 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
538 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
539 3ce1b845 2019-07-15 stsp switch (ch) {
541 3ce1b845 2019-07-15 stsp branch_name = optarg;
544 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
545 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
546 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
551 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
552 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
553 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
558 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
560 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
566 3ce1b845 2019-07-15 stsp usage_init();
567 3ce1b845 2019-07-15 stsp /* NOTREACHED */
571 3ce1b845 2019-07-15 stsp argc -= optind;
572 3ce1b845 2019-07-15 stsp argv += optind;
574 3ce1b845 2019-07-15 stsp #ifndef PROFILE
575 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
576 3ce1b845 2019-07-15 stsp NULL) == -1)
577 3ce1b845 2019-07-15 stsp err(1, "pledge");
579 3ce1b845 2019-07-15 stsp if (argc != 1)
580 3ce1b845 2019-07-15 stsp usage_import();
582 84792843 2019-08-09 stsp error = get_author(&author);
584 84792843 2019-08-09 stsp return error;
586 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
587 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
588 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
589 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
591 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
592 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
596 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
597 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
601 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
602 3ce1b845 2019-07-15 stsp if (error) {
603 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
606 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
607 3ce1b845 2019-07-15 stsp "import target branch already exists");
611 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
612 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
613 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
616 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
619 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
620 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
622 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
623 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
626 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
631 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
632 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
634 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
638 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
639 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
643 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
647 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
651 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
655 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
656 3ce1b845 2019-07-15 stsp if (error) {
657 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
660 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
661 3ce1b845 2019-07-15 stsp branch_ref);
665 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
670 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
671 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
673 2c7829a4 2019-06-17 stsp free(repo_path);
674 3ce1b845 2019-07-15 stsp free(editor);
675 3ce1b845 2019-07-15 stsp free(refname);
676 3ce1b845 2019-07-15 stsp free(new_commit_id);
677 3ce1b845 2019-07-15 stsp free(id_str);
678 3ce1b845 2019-07-15 stsp if (branch_ref)
679 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
680 3ce1b845 2019-07-15 stsp if (head_ref)
681 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
682 2c7829a4 2019-06-17 stsp return error;
685 4ed7e80c 2018-05-20 stsp __dead static void
686 c09a553d 2018-03-12 stsp usage_checkout(void)
688 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
689 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
693 1ee397ad 2019-07-12 stsp static const struct got_error *
694 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
696 92a684f4 2018-03-12 stsp char *worktree_path = arg;
698 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
699 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
700 1ee397ad 2019-07-12 stsp return NULL;
702 92a684f4 2018-03-12 stsp while (path[0] == '/')
705 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
706 1ee397ad 2019-07-12 stsp return NULL;
709 99437157 2018-11-11 stsp static const struct got_error *
710 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
712 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
713 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
714 99437157 2018-11-11 stsp return NULL;
717 8069f636 2019-01-12 stsp static const struct got_error *
718 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
719 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
721 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
722 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
724 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
725 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
727 36a38700 2019-05-10 stsp return err;
729 d5bea539 2019-05-13 stsp if (yca_id == NULL)
730 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
733 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
734 d5bea539 2019-05-13 stsp * and the work tree's base commit.
736 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
738 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
746 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
747 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
748 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
750 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
751 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
752 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
754 d5bea539 2019-05-13 stsp free(yca_id);
755 d5bea539 2019-05-13 stsp return NULL;
758 a367ff0f 2019-05-14 stsp static const struct got_error *
759 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
760 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
761 a51a74b3 2019-07-27 stsp struct got_repository *repo)
763 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
764 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
765 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
766 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
768 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
772 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
773 a51a74b3 2019-07-27 stsp is_same_branch = 1;
776 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
777 a51a74b3 2019-07-27 stsp is_same_branch = 1;
781 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
785 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
790 a367ff0f 2019-05-14 stsp struct got_object_id *id;
791 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
793 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
794 a367ff0f 2019-05-14 stsp err = NULL;
796 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
798 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
805 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
807 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
808 a367ff0f 2019-05-14 stsp is_same_branch = 1;
815 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
816 a367ff0f 2019-05-14 stsp free(head_commit_id);
817 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
818 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
819 04f57cb3 2019-07-25 stsp return err;
822 04f57cb3 2019-07-25 stsp static const struct got_error *
823 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
824 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
826 04f57cb3 2019-07-25 stsp const struct got_error *err;
827 04f57cb3 2019-07-25 stsp struct got_reference *ref;
828 303e2782 2019-08-09 stsp struct got_tag_object *tag;
830 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
831 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
832 303e2782 2019-08-09 stsp if (err == NULL) {
833 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
834 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
835 303e2782 2019-08-09 stsp if (*commit_id == NULL)
836 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
837 303e2782 2019-08-09 stsp got_object_tag_close(tag);
838 303e2782 2019-08-09 stsp return err;
839 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
840 303e2782 2019-08-09 stsp return err;
842 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
843 04f57cb3 2019-07-25 stsp if (err == NULL) {
844 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
845 04f57cb3 2019-07-25 stsp got_ref_close(ref);
847 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
848 04f57cb3 2019-07-25 stsp return err;
849 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
850 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
852 a367ff0f 2019-05-14 stsp return err;
855 4ed7e80c 2018-05-20 stsp static const struct got_error *
856 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
858 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
859 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
860 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
861 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
862 c09a553d 2018-03-12 stsp char *repo_path = NULL;
863 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
864 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
865 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
866 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
867 72151b04 2019-05-11 stsp int ch, same_path_prefix;
868 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
870 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
872 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
873 0bb8a95e 2018-03-12 stsp switch (ch) {
875 08573d5b 2019-05-14 stsp branch_name = optarg;
878 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
879 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
880 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
883 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
886 2deda0b9 2019-03-07 stsp usage_checkout();
887 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
891 0bb8a95e 2018-03-12 stsp argc -= optind;
892 0bb8a95e 2018-03-12 stsp argv += optind;
894 6715a751 2018-03-16 stsp #ifndef PROFILE
895 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
896 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
897 c09a553d 2018-03-12 stsp err(1, "pledge");
899 0bb8a95e 2018-03-12 stsp if (argc == 1) {
900 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
901 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
902 76089277 2018-04-01 stsp if (repo_path == NULL)
903 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
904 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
905 76089277 2018-04-01 stsp if (cwd == NULL) {
906 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
909 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
910 230a42bd 2019-05-11 jcs base = basename(path_prefix);
911 230a42bd 2019-05-11 jcs if (base == NULL) {
912 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
913 230a42bd 2019-05-11 jcs path_prefix);
917 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
918 230a42bd 2019-05-11 jcs if (base == NULL) {
919 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
924 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
925 c09a553d 2018-03-12 stsp if (dotgit)
926 c09a553d 2018-03-12 stsp *dotgit = '\0';
927 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
928 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
933 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
934 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
935 76089277 2018-04-01 stsp if (repo_path == NULL) {
936 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
939 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
940 76089277 2018-04-01 stsp if (worktree_path == NULL) {
941 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
942 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
946 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
947 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
948 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
953 c09a553d 2018-03-12 stsp usage_checkout();
955 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
956 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
958 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
959 c09a553d 2018-03-12 stsp if (error != NULL)
962 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
963 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
964 c530dc23 2019-07-23 stsp if (error) {
965 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
966 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
968 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
969 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
970 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
975 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
979 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
980 c09a553d 2018-03-12 stsp if (error != NULL)
983 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
984 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
987 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
988 c09a553d 2018-03-12 stsp if (error != NULL)
991 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
992 e5dc7198 2018-12-29 stsp path_prefix);
993 e5dc7198 2018-12-29 stsp if (error != NULL)
995 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
996 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1000 8069f636 2019-01-12 stsp if (commit_id_str) {
1001 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1002 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1003 30837e32 2019-07-25 stsp if (error)
1004 8069f636 2019-01-12 stsp goto done;
1005 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1006 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1007 8069f636 2019-01-12 stsp if (error != NULL) {
1008 8069f636 2019-01-12 stsp free(commit_id);
1009 8069f636 2019-01-12 stsp goto done;
1011 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1012 45d344f6 2019-05-14 stsp if (error)
1013 45d344f6 2019-05-14 stsp goto done;
1014 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1015 8069f636 2019-01-12 stsp commit_id);
1016 8069f636 2019-01-12 stsp free(commit_id);
1017 8069f636 2019-01-12 stsp if (error)
1018 8069f636 2019-01-12 stsp goto done;
1021 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1022 f2ea84fa 2019-07-27 stsp if (error)
1023 f2ea84fa 2019-07-27 stsp goto done;
1024 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1025 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1026 c09a553d 2018-03-12 stsp if (error != NULL)
1027 c09a553d 2018-03-12 stsp goto done;
1029 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1032 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1033 8069f636 2019-01-12 stsp free(commit_id_str);
1034 76089277 2018-04-01 stsp free(repo_path);
1035 507dc3bb 2018-12-29 stsp free(worktree_path);
1036 507dc3bb 2018-12-29 stsp return error;
1039 507dc3bb 2018-12-29 stsp __dead static void
1040 507dc3bb 2018-12-29 stsp usage_update(void)
1042 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1043 507dc3bb 2018-12-29 stsp getprogname());
1047 1ee397ad 2019-07-12 stsp static const struct got_error *
1048 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1050 784955db 2019-01-12 stsp int *did_something = arg;
1052 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1053 1ee397ad 2019-07-12 stsp return NULL;
1055 1545c615 2019-02-10 stsp *did_something = 1;
1057 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1058 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1059 1ee397ad 2019-07-12 stsp return NULL;
1061 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1063 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1064 1ee397ad 2019-07-12 stsp return NULL;
1067 be7061eb 2018-12-30 stsp static const struct got_error *
1068 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1069 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1070 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1072 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1073 a1fb16d8 2019-05-24 stsp char *base_id_str;
1074 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1076 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1077 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1078 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1079 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1080 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1081 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1082 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1085 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1086 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1087 a1fb16d8 2019-05-24 stsp if (err) {
1088 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1089 a1fb16d8 2019-05-24 stsp return err;
1090 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1092 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1093 a1fb16d8 2019-05-24 stsp return NULL;
1095 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1096 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1097 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1099 a1fb16d8 2019-05-24 stsp return err;
1100 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1101 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1102 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1103 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1104 0ebf8283 2019-07-24 stsp return NULL;
1107 0ebf8283 2019-07-24 stsp static const struct got_error *
1108 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1110 0ebf8283 2019-07-24 stsp const struct got_error *err;
1111 0ebf8283 2019-07-24 stsp int in_progress;
1113 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1115 0ebf8283 2019-07-24 stsp return err;
1116 0ebf8283 2019-07-24 stsp if (in_progress)
1117 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1119 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1121 0ebf8283 2019-07-24 stsp return err;
1122 0ebf8283 2019-07-24 stsp if (in_progress)
1123 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1125 a1fb16d8 2019-05-24 stsp return NULL;
1128 a5edda0a 2019-07-27 stsp static const struct got_error *
1129 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1130 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1132 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1133 a5edda0a 2019-07-27 stsp char *path;
1136 a5edda0a 2019-07-27 stsp if (argc == 0) {
1137 a5edda0a 2019-07-27 stsp path = strdup("");
1138 a5edda0a 2019-07-27 stsp if (path == NULL)
1139 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1140 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1143 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1144 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1147 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1148 a5edda0a 2019-07-27 stsp if (err) {
1149 a5edda0a 2019-07-27 stsp free(path);
1154 a5edda0a 2019-07-27 stsp return err;
1157 a1fb16d8 2019-05-24 stsp static const struct got_error *
1158 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1160 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1161 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1162 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1163 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1164 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1165 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1166 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1167 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1168 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1169 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1170 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1172 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1174 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1175 507dc3bb 2018-12-29 stsp switch (ch) {
1177 024e9686 2019-05-14 stsp branch_name = optarg;
1180 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1181 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1182 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1185 2deda0b9 2019-03-07 stsp usage_update();
1186 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1190 507dc3bb 2018-12-29 stsp argc -= optind;
1191 507dc3bb 2018-12-29 stsp argv += optind;
1193 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1194 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1195 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1196 507dc3bb 2018-12-29 stsp err(1, "pledge");
1198 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1199 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1200 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1201 c4cdcb68 2019-04-03 stsp goto done;
1203 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1204 7d5807f4 2019-07-11 stsp if (error)
1205 7d5807f4 2019-07-11 stsp goto done;
1207 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1208 f2ea84fa 2019-07-27 stsp if (error)
1209 f2ea84fa 2019-07-27 stsp goto done;
1211 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1212 507dc3bb 2018-12-29 stsp if (error != NULL)
1213 507dc3bb 2018-12-29 stsp goto done;
1215 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1216 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1217 087fb88c 2019-08-04 stsp if (error)
1218 087fb88c 2019-08-04 stsp goto done;
1220 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1221 0266afb7 2019-01-04 stsp if (error)
1222 0266afb7 2019-01-04 stsp goto done;
1224 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1225 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1226 024e9686 2019-05-14 stsp if (error != NULL)
1227 024e9686 2019-05-14 stsp goto done;
1228 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1229 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1230 9c4b8182 2019-01-02 stsp if (error != NULL)
1231 9c4b8182 2019-01-02 stsp goto done;
1232 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1233 507dc3bb 2018-12-29 stsp if (error != NULL)
1234 507dc3bb 2018-12-29 stsp goto done;
1236 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1237 04f57cb3 2019-07-25 stsp free(commit_id_str);
1238 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1239 30837e32 2019-07-25 stsp if (error)
1240 a297e751 2019-07-11 stsp goto done;
1241 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1242 a297e751 2019-07-11 stsp if (error)
1243 507dc3bb 2018-12-29 stsp goto done;
1246 a1fb16d8 2019-05-24 stsp if (branch_name) {
1247 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1248 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1249 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1251 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1252 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1253 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1254 024e9686 2019-05-14 stsp goto done;
1256 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1257 024e9686 2019-05-14 stsp if (error)
1258 024e9686 2019-05-14 stsp goto done;
1259 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1260 a367ff0f 2019-05-14 stsp free(head_commit_id);
1261 024e9686 2019-05-14 stsp if (error != NULL)
1262 024e9686 2019-05-14 stsp goto done;
1263 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1264 a367ff0f 2019-05-14 stsp if (error)
1265 a367ff0f 2019-05-14 stsp goto done;
1266 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1267 024e9686 2019-05-14 stsp if (error)
1268 024e9686 2019-05-14 stsp goto done;
1270 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1271 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1272 a367ff0f 2019-05-14 stsp if (error != NULL) {
1273 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1274 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1275 024e9686 2019-05-14 stsp goto done;
1277 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1278 a367ff0f 2019-05-14 stsp if (error)
1279 a367ff0f 2019-05-14 stsp goto done;
1282 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1283 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1284 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1285 507dc3bb 2018-12-29 stsp commit_id);
1286 507dc3bb 2018-12-29 stsp if (error)
1287 507dc3bb 2018-12-29 stsp goto done;
1290 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1291 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1292 507dc3bb 2018-12-29 stsp if (error != NULL)
1293 507dc3bb 2018-12-29 stsp goto done;
1295 784955db 2019-01-12 stsp if (did_something)
1296 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1298 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1300 c09a553d 2018-03-12 stsp free(worktree_path);
1301 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1302 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1303 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1304 507dc3bb 2018-12-29 stsp free(commit_id);
1305 9c4b8182 2019-01-02 stsp free(commit_id_str);
1306 c09a553d 2018-03-12 stsp return error;
1309 f42b1b34 2018-03-12 stsp static const struct got_error *
1310 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1311 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1313 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1314 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1315 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1316 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1317 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1319 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1320 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1322 79109fed 2018-03-27 stsp return err;
1324 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1325 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1326 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1328 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1330 79109fed 2018-03-27 stsp return err;
1332 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1333 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1334 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1336 0f2b3dca 2018-12-22 stsp return err;
1338 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1340 79109fed 2018-03-27 stsp return err;
1343 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1345 0f2b3dca 2018-12-22 stsp goto done;
1347 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1348 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1349 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1350 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1351 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1353 79109fed 2018-03-27 stsp if (tree1)
1354 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1355 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1356 0f2b3dca 2018-12-22 stsp free(id_str1);
1357 0f2b3dca 2018-12-22 stsp free(id_str2);
1358 79109fed 2018-03-27 stsp return err;
1361 4bb494d5 2018-06-16 stsp static char *
1362 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1364 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
1365 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1367 6c281f94 2018-06-11 stsp *p = '\0';
1371 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1373 79109fed 2018-03-27 stsp static const struct got_error *
1374 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1375 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1376 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1378 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1379 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1380 6c281f94 2018-06-11 stsp char datebuf[26];
1381 45d799e2 2018-12-23 stsp time_t committer_time;
1382 45d799e2 2018-12-23 stsp const char *author, *committer;
1383 199a4027 2019-02-02 stsp char *refs_str = NULL;
1384 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1386 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1388 199a4027 2019-02-02 stsp const char *name;
1389 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1392 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1393 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1395 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1396 199a4027 2019-02-02 stsp name += 5;
1397 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1399 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1400 e34f9ed6 2019-02-02 stsp name += 6;
1401 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1402 141c2bff 2019-02-04 stsp name += 8;
1403 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1404 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1408 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1409 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1411 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1412 a436ad14 2019-08-13 stsp if (cmp != 0)
1414 199a4027 2019-02-02 stsp s = refs_str;
1415 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1416 199a4027 2019-02-02 stsp name) == -1) {
1417 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1423 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1425 8bf5b3c9 2018-03-17 stsp return err;
1427 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1428 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1429 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1430 832c249c 2018-06-10 stsp free(id_str);
1431 d3d493d7 2019-02-21 stsp id_str = NULL;
1432 d3d493d7 2019-02-21 stsp free(refs_str);
1433 d3d493d7 2019-02-21 stsp refs_str = NULL;
1434 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1435 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1436 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1437 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
1438 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1439 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1440 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1441 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1442 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1443 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1444 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1445 3fe1abad 2018-06-10 stsp int n = 1;
1446 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1447 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1448 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1450 a0603db2 2018-06-10 stsp return err;
1451 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1452 a0603db2 2018-06-10 stsp free(id_str);
1456 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1457 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
1458 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1460 621015ac 2018-07-23 stsp logmsg = logmsg0;
1462 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1464 832c249c 2018-06-10 stsp printf(" %s\n", line);
1465 832c249c 2018-06-10 stsp } while (line);
1466 621015ac 2018-07-23 stsp free(logmsg0);
1468 971751ac 2018-03-27 stsp if (show_patch) {
1469 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1470 971751ac 2018-03-27 stsp if (err == 0)
1471 971751ac 2018-03-27 stsp printf("\n");
1474 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1475 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1476 07862c20 2018-09-15 stsp return err;
1479 f42b1b34 2018-03-12 stsp static const struct got_error *
1480 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1481 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1482 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1484 f42b1b34 2018-03-12 stsp const struct got_error *err;
1485 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1487 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1488 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1490 f42b1b34 2018-03-12 stsp return err;
1491 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1493 fcc85cad 2018-07-22 stsp goto done;
1495 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1496 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1498 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1501 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1502 372ccdbb 2018-06-10 stsp if (err) {
1503 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1504 9ba79e04 2018-06-11 stsp err = NULL;
1507 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1509 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1515 b43fbaa0 2018-06-11 stsp if (id == NULL)
1518 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1521 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1523 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1524 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1528 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1529 f42b1b34 2018-03-12 stsp return err;
1532 4ed7e80c 2018-05-20 stsp __dead static void
1533 6f3d1eb0 2018-03-12 stsp usage_log(void)
1535 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1536 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1540 4ed7e80c 2018-05-20 stsp static const struct got_error *
1541 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1543 f42b1b34 2018-03-12 stsp const struct got_error *error;
1544 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1545 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1546 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1547 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1548 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1549 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1550 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1551 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1552 64a96a6d 2018-04-01 stsp const char *errstr;
1553 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1555 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1557 6715a751 2018-03-16 stsp #ifndef PROFILE
1558 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1561 f42b1b34 2018-03-12 stsp err(1, "pledge");
1564 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1565 79109fed 2018-03-27 stsp switch (ch) {
1567 79109fed 2018-03-27 stsp show_patch = 1;
1570 d142fc45 2018-04-01 stsp start_commit = optarg;
1573 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1575 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1576 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1579 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1580 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1581 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1584 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1587 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1588 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1589 04ca23f4 2018-07-16 stsp err(1, "-r option");
1590 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1593 2deda0b9 2019-03-07 stsp usage_log();
1594 79109fed 2018-03-27 stsp /* NOTREACHED */
1598 79109fed 2018-03-27 stsp argc -= optind;
1599 79109fed 2018-03-27 stsp argv += optind;
1601 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1602 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1603 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1604 04ca23f4 2018-07-16 stsp goto done;
1607 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1608 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1609 cffc0aa4 2019-02-05 stsp goto done;
1610 cffc0aa4 2019-02-05 stsp error = NULL;
1612 e7301579 2019-03-18 stsp if (argc == 0) {
1613 cbd1af7a 2019-03-18 stsp path = strdup("");
1614 e7301579 2019-03-18 stsp if (path == NULL) {
1615 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1616 cbd1af7a 2019-03-18 stsp goto done;
1618 e7301579 2019-03-18 stsp } else if (argc == 1) {
1619 e7301579 2019-03-18 stsp if (worktree) {
1620 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1622 e7301579 2019-03-18 stsp if (error)
1623 e7301579 2019-03-18 stsp goto done;
1625 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1626 e7301579 2019-03-18 stsp if (path == NULL) {
1627 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1628 e7301579 2019-03-18 stsp goto done;
1632 cbd1af7a 2019-03-18 stsp usage_log();
1634 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1635 5486daa2 2019-05-11 stsp repo_path = worktree ?
1636 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1638 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1639 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1640 cffc0aa4 2019-02-05 stsp goto done;
1643 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1644 d7d4f210 2018-03-12 stsp if (error != NULL)
1645 04ca23f4 2018-07-16 stsp goto done;
1647 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1648 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1649 c02c541e 2019-03-29 stsp if (error)
1650 c02c541e 2019-03-29 stsp goto done;
1652 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1653 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1654 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1655 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1656 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1657 3235492e 2018-04-01 stsp if (error != NULL)
1658 3235492e 2018-04-01 stsp return error;
1659 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1660 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1661 3235492e 2018-04-01 stsp if (error != NULL)
1662 3235492e 2018-04-01 stsp return error;
1663 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1665 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1666 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1667 3235492e 2018-04-01 stsp if (error == NULL) {
1668 1caad61b 2019-02-01 stsp int obj_type;
1669 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1670 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1671 d1f2edc9 2018-06-13 stsp if (error != NULL)
1672 1caad61b 2019-02-01 stsp goto done;
1673 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1674 1caad61b 2019-02-01 stsp if (error != NULL)
1675 1caad61b 2019-02-01 stsp goto done;
1676 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1677 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1678 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1679 1caad61b 2019-02-01 stsp if (error != NULL)
1680 1caad61b 2019-02-01 stsp goto done;
1681 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1682 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1683 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1684 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1685 1caad61b 2019-02-01 stsp goto done;
1688 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1689 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1690 1caad61b 2019-02-01 stsp if (id == NULL)
1691 638f9024 2019-05-13 stsp error = got_error_from_errno(
1692 230a42bd 2019-05-11 jcs "got_object_id_dup");
1693 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1694 1caad61b 2019-02-01 stsp if (error)
1695 1caad61b 2019-02-01 stsp goto done;
1696 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1697 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1698 1caad61b 2019-02-01 stsp goto done;
1700 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1701 d1f2edc9 2018-06-13 stsp if (error != NULL)
1702 1caad61b 2019-02-01 stsp goto done;
1704 15a94983 2018-12-23 stsp if (commit == NULL) {
1705 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1706 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1707 d1f2edc9 2018-06-13 stsp if (error != NULL)
1708 d1f2edc9 2018-06-13 stsp return error;
1711 d7d4f210 2018-03-12 stsp if (error != NULL)
1712 04ca23f4 2018-07-16 stsp goto done;
1714 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1715 04ca23f4 2018-07-16 stsp if (error != NULL)
1716 04ca23f4 2018-07-16 stsp goto done;
1717 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1718 04ca23f4 2018-07-16 stsp free(path);
1719 04ca23f4 2018-07-16 stsp path = in_repo_path;
1722 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1723 199a4027 2019-02-02 stsp if (error)
1724 199a4027 2019-02-02 stsp goto done;
1726 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1727 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1729 04ca23f4 2018-07-16 stsp free(path);
1730 04ca23f4 2018-07-16 stsp free(repo_path);
1731 04ca23f4 2018-07-16 stsp free(cwd);
1733 cffc0aa4 2019-02-05 stsp if (worktree)
1734 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1735 ad242220 2018-09-08 stsp if (repo) {
1736 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1737 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1738 ad242220 2018-09-08 stsp if (error == NULL)
1739 ad242220 2018-09-08 stsp error = repo_error;
1741 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1742 8bf5b3c9 2018-03-17 stsp return error;
1745 4ed7e80c 2018-05-20 stsp __dead static void
1746 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1748 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1749 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1753 b72f483a 2019-02-05 stsp struct print_diff_arg {
1754 b72f483a 2019-02-05 stsp struct got_repository *repo;
1755 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1756 b72f483a 2019-02-05 stsp int diff_context;
1757 3fc0c068 2019-02-10 stsp const char *id_str;
1758 3fc0c068 2019-02-10 stsp int header_shown;
1759 98eaaa12 2019-08-03 stsp int diff_staged;
1762 4ed7e80c 2018-05-20 stsp static const struct got_error *
1763 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
1764 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
1765 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1767 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1768 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1769 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1770 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1771 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
1772 b72f483a 2019-02-05 stsp struct stat sb;
1774 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1775 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
1776 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
1777 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
1778 98eaaa12 2019-08-03 stsp return NULL;
1780 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
1781 98eaaa12 2019-08-03 stsp return NULL;
1782 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
1783 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
1784 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
1785 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
1786 98eaaa12 2019-08-03 stsp return NULL;
1789 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1790 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
1791 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
1792 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
1793 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1796 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1797 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
1798 98eaaa12 2019-08-03 stsp switch (staged_status) {
1799 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
1800 98eaaa12 2019-08-03 stsp label1 = path;
1801 98eaaa12 2019-08-03 stsp label2 = path;
1803 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
1804 98eaaa12 2019-08-03 stsp label2 = path;
1806 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
1807 98eaaa12 2019-08-03 stsp label1 = path;
1810 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
1812 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1813 98eaaa12 2019-08-03 stsp label1, label2, a->diff_context, a->repo, stdout);
1816 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
1817 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
1818 4ce46740 2019-08-08 stsp char *id_str;
1819 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1822 4ce46740 2019-08-08 stsp goto done;
1823 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
1825 4ce46740 2019-08-08 stsp goto done;
1826 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1827 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
1828 4ce46740 2019-08-08 stsp free(id_str);
1829 4ce46740 2019-08-08 stsp goto done;
1831 4ce46740 2019-08-08 stsp free(id_str);
1832 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
1833 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1835 4ce46740 2019-08-08 stsp goto done;
1838 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1839 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1840 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1841 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1842 2ec1f75b 2019-03-26 stsp goto done;
1845 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1846 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1847 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1848 2ec1f75b 2019-03-26 stsp goto done;
1850 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1851 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1852 2ec1f75b 2019-03-26 stsp goto done;
1855 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1857 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1858 4ce46740 2019-08-08 stsp a->diff_context, stdout);
1860 b72f483a 2019-02-05 stsp if (blob1)
1861 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1862 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1863 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1864 b72f483a 2019-02-05 stsp free(abspath);
1865 927df6b7 2019-02-10 stsp return err;
1868 927df6b7 2019-02-10 stsp static const struct got_error *
1869 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
1870 0adb7196 2019-08-11 stsp const char *id_str, int obj_type, struct got_repository *repo)
1872 0adb7196 2019-08-11 stsp const struct got_error *err;
1873 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
1874 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
1876 0adb7196 2019-08-11 stsp *id = NULL;
1877 0adb7196 2019-08-11 stsp *label = NULL;
1879 d24820bf 2019-08-11 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1880 d24820bf 2019-08-11 stsp if (err == NULL) {
1881 d24820bf 2019-08-11 stsp *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1882 d24820bf 2019-08-11 stsp if (*id == NULL)
1883 d24820bf 2019-08-11 stsp err = got_error_from_errno("got_object_id_dup");
1884 d24820bf 2019-08-11 stsp if (asprintf(label, "refs/tags/%s",
1885 1b7a69d3 2019-08-11 stsp got_object_tag_get_name(tag)) == -1)
1886 d24820bf 2019-08-11 stsp err = got_error_from_errno("asprintf");
1887 d24820bf 2019-08-11 stsp got_object_tag_close(tag);
1888 d24820bf 2019-08-11 stsp return err;
1889 d24820bf 2019-08-11 stsp } else if (err->code != GOT_ERR_NO_OBJ)
1890 d24820bf 2019-08-11 stsp return err;
1892 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1893 0adb7196 2019-08-11 stsp if (err) {
1894 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1895 0adb7196 2019-08-11 stsp return err;
1896 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
1897 0adb7196 2019-08-11 stsp if (err != NULL)
1898 0adb7196 2019-08-11 stsp goto done;
1899 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
1900 0adb7196 2019-08-11 stsp if (*label == NULL) {
1901 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1902 0adb7196 2019-08-11 stsp goto done;
1904 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
1906 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
1907 0adb7196 2019-08-11 stsp if (*label == NULL) {
1908 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1909 0adb7196 2019-08-11 stsp goto done;
1914 0adb7196 2019-08-11 stsp got_ref_close(ref);
1915 0adb7196 2019-08-11 stsp return err;
1919 0adb7196 2019-08-11 stsp static const struct got_error *
1920 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1922 b00d56cd 2018-04-01 stsp const struct got_error *error;
1923 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1924 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1925 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1926 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1927 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1928 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1929 15a94983 2018-12-23 stsp int type1, type2;
1930 98eaaa12 2019-08-03 stsp int diff_context = 3, diff_staged = 0, ch;
1931 c0cc5c62 2018-10-18 stsp const char *errstr;
1932 927df6b7 2019-02-10 stsp char *path = NULL;
1934 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1935 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1936 25eccc22 2019-01-04 stsp NULL) == -1)
1937 b00d56cd 2018-04-01 stsp err(1, "pledge");
1940 98eaaa12 2019-08-03 stsp while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1941 b00d56cd 2018-04-01 stsp switch (ch) {
1943 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1944 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1945 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1948 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1949 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1950 b72f483a 2019-02-05 stsp err(1, "-r option");
1951 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1954 98eaaa12 2019-08-03 stsp diff_staged = 1;
1957 2deda0b9 2019-03-07 stsp usage_diff();
1958 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1962 b00d56cd 2018-04-01 stsp argc -= optind;
1963 b00d56cd 2018-04-01 stsp argv += optind;
1965 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1966 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1967 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1968 b72f483a 2019-02-05 stsp goto done;
1970 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1971 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1972 927df6b7 2019-02-10 stsp goto done;
1973 927df6b7 2019-02-10 stsp if (argc <= 1) {
1974 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1975 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1976 927df6b7 2019-02-10 stsp goto done;
1978 b72f483a 2019-02-05 stsp if (repo_path)
1980 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1981 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1982 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1983 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1984 927df6b7 2019-02-10 stsp goto done;
1986 927df6b7 2019-02-10 stsp if (argc == 1) {
1987 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1989 927df6b7 2019-02-10 stsp if (error)
1990 927df6b7 2019-02-10 stsp goto done;
1992 927df6b7 2019-02-10 stsp path = strdup("");
1993 927df6b7 2019-02-10 stsp if (path == NULL) {
1994 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1995 927df6b7 2019-02-10 stsp goto done;
1998 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1999 98eaaa12 2019-08-03 stsp if (diff_staged)
2000 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2001 98eaaa12 2019-08-03 stsp "objects in repository");
2002 15a94983 2018-12-23 stsp id_str1 = argv[0];
2003 15a94983 2018-12-23 stsp id_str2 = argv[1];
2004 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2005 30db809c 2019-06-05 stsp repo_path =
2006 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2007 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2008 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2009 30db809c 2019-06-05 stsp goto done;
2013 b00d56cd 2018-04-01 stsp usage_diff();
2015 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2016 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2017 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2018 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2021 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
2022 76089277 2018-04-01 stsp free(repo_path);
2023 b00d56cd 2018-04-01 stsp if (error != NULL)
2024 b00d56cd 2018-04-01 stsp goto done;
2026 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2027 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2028 c02c541e 2019-03-29 stsp if (error)
2029 c02c541e 2019-03-29 stsp goto done;
2031 30db809c 2019-06-05 stsp if (argc <= 1) {
2032 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2033 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2034 b72f483a 2019-02-05 stsp char *id_str;
2036 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2038 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2039 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2040 b72f483a 2019-02-05 stsp if (error)
2041 b72f483a 2019-02-05 stsp goto done;
2042 b72f483a 2019-02-05 stsp arg.repo = repo;
2043 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2044 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2045 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2046 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2047 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2049 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2050 72ea6654 2019-07-27 stsp if (error)
2051 72ea6654 2019-07-27 stsp goto done;
2053 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2054 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2055 b72f483a 2019-02-05 stsp free(id_str);
2056 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2057 b72f483a 2019-02-05 stsp goto done;
2060 0adb7196 2019-08-11 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2061 0adb7196 2019-08-11 stsp if (error)
2062 0adb7196 2019-08-11 stsp goto done;
2064 0adb7196 2019-08-11 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2065 0adb7196 2019-08-11 stsp if (error)
2066 0adb7196 2019-08-11 stsp goto done;
2068 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2069 15a94983 2018-12-23 stsp if (error)
2070 15a94983 2018-12-23 stsp goto done;
2072 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2073 15a94983 2018-12-23 stsp if (error)
2074 15a94983 2018-12-23 stsp goto done;
2076 15a94983 2018-12-23 stsp if (type1 != type2) {
2077 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2078 b00d56cd 2018-04-01 stsp goto done;
2081 15a94983 2018-12-23 stsp switch (type1) {
2082 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2083 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2084 54156555 2018-12-24 stsp diff_context, repo, stdout);
2086 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2087 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2088 54156555 2018-12-24 stsp diff_context, repo, stdout);
2090 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2091 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2092 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2093 c0cc5c62 2018-10-18 stsp repo, stdout);
2096 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2100 5e70831e 2019-05-28 stsp free(label1);
2101 5e70831e 2019-05-28 stsp free(label2);
2102 15a94983 2018-12-23 stsp free(id1);
2103 15a94983 2018-12-23 stsp free(id2);
2104 927df6b7 2019-02-10 stsp free(path);
2105 b72f483a 2019-02-05 stsp if (worktree)
2106 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2107 ad242220 2018-09-08 stsp if (repo) {
2108 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2109 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2110 ad242220 2018-09-08 stsp if (error == NULL)
2111 ad242220 2018-09-08 stsp error = repo_error;
2113 b00d56cd 2018-04-01 stsp return error;
2116 404c43c4 2018-06-21 stsp __dead static void
2117 404c43c4 2018-06-21 stsp usage_blame(void)
2119 9270e621 2019-02-05 stsp fprintf(stderr,
2120 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2121 404c43c4 2018-06-21 stsp getprogname());
2125 404c43c4 2018-06-21 stsp static const struct got_error *
2126 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2128 404c43c4 2018-06-21 stsp const struct got_error *error;
2129 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2130 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2131 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2132 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2133 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2136 404c43c4 2018-06-21 stsp #ifndef PROFILE
2137 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2138 36e2fb66 2019-01-04 stsp NULL) == -1)
2139 404c43c4 2018-06-21 stsp err(1, "pledge");
2142 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2143 404c43c4 2018-06-21 stsp switch (ch) {
2145 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2148 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2149 66bea077 2018-08-02 stsp if (repo_path == NULL)
2150 66bea077 2018-08-02 stsp err(1, "-r option");
2151 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2154 2deda0b9 2019-03-07 stsp usage_blame();
2155 404c43c4 2018-06-21 stsp /* NOTREACHED */
2159 404c43c4 2018-06-21 stsp argc -= optind;
2160 404c43c4 2018-06-21 stsp argv += optind;
2162 a39318fd 2018-08-02 stsp if (argc == 1)
2163 404c43c4 2018-06-21 stsp path = argv[0];
2165 404c43c4 2018-06-21 stsp usage_blame();
2167 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2168 66bea077 2018-08-02 stsp if (cwd == NULL) {
2169 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2170 66bea077 2018-08-02 stsp goto done;
2172 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2173 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2174 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2175 66bea077 2018-08-02 stsp goto done;
2177 0c06baac 2019-02-05 stsp error = NULL;
2178 0c06baac 2019-02-05 stsp if (worktree) {
2179 0c06baac 2019-02-05 stsp repo_path =
2180 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2181 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2182 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2183 0c06baac 2019-02-05 stsp if (error)
2184 0c06baac 2019-02-05 stsp goto done;
2186 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2187 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2188 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2189 0c06baac 2019-02-05 stsp goto done;
2194 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2195 c02c541e 2019-03-29 stsp if (error != NULL)
2196 36e2fb66 2019-01-04 stsp goto done;
2198 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2199 c02c541e 2019-03-29 stsp if (error)
2200 404c43c4 2018-06-21 stsp goto done;
2202 0c06baac 2019-02-05 stsp if (worktree) {
2203 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2204 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2205 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2206 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2207 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2208 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2209 6efaaa2d 2019-02-05 stsp path) == -1) {
2210 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2211 0c06baac 2019-02-05 stsp goto done;
2213 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2216 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2218 0c06baac 2019-02-05 stsp if (error)
2219 66bea077 2018-08-02 stsp goto done;
2221 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2222 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2223 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2224 404c43c4 2018-06-21 stsp if (error != NULL)
2225 66bea077 2018-08-02 stsp goto done;
2226 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2227 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2228 404c43c4 2018-06-21 stsp if (error != NULL)
2229 66bea077 2018-08-02 stsp goto done;
2231 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2232 30837e32 2019-07-25 stsp if (error)
2233 66bea077 2018-08-02 stsp goto done;
2236 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
2238 66bea077 2018-08-02 stsp free(in_repo_path);
2239 66bea077 2018-08-02 stsp free(repo_path);
2240 66bea077 2018-08-02 stsp free(cwd);
2241 404c43c4 2018-06-21 stsp free(commit_id);
2242 0c06baac 2019-02-05 stsp if (worktree)
2243 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2244 ad242220 2018-09-08 stsp if (repo) {
2245 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2246 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2247 ad242220 2018-09-08 stsp if (error == NULL)
2248 ad242220 2018-09-08 stsp error = repo_error;
2250 404c43c4 2018-06-21 stsp return error;
2253 5de5890b 2018-10-18 stsp __dead static void
2254 5de5890b 2018-10-18 stsp usage_tree(void)
2256 c1669e2e 2019-01-09 stsp fprintf(stderr,
2257 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2258 5de5890b 2018-10-18 stsp getprogname());
2262 c1669e2e 2019-01-09 stsp static void
2263 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2264 c1669e2e 2019-01-09 stsp const char *root_path)
2266 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2267 848d6979 2019-08-12 stsp const char *modestr = "";
2269 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2270 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2273 848d6979 2019-08-12 stsp if (S_ISLNK(te->mode))
2274 848d6979 2019-08-12 stsp modestr = "@";
2275 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2276 848d6979 2019-08-12 stsp modestr = "/";
2277 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2278 848d6979 2019-08-12 stsp modestr = "*";
2280 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2281 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2284 5de5890b 2018-10-18 stsp static const struct got_error *
2285 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2286 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2287 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2289 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2290 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2291 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2292 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2293 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2295 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2297 5de5890b 2018-10-18 stsp goto done;
2299 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2301 5de5890b 2018-10-18 stsp goto done;
2302 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2303 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2304 5de5890b 2018-10-18 stsp while (te) {
2305 5de5890b 2018-10-18 stsp char *id = NULL;
2307 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2310 5de5890b 2018-10-18 stsp if (show_ids) {
2311 5de5890b 2018-10-18 stsp char *id_str;
2312 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2314 5de5890b 2018-10-18 stsp goto done;
2315 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2316 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2317 5de5890b 2018-10-18 stsp free(id_str);
2318 5de5890b 2018-10-18 stsp goto done;
2320 5de5890b 2018-10-18 stsp free(id_str);
2322 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2325 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2326 c1669e2e 2019-01-09 stsp char *child_path;
2327 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2328 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2329 c1669e2e 2019-01-09 stsp te->name) == -1) {
2330 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2331 c1669e2e 2019-01-09 stsp goto done;
2333 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2334 c1669e2e 2019-01-09 stsp root_path, repo);
2335 c1669e2e 2019-01-09 stsp free(child_path);
2337 c1669e2e 2019-01-09 stsp goto done;
2340 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2344 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2345 5de5890b 2018-10-18 stsp free(tree_id);
2346 5de5890b 2018-10-18 stsp return err;
2349 5de5890b 2018-10-18 stsp static const struct got_error *
2350 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2352 5de5890b 2018-10-18 stsp const struct got_error *error;
2353 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2354 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2355 7a2c19d6 2019-02-05 stsp const char *path;
2356 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2357 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2358 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2359 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2362 5de5890b 2018-10-18 stsp #ifndef PROFILE
2363 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2364 0f8d269b 2019-01-04 stsp NULL) == -1)
2365 5de5890b 2018-10-18 stsp err(1, "pledge");
2368 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2369 5de5890b 2018-10-18 stsp switch (ch) {
2371 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2374 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2375 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2376 5de5890b 2018-10-18 stsp err(1, "-r option");
2377 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2380 5de5890b 2018-10-18 stsp show_ids = 1;
2383 c1669e2e 2019-01-09 stsp recurse = 1;
2386 2deda0b9 2019-03-07 stsp usage_tree();
2387 5de5890b 2018-10-18 stsp /* NOTREACHED */
2391 5de5890b 2018-10-18 stsp argc -= optind;
2392 5de5890b 2018-10-18 stsp argv += optind;
2394 5de5890b 2018-10-18 stsp if (argc == 1)
2395 5de5890b 2018-10-18 stsp path = argv[0];
2396 5de5890b 2018-10-18 stsp else if (argc > 1)
2397 5de5890b 2018-10-18 stsp usage_tree();
2399 7a2c19d6 2019-02-05 stsp path = NULL;
2401 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2402 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2403 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2404 9bf7a39b 2019-02-05 stsp goto done;
2406 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2407 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2408 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2409 7a2c19d6 2019-02-05 stsp goto done;
2411 7a2c19d6 2019-02-05 stsp error = NULL;
2412 7a2c19d6 2019-02-05 stsp if (worktree) {
2413 7a2c19d6 2019-02-05 stsp repo_path =
2414 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2415 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2416 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2417 7a2c19d6 2019-02-05 stsp if (error)
2418 7a2c19d6 2019-02-05 stsp goto done;
2420 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2421 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2422 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2423 7a2c19d6 2019-02-05 stsp goto done;
2428 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2429 5de5890b 2018-10-18 stsp if (error != NULL)
2430 c02c541e 2019-03-29 stsp goto done;
2432 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2433 c02c541e 2019-03-29 stsp if (error)
2434 5de5890b 2018-10-18 stsp goto done;
2436 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2437 9bf7a39b 2019-02-05 stsp if (worktree) {
2438 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2439 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2440 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2441 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2442 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2443 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2444 9bf7a39b 2019-02-05 stsp goto done;
2446 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2448 9bf7a39b 2019-02-05 stsp if (error)
2449 9bf7a39b 2019-02-05 stsp goto done;
2451 9bf7a39b 2019-02-05 stsp path = "/";
2453 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2454 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2455 9bf7a39b 2019-02-05 stsp if (error != NULL)
2456 9bf7a39b 2019-02-05 stsp goto done;
2459 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2460 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2461 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2462 5de5890b 2018-10-18 stsp if (error != NULL)
2463 5de5890b 2018-10-18 stsp goto done;
2464 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2465 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2466 5de5890b 2018-10-18 stsp if (error != NULL)
2467 5de5890b 2018-10-18 stsp goto done;
2469 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2470 30837e32 2019-07-25 stsp if (error)
2471 5de5890b 2018-10-18 stsp goto done;
2474 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2475 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2477 5de5890b 2018-10-18 stsp free(in_repo_path);
2478 5de5890b 2018-10-18 stsp free(repo_path);
2479 5de5890b 2018-10-18 stsp free(cwd);
2480 5de5890b 2018-10-18 stsp free(commit_id);
2481 7a2c19d6 2019-02-05 stsp if (worktree)
2482 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2483 5de5890b 2018-10-18 stsp if (repo) {
2484 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2485 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2486 5de5890b 2018-10-18 stsp if (error == NULL)
2487 5de5890b 2018-10-18 stsp error = repo_error;
2489 5de5890b 2018-10-18 stsp return error;
2492 6bad629b 2019-02-04 stsp __dead static void
2493 6bad629b 2019-02-04 stsp usage_status(void)
2495 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2499 b72f483a 2019-02-05 stsp static const struct got_error *
2500 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2501 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2502 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2504 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2505 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2506 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2507 b72f483a 2019-02-05 stsp return NULL;
2510 6bad629b 2019-02-04 stsp static const struct got_error *
2511 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2513 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2514 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2515 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2516 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2517 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2518 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2521 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2523 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2524 6bad629b 2019-02-04 stsp switch (ch) {
2526 2deda0b9 2019-03-07 stsp usage_status();
2527 6bad629b 2019-02-04 stsp /* NOTREACHED */
2531 6bad629b 2019-02-04 stsp argc -= optind;
2532 6bad629b 2019-02-04 stsp argv += optind;
2534 6bad629b 2019-02-04 stsp #ifndef PROFILE
2535 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2536 6bad629b 2019-02-04 stsp NULL) == -1)
2537 6bad629b 2019-02-04 stsp err(1, "pledge");
2539 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2540 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2541 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2542 927df6b7 2019-02-10 stsp goto done;
2545 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2546 927df6b7 2019-02-10 stsp if (error != NULL)
2547 a5edda0a 2019-07-27 stsp goto done;
2549 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2550 6bad629b 2019-02-04 stsp if (error != NULL)
2551 6bad629b 2019-02-04 stsp goto done;
2553 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2554 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2555 087fb88c 2019-08-04 stsp if (error)
2556 087fb88c 2019-08-04 stsp goto done;
2558 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2559 6bad629b 2019-02-04 stsp if (error)
2560 6bad629b 2019-02-04 stsp goto done;
2562 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2563 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2565 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2566 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2567 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2568 927df6b7 2019-02-10 stsp free(cwd);
2569 d0eebce4 2019-03-11 stsp return error;
2572 d0eebce4 2019-03-11 stsp __dead static void
2573 d0eebce4 2019-03-11 stsp usage_ref(void)
2575 d0eebce4 2019-03-11 stsp fprintf(stderr,
2576 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2577 d0eebce4 2019-03-11 stsp getprogname());
2581 d0eebce4 2019-03-11 stsp static const struct got_error *
2582 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2584 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2585 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2586 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2588 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2589 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2591 d0eebce4 2019-03-11 stsp return err;
2593 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2594 d0eebce4 2019-03-11 stsp char *refstr;
2595 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2596 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2597 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2598 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2599 d0eebce4 2019-03-11 stsp free(refstr);
2602 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2603 d0eebce4 2019-03-11 stsp return NULL;
2606 d0eebce4 2019-03-11 stsp static const struct got_error *
2607 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2609 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2610 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2612 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2614 d0eebce4 2019-03-11 stsp return err;
2616 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2617 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2618 d0eebce4 2019-03-11 stsp return err;
2621 d0eebce4 2019-03-11 stsp static const struct got_error *
2622 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2624 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2625 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2626 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2629 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2630 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2631 d1644381 2019-07-14 stsp * an unintended typo.
2633 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2634 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2636 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2638 d83d9d5c 2019-05-13 stsp if (err) {
2639 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2641 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2642 d83d9d5c 2019-05-13 stsp return err;
2643 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2645 d83d9d5c 2019-05-13 stsp return err;
2646 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2647 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2649 d83d9d5c 2019-05-13 stsp return err;
2652 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2654 d0eebce4 2019-03-11 stsp goto done;
2656 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2659 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2661 d1c1ae5f 2019-08-12 stsp return err;
2664 d1c1ae5f 2019-08-12 stsp static const struct got_error *
2665 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
2667 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
2668 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
2669 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
2672 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
2673 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
2674 d1c1ae5f 2019-08-12 stsp * an unintended typo.
2676 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
2677 d1c1ae5f 2019-08-12 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2679 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
2681 d1c1ae5f 2019-08-12 stsp return err;
2683 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
2685 d1c1ae5f 2019-08-12 stsp goto done;
2687 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
2689 d1c1ae5f 2019-08-12 stsp if (target_ref)
2690 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
2692 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
2693 d0eebce4 2019-03-11 stsp return err;
2696 d0eebce4 2019-03-11 stsp static const struct got_error *
2697 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2699 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2700 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2701 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2702 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2703 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
2704 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2706 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2707 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2708 d0eebce4 2019-03-11 stsp switch (ch) {
2710 d0eebce4 2019-03-11 stsp delref = optarg;
2713 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2714 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2715 d0eebce4 2019-03-11 stsp err(1, "-r option");
2716 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2719 d0eebce4 2019-03-11 stsp do_list = 1;
2722 d1c1ae5f 2019-08-12 stsp create_symref = 1;
2725 d0eebce4 2019-03-11 stsp usage_ref();
2726 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2730 d0eebce4 2019-03-11 stsp if (do_list && delref)
2731 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2733 d0eebce4 2019-03-11 stsp argc -= optind;
2734 d0eebce4 2019-03-11 stsp argv += optind;
2736 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2737 d1c1ae5f 2019-08-12 stsp if (create_symref)
2738 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
2739 d1c1ae5f 2019-08-12 stsp "-l or -d options");
2740 d0eebce4 2019-03-11 stsp if (argc > 0)
2741 d0eebce4 2019-03-11 stsp usage_ref();
2742 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2743 d0eebce4 2019-03-11 stsp usage_ref();
2745 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2746 e0b57350 2019-03-12 stsp if (do_list) {
2747 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2748 e0b57350 2019-03-12 stsp NULL) == -1)
2749 e0b57350 2019-03-12 stsp err(1, "pledge");
2751 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2752 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2753 e0b57350 2019-03-12 stsp err(1, "pledge");
2756 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2757 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2758 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2759 d0eebce4 2019-03-11 stsp goto done;
2762 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2763 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2764 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2765 d0eebce4 2019-03-11 stsp goto done;
2767 d0eebce4 2019-03-11 stsp error = NULL;
2768 d0eebce4 2019-03-11 stsp if (worktree) {
2769 d0eebce4 2019-03-11 stsp repo_path =
2770 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2771 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2772 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2773 d0eebce4 2019-03-11 stsp if (error)
2774 d0eebce4 2019-03-11 stsp goto done;
2776 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2777 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2778 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2779 d0eebce4 2019-03-11 stsp goto done;
2784 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2785 d0eebce4 2019-03-11 stsp if (error != NULL)
2786 d0eebce4 2019-03-11 stsp goto done;
2788 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2789 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2790 c02c541e 2019-03-29 stsp if (error)
2791 c02c541e 2019-03-29 stsp goto done;
2793 d0eebce4 2019-03-11 stsp if (do_list)
2794 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2795 d0eebce4 2019-03-11 stsp else if (delref)
2796 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2797 d1c1ae5f 2019-08-12 stsp else if (create_symref)
2798 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
2800 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2803 4e759de4 2019-06-26 stsp got_repo_close(repo);
2804 4e759de4 2019-06-26 stsp if (worktree)
2805 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2806 4e759de4 2019-06-26 stsp free(cwd);
2807 4e759de4 2019-06-26 stsp free(repo_path);
2808 4e759de4 2019-06-26 stsp return error;
2811 4e759de4 2019-06-26 stsp __dead static void
2812 4e759de4 2019-06-26 stsp usage_branch(void)
2814 4e759de4 2019-06-26 stsp fprintf(stderr,
2815 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2816 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2820 4e759de4 2019-06-26 stsp static const struct got_error *
2821 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
2823 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2824 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2825 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2827 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2829 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2831 4e759de4 2019-06-26 stsp return err;
2833 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2834 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
2835 4e759de4 2019-06-26 stsp char *refstr;
2836 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2837 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2839 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
2840 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
2841 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
2842 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
2844 ba882ee3 2019-07-11 stsp return err;
2845 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
2846 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
2847 ba882ee3 2019-07-11 stsp marker = "* ";
2849 ba882ee3 2019-07-11 stsp marker = "~ ";
2852 4e759de4 2019-06-26 stsp refname += 11;
2853 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2854 4e759de4 2019-06-26 stsp if (refstr == NULL)
2855 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2856 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
2857 4e759de4 2019-06-26 stsp free(refstr);
2860 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2861 4e759de4 2019-06-26 stsp return NULL;
2864 4e759de4 2019-06-26 stsp static const struct got_error *
2865 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2867 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2868 4e759de4 2019-06-26 stsp struct got_reference *ref;
2869 4e759de4 2019-06-26 stsp char *refname;
2871 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2872 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2874 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2876 4e759de4 2019-06-26 stsp goto done;
2878 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2879 4e759de4 2019-06-26 stsp got_ref_close(ref);
2881 4e759de4 2019-06-26 stsp free(refname);
2882 4e759de4 2019-06-26 stsp return err;
2885 4e759de4 2019-06-26 stsp static const struct got_error *
2886 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2887 4e759de4 2019-06-26 stsp const char *base_branch)
2889 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2890 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2891 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2892 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2893 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2896 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
2897 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
2898 d3f84d51 2019-07-11 stsp * an unintended typo.
2900 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
2901 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2903 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2904 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2905 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2906 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2907 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2908 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2910 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2912 4e759de4 2019-06-26 stsp goto done;
2913 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2914 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2916 4e759de4 2019-06-26 stsp goto done;
2918 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2919 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2920 4e759de4 2019-06-26 stsp goto done;
2923 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2924 4e759de4 2019-06-26 stsp if (err == NULL) {
2925 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2926 4e759de4 2019-06-26 stsp goto done;
2927 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2928 4e759de4 2019-06-26 stsp goto done;
2930 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2932 4e759de4 2019-06-26 stsp goto done;
2934 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2937 4e759de4 2019-06-26 stsp got_ref_close(ref);
2939 4e759de4 2019-06-26 stsp free(base_refname);
2940 4e759de4 2019-06-26 stsp free(refname);
2941 4e759de4 2019-06-26 stsp return err;
2944 4e759de4 2019-06-26 stsp static const struct got_error *
2945 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2947 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2948 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2949 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2950 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2951 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2952 4e759de4 2019-06-26 stsp const char *delref = NULL;
2954 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2955 4e759de4 2019-06-26 stsp switch (ch) {
2957 4e759de4 2019-06-26 stsp delref = optarg;
2960 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2961 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2962 4e759de4 2019-06-26 stsp err(1, "-r option");
2963 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2966 4e759de4 2019-06-26 stsp do_list = 1;
2969 4e759de4 2019-06-26 stsp usage_branch();
2970 4e759de4 2019-06-26 stsp /* NOTREACHED */
2974 4e759de4 2019-06-26 stsp if (do_list && delref)
2975 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2977 4e759de4 2019-06-26 stsp argc -= optind;
2978 4e759de4 2019-06-26 stsp argv += optind;
2980 4e759de4 2019-06-26 stsp if (do_list || delref) {
2981 4e759de4 2019-06-26 stsp if (argc > 0)
2982 4e759de4 2019-06-26 stsp usage_branch();
2983 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2984 4e759de4 2019-06-26 stsp usage_branch();
2986 4e759de4 2019-06-26 stsp #ifndef PROFILE
2987 4e759de4 2019-06-26 stsp if (do_list) {
2988 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2989 4e759de4 2019-06-26 stsp NULL) == -1)
2990 4e759de4 2019-06-26 stsp err(1, "pledge");
2992 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2993 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2994 4e759de4 2019-06-26 stsp err(1, "pledge");
2997 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2998 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2999 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3000 4e759de4 2019-06-26 stsp goto done;
3003 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3004 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3005 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3006 4e759de4 2019-06-26 stsp goto done;
3008 4e759de4 2019-06-26 stsp error = NULL;
3009 4e759de4 2019-06-26 stsp if (worktree) {
3010 4e759de4 2019-06-26 stsp repo_path =
3011 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3012 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3013 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3014 4e759de4 2019-06-26 stsp if (error)
3015 4e759de4 2019-06-26 stsp goto done;
3017 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3018 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3019 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3020 4e759de4 2019-06-26 stsp goto done;
3025 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
3026 4e759de4 2019-06-26 stsp if (error != NULL)
3027 4e759de4 2019-06-26 stsp goto done;
3029 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3030 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3031 4e759de4 2019-06-26 stsp if (error)
3032 4e759de4 2019-06-26 stsp goto done;
3034 4e759de4 2019-06-26 stsp if (do_list)
3035 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3036 4e759de4 2019-06-26 stsp else if (delref)
3037 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
3039 4e759de4 2019-06-26 stsp const char *base_branch;
3040 4e759de4 2019-06-26 stsp if (argc == 1) {
3041 4e759de4 2019-06-26 stsp base_branch = worktree ?
3042 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3043 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3044 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
3045 dc5351b4 2019-07-30 stsp base_branch += 11;
3047 4e759de4 2019-06-26 stsp base_branch = argv[1];
3048 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
3052 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3053 d0eebce4 2019-03-11 stsp if (worktree)
3054 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3055 d0eebce4 2019-03-11 stsp free(cwd);
3056 d0eebce4 2019-03-11 stsp free(repo_path);
3057 d00136be 2019-03-26 stsp return error;
3060 d00136be 2019-03-26 stsp __dead static void
3061 d00136be 2019-03-26 stsp usage_add(void)
3063 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3067 d00136be 2019-03-26 stsp static const struct got_error *
3068 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
3070 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
3071 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
3072 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
3073 1dd54920 2019-05-11 stsp char *cwd = NULL;
3074 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
3075 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3078 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
3080 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3081 d00136be 2019-03-26 stsp switch (ch) {
3083 d00136be 2019-03-26 stsp usage_add();
3084 d00136be 2019-03-26 stsp /* NOTREACHED */
3088 d00136be 2019-03-26 stsp argc -= optind;
3089 d00136be 2019-03-26 stsp argv += optind;
3091 43012d58 2019-07-14 stsp #ifndef PROFILE
3092 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3093 43012d58 2019-07-14 stsp NULL) == -1)
3094 43012d58 2019-07-14 stsp err(1, "pledge");
3096 723c305c 2019-05-11 jcs if (argc < 1)
3097 d00136be 2019-03-26 stsp usage_add();
3099 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
3100 d00136be 2019-03-26 stsp if (cwd == NULL) {
3101 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3102 d00136be 2019-03-26 stsp goto done;
3105 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3106 d00136be 2019-03-26 stsp if (error)
3107 d00136be 2019-03-26 stsp goto done;
3109 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3110 031a5338 2019-03-26 stsp if (error != NULL)
3111 031a5338 2019-03-26 stsp goto done;
3113 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3114 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3115 d00136be 2019-03-26 stsp if (error)
3116 d00136be 2019-03-26 stsp goto done;
3118 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3119 6d022e97 2019-08-04 stsp if (error)
3120 6d022e97 2019-08-04 stsp goto done;
3122 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
3123 1dd54920 2019-05-11 stsp NULL, repo);
3126 031a5338 2019-03-26 stsp got_repo_close(repo);
3127 d00136be 2019-03-26 stsp if (worktree)
3128 d00136be 2019-03-26 stsp got_worktree_close(worktree);
3129 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
3130 1dd54920 2019-05-11 stsp free((char *)pe->path);
3131 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
3132 2ec1f75b 2019-03-26 stsp free(cwd);
3133 2ec1f75b 2019-03-26 stsp return error;
3136 2ec1f75b 2019-03-26 stsp __dead static void
3137 648e4ef7 2019-07-09 stsp usage_remove(void)
3139 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3143 2ec1f75b 2019-03-26 stsp static const struct got_error *
3144 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
3146 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
3147 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
3148 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
3149 17ed4618 2019-06-02 stsp char *cwd = NULL;
3150 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
3151 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3152 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
3154 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
3156 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
3157 2ec1f75b 2019-03-26 stsp switch (ch) {
3159 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
3162 2ec1f75b 2019-03-26 stsp usage_add();
3163 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
3167 2ec1f75b 2019-03-26 stsp argc -= optind;
3168 2ec1f75b 2019-03-26 stsp argv += optind;
3170 43012d58 2019-07-14 stsp #ifndef PROFILE
3171 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3172 43012d58 2019-07-14 stsp NULL) == -1)
3173 43012d58 2019-07-14 stsp err(1, "pledge");
3175 17ed4618 2019-06-02 stsp if (argc < 1)
3176 648e4ef7 2019-07-09 stsp usage_remove();
3178 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
3179 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
3180 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3181 2ec1f75b 2019-03-26 stsp goto done;
3183 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3184 2ec1f75b 2019-03-26 stsp if (error)
3185 2ec1f75b 2019-03-26 stsp goto done;
3187 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3189 2ec1f75b 2019-03-26 stsp goto done;
3191 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3192 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3193 2ec1f75b 2019-03-26 stsp if (error)
3194 2ec1f75b 2019-03-26 stsp goto done;
3196 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3197 6d022e97 2019-08-04 stsp if (error)
3198 6d022e97 2019-08-04 stsp goto done;
3200 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
3201 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
3202 a129376b 2019-03-28 stsp if (error)
3203 a129376b 2019-03-28 stsp goto done;
3206 a129376b 2019-03-28 stsp got_repo_close(repo);
3207 a129376b 2019-03-28 stsp if (worktree)
3208 a129376b 2019-03-28 stsp got_worktree_close(worktree);
3209 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
3210 17ed4618 2019-06-02 stsp free((char *)pe->path);
3211 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
3212 a129376b 2019-03-28 stsp free(cwd);
3213 a129376b 2019-03-28 stsp return error;
3216 a129376b 2019-03-28 stsp __dead static void
3217 a129376b 2019-03-28 stsp usage_revert(void)
3219 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3220 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
3224 1ee397ad 2019-07-12 stsp static const struct got_error *
3225 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3227 a129376b 2019-03-28 stsp while (path[0] == '/')
3229 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3230 33aa809d 2019-08-08 stsp return NULL;
3233 33aa809d 2019-08-08 stsp struct choose_patch_arg {
3234 33aa809d 2019-08-08 stsp FILE *patch_script_file;
3235 33aa809d 2019-08-08 stsp const char *action;
3238 33aa809d 2019-08-08 stsp static const struct got_error *
3239 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3240 33aa809d 2019-08-08 stsp int nchanges, const char *action)
3242 33aa809d 2019-08-08 stsp char *line = NULL;
3243 33aa809d 2019-08-08 stsp size_t linesize = 0;
3244 33aa809d 2019-08-08 stsp ssize_t linelen;
3246 33aa809d 2019-08-08 stsp switch (status) {
3247 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
3248 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
3250 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
3251 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
3253 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
3254 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
3255 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
3256 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3257 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3258 33aa809d 2019-08-08 stsp printf("%s", line);
3259 33aa809d 2019-08-08 stsp if (ferror(patch_file))
3260 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3261 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3262 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3263 33aa809d 2019-08-08 stsp path, n, nchanges, action);
3266 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
3269 33aa809d 2019-08-08 stsp return NULL;
3272 33aa809d 2019-08-08 stsp static const struct got_error *
3273 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3274 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
3276 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3277 33aa809d 2019-08-08 stsp char *line = NULL;
3278 33aa809d 2019-08-08 stsp size_t linesize = 0;
3279 33aa809d 2019-08-08 stsp ssize_t linelen;
3280 33aa809d 2019-08-08 stsp int resp = ' ';
3281 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
3283 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3285 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
3287 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3288 33aa809d 2019-08-08 stsp a->action);
3290 33aa809d 2019-08-08 stsp return err;
3291 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
3292 33aa809d 2019-08-08 stsp if (linelen == -1) {
3293 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
3294 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3295 33aa809d 2019-08-08 stsp return NULL;
3297 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
3299 33aa809d 2019-08-08 stsp *nl = '\0';
3300 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
3301 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3302 33aa809d 2019-08-08 stsp printf("y\n");
3303 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
3304 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3305 33aa809d 2019-08-08 stsp printf("n\n");
3306 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
3307 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
3308 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3309 33aa809d 2019-08-08 stsp printf("q\n");
3311 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
3312 33aa809d 2019-08-08 stsp free(line);
3313 33aa809d 2019-08-08 stsp return NULL;
3316 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
3317 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3318 33aa809d 2019-08-08 stsp a->action);
3320 33aa809d 2019-08-08 stsp return err;
3321 33aa809d 2019-08-08 stsp resp = getchar();
3322 33aa809d 2019-08-08 stsp if (resp == '\n')
3323 33aa809d 2019-08-08 stsp resp = getchar();
3324 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
3325 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
3326 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3327 33aa809d 2019-08-08 stsp resp = ' ';
3329 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
3330 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3331 33aa809d 2019-08-08 stsp resp = ' ';
3335 33aa809d 2019-08-08 stsp if (resp == 'y')
3336 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3337 33aa809d 2019-08-08 stsp else if (resp == 'n')
3338 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3339 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3340 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3342 1ee397ad 2019-07-12 stsp return NULL;
3346 a129376b 2019-03-28 stsp static const struct got_error *
3347 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3349 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3350 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3351 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3352 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3353 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3354 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
3355 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
3356 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
3357 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
3358 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
3360 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3362 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3363 a129376b 2019-03-28 stsp switch (ch) {
3365 33aa809d 2019-08-08 stsp pflag = 1;
3368 33aa809d 2019-08-08 stsp patch_script_path = optarg;
3371 0f6d7415 2019-08-08 stsp can_recurse = 1;
3374 a129376b 2019-03-28 stsp usage_revert();
3375 a129376b 2019-03-28 stsp /* NOTREACHED */
3379 a129376b 2019-03-28 stsp argc -= optind;
3380 a129376b 2019-03-28 stsp argv += optind;
3382 43012d58 2019-07-14 stsp #ifndef PROFILE
3383 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3384 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3385 43012d58 2019-07-14 stsp err(1, "pledge");
3387 e20a8b6f 2019-06-04 stsp if (argc < 1)
3388 a129376b 2019-03-28 stsp usage_revert();
3389 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
3390 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
3392 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3393 a129376b 2019-03-28 stsp if (cwd == NULL) {
3394 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3395 a129376b 2019-03-28 stsp goto done;
3397 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3398 2ec1f75b 2019-03-26 stsp if (error)
3399 2ec1f75b 2019-03-26 stsp goto done;
3401 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3402 a129376b 2019-03-28 stsp if (error != NULL)
3403 a129376b 2019-03-28 stsp goto done;
3405 33aa809d 2019-08-08 stsp if (patch_script_path) {
3406 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
3407 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
3408 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
3409 33aa809d 2019-08-08 stsp patch_script_path);
3410 33aa809d 2019-08-08 stsp goto done;
3413 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3414 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3415 a129376b 2019-03-28 stsp if (error)
3416 a129376b 2019-03-28 stsp goto done;
3418 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3419 6d022e97 2019-08-04 stsp if (error)
3420 6d022e97 2019-08-04 stsp goto done;
3422 0f6d7415 2019-08-08 stsp if (!can_recurse) {
3423 0f6d7415 2019-08-08 stsp char *ondisk_path;
3424 0f6d7415 2019-08-08 stsp struct stat sb;
3425 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
3426 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3427 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
3428 0f6d7415 2019-08-08 stsp pe->path) == -1) {
3429 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
3430 0f6d7415 2019-08-08 stsp goto done;
3432 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
3433 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
3434 0f6d7415 2019-08-08 stsp free(ondisk_path);
3437 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
3438 0f6d7415 2019-08-08 stsp ondisk_path);
3439 0f6d7415 2019-08-08 stsp free(ondisk_path);
3440 0f6d7415 2019-08-08 stsp goto done;
3442 0f6d7415 2019-08-08 stsp free(ondisk_path);
3443 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
3444 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3445 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
3446 0f6d7415 2019-08-08 stsp goto done;
3451 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
3452 33aa809d 2019-08-08 stsp cpa.action = "revert";
3453 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3454 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
3455 a129376b 2019-03-28 stsp if (error)
3456 a129376b 2019-03-28 stsp goto done;
3458 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
3459 33aa809d 2019-08-08 stsp error == NULL)
3460 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
3462 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3463 2ec1f75b 2019-03-26 stsp if (worktree)
3464 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3465 2ec1f75b 2019-03-26 stsp free(path);
3466 d00136be 2019-03-26 stsp free(cwd);
3467 6bad629b 2019-02-04 stsp return error;
3470 c4296144 2019-05-09 stsp __dead static void
3471 c4296144 2019-05-09 stsp usage_commit(void)
3473 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3474 5c1e53bc 2019-07-28 stsp getprogname());
3478 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3479 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3480 e2ba3d07 2019-05-13 stsp const char *editor;
3481 e0870e44 2019-05-13 stsp const char *worktree_path;
3482 76d98825 2019-06-03 stsp const char *branch_name;
3483 314a6357 2019-05-13 stsp const char *repo_path;
3484 e0870e44 2019-05-13 stsp char *logmsg_path;
3488 33ad4cbe 2019-05-12 jcs static const struct got_error *
3489 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3492 76d98825 2019-06-03 stsp char *initial_content = NULL;
3493 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3494 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3495 e0870e44 2019-05-13 stsp char *template = NULL;
3496 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3498 33ad4cbe 2019-05-12 jcs size_t len;
3500 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3501 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3502 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3503 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3504 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3505 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3506 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3507 33ad4cbe 2019-05-12 jcs return NULL;
3510 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3511 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3513 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3514 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3515 76d98825 2019-06-03 stsp a->branch_name) == -1)
3516 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3518 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3520 e0870e44 2019-05-13 stsp goto done;
3522 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3524 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3525 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3526 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3527 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3528 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3532 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3534 e2af0fd1 2019-08-08 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3535 e2af0fd1 2019-08-08 stsp unlink(a->logmsg_path);
3536 e2af0fd1 2019-08-08 stsp free(a->logmsg_path);
3537 e2af0fd1 2019-08-08 stsp a->logmsg_path = NULL;
3539 76d98825 2019-06-03 stsp free(initial_content);
3540 e0870e44 2019-05-13 stsp free(template);
3542 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3543 3ce1b845 2019-07-15 stsp if (err == NULL) {
3544 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3545 3ce1b845 2019-07-15 stsp if (err) {
3546 3ce1b845 2019-07-15 stsp free(*logmsg);
3547 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3550 33ad4cbe 2019-05-12 jcs return err;
3553 c4296144 2019-05-09 stsp static const struct got_error *
3554 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3556 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3557 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3558 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3559 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
3560 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3561 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3562 84792843 2019-08-09 stsp const char *author;
3563 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3564 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3565 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
3566 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
3568 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
3569 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
3571 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3572 c4296144 2019-05-09 stsp switch (ch) {
3574 c4296144 2019-05-09 stsp logmsg = optarg;
3577 c4296144 2019-05-09 stsp usage_commit();
3578 c4296144 2019-05-09 stsp /* NOTREACHED */
3582 c4296144 2019-05-09 stsp argc -= optind;
3583 c4296144 2019-05-09 stsp argv += optind;
3585 43012d58 2019-07-14 stsp #ifndef PROFILE
3586 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3587 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3588 43012d58 2019-07-14 stsp err(1, "pledge");
3590 84792843 2019-08-09 stsp error = get_author(&author);
3591 84792843 2019-08-09 stsp if (error)
3592 84792843 2019-08-09 stsp return error;
3594 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3595 c4296144 2019-05-09 stsp if (cwd == NULL) {
3596 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3597 c4296144 2019-05-09 stsp goto done;
3599 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3600 7d5807f4 2019-07-11 stsp if (error)
3601 7d5807f4 2019-07-11 stsp goto done;
3603 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3604 c4296144 2019-05-09 stsp if (error)
3605 a698f62e 2019-07-25 stsp goto done;
3606 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
3607 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
3608 c4296144 2019-05-09 stsp goto done;
3611 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
3612 916f288c 2019-07-30 stsp worktree);
3613 5c1e53bc 2019-07-28 stsp if (error)
3614 5c1e53bc 2019-07-28 stsp goto done;
3616 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3617 c4296144 2019-05-09 stsp if (error != NULL)
3618 c4296144 2019-05-09 stsp goto done;
3621 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3622 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3624 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3625 314a6357 2019-05-13 stsp error = get_editor(&editor);
3627 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3628 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3629 c4296144 2019-05-09 stsp if (error)
3630 c4296144 2019-05-09 stsp goto done;
3632 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3633 087fb88c 2019-08-04 stsp if (error)
3634 087fb88c 2019-08-04 stsp goto done;
3636 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3637 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3638 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3639 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3640 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
3641 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3642 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
3643 916f288c 2019-07-30 stsp goto done;
3645 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
3647 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3648 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3649 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3650 e0870e44 2019-05-13 stsp if (error) {
3651 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3652 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3653 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3654 c4296144 2019-05-09 stsp goto done;
3657 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3658 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3660 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3661 c4296144 2019-05-09 stsp if (error)
3662 c4296144 2019-05-09 stsp goto done;
3663 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3665 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
3667 c4296144 2019-05-09 stsp got_repo_close(repo);
3668 c4296144 2019-05-09 stsp if (worktree)
3669 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3670 c4296144 2019-05-09 stsp free(cwd);
3671 c4296144 2019-05-09 stsp free(id_str);
3672 e2ba3d07 2019-05-13 stsp free(editor);
3673 234035bc 2019-06-01 stsp return error;
3676 234035bc 2019-06-01 stsp __dead static void
3677 234035bc 2019-06-01 stsp usage_cherrypick(void)
3679 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3683 234035bc 2019-06-01 stsp static const struct got_error *
3684 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3686 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3687 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3688 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3689 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3690 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3691 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3692 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3693 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3694 234035bc 2019-06-01 stsp int ch, did_something = 0;
3696 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3697 234035bc 2019-06-01 stsp switch (ch) {
3699 234035bc 2019-06-01 stsp usage_cherrypick();
3700 234035bc 2019-06-01 stsp /* NOTREACHED */
3704 234035bc 2019-06-01 stsp argc -= optind;
3705 234035bc 2019-06-01 stsp argv += optind;
3707 43012d58 2019-07-14 stsp #ifndef PROFILE
3708 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3709 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3710 43012d58 2019-07-14 stsp err(1, "pledge");
3712 234035bc 2019-06-01 stsp if (argc != 1)
3713 234035bc 2019-06-01 stsp usage_cherrypick();
3715 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3716 234035bc 2019-06-01 stsp if (cwd == NULL) {
3717 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3718 234035bc 2019-06-01 stsp goto done;
3720 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3721 234035bc 2019-06-01 stsp if (error)
3722 234035bc 2019-06-01 stsp goto done;
3724 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3725 234035bc 2019-06-01 stsp if (error != NULL)
3726 234035bc 2019-06-01 stsp goto done;
3728 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3729 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3730 234035bc 2019-06-01 stsp if (error)
3731 234035bc 2019-06-01 stsp goto done;
3733 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3734 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3735 234035bc 2019-06-01 stsp if (error != NULL) {
3736 234035bc 2019-06-01 stsp struct got_reference *ref;
3737 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3738 234035bc 2019-06-01 stsp goto done;
3739 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3740 234035bc 2019-06-01 stsp if (error != NULL)
3741 234035bc 2019-06-01 stsp goto done;
3742 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3743 234035bc 2019-06-01 stsp got_ref_close(ref);
3744 234035bc 2019-06-01 stsp if (error != NULL)
3745 234035bc 2019-06-01 stsp goto done;
3747 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3748 234035bc 2019-06-01 stsp if (error)
3749 234035bc 2019-06-01 stsp goto done;
3751 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3752 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3753 234035bc 2019-06-01 stsp if (error != NULL)
3754 234035bc 2019-06-01 stsp goto done;
3756 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3757 234035bc 2019-06-01 stsp if (error) {
3758 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3759 234035bc 2019-06-01 stsp goto done;
3760 234035bc 2019-06-01 stsp error = NULL;
3762 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3763 234035bc 2019-06-01 stsp goto done;
3766 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3767 234035bc 2019-06-01 stsp if (error)
3768 234035bc 2019-06-01 stsp goto done;
3769 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3770 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3771 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3773 234035bc 2019-06-01 stsp if (error != NULL)
3774 234035bc 2019-06-01 stsp goto done;
3776 234035bc 2019-06-01 stsp if (did_something)
3777 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3779 234035bc 2019-06-01 stsp if (commit)
3780 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3781 234035bc 2019-06-01 stsp free(commit_id_str);
3782 234035bc 2019-06-01 stsp if (head_ref)
3783 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3784 234035bc 2019-06-01 stsp if (worktree)
3785 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3787 234035bc 2019-06-01 stsp got_repo_close(repo);
3788 c4296144 2019-05-09 stsp return error;
3791 5ef14e63 2019-06-02 stsp __dead static void
3792 5ef14e63 2019-06-02 stsp usage_backout(void)
3794 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3798 5ef14e63 2019-06-02 stsp static const struct got_error *
3799 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3801 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3802 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3803 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3804 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3805 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3806 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3807 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3808 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3809 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3811 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3812 5ef14e63 2019-06-02 stsp switch (ch) {
3814 5ef14e63 2019-06-02 stsp usage_backout();
3815 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3819 5ef14e63 2019-06-02 stsp argc -= optind;
3820 5ef14e63 2019-06-02 stsp argv += optind;
3822 43012d58 2019-07-14 stsp #ifndef PROFILE
3823 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3824 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3825 43012d58 2019-07-14 stsp err(1, "pledge");
3827 5ef14e63 2019-06-02 stsp if (argc != 1)
3828 5ef14e63 2019-06-02 stsp usage_backout();
3830 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3831 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3832 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3833 5ef14e63 2019-06-02 stsp goto done;
3835 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3836 5ef14e63 2019-06-02 stsp if (error)
3837 5ef14e63 2019-06-02 stsp goto done;
3839 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3840 5ef14e63 2019-06-02 stsp if (error != NULL)
3841 5ef14e63 2019-06-02 stsp goto done;
3843 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3844 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3845 5ef14e63 2019-06-02 stsp if (error)
3846 5ef14e63 2019-06-02 stsp goto done;
3848 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3849 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3850 5ef14e63 2019-06-02 stsp if (error != NULL) {
3851 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3852 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3853 5ef14e63 2019-06-02 stsp goto done;
3854 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3855 5ef14e63 2019-06-02 stsp if (error != NULL)
3856 5ef14e63 2019-06-02 stsp goto done;
3857 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3858 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3859 5ef14e63 2019-06-02 stsp if (error != NULL)
3860 5ef14e63 2019-06-02 stsp goto done;
3862 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3863 5ef14e63 2019-06-02 stsp if (error)
3864 5ef14e63 2019-06-02 stsp goto done;
3866 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3867 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3868 5ef14e63 2019-06-02 stsp if (error != NULL)
3869 5ef14e63 2019-06-02 stsp goto done;
3871 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3872 5ef14e63 2019-06-02 stsp if (error)
3873 5ef14e63 2019-06-02 stsp goto done;
3875 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3876 5ef14e63 2019-06-02 stsp if (error)
3877 5ef14e63 2019-06-02 stsp goto done;
3878 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3879 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3880 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3881 5ef14e63 2019-06-02 stsp goto done;
3884 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3885 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3886 5ef14e63 2019-06-02 stsp if (error != NULL)
3887 5ef14e63 2019-06-02 stsp goto done;
3889 5ef14e63 2019-06-02 stsp if (did_something)
3890 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3892 5ef14e63 2019-06-02 stsp if (commit)
3893 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3894 5ef14e63 2019-06-02 stsp free(commit_id_str);
3895 5ef14e63 2019-06-02 stsp if (head_ref)
3896 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3897 818c7501 2019-07-11 stsp if (worktree)
3898 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3900 818c7501 2019-07-11 stsp got_repo_close(repo);
3901 818c7501 2019-07-11 stsp return error;
3904 818c7501 2019-07-11 stsp __dead static void
3905 818c7501 2019-07-11 stsp usage_rebase(void)
3907 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3908 af54c8f8 2019-07-11 stsp getprogname());
3913 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
3916 0ebf8283 2019-07-24 stsp size_t len;
3918 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
3919 0ebf8283 2019-07-24 stsp if (len > limit)
3920 0ebf8283 2019-07-24 stsp len = limit;
3921 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
3922 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
3924 0ebf8283 2019-07-24 stsp *nl = '\0';
3927 0ebf8283 2019-07-24 stsp static const struct got_error *
3928 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3930 0ebf8283 2019-07-24 stsp const char *logmsg0 = NULL;
3932 0ebf8283 2019-07-24 stsp logmsg0 = got_object_commit_get_logmsg(commit);
3934 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)logmsg0[0]))
3935 0ebf8283 2019-07-24 stsp logmsg0++;
3937 0ebf8283 2019-07-24 stsp *logmsg = strdup(logmsg0);
3938 0ebf8283 2019-07-24 stsp if (*logmsg == NULL)
3939 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
3941 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
3942 0ebf8283 2019-07-24 stsp return NULL;
3945 818c7501 2019-07-11 stsp static const struct got_error *
3946 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3947 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3949 818c7501 2019-07-11 stsp const struct got_error *err;
3950 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3952 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3954 818c7501 2019-07-11 stsp goto done;
3956 ff0d2220 2019-07-11 stsp if (new_id) {
3957 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3959 ff0d2220 2019-07-11 stsp goto done;
3962 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3963 ff0d2220 2019-07-11 stsp if (new_id_str)
3964 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3966 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
3968 0ebf8283 2019-07-24 stsp goto done;
3970 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3971 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3973 818c7501 2019-07-11 stsp free(old_id_str);
3974 818c7501 2019-07-11 stsp free(new_id_str);
3975 818c7501 2019-07-11 stsp return err;
3978 1ee397ad 2019-07-12 stsp static const struct got_error *
3979 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
3981 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
3983 818c7501 2019-07-11 stsp while (path[0] == '/')
3985 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
3987 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
3988 1ee397ad 2019-07-12 stsp return NULL;
3989 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3990 818c7501 2019-07-11 stsp *rebase_status = status;
3991 1ee397ad 2019-07-12 stsp return NULL;
3994 818c7501 2019-07-11 stsp static const struct got_error *
3995 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3996 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
3997 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
3999 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
4000 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
4001 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
4004 818c7501 2019-07-11 stsp static const struct got_error *
4005 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
4006 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4007 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
4008 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4010 ff0d2220 2019-07-11 stsp const struct got_error *error;
4011 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
4012 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
4014 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4015 ff0d2220 2019-07-11 stsp if (error)
4016 ff0d2220 2019-07-11 stsp return error;
4018 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4019 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
4020 ff0d2220 2019-07-11 stsp if (error) {
4021 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4022 ff0d2220 2019-07-11 stsp goto done;
4023 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
4025 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
4026 ff0d2220 2019-07-11 stsp free(new_commit_id);
4029 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
4030 ff0d2220 2019-07-11 stsp return error;
4033 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
4034 64c6d990 2019-07-11 stsp const char *path_prefix;
4035 64c6d990 2019-07-11 stsp size_t len;
4036 8ca9bd68 2019-07-25 stsp int errcode;
4039 64c6d990 2019-07-11 stsp static const struct got_error *
4040 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4041 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
4042 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
4043 64c6d990 2019-07-11 stsp struct got_repository *repo)
4045 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
4047 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4048 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4049 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
4051 64c6d990 2019-07-11 stsp return NULL;
4054 64c6d990 2019-07-11 stsp static const struct got_error *
4055 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
4056 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
4057 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
4059 64c6d990 2019-07-11 stsp const struct got_error *err;
4060 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4061 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
4062 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
4064 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
4065 64c6d990 2019-07-11 stsp return NULL;
4067 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4069 64c6d990 2019-07-11 stsp goto done;
4071 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4073 64c6d990 2019-07-11 stsp goto done;
4075 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
4076 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
4078 64c6d990 2019-07-11 stsp goto done;
4080 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
4081 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
4083 64c6d990 2019-07-11 stsp goto done;
4085 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
4086 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
4087 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
4088 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
4089 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
4090 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
4091 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
4093 64c6d990 2019-07-11 stsp if (tree1)
4094 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
4095 64c6d990 2019-07-11 stsp if (tree2)
4096 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
4097 64c6d990 2019-07-11 stsp if (commit)
4098 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
4099 64c6d990 2019-07-11 stsp if (parent_commit)
4100 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
4101 64c6d990 2019-07-11 stsp return err;
4104 ff0d2220 2019-07-11 stsp static const struct got_error *
4105 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
4106 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
4107 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4108 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
4109 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
4111 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
4112 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
4113 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
4114 af61c510 2019-07-19 stsp struct got_object_qid *qid;
4115 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
4117 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4119 af61c510 2019-07-19 stsp return err;
4121 af61c510 2019-07-19 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4123 af61c510 2019-07-19 stsp goto done;
4124 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4125 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
4126 af61c510 2019-07-19 stsp if (err) {
4127 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
4128 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
4129 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
4130 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
4131 af61c510 2019-07-19 stsp "been reached?!?");
4132 af61c510 2019-07-19 stsp goto done;
4133 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4134 af61c510 2019-07-19 stsp goto done;
4135 af61c510 2019-07-19 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
4137 af61c510 2019-07-19 stsp goto done;
4139 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
4140 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
4142 af61c510 2019-07-19 stsp goto done;
4144 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
4146 af61c510 2019-07-19 stsp goto done;
4147 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4148 af61c510 2019-07-19 stsp commit_id = parent_id;
4152 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
4153 af61c510 2019-07-19 stsp return err;
4156 af61c510 2019-07-19 stsp static const struct got_error *
4157 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
4159 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
4160 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
4161 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
4162 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4163 818c7501 2019-07-11 stsp char *cwd = NULL;
4164 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
4165 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4166 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
4167 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
4168 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4169 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
4170 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4171 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4172 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
4173 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
4174 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
4175 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
4177 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
4178 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
4180 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
4181 818c7501 2019-07-11 stsp switch (ch) {
4183 818c7501 2019-07-11 stsp abort_rebase = 1;
4186 818c7501 2019-07-11 stsp continue_rebase = 1;
4189 818c7501 2019-07-11 stsp usage_rebase();
4190 818c7501 2019-07-11 stsp /* NOTREACHED */
4194 818c7501 2019-07-11 stsp argc -= optind;
4195 818c7501 2019-07-11 stsp argv += optind;
4197 43012d58 2019-07-14 stsp #ifndef PROFILE
4198 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4199 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4200 43012d58 2019-07-14 stsp err(1, "pledge");
4202 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
4203 818c7501 2019-07-11 stsp usage_rebase();
4204 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
4205 818c7501 2019-07-11 stsp if (argc != 0)
4206 818c7501 2019-07-11 stsp usage_rebase();
4207 818c7501 2019-07-11 stsp } else if (argc != 1)
4208 818c7501 2019-07-11 stsp usage_rebase();
4210 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
4211 818c7501 2019-07-11 stsp if (cwd == NULL) {
4212 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
4213 818c7501 2019-07-11 stsp goto done;
4215 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
4216 818c7501 2019-07-11 stsp if (error)
4217 818c7501 2019-07-11 stsp goto done;
4219 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4220 818c7501 2019-07-11 stsp if (error != NULL)
4221 818c7501 2019-07-11 stsp goto done;
4223 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4224 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4225 818c7501 2019-07-11 stsp if (error)
4226 818c7501 2019-07-11 stsp goto done;
4228 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4229 818c7501 2019-07-11 stsp if (error)
4230 818c7501 2019-07-11 stsp goto done;
4232 f6794adc 2019-07-23 stsp if (abort_rebase) {
4233 818c7501 2019-07-11 stsp int did_something;
4234 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4235 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4236 f6794adc 2019-07-23 stsp goto done;
4238 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4239 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4240 3e3a69f1 2019-07-25 stsp worktree, repo);
4241 818c7501 2019-07-11 stsp if (error)
4242 818c7501 2019-07-11 stsp goto done;
4243 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
4244 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
4245 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
4246 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
4247 818c7501 2019-07-11 stsp if (error)
4248 818c7501 2019-07-11 stsp goto done;
4249 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4250 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
4253 818c7501 2019-07-11 stsp if (continue_rebase) {
4254 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4255 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4256 f6794adc 2019-07-23 stsp goto done;
4258 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4259 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4260 3e3a69f1 2019-07-25 stsp worktree, repo);
4261 818c7501 2019-07-11 stsp if (error)
4262 818c7501 2019-07-11 stsp goto done;
4264 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4265 01757395 2019-07-12 stsp resume_commit_id, repo);
4266 818c7501 2019-07-11 stsp if (error)
4267 818c7501 2019-07-11 stsp goto done;
4269 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
4270 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
4271 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
4272 818c7501 2019-07-11 stsp goto done;
4275 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
4276 818c7501 2019-07-11 stsp if (error != NULL)
4277 818c7501 2019-07-11 stsp goto done;
4280 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4281 ff0d2220 2019-07-11 stsp if (error)
4282 ff0d2220 2019-07-11 stsp goto done;
4284 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
4285 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
4287 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
4288 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4289 a51a74b3 2019-07-27 stsp base_commit_id, branch_head_commit_id, repo);
4290 818c7501 2019-07-11 stsp if (error)
4291 818c7501 2019-07-11 stsp goto done;
4292 818c7501 2019-07-11 stsp if (yca_id == NULL) {
4293 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
4294 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
4295 818c7501 2019-07-11 stsp "with work tree's branch");
4296 818c7501 2019-07-11 stsp goto done;
4299 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
4300 a51a74b3 2019-07-27 stsp if (error) {
4301 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
4302 a51a74b3 2019-07-27 stsp goto done;
4303 a51a74b3 2019-07-27 stsp error = NULL;
4305 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
4306 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
4307 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
4308 a51a74b3 2019-07-27 stsp goto done;
4310 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
4311 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
4312 818c7501 2019-07-11 stsp if (error)
4313 818c7501 2019-07-11 stsp goto done;
4316 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
4317 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4318 818c7501 2019-07-11 stsp if (error)
4319 818c7501 2019-07-11 stsp goto done;
4321 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4322 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
4323 fc66b545 2019-08-12 stsp if (pid == NULL) {
4324 fc66b545 2019-08-12 stsp if (!continue_rebase) {
4325 fc66b545 2019-08-12 stsp int did_something;
4326 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
4327 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
4328 fc66b545 2019-08-12 stsp &did_something);
4329 fc66b545 2019-08-12 stsp if (error)
4330 fc66b545 2019-08-12 stsp goto done;
4331 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
4332 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
4334 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4335 fc66b545 2019-08-12 stsp goto done;
4337 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
4338 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
4339 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
4340 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4341 818c7501 2019-07-11 stsp commit = NULL;
4342 818c7501 2019-07-11 stsp if (error)
4343 818c7501 2019-07-11 stsp goto done;
4345 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
4346 ff0d2220 2019-07-11 stsp if (continue_rebase)
4347 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
4348 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
4350 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4351 818c7501 2019-07-11 stsp goto done;
4354 818c7501 2019-07-11 stsp pid = NULL;
4355 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
4356 818c7501 2019-07-11 stsp commit_id = qid->id;
4357 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
4358 818c7501 2019-07-11 stsp pid = qid;
4360 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
4361 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
4362 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4363 818c7501 2019-07-11 stsp if (error)
4364 818c7501 2019-07-11 stsp goto done;
4366 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4367 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4371 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
4372 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
4373 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4374 818c7501 2019-07-11 stsp if (error)
4375 818c7501 2019-07-11 stsp goto done;
4378 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4379 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
4380 818c7501 2019-07-11 stsp if (error)
4381 818c7501 2019-07-11 stsp goto done;
4382 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4383 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
4385 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
4386 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
4388 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
4389 818c7501 2019-07-11 stsp free(branch_head_commit_id);
4390 818c7501 2019-07-11 stsp free(resume_commit_id);
4391 818c7501 2019-07-11 stsp free(yca_id);
4392 818c7501 2019-07-11 stsp if (commit)
4393 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4394 818c7501 2019-07-11 stsp if (branch)
4395 818c7501 2019-07-11 stsp got_ref_close(branch);
4396 818c7501 2019-07-11 stsp if (new_base_branch)
4397 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
4398 818c7501 2019-07-11 stsp if (tmp_branch)
4399 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
4400 5ef14e63 2019-06-02 stsp if (worktree)
4401 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
4403 5ef14e63 2019-06-02 stsp got_repo_close(repo);
4404 5ef14e63 2019-06-02 stsp return error;
4407 0ebf8283 2019-07-24 stsp __dead static void
4408 0ebf8283 2019-07-24 stsp usage_histedit(void)
4410 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4411 0ebf8283 2019-07-24 stsp getprogname());
4415 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
4416 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
4417 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
4418 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
4419 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
4421 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
4422 0ebf8283 2019-07-24 stsp unsigned char code;
4423 0ebf8283 2019-07-24 stsp const char *name;
4424 0ebf8283 2019-07-24 stsp const char *desc;
4425 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
4426 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
4427 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4428 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4429 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4430 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
4431 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4434 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4435 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4436 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4437 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4438 0ebf8283 2019-07-24 stsp char *logmsg;
4440 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4442 0ebf8283 2019-07-24 stsp static const struct got_error *
4443 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4444 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4446 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4447 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4448 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4451 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4453 0ebf8283 2019-07-24 stsp goto done;
4455 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4457 0ebf8283 2019-07-24 stsp goto done;
4459 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4461 0ebf8283 2019-07-24 stsp goto done;
4463 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4464 0ebf8283 2019-07-24 stsp if (n < 0)
4465 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4467 0ebf8283 2019-07-24 stsp if (commit)
4468 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4469 0ebf8283 2019-07-24 stsp free(id_str);
4470 0ebf8283 2019-07-24 stsp free(logmsg);
4471 0ebf8283 2019-07-24 stsp return err;
4474 0ebf8283 2019-07-24 stsp static const struct got_error *
4475 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4476 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4478 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4479 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4481 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4482 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4484 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4485 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4491 0ebf8283 2019-07-24 stsp return err;
4494 0ebf8283 2019-07-24 stsp static const struct got_error *
4495 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4497 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4500 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4501 0ebf8283 2019-07-24 stsp if (n < 0)
4502 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4504 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4505 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4506 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4507 0ebf8283 2019-07-24 stsp cmd->desc);
4508 0ebf8283 2019-07-24 stsp if (n < 0) {
4509 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4513 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4514 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4515 0ebf8283 2019-07-24 stsp if (n < 0)
4516 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4517 0ebf8283 2019-07-24 stsp return err;
4520 0ebf8283 2019-07-24 stsp static const struct got_error *
4521 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4523 0ebf8283 2019-07-24 stsp static char msg[42];
4526 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4528 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4529 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4531 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4534 0ebf8283 2019-07-24 stsp static const struct got_error *
4535 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4536 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4538 0ebf8283 2019-07-24 stsp const struct got_error *err;
4539 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4540 0ebf8283 2019-07-24 stsp char *id_str;
4542 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4544 0ebf8283 2019-07-24 stsp return err;
4546 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4548 0ebf8283 2019-07-24 stsp goto done;
4550 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4551 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4552 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(folded_commit)) == -1) {
4553 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4554 0ebf8283 2019-07-24 stsp goto done;
4557 0ebf8283 2019-07-24 stsp if (folded_commit)
4558 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4559 0ebf8283 2019-07-24 stsp free(id_str);
4560 0ebf8283 2019-07-24 stsp return err;
4563 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4564 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4566 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4568 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4569 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4570 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
4571 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4572 3f9de99f 2019-07-24 stsp folded = prev;
4573 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4576 0ebf8283 2019-07-24 stsp return folded;
4579 0ebf8283 2019-07-24 stsp static const struct got_error *
4580 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4581 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4583 0ebf8283 2019-07-24 stsp char *logmsg_path = NULL, *id_str = NULL;
4584 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4585 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4586 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4588 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4590 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4592 0ebf8283 2019-07-24 stsp return err;
4594 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4595 0ebf8283 2019-07-24 stsp if (folded) {
4596 0ebf8283 2019-07-24 stsp while (folded != hle) {
4597 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4598 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4601 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4602 0ebf8283 2019-07-24 stsp logmsg, repo);
4604 0ebf8283 2019-07-24 stsp goto done;
4605 0ebf8283 2019-07-24 stsp free(logmsg);
4606 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4607 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4611 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4613 0ebf8283 2019-07-24 stsp goto done;
4614 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4615 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4616 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", id_str,
4617 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(commit)) == -1) {
4618 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4619 0ebf8283 2019-07-24 stsp goto done;
4621 0ebf8283 2019-07-24 stsp free(logmsg);
4622 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4624 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4626 0ebf8283 2019-07-24 stsp goto done;
4628 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4630 0ebf8283 2019-07-24 stsp goto done;
4632 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4633 0ebf8283 2019-07-24 stsp close(fd);
4635 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4637 0ebf8283 2019-07-24 stsp goto done;
4639 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4640 0ebf8283 2019-07-24 stsp if (err) {
4641 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4642 0ebf8283 2019-07-24 stsp goto done;
4643 0ebf8283 2019-07-24 stsp err = NULL;
4644 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4645 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL)
4646 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4649 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4650 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4651 0ebf8283 2019-07-24 stsp free(logmsg_path);
4652 0ebf8283 2019-07-24 stsp free(logmsg);
4653 0ebf8283 2019-07-24 stsp free(editor);
4654 0ebf8283 2019-07-24 stsp if (commit)
4655 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4656 0ebf8283 2019-07-24 stsp return err;
4659 0ebf8283 2019-07-24 stsp static const struct got_error *
4660 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4661 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4663 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4664 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4665 0ebf8283 2019-07-24 stsp size_t size;
4666 0ebf8283 2019-07-24 stsp ssize_t len;
4667 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4668 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4669 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4670 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4672 0ebf8283 2019-07-24 stsp for (;;) {
4673 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4674 0ebf8283 2019-07-24 stsp if (len == -1) {
4675 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4676 0ebf8283 2019-07-24 stsp if (feof(f))
4678 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4679 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4684 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4686 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4687 0ebf8283 2019-07-24 stsp free(line);
4688 0ebf8283 2019-07-24 stsp line = NULL;
4691 0ebf8283 2019-07-24 stsp cmd = NULL;
4692 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4693 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4694 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4695 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4696 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4699 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4704 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
4705 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4708 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4710 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4711 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4712 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4715 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4716 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4720 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4721 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4722 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4726 0ebf8283 2019-07-24 stsp free(line);
4727 0ebf8283 2019-07-24 stsp line = NULL;
4731 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4733 0ebf8283 2019-07-24 stsp *end = '\0';
4735 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4736 0ebf8283 2019-07-24 stsp if (err) {
4737 0ebf8283 2019-07-24 stsp /* override error code */
4738 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4742 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4743 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4744 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4747 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
4748 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
4749 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
4750 0ebf8283 2019-07-24 stsp commit_id = NULL;
4751 0ebf8283 2019-07-24 stsp free(line);
4752 0ebf8283 2019-07-24 stsp line = NULL;
4753 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4756 0ebf8283 2019-07-24 stsp free(line);
4757 0ebf8283 2019-07-24 stsp free(commit_id);
4758 0ebf8283 2019-07-24 stsp return err;
4761 0ebf8283 2019-07-24 stsp static const struct got_error *
4762 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
4763 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4765 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
4766 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
4767 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4768 bfce7f83 2019-07-27 stsp static char msg[80];
4769 bfce7f83 2019-07-27 stsp char *id_str;
4771 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
4772 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4773 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
4774 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
4775 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4777 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4778 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4779 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4782 bfce7f83 2019-07-27 stsp if (hle == NULL) {
4783 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
4785 bfce7f83 2019-07-27 stsp return err;
4786 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
4787 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
4788 bfce7f83 2019-07-27 stsp free(id_str);
4789 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4793 bfce7f83 2019-07-27 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4794 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4795 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
4797 bfce7f83 2019-07-27 stsp return NULL;
4800 bfce7f83 2019-07-27 stsp static const struct got_error *
4801 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
4802 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
4803 bfce7f83 2019-07-27 stsp struct got_repository *repo)
4805 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4806 0ebf8283 2019-07-24 stsp char *editor;
4807 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4809 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4811 0ebf8283 2019-07-24 stsp return err;
4813 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
4814 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
4815 0ebf8283 2019-07-24 stsp goto done;
4818 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4819 0ebf8283 2019-07-24 stsp if (f == NULL) {
4820 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
4821 0ebf8283 2019-07-24 stsp goto done;
4823 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4825 bfce7f83 2019-07-27 stsp goto done;
4827 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
4829 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4830 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4831 0ebf8283 2019-07-24 stsp free(editor);
4832 0ebf8283 2019-07-24 stsp return err;
4835 0ebf8283 2019-07-24 stsp static const struct got_error *
4836 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4837 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
4839 0ebf8283 2019-07-24 stsp static const struct got_error *
4840 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
4841 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4843 0ebf8283 2019-07-24 stsp const struct got_error *err;
4844 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4845 0ebf8283 2019-07-24 stsp char *path = NULL;
4847 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
4849 0ebf8283 2019-07-24 stsp return err;
4851 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
4853 0ebf8283 2019-07-24 stsp goto done;
4855 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
4857 0ebf8283 2019-07-24 stsp goto done;
4859 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
4860 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4861 0ebf8283 2019-07-24 stsp goto done;
4865 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
4866 0ebf8283 2019-07-24 stsp if (err) {
4867 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4868 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4869 0ebf8283 2019-07-24 stsp goto done;
4870 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
4871 0ebf8283 2019-07-24 stsp commits, path, repo);
4874 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4875 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4876 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
4877 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
4878 0ebf8283 2019-07-24 stsp free(path);
4879 0ebf8283 2019-07-24 stsp return err;
4882 0ebf8283 2019-07-24 stsp static const struct got_error *
4883 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
4884 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4886 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4887 0ebf8283 2019-07-24 stsp char *path = NULL;
4888 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4889 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4890 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4892 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
4894 0ebf8283 2019-07-24 stsp return err;
4896 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
4897 0ebf8283 2019-07-24 stsp if (f == NULL) {
4898 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4899 0ebf8283 2019-07-24 stsp goto done;
4901 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4902 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4907 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4908 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
4909 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
4910 0ebf8283 2019-07-24 stsp if (n < 0) {
4911 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4917 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4918 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4919 0ebf8283 2019-07-24 stsp free(path);
4920 0ebf8283 2019-07-24 stsp if (commit)
4921 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4922 0ebf8283 2019-07-24 stsp return err;
4926 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
4928 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4930 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
4931 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
4932 bfce7f83 2019-07-27 stsp free(hle);
4936 0ebf8283 2019-07-24 stsp static const struct got_error *
4937 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
4938 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4940 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4941 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4943 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4944 0ebf8283 2019-07-24 stsp if (f == NULL) {
4945 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4946 0ebf8283 2019-07-24 stsp goto done;
4949 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4951 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4952 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4953 0ebf8283 2019-07-24 stsp return err;
4956 0ebf8283 2019-07-24 stsp static const struct got_error *
4957 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4958 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
4959 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4961 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
4962 0ebf8283 2019-07-24 stsp int resp = ' ';
4964 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
4965 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4966 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
4967 0ebf8283 2019-07-24 stsp resp = getchar();
4968 a61a4414 2019-08-07 stsp if (resp == '\n')
4969 a61a4414 2019-08-07 stsp resp = getchar();
4970 426ebf2e 2019-07-25 stsp if (resp == 'c') {
4971 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
4972 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
4974 426ebf2e 2019-07-25 stsp if (err) {
4975 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4976 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4978 bfce7f83 2019-07-27 stsp prev_err = err;
4979 426ebf2e 2019-07-25 stsp resp = ' ';
4983 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
4984 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
4985 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
4986 0ebf8283 2019-07-24 stsp commits, repo);
4987 426ebf2e 2019-07-25 stsp if (err) {
4988 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4989 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4991 bfce7f83 2019-07-27 stsp prev_err = err;
4992 426ebf2e 2019-07-25 stsp resp = ' ';
4996 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
4997 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5000 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
5003 0ebf8283 2019-07-24 stsp return err;
5006 0ebf8283 2019-07-24 stsp static const struct got_error *
5007 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
5008 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5009 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
5011 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5012 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5013 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5014 3e3a69f1 2019-07-25 stsp branch, repo);
5017 0ebf8283 2019-07-24 stsp static const struct got_error *
5018 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
5019 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5021 0ebf8283 2019-07-24 stsp const struct got_error *err;
5022 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5024 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
5026 0ebf8283 2019-07-24 stsp goto done;
5028 0ebf8283 2019-07-24 stsp if (new_id) {
5029 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
5031 0ebf8283 2019-07-24 stsp goto done;
5034 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
5035 0ebf8283 2019-07-24 stsp if (new_id_str)
5036 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
5038 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5039 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
5040 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
5041 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5042 0ebf8283 2019-07-24 stsp goto done;
5044 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
5046 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5048 0ebf8283 2019-07-24 stsp goto done;
5051 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
5052 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
5053 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
5054 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
5055 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5057 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
5058 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
5059 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5067 0ebf8283 2019-07-24 stsp free(old_id_str);
5068 0ebf8283 2019-07-24 stsp free(new_id_str);
5069 0ebf8283 2019-07-24 stsp return err;
5072 0ebf8283 2019-07-24 stsp static const struct got_error *
5073 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
5074 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5075 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5076 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5078 0ebf8283 2019-07-24 stsp const struct got_error *err;
5079 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5080 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
5082 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5083 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
5084 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5086 0ebf8283 2019-07-24 stsp return err;
5089 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5091 0ebf8283 2019-07-24 stsp return err;
5093 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5094 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
5095 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
5096 0ebf8283 2019-07-24 stsp if (err) {
5097 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5098 0ebf8283 2019-07-24 stsp goto done;
5099 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
5101 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
5102 0ebf8283 2019-07-24 stsp free(new_commit_id);
5105 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5106 0ebf8283 2019-07-24 stsp return err;
5109 0ebf8283 2019-07-24 stsp static const struct got_error *
5110 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
5111 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5113 0ebf8283 2019-07-24 stsp const struct got_error *error;
5114 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5116 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5118 0ebf8283 2019-07-24 stsp if (error)
5119 0ebf8283 2019-07-24 stsp return error;
5121 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5122 0ebf8283 2019-07-24 stsp if (error)
5123 0ebf8283 2019-07-24 stsp return error;
5125 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
5126 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5127 0ebf8283 2019-07-24 stsp return error;
5130 0ebf8283 2019-07-24 stsp static const struct got_error *
5131 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
5133 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
5134 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
5135 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5136 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
5137 0ebf8283 2019-07-24 stsp char *cwd = NULL;
5138 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
5139 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
5140 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
5141 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
5142 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
5143 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5144 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
5145 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5146 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
5147 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5148 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
5149 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
5150 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
5151 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
5152 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
5153 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5155 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
5156 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
5157 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
5159 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
5160 0ebf8283 2019-07-24 stsp switch (ch) {
5162 0ebf8283 2019-07-24 stsp abort_edit = 1;
5165 0ebf8283 2019-07-24 stsp continue_edit = 1;
5168 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
5171 0ebf8283 2019-07-24 stsp usage_histedit();
5172 0ebf8283 2019-07-24 stsp /* NOTREACHED */
5176 0ebf8283 2019-07-24 stsp argc -= optind;
5177 0ebf8283 2019-07-24 stsp argv += optind;
5179 0ebf8283 2019-07-24 stsp #ifndef PROFILE
5180 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5181 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
5182 0ebf8283 2019-07-24 stsp err(1, "pledge");
5184 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
5185 0ebf8283 2019-07-24 stsp usage_histedit();
5186 0ebf8283 2019-07-24 stsp if (argc != 0)
5187 0ebf8283 2019-07-24 stsp usage_histedit();
5190 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
5191 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
5192 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
5193 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
5194 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
5195 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
5198 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
5199 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
5200 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
5201 0ebf8283 2019-07-24 stsp goto done;
5203 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
5204 0ebf8283 2019-07-24 stsp if (error)
5205 0ebf8283 2019-07-24 stsp goto done;
5207 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5208 0ebf8283 2019-07-24 stsp if (error != NULL)
5209 0ebf8283 2019-07-24 stsp goto done;
5211 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5212 0ebf8283 2019-07-24 stsp if (error)
5213 0ebf8283 2019-07-24 stsp goto done;
5214 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
5215 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
5216 0ebf8283 2019-07-24 stsp goto done;
5219 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5220 0ebf8283 2019-07-24 stsp if (error)
5221 0ebf8283 2019-07-24 stsp goto done;
5223 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
5224 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5225 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5226 3e3a69f1 2019-07-25 stsp worktree, repo);
5227 0ebf8283 2019-07-24 stsp if (error)
5228 0ebf8283 2019-07-24 stsp goto done;
5229 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5230 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5231 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
5232 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
5233 0ebf8283 2019-07-24 stsp if (error)
5234 0ebf8283 2019-07-24 stsp goto done;
5235 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
5236 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5237 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
5238 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
5239 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5240 0ebf8283 2019-07-24 stsp goto done;
5243 0ebf8283 2019-07-24 stsp if (continue_edit) {
5244 0ebf8283 2019-07-24 stsp char *path;
5246 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
5247 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5248 0ebf8283 2019-07-24 stsp goto done;
5251 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
5252 0ebf8283 2019-07-24 stsp if (error)
5253 0ebf8283 2019-07-24 stsp goto done;
5255 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
5256 0ebf8283 2019-07-24 stsp free(path);
5257 0ebf8283 2019-07-24 stsp if (error)
5258 0ebf8283 2019-07-24 stsp goto done;
5260 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5261 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5262 3e3a69f1 2019-07-25 stsp worktree, repo);
5263 0ebf8283 2019-07-24 stsp if (error)
5264 0ebf8283 2019-07-24 stsp goto done;
5266 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5267 0ebf8283 2019-07-24 stsp if (error)
5268 0ebf8283 2019-07-24 stsp goto done;
5270 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5271 0ebf8283 2019-07-24 stsp head_commit_id);
5272 0ebf8283 2019-07-24 stsp if (error)
5273 0ebf8283 2019-07-24 stsp goto done;
5274 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5275 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5276 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5277 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5278 3aac7cf7 2019-07-25 stsp goto done;
5280 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5281 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
5282 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5283 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5284 0ebf8283 2019-07-24 stsp commit = NULL;
5285 0ebf8283 2019-07-24 stsp if (error)
5286 0ebf8283 2019-07-24 stsp goto done;
5288 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
5289 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5290 0ebf8283 2019-07-24 stsp goto done;
5293 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
5294 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
5295 0ebf8283 2019-07-24 stsp if (error != NULL)
5296 0ebf8283 2019-07-24 stsp goto done;
5298 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5299 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5300 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
5301 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
5302 c7d20a3f 2019-07-30 stsp goto done;
5305 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5306 bfce7f83 2019-07-27 stsp got_ref_close(branch);
5307 bfce7f83 2019-07-27 stsp branch = NULL;
5308 0ebf8283 2019-07-24 stsp if (error)
5309 0ebf8283 2019-07-24 stsp goto done;
5311 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5312 0ebf8283 2019-07-24 stsp head_commit_id);
5313 0ebf8283 2019-07-24 stsp if (error)
5314 0ebf8283 2019-07-24 stsp goto done;
5315 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5316 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5317 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5318 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5319 3aac7cf7 2019-07-25 stsp goto done;
5321 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5322 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
5323 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
5324 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5325 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5326 0ebf8283 2019-07-24 stsp commit = NULL;
5327 0ebf8283 2019-07-24 stsp if (error)
5328 0ebf8283 2019-07-24 stsp goto done;
5330 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5331 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
5332 bfce7f83 2019-07-27 stsp if (error)
5333 bfce7f83 2019-07-27 stsp goto done;
5335 0ebf8283 2019-07-24 stsp if (edit_script_path) {
5336 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
5337 0ebf8283 2019-07-24 stsp edit_script_path, repo);
5338 6ba2bea2 2019-07-27 stsp if (error) {
5339 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5340 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5341 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5342 0ebf8283 2019-07-24 stsp goto done;
5345 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
5347 6ba2bea2 2019-07-27 stsp if (error) {
5348 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5349 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5350 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5351 0ebf8283 2019-07-24 stsp goto done;
5356 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
5358 6ba2bea2 2019-07-27 stsp if (error) {
5359 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5360 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5361 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5362 0ebf8283 2019-07-24 stsp goto done;
5367 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
5368 0ebf8283 2019-07-24 stsp if (error)
5369 0ebf8283 2019-07-24 stsp goto done;
5371 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5372 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
5373 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
5374 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
5377 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
5378 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5379 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
5380 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
5383 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
5384 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
5386 0ebf8283 2019-07-24 stsp if (error)
5387 0ebf8283 2019-07-24 stsp goto done;
5391 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5392 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5393 0ebf8283 2019-07-24 stsp if (error)
5394 0ebf8283 2019-07-24 stsp goto done;
5398 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5399 0ebf8283 2019-07-24 stsp hle->commit_id);
5400 0ebf8283 2019-07-24 stsp if (error)
5401 0ebf8283 2019-07-24 stsp goto done;
5402 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5403 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5405 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
5406 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
5407 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5408 0ebf8283 2019-07-24 stsp if (error)
5409 0ebf8283 2019-07-24 stsp goto done;
5410 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5411 0ebf8283 2019-07-24 stsp commit = NULL;
5413 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5414 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5418 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5419 0ebf8283 2019-07-24 stsp char *id_str;
5420 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
5421 0ebf8283 2019-07-24 stsp if (error)
5422 0ebf8283 2019-07-24 stsp goto done;
5423 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
5425 0ebf8283 2019-07-24 stsp free(id_str);
5426 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5427 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
5428 3e3a69f1 2019-07-25 stsp fileindex);
5429 0ebf8283 2019-07-24 stsp goto done;
5432 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5433 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5434 0ebf8283 2019-07-24 stsp if (error)
5435 0ebf8283 2019-07-24 stsp goto done;
5439 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
5440 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
5441 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5442 0ebf8283 2019-07-24 stsp if (error)
5443 0ebf8283 2019-07-24 stsp goto done;
5446 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5447 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
5448 0ebf8283 2019-07-24 stsp if (error)
5449 0ebf8283 2019-07-24 stsp goto done;
5450 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5451 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
5453 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
5454 3e3a69f1 2019-07-25 stsp branch, repo);
5456 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
5457 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
5458 0ebf8283 2019-07-24 stsp free(head_commit_id);
5459 0ebf8283 2019-07-24 stsp free(base_commit_id);
5460 0ebf8283 2019-07-24 stsp free(resume_commit_id);
5461 0ebf8283 2019-07-24 stsp if (commit)
5462 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5463 0ebf8283 2019-07-24 stsp if (branch)
5464 0ebf8283 2019-07-24 stsp got_ref_close(branch);
5465 0ebf8283 2019-07-24 stsp if (tmp_branch)
5466 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
5467 0ebf8283 2019-07-24 stsp if (worktree)
5468 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
5470 715dc77e 2019-08-03 stsp got_repo_close(repo);
5471 715dc77e 2019-08-03 stsp return error;
5474 715dc77e 2019-08-03 stsp __dead static void
5475 715dc77e 2019-08-03 stsp usage_stage(void)
5477 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5478 f3055044 2019-08-07 stsp "[file-path ...]\n",
5479 715dc77e 2019-08-03 stsp getprogname());
5483 715dc77e 2019-08-03 stsp static const struct got_error *
5484 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
5485 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5486 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5488 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
5489 408b4ebc 2019-08-03 stsp char *id_str = NULL;
5491 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
5492 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
5493 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5494 408b4ebc 2019-08-03 stsp return NULL;
5496 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5497 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
5498 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
5500 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
5502 408b4ebc 2019-08-03 stsp return err;
5504 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
5505 408b4ebc 2019-08-03 stsp free(id_str);
5506 dc424a06 2019-08-07 stsp return NULL;
5509 dc424a06 2019-08-07 stsp static const struct got_error *
5510 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
5512 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
5513 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
5514 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
5515 715dc77e 2019-08-03 stsp char *cwd = NULL;
5516 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
5517 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
5518 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
5519 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
5520 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5521 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5523 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
5525 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5526 715dc77e 2019-08-03 stsp switch (ch) {
5528 408b4ebc 2019-08-03 stsp list_stage = 1;
5531 dc424a06 2019-08-07 stsp pflag = 1;
5534 dc424a06 2019-08-07 stsp patch_script_path = optarg;
5537 715dc77e 2019-08-03 stsp usage_stage();
5538 715dc77e 2019-08-03 stsp /* NOTREACHED */
5542 715dc77e 2019-08-03 stsp argc -= optind;
5543 715dc77e 2019-08-03 stsp argv += optind;
5545 715dc77e 2019-08-03 stsp #ifndef PROFILE
5546 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5547 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
5548 715dc77e 2019-08-03 stsp err(1, "pledge");
5550 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
5551 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
5552 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
5553 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
5555 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
5556 715dc77e 2019-08-03 stsp if (cwd == NULL) {
5557 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
5558 715dc77e 2019-08-03 stsp goto done;
5561 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5562 715dc77e 2019-08-03 stsp if (error)
5563 715dc77e 2019-08-03 stsp goto done;
5565 715dc77e 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5566 715dc77e 2019-08-03 stsp if (error != NULL)
5567 715dc77e 2019-08-03 stsp goto done;
5569 dc424a06 2019-08-07 stsp if (patch_script_path) {
5570 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
5571 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
5572 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
5573 dc424a06 2019-08-07 stsp patch_script_path);
5574 dc424a06 2019-08-07 stsp goto done;
5577 715dc77e 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5578 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
5579 715dc77e 2019-08-03 stsp if (error)
5580 715dc77e 2019-08-03 stsp goto done;
5582 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5583 6d022e97 2019-08-04 stsp if (error)
5584 6d022e97 2019-08-04 stsp goto done;
5586 6d022e97 2019-08-04 stsp if (list_stage)
5587 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
5588 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
5590 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5591 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
5592 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
5593 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
5594 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5597 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5598 2e1f37b0 2019-08-08 stsp error == NULL)
5599 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5601 ad493afc 2019-08-03 stsp got_repo_close(repo);
5602 ad493afc 2019-08-03 stsp if (worktree)
5603 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
5604 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5605 ad493afc 2019-08-03 stsp free((char *)pe->path);
5606 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
5607 ad493afc 2019-08-03 stsp free(cwd);
5608 ad493afc 2019-08-03 stsp return error;
5611 ad493afc 2019-08-03 stsp __dead static void
5612 ad493afc 2019-08-03 stsp usage_unstage(void)
5614 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5615 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
5616 ad493afc 2019-08-03 stsp getprogname());
5621 ad493afc 2019-08-03 stsp static const struct got_error *
5622 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
5624 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
5625 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
5626 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
5627 ad493afc 2019-08-03 stsp char *cwd = NULL;
5628 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
5629 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
5630 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
5631 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
5632 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5633 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5635 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
5637 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
5638 ad493afc 2019-08-03 stsp switch (ch) {
5640 2e1f37b0 2019-08-08 stsp pflag = 1;
5643 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
5646 ad493afc 2019-08-03 stsp usage_unstage();
5647 ad493afc 2019-08-03 stsp /* NOTREACHED */
5651 ad493afc 2019-08-03 stsp argc -= optind;
5652 ad493afc 2019-08-03 stsp argv += optind;
5654 ad493afc 2019-08-03 stsp #ifndef PROFILE
5655 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5656 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
5657 ad493afc 2019-08-03 stsp err(1, "pledge");
5659 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
5660 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5662 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
5663 ad493afc 2019-08-03 stsp if (cwd == NULL) {
5664 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
5665 ad493afc 2019-08-03 stsp goto done;
5668 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5669 ad493afc 2019-08-03 stsp if (error)
5670 ad493afc 2019-08-03 stsp goto done;
5672 ad493afc 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5673 ad493afc 2019-08-03 stsp if (error != NULL)
5674 ad493afc 2019-08-03 stsp goto done;
5676 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
5677 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5678 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
5679 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
5680 2e1f37b0 2019-08-08 stsp patch_script_path);
5681 2e1f37b0 2019-08-08 stsp goto done;
5685 ad493afc 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5686 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
5687 ad493afc 2019-08-03 stsp if (error)
5688 ad493afc 2019-08-03 stsp goto done;
5690 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5691 ad493afc 2019-08-03 stsp if (error)
5692 ad493afc 2019-08-03 stsp goto done;
5694 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5695 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
5696 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
5697 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5699 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5700 2e1f37b0 2019-08-08 stsp error == NULL)
5701 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5703 0ebf8283 2019-07-24 stsp got_repo_close(repo);
5704 715dc77e 2019-08-03 stsp if (worktree)
5705 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
5706 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5707 715dc77e 2019-08-03 stsp free((char *)pe->path);
5708 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
5709 715dc77e 2019-08-03 stsp free(cwd);
5710 0ebf8283 2019-07-24 stsp return error;