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 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 c09a553d 2018-03-12 stsp #include "got_worktree.h"
46 79109fed 2018-03-27 stsp #include "got_diff.h"
47 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
48 404c43c4 2018-06-21 stsp #include "got_blame.h"
49 63219cd2 2019-01-04 stsp #include "got_privsep.h"
50 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
52 5c860e29 2018-03-12 stsp #ifndef nitems
53 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
57 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
60 99437157 2018-11-11 stsp catch_sigint(int signo)
62 99437157 2018-11-11 stsp sigint_received = 1;
66 99437157 2018-11-11 stsp catch_sigpipe(int signo)
68 99437157 2018-11-11 stsp sigpipe_received = 1;
72 8cfb4057 2019-07-09 stsp struct got_cmd {
73 5c860e29 2018-03-12 stsp const char *cmd_name;
74 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
75 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
76 97b3a7be 2019-07-09 stsp const char *cmd_alias;
79 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
80 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
81 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
83 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
86 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
87 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
88 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
89 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
90 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
91 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
92 d00136be 2019-03-26 stsp __dead static void usage_add(void);
93 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
94 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
95 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
96 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
97 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
98 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
99 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
100 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
101 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
102 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
103 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
105 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
106 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
107 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
108 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
110 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
111 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
112 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
113 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
114 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
115 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
116 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
117 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
118 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
119 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
120 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
121 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
122 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
123 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
124 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
125 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
126 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
127 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
128 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
130 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
131 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
132 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
133 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
134 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
135 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
136 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
137 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
138 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
139 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
140 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
141 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
142 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
143 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
144 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
145 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
146 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
147 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
149 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
150 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
151 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
152 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
153 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
154 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
157 ce5b7c56 2019-07-09 stsp static void
158 ce5b7c56 2019-07-09 stsp list_commands(void)
162 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
163 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
164 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
165 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
167 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
171 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
173 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
174 5c860e29 2018-03-12 stsp unsigned int i;
176 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
178 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
180 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
181 5c860e29 2018-03-12 stsp switch (ch) {
189 ce5b7c56 2019-07-09 stsp usage(hflag);
190 5c860e29 2018-03-12 stsp /* NOTREACHED */
194 5c860e29 2018-03-12 stsp argc -= optind;
195 5c860e29 2018-03-12 stsp argv += optind;
196 1e70621d 2018-03-27 stsp optind = 0;
198 53ccebc2 2019-07-30 stsp if (Vflag) {
199 53ccebc2 2019-07-30 stsp got_version_print_str();
203 5c860e29 2018-03-12 stsp if (argc <= 0)
204 ce5b7c56 2019-07-09 stsp usage(hflag);
206 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
207 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
209 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
210 d7d4f210 2018-03-12 stsp const struct got_error *error;
212 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
214 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
219 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
221 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
222 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
223 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
224 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
225 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
226 70015d7a 2019-11-08 stsp !(sigint_received &&
227 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
228 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
235 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
236 ce5b7c56 2019-07-09 stsp list_commands();
240 4ed7e80c 2018-05-20 stsp __dead static void
241 ce5b7c56 2019-07-09 stsp usage(int hflag)
243 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
244 53ccebc2 2019-07-30 stsp getprogname());
246 ce5b7c56 2019-07-09 stsp list_commands();
250 0266afb7 2019-01-04 stsp static const struct got_error *
251 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
253 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
254 e2ba3d07 2019-05-13 stsp const char *editor;
256 8920fa04 2019-08-18 stsp *abspath = NULL;
258 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
259 e2ba3d07 2019-05-13 stsp if (editor == NULL)
260 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
262 0ee7065d 2019-05-13 stsp if (editor) {
263 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
265 0ee7065d 2019-05-13 stsp return err;
268 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
269 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
270 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
271 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
274 e2ba3d07 2019-05-13 stsp return NULL;
277 e2ba3d07 2019-05-13 stsp static const struct got_error *
278 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
279 c530dc23 2019-07-23 stsp const char *worktree_path)
281 163ce85a 2019-05-13 stsp const struct got_error *err;
283 37c06ea4 2019-07-15 stsp #ifdef PROFILE
284 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
285 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
287 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
288 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
290 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
291 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
293 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
294 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
296 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
297 163ce85a 2019-05-13 stsp if (err != NULL)
298 163ce85a 2019-05-13 stsp return err;
300 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
301 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
303 0266afb7 2019-01-04 stsp return NULL;
306 2c7829a4 2019-06-17 stsp __dead static void
307 2c7829a4 2019-06-17 stsp usage_init(void)
309 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
313 2c7829a4 2019-06-17 stsp static const struct got_error *
314 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
316 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
317 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
320 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
321 2c7829a4 2019-06-17 stsp switch (ch) {
323 2c7829a4 2019-06-17 stsp usage_init();
324 2c7829a4 2019-06-17 stsp /* NOTREACHED */
328 2c7829a4 2019-06-17 stsp argc -= optind;
329 2c7829a4 2019-06-17 stsp argv += optind;
331 2c7829a4 2019-06-17 stsp #ifndef PROFILE
332 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
333 2c7829a4 2019-06-17 stsp err(1, "pledge");
335 2c7829a4 2019-06-17 stsp if (argc != 1)
336 bc20e173 2019-06-17 stsp usage_init();
338 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
339 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
340 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
342 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
344 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
345 2c7829a4 2019-06-17 stsp if (error &&
346 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
349 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
353 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
354 2c7829a4 2019-06-17 stsp if (error != NULL)
358 3ce1b845 2019-07-15 stsp free(repo_path);
359 3ce1b845 2019-07-15 stsp return error;
362 3ce1b845 2019-07-15 stsp __dead static void
363 3ce1b845 2019-07-15 stsp usage_import(void)
365 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
366 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
371 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
374 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
375 3ce1b845 2019-07-15 stsp int st = -1;
377 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
378 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
379 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
381 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
383 3ce1b845 2019-07-15 stsp goto doneediting;
385 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
386 3ce1b845 2019-07-15 stsp _exit(127);
389 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
390 3ce1b845 2019-07-15 stsp if (errno != EINTR)
393 3ce1b845 2019-07-15 stsp doneediting:
394 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
395 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
396 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
398 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
399 3ce1b845 2019-07-15 stsp errno = EINTR;
403 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
406 3ce1b845 2019-07-15 stsp static const struct got_error *
407 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
408 3ce1b845 2019-07-15 stsp const char *initial_content)
410 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
411 3ce1b845 2019-07-15 stsp char buf[1024];
412 3ce1b845 2019-07-15 stsp struct stat st, st2;
414 3ce1b845 2019-07-15 stsp int content_changed = 0;
415 3ce1b845 2019-07-15 stsp size_t len;
417 3ce1b845 2019-07-15 stsp *logmsg = NULL;
419 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
420 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
422 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
423 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
425 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
426 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
428 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
429 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
430 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
432 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
433 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
434 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
435 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
438 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
439 3ce1b845 2019-07-15 stsp if (fp == NULL) {
440 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
443 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
444 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
445 3ce1b845 2019-07-15 stsp content_changed = 1;
446 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
447 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
448 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
450 3ce1b845 2019-07-15 stsp fclose(fp);
452 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
453 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
457 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
458 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
459 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
462 3ce1b845 2019-07-15 stsp free(*logmsg);
463 3ce1b845 2019-07-15 stsp *logmsg = NULL;
465 3ce1b845 2019-07-15 stsp return err;
468 3ce1b845 2019-07-15 stsp static const struct got_error *
469 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
470 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
472 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
473 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
476 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
477 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
478 3ce1b845 2019-07-15 stsp branch_name) == -1)
479 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
481 ef293bdd 2019-10-21 stsp err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
485 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
488 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
490 3ce1b845 2019-07-15 stsp free(initial_content);
491 3ce1b845 2019-07-15 stsp return err;
494 3ce1b845 2019-07-15 stsp static const struct got_error *
495 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
497 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
498 3ce1b845 2019-07-15 stsp return NULL;
501 3ce1b845 2019-07-15 stsp static const struct got_error *
502 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
504 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
505 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
507 84792843 2019-08-09 stsp *author = NULL;
509 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
510 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
511 c9956ddf 2019-09-08 stsp if (name && email) {
512 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
513 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
514 aba9c984 2019-09-08 stsp return NULL;
517 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
518 84792843 2019-08-09 stsp if (got_author == NULL) {
519 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
520 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
521 c9956ddf 2019-09-08 stsp if (name && email) {
522 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
523 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
524 c9956ddf 2019-09-08 stsp return NULL;
526 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
527 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
530 aba9c984 2019-09-08 stsp *author = strdup(got_author);
531 aba9c984 2019-09-08 stsp if (*author == NULL)
532 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
535 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
536 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
538 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
539 84792843 2019-08-09 stsp got_author++;
540 aba9c984 2019-09-08 stsp if (*got_author != '<') {
541 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
544 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
545 84792843 2019-08-09 stsp got_author++;
546 aba9c984 2019-09-08 stsp if (*got_author != '@') {
547 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
550 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
551 84792843 2019-08-09 stsp got_author++;
552 84792843 2019-08-09 stsp if (*got_author != '>')
553 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 aba9c984 2019-09-08 stsp free(*author);
557 aba9c984 2019-09-08 stsp *author = NULL;
559 aba9c984 2019-09-08 stsp return err;
562 c9956ddf 2019-09-08 stsp static const struct got_error *
563 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
565 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
567 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
568 c9956ddf 2019-09-08 stsp if (homedir) {
569 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
570 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
573 c9956ddf 2019-09-08 stsp return NULL;
576 84792843 2019-08-09 stsp static const struct got_error *
577 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
579 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
580 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
581 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
582 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
583 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
584 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
585 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
586 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
588 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
589 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
590 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
592 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
594 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
595 3ce1b845 2019-07-15 stsp switch (ch) {
597 3ce1b845 2019-07-15 stsp branch_name = optarg;
600 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
601 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
602 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
607 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
608 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
609 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
615 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
617 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
623 b2b65d18 2019-08-22 stsp usage_import();
624 3ce1b845 2019-07-15 stsp /* NOTREACHED */
628 3ce1b845 2019-07-15 stsp argc -= optind;
629 3ce1b845 2019-07-15 stsp argv += optind;
631 3ce1b845 2019-07-15 stsp #ifndef PROFILE
632 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
634 3ce1b845 2019-07-15 stsp NULL) == -1)
635 3ce1b845 2019-07-15 stsp err(1, "pledge");
637 3ce1b845 2019-07-15 stsp if (argc != 1)
638 3ce1b845 2019-07-15 stsp usage_import();
640 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
641 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
642 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
643 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
645 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
646 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
649 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
653 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
655 aba9c984 2019-09-08 stsp return error;
658 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
659 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
660 e560b7e0 2019-11-28 stsp * an unintended typo.
662 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
663 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
665 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
666 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
670 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
671 3ce1b845 2019-07-15 stsp if (error) {
672 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
675 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
676 3ce1b845 2019-07-15 stsp "import target branch already exists");
680 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
681 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
682 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
685 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
688 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
689 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
691 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
692 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
695 8e158b01 2019-09-22 stsp free(logmsg);
696 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
697 ef293bdd 2019-10-21 stsp path_dir, refname);
698 ef293bdd 2019-10-21 stsp if (error) {
699 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
700 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
701 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
706 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
707 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
708 ef293bdd 2019-10-21 stsp if (logmsg_path)
709 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
713 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
714 ef293bdd 2019-10-21 stsp if (error) {
715 ef293bdd 2019-10-21 stsp if (logmsg_path)
716 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
720 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
721 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
722 ef293bdd 2019-10-21 stsp if (error) {
723 ef293bdd 2019-10-21 stsp if (logmsg_path)
724 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
728 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
729 ef293bdd 2019-10-21 stsp if (error) {
730 ef293bdd 2019-10-21 stsp if (logmsg_path)
731 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
735 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
736 ef293bdd 2019-10-21 stsp if (error) {
737 ef293bdd 2019-10-21 stsp if (logmsg_path)
738 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
742 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
743 ef293bdd 2019-10-21 stsp if (error) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
749 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
750 3ce1b845 2019-07-15 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
752 ef293bdd 2019-10-21 stsp if (logmsg_path)
753 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
757 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
758 3ce1b845 2019-07-15 stsp branch_ref);
759 ef293bdd 2019-10-21 stsp if (error) {
760 ef293bdd 2019-10-21 stsp if (logmsg_path)
761 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
765 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
766 ef293bdd 2019-10-21 stsp if (error) {
767 ef293bdd 2019-10-21 stsp if (logmsg_path)
768 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
773 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
774 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
776 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
777 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
778 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
779 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
780 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
781 8e158b01 2019-09-22 stsp free(logmsg);
782 ef293bdd 2019-10-21 stsp free(logmsg_path);
783 2c7829a4 2019-06-17 stsp free(repo_path);
784 3ce1b845 2019-07-15 stsp free(editor);
785 3ce1b845 2019-07-15 stsp free(refname);
786 3ce1b845 2019-07-15 stsp free(new_commit_id);
787 3ce1b845 2019-07-15 stsp free(id_str);
788 aba9c984 2019-09-08 stsp free(author);
789 c9956ddf 2019-09-08 stsp free(gitconfig_path);
790 3ce1b845 2019-07-15 stsp if (branch_ref)
791 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
792 3ce1b845 2019-07-15 stsp if (head_ref)
793 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
794 2c7829a4 2019-06-17 stsp return error;
797 4ed7e80c 2018-05-20 stsp __dead static void
798 c09a553d 2018-03-12 stsp usage_checkout(void)
800 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
801 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
805 1ee397ad 2019-07-12 stsp static const struct got_error *
806 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
808 92a684f4 2018-03-12 stsp char *worktree_path = arg;
810 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
811 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
812 1ee397ad 2019-07-12 stsp return NULL;
814 92a684f4 2018-03-12 stsp while (path[0] == '/')
817 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
818 1ee397ad 2019-07-12 stsp return NULL;
821 99437157 2018-11-11 stsp static const struct got_error *
822 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
824 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
825 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
826 99437157 2018-11-11 stsp return NULL;
829 8069f636 2019-01-12 stsp static const struct got_error *
830 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
831 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
832 3aef623b 2019-10-15 stsp struct got_repository *repo)
834 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
835 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
837 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
838 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
840 36a38700 2019-05-10 stsp return err;
842 d5bea539 2019-05-13 stsp if (yca_id == NULL)
843 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
846 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
847 d5bea539 2019-05-13 stsp * and the work tree's base commit.
849 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
851 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
859 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
860 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
861 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
863 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
864 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
865 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
866 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
867 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
868 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
870 d5bea539 2019-05-13 stsp free(yca_id);
871 d5bea539 2019-05-13 stsp return NULL;
874 a367ff0f 2019-05-14 stsp static const struct got_error *
875 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
876 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
877 a51a74b3 2019-07-27 stsp struct got_repository *repo)
879 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
880 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
881 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
882 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
884 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
888 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
889 a51a74b3 2019-07-27 stsp is_same_branch = 1;
892 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
893 a51a74b3 2019-07-27 stsp is_same_branch = 1;
897 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
901 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
902 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
907 a367ff0f 2019-05-14 stsp struct got_object_id *id;
908 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
910 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
911 a367ff0f 2019-05-14 stsp err = NULL;
913 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
915 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
916 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
922 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
924 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
925 a367ff0f 2019-05-14 stsp is_same_branch = 1;
932 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
933 a367ff0f 2019-05-14 stsp free(head_commit_id);
934 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
935 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
936 04f57cb3 2019-07-25 stsp return err;
939 04f57cb3 2019-07-25 stsp static const struct got_error *
940 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
941 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
943 04f57cb3 2019-07-25 stsp const struct got_error *err;
944 04f57cb3 2019-07-25 stsp struct got_reference *ref;
945 303e2782 2019-08-09 stsp struct got_tag_object *tag;
947 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
948 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
949 303e2782 2019-08-09 stsp if (err == NULL) {
950 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
951 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
952 303e2782 2019-08-09 stsp if (*commit_id == NULL)
953 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
954 303e2782 2019-08-09 stsp got_object_tag_close(tag);
955 303e2782 2019-08-09 stsp return err;
956 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
957 303e2782 2019-08-09 stsp return err;
959 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
960 04f57cb3 2019-07-25 stsp if (err == NULL) {
961 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
962 04f57cb3 2019-07-25 stsp got_ref_close(ref);
964 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
965 04f57cb3 2019-07-25 stsp return err;
966 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
967 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
969 a367ff0f 2019-05-14 stsp return err;
972 4ed7e80c 2018-05-20 stsp static const struct got_error *
973 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
975 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
976 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
977 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
978 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
979 c09a553d 2018-03-12 stsp char *repo_path = NULL;
980 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
981 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
982 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
983 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
984 72151b04 2019-05-11 stsp int ch, same_path_prefix;
985 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
987 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
989 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
990 0bb8a95e 2018-03-12 stsp switch (ch) {
992 08573d5b 2019-05-14 stsp branch_name = optarg;
995 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
996 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
997 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1000 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1003 2deda0b9 2019-03-07 stsp usage_checkout();
1004 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1008 0bb8a95e 2018-03-12 stsp argc -= optind;
1009 0bb8a95e 2018-03-12 stsp argv += optind;
1011 6715a751 2018-03-16 stsp #ifndef PROFILE
1012 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1013 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1014 c09a553d 2018-03-12 stsp err(1, "pledge");
1016 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1017 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1018 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1019 76089277 2018-04-01 stsp if (repo_path == NULL)
1020 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1021 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1022 76089277 2018-04-01 stsp if (cwd == NULL) {
1023 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1024 76089277 2018-04-01 stsp goto done;
1026 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1027 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1028 230a42bd 2019-05-11 jcs if (base == NULL) {
1029 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1030 230a42bd 2019-05-11 jcs path_prefix);
1034 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1035 230a42bd 2019-05-11 jcs if (base == NULL) {
1036 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1037 230a42bd 2019-05-11 jcs repo_path);
1041 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1042 c09a553d 2018-03-12 stsp if (dotgit)
1043 c09a553d 2018-03-12 stsp *dotgit = '\0';
1044 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1045 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1046 c09a553d 2018-03-12 stsp free(cwd);
1047 76089277 2018-04-01 stsp goto done;
1049 c09a553d 2018-03-12 stsp free(cwd);
1050 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1051 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1052 76089277 2018-04-01 stsp if (repo_path == NULL) {
1053 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1054 76089277 2018-04-01 stsp goto done;
1056 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1057 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1058 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1059 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1061 b4b3a7dd 2019-07-22 stsp goto done;
1063 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1064 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1065 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1066 b4b3a7dd 2019-07-22 stsp goto done;
1070 c09a553d 2018-03-12 stsp usage_checkout();
1072 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1073 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1075 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1076 c09a553d 2018-03-12 stsp if (error != NULL)
1077 c02c541e 2019-03-29 stsp goto done;
1079 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1080 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1081 c530dc23 2019-07-23 stsp if (error) {
1082 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1083 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1084 c530dc23 2019-07-23 stsp goto done;
1085 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
1086 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1087 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1088 c530dc23 2019-07-23 stsp goto done;
1092 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1093 c02c541e 2019-03-29 stsp if (error)
1094 c09a553d 2018-03-12 stsp goto done;
1096 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1097 c09a553d 2018-03-12 stsp if (error != NULL)
1098 c09a553d 2018-03-12 stsp goto done;
1100 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1101 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1102 c09a553d 2018-03-12 stsp goto done;
1104 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1105 c09a553d 2018-03-12 stsp if (error != NULL)
1106 c09a553d 2018-03-12 stsp goto done;
1108 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1109 e5dc7198 2018-12-29 stsp path_prefix);
1110 e5dc7198 2018-12-29 stsp if (error != NULL)
1111 e5dc7198 2018-12-29 stsp goto done;
1112 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1113 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1114 49520a32 2018-12-29 stsp goto done;
1117 8069f636 2019-01-12 stsp if (commit_id_str) {
1118 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1119 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1120 30837e32 2019-07-25 stsp if (error)
1121 8069f636 2019-01-12 stsp goto done;
1122 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1123 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1124 8069f636 2019-01-12 stsp if (error != NULL) {
1125 8069f636 2019-01-12 stsp free(commit_id);
1126 8069f636 2019-01-12 stsp goto done;
1128 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1129 45d344f6 2019-05-14 stsp if (error)
1130 45d344f6 2019-05-14 stsp goto done;
1131 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1132 8069f636 2019-01-12 stsp commit_id);
1133 8069f636 2019-01-12 stsp free(commit_id);
1134 8069f636 2019-01-12 stsp if (error)
1135 8069f636 2019-01-12 stsp goto done;
1138 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1139 f2ea84fa 2019-07-27 stsp if (error)
1140 f2ea84fa 2019-07-27 stsp goto done;
1141 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1142 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1143 c09a553d 2018-03-12 stsp if (error != NULL)
1144 c09a553d 2018-03-12 stsp goto done;
1146 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1149 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1150 8069f636 2019-01-12 stsp free(commit_id_str);
1151 76089277 2018-04-01 stsp free(repo_path);
1152 507dc3bb 2018-12-29 stsp free(worktree_path);
1153 507dc3bb 2018-12-29 stsp return error;
1156 507dc3bb 2018-12-29 stsp __dead static void
1157 507dc3bb 2018-12-29 stsp usage_update(void)
1159 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1160 507dc3bb 2018-12-29 stsp getprogname());
1164 1ee397ad 2019-07-12 stsp static const struct got_error *
1165 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1167 784955db 2019-01-12 stsp int *did_something = arg;
1169 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1170 1ee397ad 2019-07-12 stsp return NULL;
1172 1545c615 2019-02-10 stsp *did_something = 1;
1174 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1175 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1176 1ee397ad 2019-07-12 stsp return NULL;
1178 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1180 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1181 1ee397ad 2019-07-12 stsp return NULL;
1184 be7061eb 2018-12-30 stsp static const struct got_error *
1185 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1186 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1187 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1189 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1190 a1fb16d8 2019-05-24 stsp char *base_id_str;
1191 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1193 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1194 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1195 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1196 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1197 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1198 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1199 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1202 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1203 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1204 a1fb16d8 2019-05-24 stsp if (err) {
1205 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1206 a1fb16d8 2019-05-24 stsp return err;
1207 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1209 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1210 a1fb16d8 2019-05-24 stsp return NULL;
1212 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1213 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1214 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1216 a1fb16d8 2019-05-24 stsp return err;
1217 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1218 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1219 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1220 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1221 0ebf8283 2019-07-24 stsp return NULL;
1224 0ebf8283 2019-07-24 stsp static const struct got_error *
1225 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1227 0ebf8283 2019-07-24 stsp const struct got_error *err;
1228 0ebf8283 2019-07-24 stsp int in_progress;
1230 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1232 0ebf8283 2019-07-24 stsp return err;
1233 0ebf8283 2019-07-24 stsp if (in_progress)
1234 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1236 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1238 0ebf8283 2019-07-24 stsp return err;
1239 0ebf8283 2019-07-24 stsp if (in_progress)
1240 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1242 a1fb16d8 2019-05-24 stsp return NULL;
1245 a5edda0a 2019-07-27 stsp static const struct got_error *
1246 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1247 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1249 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1250 a5edda0a 2019-07-27 stsp char *path;
1253 a5edda0a 2019-07-27 stsp if (argc == 0) {
1254 a5edda0a 2019-07-27 stsp path = strdup("");
1255 a5edda0a 2019-07-27 stsp if (path == NULL)
1256 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1257 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1260 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1261 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1264 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1265 a5edda0a 2019-07-27 stsp if (err) {
1266 a5edda0a 2019-07-27 stsp free(path);
1271 a5edda0a 2019-07-27 stsp return err;
1274 a1fb16d8 2019-05-24 stsp static const struct got_error *
1275 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1277 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1278 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1279 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1280 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1281 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1282 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1283 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1284 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1285 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1286 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1287 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1289 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1291 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1292 507dc3bb 2018-12-29 stsp switch (ch) {
1294 024e9686 2019-05-14 stsp branch_name = optarg;
1297 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1298 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1299 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1302 2deda0b9 2019-03-07 stsp usage_update();
1303 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1307 507dc3bb 2018-12-29 stsp argc -= optind;
1308 507dc3bb 2018-12-29 stsp argv += optind;
1310 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1311 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1312 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1313 507dc3bb 2018-12-29 stsp err(1, "pledge");
1315 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1316 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1317 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1318 c4cdcb68 2019-04-03 stsp goto done;
1320 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1321 7d5807f4 2019-07-11 stsp if (error)
1322 7d5807f4 2019-07-11 stsp goto done;
1324 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1325 f2ea84fa 2019-07-27 stsp if (error)
1326 f2ea84fa 2019-07-27 stsp goto done;
1328 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1330 507dc3bb 2018-12-29 stsp if (error != NULL)
1331 507dc3bb 2018-12-29 stsp goto done;
1333 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1334 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1335 087fb88c 2019-08-04 stsp if (error)
1336 087fb88c 2019-08-04 stsp goto done;
1338 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1339 0266afb7 2019-01-04 stsp if (error)
1340 0266afb7 2019-01-04 stsp goto done;
1342 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1343 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1344 024e9686 2019-05-14 stsp if (error != NULL)
1345 024e9686 2019-05-14 stsp goto done;
1346 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1347 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1348 9c4b8182 2019-01-02 stsp if (error != NULL)
1349 9c4b8182 2019-01-02 stsp goto done;
1350 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1351 507dc3bb 2018-12-29 stsp if (error != NULL)
1352 507dc3bb 2018-12-29 stsp goto done;
1354 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1355 04f57cb3 2019-07-25 stsp free(commit_id_str);
1356 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1357 30837e32 2019-07-25 stsp if (error)
1358 a297e751 2019-07-11 stsp goto done;
1359 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1360 a297e751 2019-07-11 stsp if (error)
1361 507dc3bb 2018-12-29 stsp goto done;
1364 a1fb16d8 2019-05-24 stsp if (branch_name) {
1365 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1366 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1367 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1369 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1370 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1371 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1372 024e9686 2019-05-14 stsp goto done;
1374 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1375 024e9686 2019-05-14 stsp if (error)
1376 024e9686 2019-05-14 stsp goto done;
1377 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1379 a367ff0f 2019-05-14 stsp free(head_commit_id);
1380 024e9686 2019-05-14 stsp if (error != NULL)
1381 024e9686 2019-05-14 stsp goto done;
1382 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1383 a367ff0f 2019-05-14 stsp if (error)
1384 a367ff0f 2019-05-14 stsp goto done;
1385 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1386 024e9686 2019-05-14 stsp if (error)
1387 024e9686 2019-05-14 stsp goto done;
1389 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1390 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1391 a367ff0f 2019-05-14 stsp if (error != NULL) {
1392 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1393 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1394 024e9686 2019-05-14 stsp goto done;
1396 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1397 a367ff0f 2019-05-14 stsp if (error)
1398 a367ff0f 2019-05-14 stsp goto done;
1401 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1402 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1403 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1404 507dc3bb 2018-12-29 stsp commit_id);
1405 507dc3bb 2018-12-29 stsp if (error)
1406 507dc3bb 2018-12-29 stsp goto done;
1409 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1410 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1411 507dc3bb 2018-12-29 stsp if (error != NULL)
1412 507dc3bb 2018-12-29 stsp goto done;
1414 784955db 2019-01-12 stsp if (did_something)
1415 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1417 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1419 c09a553d 2018-03-12 stsp free(worktree_path);
1420 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1421 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1422 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1423 507dc3bb 2018-12-29 stsp free(commit_id);
1424 9c4b8182 2019-01-02 stsp free(commit_id_str);
1425 c09a553d 2018-03-12 stsp return error;
1428 f42b1b34 2018-03-12 stsp static const struct got_error *
1429 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1430 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1431 63035f9f 2019-10-06 stsp struct got_repository *repo)
1433 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1434 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1436 44392932 2019-08-25 stsp if (blob_id1) {
1437 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1439 44392932 2019-08-25 stsp goto done;
1442 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1444 44392932 2019-08-25 stsp goto done;
1446 44392932 2019-08-25 stsp while (path[0] == '/')
1448 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1449 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1451 44392932 2019-08-25 stsp if (blob1)
1452 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1453 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1454 44392932 2019-08-25 stsp return err;
1457 44392932 2019-08-25 stsp static const struct got_error *
1458 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1459 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1460 63035f9f 2019-10-06 stsp struct got_repository *repo)
1462 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1463 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1464 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1466 44392932 2019-08-25 stsp if (tree_id1) {
1467 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1469 44392932 2019-08-25 stsp goto done;
1472 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1474 0f2b3dca 2018-12-22 stsp goto done;
1476 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1477 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1478 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1479 44392932 2019-08-25 stsp while (path[0] == '/')
1481 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1482 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1484 79109fed 2018-03-27 stsp if (tree1)
1485 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1486 366e0a5f 2019-10-10 stsp if (tree2)
1487 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1488 44392932 2019-08-25 stsp return err;
1491 44392932 2019-08-25 stsp static const struct got_error *
1492 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1493 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1495 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1496 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1497 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1498 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1499 44392932 2019-08-25 stsp struct got_object_qid *qid;
1501 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1502 44392932 2019-08-25 stsp if (qid != NULL) {
1503 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1506 44392932 2019-08-25 stsp return err;
1509 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1510 44392932 2019-08-25 stsp int obj_type;
1511 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1513 44392932 2019-08-25 stsp goto done;
1514 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1515 44392932 2019-08-25 stsp if (err) {
1516 44392932 2019-08-25 stsp free(obj_id2);
1517 44392932 2019-08-25 stsp goto done;
1519 44392932 2019-08-25 stsp if (pcommit) {
1520 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1521 44392932 2019-08-25 stsp qid->id, path);
1522 44392932 2019-08-25 stsp if (err) {
1523 44392932 2019-08-25 stsp free(obj_id2);
1524 44392932 2019-08-25 stsp goto done;
1526 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1527 44392932 2019-08-25 stsp if (err) {
1528 44392932 2019-08-25 stsp free(obj_id2);
1529 44392932 2019-08-25 stsp goto done;
1532 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1533 44392932 2019-08-25 stsp if (err) {
1534 44392932 2019-08-25 stsp free(obj_id2);
1535 44392932 2019-08-25 stsp goto done;
1537 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1538 44392932 2019-08-25 stsp switch (obj_type) {
1539 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1540 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1543 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1544 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1548 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1551 44392932 2019-08-25 stsp free(obj_id1);
1552 44392932 2019-08-25 stsp free(obj_id2);
1554 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1555 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1557 44392932 2019-08-25 stsp goto done;
1558 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1559 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1561 44392932 2019-08-25 stsp goto done;
1562 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1563 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1567 0f2b3dca 2018-12-22 stsp free(id_str1);
1568 0f2b3dca 2018-12-22 stsp free(id_str2);
1569 44392932 2019-08-25 stsp if (pcommit)
1570 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1571 79109fed 2018-03-27 stsp return err;
1574 4bb494d5 2018-06-16 stsp static char *
1575 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1577 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1578 09867e48 2019-08-13 stsp char *p, *s;
1580 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1581 09867e48 2019-08-13 stsp if (tm == NULL)
1582 09867e48 2019-08-13 stsp return NULL;
1583 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1584 09867e48 2019-08-13 stsp if (s == NULL)
1585 09867e48 2019-08-13 stsp return NULL;
1586 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1588 6c281f94 2018-06-11 stsp *p = '\0';
1592 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1594 79109fed 2018-03-27 stsp static const struct got_error *
1595 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1596 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1597 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1599 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1600 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1601 6c281f94 2018-06-11 stsp char datebuf[26];
1602 45d799e2 2018-12-23 stsp time_t committer_time;
1603 45d799e2 2018-12-23 stsp const char *author, *committer;
1604 199a4027 2019-02-02 stsp char *refs_str = NULL;
1605 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1607 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1609 199a4027 2019-02-02 stsp const char *name;
1610 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1613 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1614 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1616 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1617 199a4027 2019-02-02 stsp name += 5;
1618 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1620 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1621 e34f9ed6 2019-02-02 stsp name += 6;
1622 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1623 141c2bff 2019-02-04 stsp name += 8;
1624 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1625 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1626 5d844a1e 2019-08-13 stsp if (err) {
1627 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1628 5d844a1e 2019-08-13 stsp return err;
1629 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1630 5d844a1e 2019-08-13 stsp err = NULL;
1631 5d844a1e 2019-08-13 stsp tag = NULL;
1634 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1635 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1637 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1638 a436ad14 2019-08-13 stsp if (cmp != 0)
1640 199a4027 2019-02-02 stsp s = refs_str;
1641 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1642 199a4027 2019-02-02 stsp name) == -1) {
1643 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1645 34ca4898 2019-08-13 stsp return err;
1649 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1651 8bf5b3c9 2018-03-17 stsp return err;
1653 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1654 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1655 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1656 832c249c 2018-06-10 stsp free(id_str);
1657 d3d493d7 2019-02-21 stsp id_str = NULL;
1658 d3d493d7 2019-02-21 stsp free(refs_str);
1659 d3d493d7 2019-02-21 stsp refs_str = NULL;
1660 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1661 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1662 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1663 09867e48 2019-08-13 stsp if (datestr)
1664 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1665 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1666 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1667 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1668 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1669 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1670 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1671 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1672 3fe1abad 2018-06-10 stsp int n = 1;
1673 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1674 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1675 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1677 a0603db2 2018-06-10 stsp return err;
1678 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1679 a0603db2 2018-06-10 stsp free(id_str);
1683 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1685 5943eee2 2019-08-13 stsp return err;
1687 621015ac 2018-07-23 stsp logmsg = logmsg0;
1689 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1691 832c249c 2018-06-10 stsp printf(" %s\n", line);
1692 832c249c 2018-06-10 stsp } while (line);
1693 621015ac 2018-07-23 stsp free(logmsg0);
1695 971751ac 2018-03-27 stsp if (show_patch) {
1696 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1697 971751ac 2018-03-27 stsp if (err == 0)
1698 971751ac 2018-03-27 stsp printf("\n");
1701 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1702 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1703 07862c20 2018-09-15 stsp return err;
1706 f42b1b34 2018-03-12 stsp static const struct got_error *
1707 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1708 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1709 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1711 f42b1b34 2018-03-12 stsp const struct got_error *err;
1712 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1714 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1715 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1717 f42b1b34 2018-03-12 stsp return err;
1718 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1719 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1721 fcc85cad 2018-07-22 stsp goto done;
1723 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1724 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1726 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1729 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1730 372ccdbb 2018-06-10 stsp if (err) {
1731 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1732 9ba79e04 2018-06-11 stsp err = NULL;
1735 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1737 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1738 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1744 b43fbaa0 2018-06-11 stsp if (id == NULL)
1747 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1750 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1751 44392932 2019-08-25 stsp diff_context, refs);
1752 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1753 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1757 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1758 f42b1b34 2018-03-12 stsp return err;
1761 4ed7e80c 2018-05-20 stsp __dead static void
1762 6f3d1eb0 2018-03-12 stsp usage_log(void)
1764 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1765 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1769 b1ebc001 2019-08-13 stsp static int
1770 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1772 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1773 b1ebc001 2019-08-13 stsp long long n;
1774 b1ebc001 2019-08-13 stsp const char *errstr;
1776 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1777 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1779 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1780 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1785 4ed7e80c 2018-05-20 stsp static const struct got_error *
1786 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1788 f42b1b34 2018-03-12 stsp const struct got_error *error;
1789 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1790 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1791 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1792 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1793 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1794 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1795 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1796 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1797 64a96a6d 2018-04-01 stsp const char *errstr;
1798 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1800 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1802 6715a751 2018-03-16 stsp #ifndef PROFILE
1803 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1806 f42b1b34 2018-03-12 stsp err(1, "pledge");
1809 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1811 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1812 79109fed 2018-03-27 stsp switch (ch) {
1814 79109fed 2018-03-27 stsp show_patch = 1;
1817 d142fc45 2018-04-01 stsp start_commit = optarg;
1820 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1822 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1823 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1826 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1827 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1828 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1831 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1834 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1835 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1836 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1838 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1841 2deda0b9 2019-03-07 stsp usage_log();
1842 79109fed 2018-03-27 stsp /* NOTREACHED */
1846 79109fed 2018-03-27 stsp argc -= optind;
1847 79109fed 2018-03-27 stsp argv += optind;
1849 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1850 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1851 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1852 04ca23f4 2018-07-16 stsp goto done;
1855 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1856 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1857 cffc0aa4 2019-02-05 stsp goto done;
1858 cffc0aa4 2019-02-05 stsp error = NULL;
1860 e7301579 2019-03-18 stsp if (argc == 0) {
1861 cbd1af7a 2019-03-18 stsp path = strdup("");
1862 e7301579 2019-03-18 stsp if (path == NULL) {
1863 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1864 cbd1af7a 2019-03-18 stsp goto done;
1866 e7301579 2019-03-18 stsp } else if (argc == 1) {
1867 e7301579 2019-03-18 stsp if (worktree) {
1868 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1870 e7301579 2019-03-18 stsp if (error)
1871 e7301579 2019-03-18 stsp goto done;
1873 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1874 e7301579 2019-03-18 stsp if (path == NULL) {
1875 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1876 e7301579 2019-03-18 stsp goto done;
1880 cbd1af7a 2019-03-18 stsp usage_log();
1882 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1883 5486daa2 2019-05-11 stsp repo_path = worktree ?
1884 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1886 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1887 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1888 cffc0aa4 2019-02-05 stsp goto done;
1891 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1892 d7d4f210 2018-03-12 stsp if (error != NULL)
1893 04ca23f4 2018-07-16 stsp goto done;
1895 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1896 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1897 c02c541e 2019-03-29 stsp if (error)
1898 c02c541e 2019-03-29 stsp goto done;
1900 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1901 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1902 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1903 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1904 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1905 3235492e 2018-04-01 stsp if (error != NULL)
1906 3235492e 2018-04-01 stsp return error;
1907 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1908 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1909 3235492e 2018-04-01 stsp if (error != NULL)
1910 3235492e 2018-04-01 stsp return error;
1911 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1913 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1914 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1915 3235492e 2018-04-01 stsp if (error == NULL) {
1916 1caad61b 2019-02-01 stsp int obj_type;
1917 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1918 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1919 d1f2edc9 2018-06-13 stsp if (error != NULL)
1920 1caad61b 2019-02-01 stsp goto done;
1921 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1922 1caad61b 2019-02-01 stsp if (error != NULL)
1923 1caad61b 2019-02-01 stsp goto done;
1924 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1925 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1926 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1927 1caad61b 2019-02-01 stsp if (error != NULL)
1928 1caad61b 2019-02-01 stsp goto done;
1929 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1930 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1931 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1932 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1933 1caad61b 2019-02-01 stsp goto done;
1936 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1937 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1938 1caad61b 2019-02-01 stsp if (id == NULL)
1939 638f9024 2019-05-13 stsp error = got_error_from_errno(
1940 230a42bd 2019-05-11 jcs "got_object_id_dup");
1941 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1942 1caad61b 2019-02-01 stsp if (error)
1943 1caad61b 2019-02-01 stsp goto done;
1944 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1945 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1946 1caad61b 2019-02-01 stsp goto done;
1948 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1949 d1f2edc9 2018-06-13 stsp if (error != NULL)
1950 1caad61b 2019-02-01 stsp goto done;
1952 15a94983 2018-12-23 stsp if (commit == NULL) {
1953 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1954 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1955 d1f2edc9 2018-06-13 stsp if (error != NULL)
1956 d1f2edc9 2018-06-13 stsp return error;
1959 d7d4f210 2018-03-12 stsp if (error != NULL)
1960 04ca23f4 2018-07-16 stsp goto done;
1962 deeabeae 2019-08-27 stsp if (worktree) {
1963 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1965 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
1966 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1967 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
1968 deeabeae 2019-08-27 stsp goto done;
1970 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1973 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1974 04ca23f4 2018-07-16 stsp if (error != NULL)
1975 04ca23f4 2018-07-16 stsp goto done;
1976 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1977 04ca23f4 2018-07-16 stsp free(path);
1978 04ca23f4 2018-07-16 stsp path = in_repo_path;
1981 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1982 199a4027 2019-02-02 stsp if (error)
1983 199a4027 2019-02-02 stsp goto done;
1985 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1986 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1988 04ca23f4 2018-07-16 stsp free(path);
1989 04ca23f4 2018-07-16 stsp free(repo_path);
1990 04ca23f4 2018-07-16 stsp free(cwd);
1992 cffc0aa4 2019-02-05 stsp if (worktree)
1993 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1994 ad242220 2018-09-08 stsp if (repo) {
1995 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1996 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1997 ad242220 2018-09-08 stsp if (error == NULL)
1998 ad242220 2018-09-08 stsp error = repo_error;
2000 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2001 8bf5b3c9 2018-03-17 stsp return error;
2004 4ed7e80c 2018-05-20 stsp __dead static void
2005 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2007 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2008 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2012 b72f483a 2019-02-05 stsp struct print_diff_arg {
2013 b72f483a 2019-02-05 stsp struct got_repository *repo;
2014 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2015 b72f483a 2019-02-05 stsp int diff_context;
2016 3fc0c068 2019-02-10 stsp const char *id_str;
2017 3fc0c068 2019-02-10 stsp int header_shown;
2018 98eaaa12 2019-08-03 stsp int diff_staged;
2019 63035f9f 2019-10-06 stsp int ignore_whitespace;
2022 4ed7e80c 2018-05-20 stsp static const struct got_error *
2023 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2024 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2025 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2027 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2028 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2029 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2030 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2031 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2032 b72f483a 2019-02-05 stsp struct stat sb;
2034 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2035 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2036 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2037 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2038 98eaaa12 2019-08-03 stsp return NULL;
2040 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2041 98eaaa12 2019-08-03 stsp return NULL;
2042 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2043 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2044 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2045 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2046 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2047 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2048 98eaaa12 2019-08-03 stsp return NULL;
2051 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2052 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2053 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2054 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2055 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2058 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2059 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2060 98eaaa12 2019-08-03 stsp switch (staged_status) {
2061 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2062 98eaaa12 2019-08-03 stsp label1 = path;
2063 98eaaa12 2019-08-03 stsp label2 = path;
2065 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2066 98eaaa12 2019-08-03 stsp label2 = path;
2068 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2069 98eaaa12 2019-08-03 stsp label1 = path;
2072 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2074 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2075 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2076 63035f9f 2019-10-06 stsp a->repo, stdout);
2079 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2080 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2081 4ce46740 2019-08-08 stsp char *id_str;
2082 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2085 4ce46740 2019-08-08 stsp goto done;
2086 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2088 4ce46740 2019-08-08 stsp goto done;
2089 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2090 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2091 4ce46740 2019-08-08 stsp free(id_str);
2092 4ce46740 2019-08-08 stsp goto done;
2094 4ce46740 2019-08-08 stsp free(id_str);
2095 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2096 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2098 4ce46740 2019-08-08 stsp goto done;
2101 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2102 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2103 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2104 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2105 2ec1f75b 2019-03-26 stsp goto done;
2108 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
2109 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
2110 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
2111 2ec1f75b 2019-03-26 stsp goto done;
2113 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
2114 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
2115 2ec1f75b 2019-03-26 stsp goto done;
2118 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2120 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2121 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2123 b72f483a 2019-02-05 stsp if (blob1)
2124 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2125 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
2126 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2127 b72f483a 2019-02-05 stsp free(abspath);
2128 927df6b7 2019-02-10 stsp return err;
2131 927df6b7 2019-02-10 stsp static const struct got_error *
2132 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
2133 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
2134 01073a5d 2019-08-22 stsp struct got_repository *repo)
2136 0adb7196 2019-08-11 stsp const struct got_error *err;
2137 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
2138 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
2140 0adb7196 2019-08-11 stsp *id = NULL;
2141 0adb7196 2019-08-11 stsp *label = NULL;
2143 01073a5d 2019-08-22 stsp if (resolve_tags) {
2144 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2146 01073a5d 2019-08-22 stsp if (err == NULL) {
2147 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
2148 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
2149 01073a5d 2019-08-22 stsp if (*id == NULL)
2150 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
2151 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
2152 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
2153 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
2154 32f0ab81 2019-08-28 hiltjo free(*id);
2155 01073a5d 2019-08-22 stsp *id = NULL;
2157 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
2158 01073a5d 2019-08-22 stsp return err;
2159 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
2160 01073a5d 2019-08-22 stsp return err;
2163 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2164 0adb7196 2019-08-11 stsp if (err) {
2165 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2166 0adb7196 2019-08-11 stsp return err;
2167 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
2168 0adb7196 2019-08-11 stsp if (err != NULL)
2169 0adb7196 2019-08-11 stsp goto done;
2170 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
2171 0adb7196 2019-08-11 stsp if (*label == NULL) {
2172 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2173 0adb7196 2019-08-11 stsp goto done;
2175 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
2177 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
2178 0adb7196 2019-08-11 stsp if (*label == NULL) {
2179 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2180 0adb7196 2019-08-11 stsp goto done;
2185 0adb7196 2019-08-11 stsp got_ref_close(ref);
2186 0adb7196 2019-08-11 stsp return err;
2190 0adb7196 2019-08-11 stsp static const struct got_error *
2191 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2193 b00d56cd 2018-04-01 stsp const struct got_error *error;
2194 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2195 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2196 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2197 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2198 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2199 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2200 15a94983 2018-12-23 stsp int type1, type2;
2201 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2202 c0cc5c62 2018-10-18 stsp const char *errstr;
2203 927df6b7 2019-02-10 stsp char *path = NULL;
2205 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2206 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2207 25eccc22 2019-01-04 stsp NULL) == -1)
2208 b00d56cd 2018-04-01 stsp err(1, "pledge");
2211 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2212 b00d56cd 2018-04-01 stsp switch (ch) {
2214 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2215 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2216 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2219 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2220 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2221 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2223 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2226 98eaaa12 2019-08-03 stsp diff_staged = 1;
2229 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2232 2deda0b9 2019-03-07 stsp usage_diff();
2233 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2237 b00d56cd 2018-04-01 stsp argc -= optind;
2238 b00d56cd 2018-04-01 stsp argv += optind;
2240 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2241 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2242 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2243 b72f483a 2019-02-05 stsp goto done;
2245 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2246 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2247 927df6b7 2019-02-10 stsp goto done;
2248 927df6b7 2019-02-10 stsp if (argc <= 1) {
2249 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2250 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2251 927df6b7 2019-02-10 stsp goto done;
2253 b72f483a 2019-02-05 stsp if (repo_path)
2255 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2256 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2257 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2258 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2259 927df6b7 2019-02-10 stsp goto done;
2261 927df6b7 2019-02-10 stsp if (argc == 1) {
2262 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2264 927df6b7 2019-02-10 stsp if (error)
2265 927df6b7 2019-02-10 stsp goto done;
2267 927df6b7 2019-02-10 stsp path = strdup("");
2268 927df6b7 2019-02-10 stsp if (path == NULL) {
2269 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2270 927df6b7 2019-02-10 stsp goto done;
2273 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2274 98eaaa12 2019-08-03 stsp if (diff_staged)
2275 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2276 98eaaa12 2019-08-03 stsp "objects in repository");
2277 15a94983 2018-12-23 stsp id_str1 = argv[0];
2278 15a94983 2018-12-23 stsp id_str2 = argv[1];
2279 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2280 30db809c 2019-06-05 stsp repo_path =
2281 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2282 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2283 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2284 30db809c 2019-06-05 stsp goto done;
2288 b00d56cd 2018-04-01 stsp usage_diff();
2290 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2291 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2292 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2293 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2296 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2297 76089277 2018-04-01 stsp free(repo_path);
2298 b00d56cd 2018-04-01 stsp if (error != NULL)
2299 b00d56cd 2018-04-01 stsp goto done;
2301 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2302 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2303 c02c541e 2019-03-29 stsp if (error)
2304 c02c541e 2019-03-29 stsp goto done;
2306 30db809c 2019-06-05 stsp if (argc <= 1) {
2307 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2308 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2309 b72f483a 2019-02-05 stsp char *id_str;
2311 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2313 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2314 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2315 b72f483a 2019-02-05 stsp if (error)
2316 b72f483a 2019-02-05 stsp goto done;
2317 b72f483a 2019-02-05 stsp arg.repo = repo;
2318 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2319 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2320 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2321 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2322 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2323 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2325 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2326 72ea6654 2019-07-27 stsp if (error)
2327 72ea6654 2019-07-27 stsp goto done;
2329 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2330 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2331 b72f483a 2019-02-05 stsp free(id_str);
2332 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2333 b72f483a 2019-02-05 stsp goto done;
2336 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2338 0adb7196 2019-08-11 stsp if (error)
2339 0adb7196 2019-08-11 stsp goto done;
2341 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2343 0adb7196 2019-08-11 stsp if (error)
2344 0adb7196 2019-08-11 stsp goto done;
2346 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2347 15a94983 2018-12-23 stsp if (error)
2348 15a94983 2018-12-23 stsp goto done;
2350 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2351 15a94983 2018-12-23 stsp if (error)
2352 15a94983 2018-12-23 stsp goto done;
2354 15a94983 2018-12-23 stsp if (type1 != type2) {
2355 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2356 b00d56cd 2018-04-01 stsp goto done;
2359 15a94983 2018-12-23 stsp switch (type1) {
2360 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2361 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2362 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2364 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2365 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2366 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2368 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2369 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2370 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2371 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2374 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2378 5e70831e 2019-05-28 stsp free(label1);
2379 5e70831e 2019-05-28 stsp free(label2);
2380 15a94983 2018-12-23 stsp free(id1);
2381 15a94983 2018-12-23 stsp free(id2);
2382 927df6b7 2019-02-10 stsp free(path);
2383 b72f483a 2019-02-05 stsp if (worktree)
2384 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2385 ad242220 2018-09-08 stsp if (repo) {
2386 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2387 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2388 ad242220 2018-09-08 stsp if (error == NULL)
2389 ad242220 2018-09-08 stsp error = repo_error;
2391 b00d56cd 2018-04-01 stsp return error;
2394 404c43c4 2018-06-21 stsp __dead static void
2395 404c43c4 2018-06-21 stsp usage_blame(void)
2397 9270e621 2019-02-05 stsp fprintf(stderr,
2398 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2399 404c43c4 2018-06-21 stsp getprogname());
2403 28315671 2019-08-14 stsp struct blame_line {
2404 28315671 2019-08-14 stsp int annotated;
2405 28315671 2019-08-14 stsp char *id_str;
2406 82f6abb8 2019-08-14 stsp char *committer;
2407 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2410 28315671 2019-08-14 stsp struct blame_cb_args {
2411 28315671 2019-08-14 stsp struct blame_line *lines;
2412 28315671 2019-08-14 stsp int nlines;
2413 7ef28ff8 2019-08-14 stsp int nlines_prec;
2414 28315671 2019-08-14 stsp int lineno_cur;
2415 28315671 2019-08-14 stsp off_t *line_offsets;
2417 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2420 404c43c4 2018-06-21 stsp static const struct got_error *
2421 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2423 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2424 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2425 28315671 2019-08-14 stsp struct blame_line *bline;
2426 82f6abb8 2019-08-14 stsp char *line = NULL;
2427 28315671 2019-08-14 stsp size_t linesize = 0;
2428 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2429 28315671 2019-08-14 stsp off_t offset;
2430 bcb49d15 2019-08-14 stsp struct tm tm;
2431 bcb49d15 2019-08-14 stsp time_t committer_time;
2433 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2434 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2435 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2437 28315671 2019-08-14 stsp if (sigint_received)
2438 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2440 2839f8b9 2019-08-18 stsp if (lineno == -1)
2441 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2443 28315671 2019-08-14 stsp /* Annotate this line. */
2444 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2445 28315671 2019-08-14 stsp if (bline->annotated)
2446 28315671 2019-08-14 stsp return NULL;
2447 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2449 28315671 2019-08-14 stsp return err;
2451 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2453 82f6abb8 2019-08-14 stsp goto done;
2455 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2456 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2457 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2458 bcb49d15 2019-08-14 stsp goto done;
2461 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2462 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2463 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2464 11db6024 2019-10-21 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2465 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2466 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2467 82f6abb8 2019-08-14 stsp goto done;
2469 28315671 2019-08-14 stsp bline->annotated = 1;
2471 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2472 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2473 28315671 2019-08-14 stsp if (!bline->annotated)
2474 82f6abb8 2019-08-14 stsp goto done;
2476 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2477 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2478 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2479 82f6abb8 2019-08-14 stsp goto done;
2482 28315671 2019-08-14 stsp while (bline->annotated) {
2483 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2484 82f6abb8 2019-08-14 stsp size_t len;
2486 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2487 28315671 2019-08-14 stsp if (ferror(a->f))
2488 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2492 82f6abb8 2019-08-14 stsp committer = bline->committer;
2493 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2494 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2495 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2496 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2498 82f6abb8 2019-08-14 stsp *at = '\0';
2499 82f6abb8 2019-08-14 stsp len = strlen(committer);
2500 82f6abb8 2019-08-14 stsp if (len >= 9)
2501 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2503 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2505 28315671 2019-08-14 stsp *nl = '\0';
2506 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2507 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2509 28315671 2019-08-14 stsp a->lineno_cur++;
2510 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2513 82f6abb8 2019-08-14 stsp if (commit)
2514 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2515 28315671 2019-08-14 stsp free(line);
2516 28315671 2019-08-14 stsp return err;
2519 28315671 2019-08-14 stsp static const struct got_error *
2520 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2522 404c43c4 2018-06-21 stsp const struct got_error *error;
2523 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2524 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2525 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2526 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2527 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2528 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2529 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2530 28315671 2019-08-14 stsp struct blame_cb_args bca;
2531 28315671 2019-08-14 stsp int ch, obj_type, i;
2532 b02560ec 2019-08-19 stsp size_t filesize;
2534 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2536 404c43c4 2018-06-21 stsp #ifndef PROFILE
2537 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2538 36e2fb66 2019-01-04 stsp NULL) == -1)
2539 404c43c4 2018-06-21 stsp err(1, "pledge");
2542 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2543 404c43c4 2018-06-21 stsp switch (ch) {
2545 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2548 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2549 66bea077 2018-08-02 stsp if (repo_path == NULL)
2550 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2552 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2555 2deda0b9 2019-03-07 stsp usage_blame();
2556 404c43c4 2018-06-21 stsp /* NOTREACHED */
2560 404c43c4 2018-06-21 stsp argc -= optind;
2561 404c43c4 2018-06-21 stsp argv += optind;
2563 a39318fd 2018-08-02 stsp if (argc == 1)
2564 404c43c4 2018-06-21 stsp path = argv[0];
2566 404c43c4 2018-06-21 stsp usage_blame();
2568 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2569 66bea077 2018-08-02 stsp if (cwd == NULL) {
2570 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2571 66bea077 2018-08-02 stsp goto done;
2573 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2574 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2575 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2576 66bea077 2018-08-02 stsp goto done;
2578 0c06baac 2019-02-05 stsp error = NULL;
2579 0c06baac 2019-02-05 stsp if (worktree) {
2580 0c06baac 2019-02-05 stsp repo_path =
2581 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2582 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2583 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2584 0c06baac 2019-02-05 stsp if (error)
2585 0c06baac 2019-02-05 stsp goto done;
2587 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2588 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2589 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2590 0c06baac 2019-02-05 stsp goto done;
2595 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2596 c02c541e 2019-03-29 stsp if (error != NULL)
2597 36e2fb66 2019-01-04 stsp goto done;
2599 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2600 c02c541e 2019-03-29 stsp if (error)
2601 404c43c4 2018-06-21 stsp goto done;
2603 0c06baac 2019-02-05 stsp if (worktree) {
2604 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2605 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2606 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2607 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2608 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2609 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2610 6efaaa2d 2019-02-05 stsp path) == -1) {
2611 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2612 0c06baac 2019-02-05 stsp goto done;
2614 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2617 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2619 0c06baac 2019-02-05 stsp if (error)
2620 66bea077 2018-08-02 stsp goto done;
2622 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2623 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2624 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2625 404c43c4 2018-06-21 stsp if (error != NULL)
2626 66bea077 2018-08-02 stsp goto done;
2627 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2628 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2629 404c43c4 2018-06-21 stsp if (error != NULL)
2630 66bea077 2018-08-02 stsp goto done;
2632 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2633 30837e32 2019-07-25 stsp if (error)
2634 66bea077 2018-08-02 stsp goto done;
2637 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2638 28315671 2019-08-14 stsp if (error)
2639 28315671 2019-08-14 stsp goto done;
2640 28315671 2019-08-14 stsp if (obj_id == NULL) {
2641 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2642 28315671 2019-08-14 stsp goto done;
2645 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2646 28315671 2019-08-14 stsp if (error)
2647 28315671 2019-08-14 stsp goto done;
2649 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2650 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2651 28315671 2019-08-14 stsp goto done;
2654 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2655 28315671 2019-08-14 stsp if (error)
2656 28315671 2019-08-14 stsp goto done;
2657 28315671 2019-08-14 stsp bca.f = got_opentemp();
2658 28315671 2019-08-14 stsp if (bca.f == NULL) {
2659 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2660 28315671 2019-08-14 stsp goto done;
2662 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2663 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2664 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2665 28315671 2019-08-14 stsp goto done;
2667 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2668 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2669 b02560ec 2019-08-19 stsp bca.nlines--;
2671 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2672 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2673 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2674 28315671 2019-08-14 stsp goto done;
2676 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2677 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2678 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2679 7ef28ff8 2019-08-14 stsp while (i > 0) {
2681 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2683 82f6abb8 2019-08-14 stsp bca.repo = repo;
2685 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2686 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2687 28315671 2019-08-14 stsp if (error)
2688 28315671 2019-08-14 stsp goto done;
2690 66bea077 2018-08-02 stsp free(in_repo_path);
2691 66bea077 2018-08-02 stsp free(repo_path);
2692 66bea077 2018-08-02 stsp free(cwd);
2693 404c43c4 2018-06-21 stsp free(commit_id);
2694 28315671 2019-08-14 stsp free(obj_id);
2696 28315671 2019-08-14 stsp got_object_blob_close(blob);
2697 0c06baac 2019-02-05 stsp if (worktree)
2698 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2699 ad242220 2018-09-08 stsp if (repo) {
2700 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2701 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2702 ad242220 2018-09-08 stsp if (error == NULL)
2703 ad242220 2018-09-08 stsp error = repo_error;
2705 3affba96 2019-09-22 stsp if (bca.lines) {
2706 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2707 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2708 3affba96 2019-09-22 stsp free(bline->id_str);
2709 3affba96 2019-09-22 stsp free(bline->committer);
2711 3affba96 2019-09-22 stsp free(bca.lines);
2713 28315671 2019-08-14 stsp free(bca.line_offsets);
2714 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2715 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2716 404c43c4 2018-06-21 stsp return error;
2719 5de5890b 2018-10-18 stsp __dead static void
2720 5de5890b 2018-10-18 stsp usage_tree(void)
2722 c1669e2e 2019-01-09 stsp fprintf(stderr,
2723 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2724 5de5890b 2018-10-18 stsp getprogname());
2728 c1669e2e 2019-01-09 stsp static void
2729 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2730 c1669e2e 2019-01-09 stsp const char *root_path)
2732 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2733 848d6979 2019-08-12 stsp const char *modestr = "";
2734 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2736 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2737 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2740 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2741 63c5ca5d 2019-08-24 stsp modestr = "$";
2742 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2743 848d6979 2019-08-12 stsp modestr = "@";
2744 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2745 848d6979 2019-08-12 stsp modestr = "/";
2746 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2747 848d6979 2019-08-12 stsp modestr = "*";
2749 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2750 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2753 5de5890b 2018-10-18 stsp static const struct got_error *
2754 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2755 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2756 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2758 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2759 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2760 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2761 56e0773d 2019-11-28 stsp int nentries, i;
2763 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2765 5de5890b 2018-10-18 stsp goto done;
2767 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2769 5de5890b 2018-10-18 stsp goto done;
2770 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2771 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2772 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2773 5de5890b 2018-10-18 stsp char *id = NULL;
2775 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2778 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2779 5de5890b 2018-10-18 stsp if (show_ids) {
2780 5de5890b 2018-10-18 stsp char *id_str;
2781 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2782 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2784 5de5890b 2018-10-18 stsp goto done;
2785 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2786 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2787 5de5890b 2018-10-18 stsp free(id_str);
2788 5de5890b 2018-10-18 stsp goto done;
2790 5de5890b 2018-10-18 stsp free(id_str);
2792 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2795 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2796 c1669e2e 2019-01-09 stsp char *child_path;
2797 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2798 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2799 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2800 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2801 c1669e2e 2019-01-09 stsp goto done;
2803 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2804 c1669e2e 2019-01-09 stsp root_path, repo);
2805 c1669e2e 2019-01-09 stsp free(child_path);
2807 c1669e2e 2019-01-09 stsp goto done;
2812 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2813 5de5890b 2018-10-18 stsp free(tree_id);
2814 5de5890b 2018-10-18 stsp return err;
2817 5de5890b 2018-10-18 stsp static const struct got_error *
2818 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2820 5de5890b 2018-10-18 stsp const struct got_error *error;
2821 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2822 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2823 7a2c19d6 2019-02-05 stsp const char *path;
2824 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2825 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2826 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2827 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2830 5de5890b 2018-10-18 stsp #ifndef PROFILE
2831 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2832 0f8d269b 2019-01-04 stsp NULL) == -1)
2833 5de5890b 2018-10-18 stsp err(1, "pledge");
2836 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2837 5de5890b 2018-10-18 stsp switch (ch) {
2839 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2842 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2843 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2844 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2846 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2849 5de5890b 2018-10-18 stsp show_ids = 1;
2852 c1669e2e 2019-01-09 stsp recurse = 1;
2855 2deda0b9 2019-03-07 stsp usage_tree();
2856 5de5890b 2018-10-18 stsp /* NOTREACHED */
2860 5de5890b 2018-10-18 stsp argc -= optind;
2861 5de5890b 2018-10-18 stsp argv += optind;
2863 5de5890b 2018-10-18 stsp if (argc == 1)
2864 5de5890b 2018-10-18 stsp path = argv[0];
2865 5de5890b 2018-10-18 stsp else if (argc > 1)
2866 5de5890b 2018-10-18 stsp usage_tree();
2868 7a2c19d6 2019-02-05 stsp path = NULL;
2870 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2871 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2872 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2873 9bf7a39b 2019-02-05 stsp goto done;
2875 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2876 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2877 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2878 7a2c19d6 2019-02-05 stsp goto done;
2880 7a2c19d6 2019-02-05 stsp error = NULL;
2881 7a2c19d6 2019-02-05 stsp if (worktree) {
2882 7a2c19d6 2019-02-05 stsp repo_path =
2883 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2884 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2885 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2886 7a2c19d6 2019-02-05 stsp if (error)
2887 7a2c19d6 2019-02-05 stsp goto done;
2889 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2890 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2891 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2892 7a2c19d6 2019-02-05 stsp goto done;
2897 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2898 5de5890b 2018-10-18 stsp if (error != NULL)
2899 c02c541e 2019-03-29 stsp goto done;
2901 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2902 c02c541e 2019-03-29 stsp if (error)
2903 5de5890b 2018-10-18 stsp goto done;
2905 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2906 9bf7a39b 2019-02-05 stsp if (worktree) {
2907 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2908 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2909 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2910 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2911 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2912 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2913 9bf7a39b 2019-02-05 stsp goto done;
2915 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2917 9bf7a39b 2019-02-05 stsp if (error)
2918 9bf7a39b 2019-02-05 stsp goto done;
2920 9bf7a39b 2019-02-05 stsp path = "/";
2922 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2923 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2924 9bf7a39b 2019-02-05 stsp if (error != NULL)
2925 9bf7a39b 2019-02-05 stsp goto done;
2928 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2929 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2930 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2931 5de5890b 2018-10-18 stsp if (error != NULL)
2932 5de5890b 2018-10-18 stsp goto done;
2933 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2934 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2935 5de5890b 2018-10-18 stsp if (error != NULL)
2936 5de5890b 2018-10-18 stsp goto done;
2938 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2939 30837e32 2019-07-25 stsp if (error)
2940 5de5890b 2018-10-18 stsp goto done;
2943 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2944 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2946 5de5890b 2018-10-18 stsp free(in_repo_path);
2947 5de5890b 2018-10-18 stsp free(repo_path);
2948 5de5890b 2018-10-18 stsp free(cwd);
2949 5de5890b 2018-10-18 stsp free(commit_id);
2950 7a2c19d6 2019-02-05 stsp if (worktree)
2951 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2952 5de5890b 2018-10-18 stsp if (repo) {
2953 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2954 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2955 5de5890b 2018-10-18 stsp if (error == NULL)
2956 5de5890b 2018-10-18 stsp error = repo_error;
2958 5de5890b 2018-10-18 stsp return error;
2961 6bad629b 2019-02-04 stsp __dead static void
2962 6bad629b 2019-02-04 stsp usage_status(void)
2964 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2968 b72f483a 2019-02-05 stsp static const struct got_error *
2969 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2970 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2971 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2973 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2974 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2975 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2976 b72f483a 2019-02-05 stsp return NULL;
2979 6bad629b 2019-02-04 stsp static const struct got_error *
2980 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2982 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2983 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2984 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2985 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2986 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2987 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2990 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2992 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2993 6bad629b 2019-02-04 stsp switch (ch) {
2995 2deda0b9 2019-03-07 stsp usage_status();
2996 6bad629b 2019-02-04 stsp /* NOTREACHED */
3000 6bad629b 2019-02-04 stsp argc -= optind;
3001 6bad629b 2019-02-04 stsp argv += optind;
3003 6bad629b 2019-02-04 stsp #ifndef PROFILE
3004 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3005 6bad629b 2019-02-04 stsp NULL) == -1)
3006 6bad629b 2019-02-04 stsp err(1, "pledge");
3008 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3009 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3010 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3011 927df6b7 2019-02-10 stsp goto done;
3014 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3015 927df6b7 2019-02-10 stsp if (error != NULL)
3016 a5edda0a 2019-07-27 stsp goto done;
3018 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3020 6bad629b 2019-02-04 stsp if (error != NULL)
3021 6bad629b 2019-02-04 stsp goto done;
3023 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3024 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3025 087fb88c 2019-08-04 stsp if (error)
3026 087fb88c 2019-08-04 stsp goto done;
3028 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3029 6bad629b 2019-02-04 stsp if (error)
3030 6bad629b 2019-02-04 stsp goto done;
3032 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3033 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3035 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3036 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3037 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3038 927df6b7 2019-02-10 stsp free(cwd);
3039 d0eebce4 2019-03-11 stsp return error;
3042 d0eebce4 2019-03-11 stsp __dead static void
3043 d0eebce4 2019-03-11 stsp usage_ref(void)
3045 d0eebce4 2019-03-11 stsp fprintf(stderr,
3046 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3047 d0eebce4 2019-03-11 stsp getprogname());
3051 d0eebce4 2019-03-11 stsp static const struct got_error *
3052 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3054 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3055 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3056 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3058 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3059 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3061 d0eebce4 2019-03-11 stsp return err;
3063 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3064 d0eebce4 2019-03-11 stsp char *refstr;
3065 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3066 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3067 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3068 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3069 d0eebce4 2019-03-11 stsp free(refstr);
3072 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3073 d0eebce4 2019-03-11 stsp return NULL;
3076 d0eebce4 2019-03-11 stsp static const struct got_error *
3077 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3079 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3080 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3082 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3084 d0eebce4 2019-03-11 stsp return err;
3086 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3087 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3088 d0eebce4 2019-03-11 stsp return err;
3091 d0eebce4 2019-03-11 stsp static const struct got_error *
3092 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3094 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3095 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3096 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3099 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3100 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3101 d1644381 2019-07-14 stsp * an unintended typo.
3103 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3104 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3106 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3108 d83d9d5c 2019-05-13 stsp if (err) {
3109 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3111 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3112 d83d9d5c 2019-05-13 stsp return err;
3113 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3115 d83d9d5c 2019-05-13 stsp return err;
3116 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3117 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3119 d83d9d5c 2019-05-13 stsp return err;
3122 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3124 d0eebce4 2019-03-11 stsp goto done;
3126 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3129 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3131 d1c1ae5f 2019-08-12 stsp return err;
3134 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3135 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3137 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3138 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3139 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3142 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3143 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3144 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3146 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3147 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3149 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3151 d1c1ae5f 2019-08-12 stsp return err;
3153 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3155 d1c1ae5f 2019-08-12 stsp goto done;
3157 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3159 d1c1ae5f 2019-08-12 stsp if (target_ref)
3160 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3162 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3163 d0eebce4 2019-03-11 stsp return err;
3166 d0eebce4 2019-03-11 stsp static const struct got_error *
3167 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3169 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3170 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3171 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3172 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3173 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3174 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3176 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3177 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3178 d0eebce4 2019-03-11 stsp switch (ch) {
3180 d0eebce4 2019-03-11 stsp delref = optarg;
3183 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3184 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3185 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3187 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3190 d0eebce4 2019-03-11 stsp do_list = 1;
3193 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3196 d0eebce4 2019-03-11 stsp usage_ref();
3197 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3201 d0eebce4 2019-03-11 stsp if (do_list && delref)
3202 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3204 d0eebce4 2019-03-11 stsp argc -= optind;
3205 d0eebce4 2019-03-11 stsp argv += optind;
3207 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3208 d1c1ae5f 2019-08-12 stsp if (create_symref)
3209 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3210 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3211 d0eebce4 2019-03-11 stsp if (argc > 0)
3212 d0eebce4 2019-03-11 stsp usage_ref();
3213 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3214 d0eebce4 2019-03-11 stsp usage_ref();
3216 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3217 e0b57350 2019-03-12 stsp if (do_list) {
3218 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3219 e0b57350 2019-03-12 stsp NULL) == -1)
3220 e0b57350 2019-03-12 stsp err(1, "pledge");
3222 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3223 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3224 e0b57350 2019-03-12 stsp err(1, "pledge");
3227 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3228 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3229 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3230 d0eebce4 2019-03-11 stsp goto done;
3233 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3234 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3235 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3236 d0eebce4 2019-03-11 stsp goto done;
3238 d0eebce4 2019-03-11 stsp error = NULL;
3239 d0eebce4 2019-03-11 stsp if (worktree) {
3240 d0eebce4 2019-03-11 stsp repo_path =
3241 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3242 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3243 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3244 d0eebce4 2019-03-11 stsp if (error)
3245 d0eebce4 2019-03-11 stsp goto done;
3247 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3248 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3249 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3250 d0eebce4 2019-03-11 stsp goto done;
3255 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3256 d0eebce4 2019-03-11 stsp if (error != NULL)
3257 d0eebce4 2019-03-11 stsp goto done;
3259 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3260 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3261 c02c541e 2019-03-29 stsp if (error)
3262 c02c541e 2019-03-29 stsp goto done;
3264 d0eebce4 2019-03-11 stsp if (do_list)
3265 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3266 d0eebce4 2019-03-11 stsp else if (delref)
3267 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3268 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3269 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3271 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3274 4e759de4 2019-06-26 stsp got_repo_close(repo);
3275 4e759de4 2019-06-26 stsp if (worktree)
3276 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3277 4e759de4 2019-06-26 stsp free(cwd);
3278 4e759de4 2019-06-26 stsp free(repo_path);
3279 4e759de4 2019-06-26 stsp return error;
3282 4e759de4 2019-06-26 stsp __dead static void
3283 4e759de4 2019-06-26 stsp usage_branch(void)
3285 4e759de4 2019-06-26 stsp fprintf(stderr,
3286 a74f7e83 2019-11-10 stsp "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3287 a74f7e83 2019-11-10 stsp "[name]\n", getprogname());
3291 4e759de4 2019-06-26 stsp static const struct got_error *
3292 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3293 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3295 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3296 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3297 4e99b47e 2019-10-04 stsp char *refstr;
3299 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3300 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3301 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3302 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3304 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3306 4e99b47e 2019-10-04 stsp return err;
3307 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3308 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3309 4e99b47e 2019-10-04 stsp marker = "* ";
3311 4e99b47e 2019-10-04 stsp marker = "~ ";
3315 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3316 4e99b47e 2019-10-04 stsp refname += 11;
3317 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3318 4e99b47e 2019-10-04 stsp refname += 18;
3320 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3321 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3322 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3324 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3325 4e99b47e 2019-10-04 stsp free(refstr);
3326 4e99b47e 2019-10-04 stsp return NULL;
3329 4e99b47e 2019-10-04 stsp static const struct got_error *
3330 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3332 ad89fa31 2019-10-04 stsp const char *refname;
3334 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3335 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3337 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3339 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3340 ad89fa31 2019-10-04 stsp refname += 11;
3341 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3342 ad89fa31 2019-10-04 stsp refname += 18;
3344 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3346 ad89fa31 2019-10-04 stsp return NULL;
3349 ad89fa31 2019-10-04 stsp static const struct got_error *
3350 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3352 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3353 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3354 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3355 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3356 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3358 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3360 4e99b47e 2019-10-04 stsp if (worktree) {
3361 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3362 4e99b47e 2019-10-04 stsp worktree);
3364 4e99b47e 2019-10-04 stsp return err;
3366 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3367 4e99b47e 2019-10-04 stsp worktree);
3369 4e99b47e 2019-10-04 stsp return err;
3371 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3372 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3373 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3375 4e99b47e 2019-10-04 stsp return err;
3376 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3377 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3381 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3382 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3384 4e759de4 2019-06-26 stsp return err;
3386 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3387 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3389 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3390 4e759de4 2019-06-26 stsp return NULL;
3393 4e759de4 2019-06-26 stsp static const struct got_error *
3394 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3395 45cd4e47 2019-08-25 stsp const char *branch_name)
3397 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3398 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3399 4e759de4 2019-06-26 stsp char *refname;
3401 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3402 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3404 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3406 4e759de4 2019-06-26 stsp goto done;
3408 45cd4e47 2019-08-25 stsp if (worktree &&
3409 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3410 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3411 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3412 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3413 45cd4e47 2019-08-25 stsp goto done;
3416 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3419 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3420 4e759de4 2019-06-26 stsp free(refname);
3421 4e759de4 2019-06-26 stsp return err;
3424 4e759de4 2019-06-26 stsp static const struct got_error *
3425 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3426 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3428 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3429 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3430 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3433 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3434 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3435 d3f84d51 2019-07-11 stsp * an unintended typo.
3437 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3438 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3440 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3441 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3442 4e759de4 2019-06-26 stsp goto done;
3445 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3446 4e759de4 2019-06-26 stsp if (err == NULL) {
3447 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3448 4e759de4 2019-06-26 stsp goto done;
3449 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3450 4e759de4 2019-06-26 stsp goto done;
3452 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3454 4e759de4 2019-06-26 stsp goto done;
3456 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3459 4e759de4 2019-06-26 stsp got_ref_close(ref);
3460 4e759de4 2019-06-26 stsp free(base_refname);
3461 4e759de4 2019-06-26 stsp free(refname);
3462 4e759de4 2019-06-26 stsp return err;
3465 4e759de4 2019-06-26 stsp static const struct got_error *
3466 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3468 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3469 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3470 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3471 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3472 ad89fa31 2019-10-04 stsp int ch, do_list = 0, do_show = 0;
3473 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3475 a74f7e83 2019-11-10 stsp while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3476 4e759de4 2019-06-26 stsp switch (ch) {
3478 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3481 4e759de4 2019-06-26 stsp delref = optarg;
3484 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3485 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3486 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3488 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3491 4e759de4 2019-06-26 stsp do_list = 1;
3494 4e759de4 2019-06-26 stsp usage_branch();
3495 4e759de4 2019-06-26 stsp /* NOTREACHED */
3499 4e759de4 2019-06-26 stsp if (do_list && delref)
3500 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3502 4e759de4 2019-06-26 stsp argc -= optind;
3503 4e759de4 2019-06-26 stsp argv += optind;
3505 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3506 ad89fa31 2019-10-04 stsp do_show = 1;
3508 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3509 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3511 4e759de4 2019-06-26 stsp if (do_list || delref) {
3512 4e759de4 2019-06-26 stsp if (argc > 0)
3513 4e759de4 2019-06-26 stsp usage_branch();
3514 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3515 4e759de4 2019-06-26 stsp usage_branch();
3517 4e759de4 2019-06-26 stsp #ifndef PROFILE
3518 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3519 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3520 4e759de4 2019-06-26 stsp NULL) == -1)
3521 4e759de4 2019-06-26 stsp err(1, "pledge");
3523 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3524 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3525 4e759de4 2019-06-26 stsp err(1, "pledge");
3528 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3529 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3530 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3531 4e759de4 2019-06-26 stsp goto done;
3534 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3535 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3536 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3537 4e759de4 2019-06-26 stsp goto done;
3539 4e759de4 2019-06-26 stsp error = NULL;
3540 4e759de4 2019-06-26 stsp if (worktree) {
3541 4e759de4 2019-06-26 stsp repo_path =
3542 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3543 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3544 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3545 4e759de4 2019-06-26 stsp if (error)
3546 4e759de4 2019-06-26 stsp goto done;
3548 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3549 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3550 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3551 4e759de4 2019-06-26 stsp goto done;
3556 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3557 4e759de4 2019-06-26 stsp if (error != NULL)
3558 4e759de4 2019-06-26 stsp goto done;
3560 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3561 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3562 4e759de4 2019-06-26 stsp if (error)
3563 4e759de4 2019-06-26 stsp goto done;
3565 ad89fa31 2019-10-04 stsp if (do_show)
3566 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3567 ad89fa31 2019-10-04 stsp else if (do_list)
3568 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3569 4e759de4 2019-06-26 stsp else if (delref)
3570 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3572 a74f7e83 2019-11-10 stsp struct got_object_id *commit_id;
3573 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3574 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3575 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3576 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3577 a74f7e83 2019-11-10 stsp error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3578 a74f7e83 2019-11-10 stsp if (error)
3579 a74f7e83 2019-11-10 stsp goto done;
3580 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3581 a74f7e83 2019-11-10 stsp free(commit_id);
3585 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3586 d0eebce4 2019-03-11 stsp if (worktree)
3587 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3588 d0eebce4 2019-03-11 stsp free(cwd);
3589 d0eebce4 2019-03-11 stsp free(repo_path);
3590 d00136be 2019-03-26 stsp return error;
3594 d00136be 2019-03-26 stsp __dead static void
3595 8e7bd50a 2019-08-22 stsp usage_tag(void)
3597 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3598 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3599 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3604 b8bad2ba 2019-08-23 stsp static const struct got_error *
3605 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3607 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3608 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3609 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3610 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3611 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3613 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3614 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3615 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3616 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3618 b8bad2ba 2019-08-23 stsp return err;
3619 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3622 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3625 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3626 b8bad2ba 2019-08-23 stsp free(re_id);
3629 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3630 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3633 b8bad2ba 2019-08-23 stsp while (se) {
3634 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3637 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3638 b8bad2ba 2019-08-23 stsp free(se_id);
3641 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3642 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3644 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3645 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3647 b8bad2ba 2019-08-23 stsp return err;
3648 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3651 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3656 b8bad2ba 2019-08-23 stsp return err;
3660 8e7bd50a 2019-08-22 stsp static const struct got_error *
3661 b8bad2ba 2019-08-23 stsp cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3662 b8bad2ba 2019-08-23 stsp struct got_reference *ref2)
3664 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3665 b8bad2ba 2019-08-23 stsp struct got_repository *repo = arg;
3666 b8bad2ba 2019-08-23 stsp struct got_object_id *id1, *id2 = NULL;
3667 b8bad2ba 2019-08-23 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3668 b8bad2ba 2019-08-23 stsp time_t time1, time2;
3672 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id1, repo, ref1);
3674 b8bad2ba 2019-08-23 stsp return err;
3675 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag1, repo, id1);
3677 b8bad2ba 2019-08-23 stsp goto done;
3679 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id2, repo, ref2);
3681 b8bad2ba 2019-08-23 stsp goto done;
3682 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag2, repo, id2);
3684 b8bad2ba 2019-08-23 stsp goto done;
3686 b8bad2ba 2019-08-23 stsp time1 = got_object_tag_get_tagger_time(tag1);
3687 b8bad2ba 2019-08-23 stsp time2 = got_object_tag_get_tagger_time(tag2);
3689 b8bad2ba 2019-08-23 stsp /* Put latest tags first. */
3690 b8bad2ba 2019-08-23 stsp if (time1 < time2)
3692 b8bad2ba 2019-08-23 stsp else if (time1 > time2)
3693 b8bad2ba 2019-08-23 stsp *cmp = -1;
3695 b8bad2ba 2019-08-23 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3697 b8bad2ba 2019-08-23 stsp free(id1);
3698 b8bad2ba 2019-08-23 stsp free(id2);
3700 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag1);
3702 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag2);
3703 b8bad2ba 2019-08-23 stsp return err;
3706 b8bad2ba 2019-08-23 stsp static const struct got_error *
3707 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3709 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3710 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3711 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3713 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3715 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3717 8e7bd50a 2019-08-22 stsp return err;
3719 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3720 8e7bd50a 2019-08-22 stsp const char *refname;
3721 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3722 8e7bd50a 2019-08-22 stsp char datebuf[26];
3723 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3724 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3725 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3727 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3728 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3730 8e7bd50a 2019-08-22 stsp refname += 10;
3731 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3732 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3733 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3736 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3737 8e7bd50a 2019-08-22 stsp free(refstr);
3739 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3742 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3746 2417344c 2019-08-23 stsp printf("from: %s\n", got_object_tag_get_tagger(tag));
3747 2417344c 2019-08-23 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3748 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3749 2417344c 2019-08-23 stsp if (datestr)
3750 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3751 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str,
3752 8e7bd50a 2019-08-22 stsp got_object_tag_get_object_id(tag));
3755 8e7bd50a 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
3756 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
3757 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3759 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
3760 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3762 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
3763 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3765 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
3766 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3771 8e7bd50a 2019-08-22 stsp free(id_str);
3772 8e7bd50a 2019-08-22 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3773 8e7bd50a 2019-08-22 stsp got_object_tag_close(tag);
3774 8e7bd50a 2019-08-22 stsp if (tagmsg0 == NULL) {
3775 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3779 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3781 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3783 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3784 8e7bd50a 2019-08-22 stsp } while (line);
3785 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3788 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3789 8e7bd50a 2019-08-22 stsp return NULL;
3792 8e7bd50a 2019-08-22 stsp static const struct got_error *
3793 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3794 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3796 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3797 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3798 f372d5cd 2019-10-21 stsp char *editor = NULL;
3799 8e7bd50a 2019-08-22 stsp int fd = -1;
3801 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3802 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3803 8e7bd50a 2019-08-22 stsp goto done;
3806 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3807 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3808 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3809 8e7bd50a 2019-08-22 stsp goto done;
3812 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3814 8e7bd50a 2019-08-22 stsp goto done;
3816 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3817 8e7bd50a 2019-08-22 stsp close(fd);
3819 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3821 8e7bd50a 2019-08-22 stsp goto done;
3822 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3824 8e7bd50a 2019-08-22 stsp free(initial_content);
3825 8e7bd50a 2019-08-22 stsp free(template);
3826 8e7bd50a 2019-08-22 stsp free(editor);
3828 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3829 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3830 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3831 8e7bd50a 2019-08-22 stsp if (err) {
3832 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3833 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3836 8e7bd50a 2019-08-22 stsp return err;
3839 8e7bd50a 2019-08-22 stsp static const struct got_error *
3840 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3841 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3843 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3844 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3845 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3846 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3847 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3848 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3849 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3852 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
3853 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3854 8e7bd50a 2019-08-22 stsp * an unintended typo.
3856 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
3857 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3859 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3861 8e7bd50a 2019-08-22 stsp return err;
3863 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
3864 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3866 8e7bd50a 2019-08-22 stsp goto done;
3868 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3870 8e7bd50a 2019-08-22 stsp goto done;
3872 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3873 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3874 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3875 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3876 8e7bd50a 2019-08-22 stsp goto done;
3878 8e7bd50a 2019-08-22 stsp tag_name += 10;
3879 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3880 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3881 8e7bd50a 2019-08-22 stsp goto done;
3884 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3885 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3886 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3887 8e7bd50a 2019-08-22 stsp goto done;
3888 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3889 8e7bd50a 2019-08-22 stsp goto done;
3891 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3892 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3893 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3894 f372d5cd 2019-10-21 stsp if (err) {
3895 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3896 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
3897 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3898 8e7bd50a 2019-08-22 stsp goto done;
3902 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3903 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3904 f372d5cd 2019-10-21 stsp if (err) {
3905 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3906 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3907 8e7bd50a 2019-08-22 stsp goto done;
3910 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3911 f372d5cd 2019-10-21 stsp if (err) {
3912 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3913 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3914 8e7bd50a 2019-08-22 stsp goto done;
3917 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3918 f372d5cd 2019-10-21 stsp if (err) {
3919 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3920 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3921 f372d5cd 2019-10-21 stsp goto done;
3924 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
3925 f372d5cd 2019-10-21 stsp if (err) {
3926 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3927 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3928 f372d5cd 2019-10-21 stsp goto done;
3930 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
3932 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
3933 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
3934 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
3935 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3936 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
3937 f372d5cd 2019-10-21 stsp free(tag_id_str);
3939 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
3940 8e7bd50a 2019-08-22 stsp free(commit_id);
3941 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3942 8e7bd50a 2019-08-22 stsp free(refname);
3943 8e7bd50a 2019-08-22 stsp free(tagmsg);
3944 f372d5cd 2019-10-21 stsp free(tagmsg_path);
3945 aba9c984 2019-09-08 stsp free(tagger);
3946 8e7bd50a 2019-08-22 stsp return err;
3949 8e7bd50a 2019-08-22 stsp static const struct got_error *
3950 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
3952 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
3953 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
3954 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
3955 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3956 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
3957 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3958 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
3960 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3961 8e7bd50a 2019-08-22 stsp switch (ch) {
3963 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
3966 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
3967 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3968 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3970 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
3973 8e7bd50a 2019-08-22 stsp do_list = 1;
3976 8e7bd50a 2019-08-22 stsp usage_tag();
3977 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
3981 8e7bd50a 2019-08-22 stsp argc -= optind;
3982 8e7bd50a 2019-08-22 stsp argv += optind;
3984 8e7bd50a 2019-08-22 stsp if (do_list) {
3985 8e7bd50a 2019-08-22 stsp if (tagmsg)
3986 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
3987 8e7bd50a 2019-08-22 stsp if (argc > 0)
3988 8e7bd50a 2019-08-22 stsp usage_tag();
3989 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
3990 8e7bd50a 2019-08-22 stsp usage_tag();
3991 a2887370 2019-08-23 stsp else if (argc > 1)
3992 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
3993 a2887370 2019-08-23 stsp tag_name = argv[0];
3995 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
3996 8e7bd50a 2019-08-22 stsp if (do_list) {
3997 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3998 8e7bd50a 2019-08-22 stsp NULL) == -1)
3999 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4001 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4002 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4003 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4006 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4007 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4008 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4009 8e7bd50a 2019-08-22 stsp goto done;
4012 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4013 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4014 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4015 8e7bd50a 2019-08-22 stsp goto done;
4017 8e7bd50a 2019-08-22 stsp error = NULL;
4018 8e7bd50a 2019-08-22 stsp if (worktree) {
4019 8e7bd50a 2019-08-22 stsp repo_path =
4020 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4021 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4022 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4023 8e7bd50a 2019-08-22 stsp if (error)
4024 8e7bd50a 2019-08-22 stsp goto done;
4026 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4027 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4028 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4029 8e7bd50a 2019-08-22 stsp goto done;
4034 8e7bd50a 2019-08-22 stsp if (do_list) {
4035 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4036 c9956ddf 2019-09-08 stsp if (error != NULL)
4037 c9956ddf 2019-09-08 stsp goto done;
4038 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4039 8e7bd50a 2019-08-22 stsp if (error)
4040 8e7bd50a 2019-08-22 stsp goto done;
4041 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4043 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4044 c9956ddf 2019-09-08 stsp if (error)
4045 c9956ddf 2019-09-08 stsp goto done;
4046 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4047 c9956ddf 2019-09-08 stsp if (error != NULL)
4048 c9956ddf 2019-09-08 stsp goto done;
4050 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4051 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4052 8e7bd50a 2019-08-22 stsp if (error)
4053 8e7bd50a 2019-08-22 stsp goto done;
4056 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4057 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4058 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4059 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4060 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4061 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4062 8e7bd50a 2019-08-22 stsp if (error)
4063 8e7bd50a 2019-08-22 stsp goto done;
4064 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4065 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4066 8e7bd50a 2019-08-22 stsp if (error)
4067 8e7bd50a 2019-08-22 stsp goto done;
4068 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4069 8e7bd50a 2019-08-22 stsp free(commit_id);
4070 8e7bd50a 2019-08-22 stsp if (error)
4071 8e7bd50a 2019-08-22 stsp goto done;
4074 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4075 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4079 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4080 8e7bd50a 2019-08-22 stsp if (worktree)
4081 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4082 8e7bd50a 2019-08-22 stsp free(cwd);
4083 8e7bd50a 2019-08-22 stsp free(repo_path);
4084 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4085 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4086 8e7bd50a 2019-08-22 stsp return error;
4089 8e7bd50a 2019-08-22 stsp __dead static void
4090 d00136be 2019-03-26 stsp usage_add(void)
4092 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4096 d00136be 2019-03-26 stsp static const struct got_error *
4097 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4099 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4101 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4102 4e68cba3 2019-11-23 stsp return NULL;
4105 4e68cba3 2019-11-23 stsp static const struct got_error *
4106 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4108 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4109 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4110 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4111 1dd54920 2019-05-11 stsp char *cwd = NULL;
4112 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4113 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4114 4e68cba3 2019-11-23 stsp int ch, can_recurse = 0;
4116 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4118 4e68cba3 2019-11-23 stsp while ((ch = getopt(argc, argv, "R")) != -1) {
4119 d00136be 2019-03-26 stsp switch (ch) {
4121 4e68cba3 2019-11-23 stsp can_recurse = 1;
4124 d00136be 2019-03-26 stsp usage_add();
4125 d00136be 2019-03-26 stsp /* NOTREACHED */
4129 d00136be 2019-03-26 stsp argc -= optind;
4130 d00136be 2019-03-26 stsp argv += optind;
4132 43012d58 2019-07-14 stsp #ifndef PROFILE
4133 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4134 43012d58 2019-07-14 stsp NULL) == -1)
4135 43012d58 2019-07-14 stsp err(1, "pledge");
4137 723c305c 2019-05-11 jcs if (argc < 1)
4138 d00136be 2019-03-26 stsp usage_add();
4140 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4141 d00136be 2019-03-26 stsp if (cwd == NULL) {
4142 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4143 d00136be 2019-03-26 stsp goto done;
4146 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4147 d00136be 2019-03-26 stsp if (error)
4148 d00136be 2019-03-26 stsp goto done;
4150 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4152 031a5338 2019-03-26 stsp if (error != NULL)
4153 031a5338 2019-03-26 stsp goto done;
4155 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4156 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4157 d00136be 2019-03-26 stsp if (error)
4158 d00136be 2019-03-26 stsp goto done;
4160 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4161 6d022e97 2019-08-04 stsp if (error)
4162 6d022e97 2019-08-04 stsp goto done;
4164 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4165 4e68cba3 2019-11-23 stsp char *ondisk_path;
4166 4e68cba3 2019-11-23 stsp struct stat sb;
4167 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4168 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4169 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4170 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4171 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4172 4e68cba3 2019-11-23 stsp goto done;
4174 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4175 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4176 4e68cba3 2019-11-23 stsp free(ondisk_path);
4179 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4180 4e68cba3 2019-11-23 stsp ondisk_path);
4181 4e68cba3 2019-11-23 stsp free(ondisk_path);
4182 4e68cba3 2019-11-23 stsp goto done;
4184 4e68cba3 2019-11-23 stsp free(ondisk_path);
4185 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4186 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4187 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4188 4e68cba3 2019-11-23 stsp goto done;
4192 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4193 1dd54920 2019-05-11 stsp NULL, repo);
4196 031a5338 2019-03-26 stsp got_repo_close(repo);
4197 d00136be 2019-03-26 stsp if (worktree)
4198 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4199 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4200 1dd54920 2019-05-11 stsp free((char *)pe->path);
4201 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4202 2ec1f75b 2019-03-26 stsp free(cwd);
4203 2ec1f75b 2019-03-26 stsp return error;
4206 2ec1f75b 2019-03-26 stsp __dead static void
4207 648e4ef7 2019-07-09 stsp usage_remove(void)
4209 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4213 2a06fe5f 2019-08-24 stsp static const struct got_error *
4214 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4215 2a06fe5f 2019-08-24 stsp unsigned char staged_status, const char *path,
4216 2a06fe5f 2019-08-24 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4217 2a06fe5f 2019-08-24 stsp struct got_object_id *commit_id)
4219 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4220 2a06fe5f 2019-08-24 stsp return NULL;
4221 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4222 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4223 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4224 2a06fe5f 2019-08-24 stsp return NULL;
4227 2ec1f75b 2019-03-26 stsp static const struct got_error *
4228 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4230 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4231 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4232 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4233 17ed4618 2019-06-02 stsp char *cwd = NULL;
4234 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4235 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4236 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
4238 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4240 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
4241 2ec1f75b 2019-03-26 stsp switch (ch) {
4243 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4246 2ec1f75b 2019-03-26 stsp usage_add();
4247 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4251 2ec1f75b 2019-03-26 stsp argc -= optind;
4252 2ec1f75b 2019-03-26 stsp argv += optind;
4254 43012d58 2019-07-14 stsp #ifndef PROFILE
4255 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4256 43012d58 2019-07-14 stsp NULL) == -1)
4257 43012d58 2019-07-14 stsp err(1, "pledge");
4259 17ed4618 2019-06-02 stsp if (argc < 1)
4260 648e4ef7 2019-07-09 stsp usage_remove();
4262 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4263 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4264 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4265 2ec1f75b 2019-03-26 stsp goto done;
4267 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4268 2ec1f75b 2019-03-26 stsp if (error)
4269 2ec1f75b 2019-03-26 stsp goto done;
4271 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4274 2ec1f75b 2019-03-26 stsp goto done;
4276 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4277 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4278 2ec1f75b 2019-03-26 stsp if (error)
4279 2ec1f75b 2019-03-26 stsp goto done;
4281 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4282 6d022e97 2019-08-04 stsp if (error)
4283 6d022e97 2019-08-04 stsp goto done;
4285 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4286 2a06fe5f 2019-08-24 stsp delete_local_mods, print_remove_status, NULL, repo);
4287 a129376b 2019-03-28 stsp if (error)
4288 a129376b 2019-03-28 stsp goto done;
4291 a129376b 2019-03-28 stsp got_repo_close(repo);
4292 a129376b 2019-03-28 stsp if (worktree)
4293 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4294 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4295 17ed4618 2019-06-02 stsp free((char *)pe->path);
4296 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4297 a129376b 2019-03-28 stsp free(cwd);
4298 a129376b 2019-03-28 stsp return error;
4301 a129376b 2019-03-28 stsp __dead static void
4302 a129376b 2019-03-28 stsp usage_revert(void)
4304 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4305 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4309 1ee397ad 2019-07-12 stsp static const struct got_error *
4310 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4312 a129376b 2019-03-28 stsp while (path[0] == '/')
4314 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4315 33aa809d 2019-08-08 stsp return NULL;
4318 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4319 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4320 33aa809d 2019-08-08 stsp const char *action;
4323 33aa809d 2019-08-08 stsp static const struct got_error *
4324 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4325 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4327 33aa809d 2019-08-08 stsp char *line = NULL;
4328 33aa809d 2019-08-08 stsp size_t linesize = 0;
4329 33aa809d 2019-08-08 stsp ssize_t linelen;
4331 33aa809d 2019-08-08 stsp switch (status) {
4332 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4333 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4335 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4336 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4338 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4339 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4340 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4341 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4342 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4343 33aa809d 2019-08-08 stsp printf("%s", line);
4344 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4345 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4346 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4347 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4348 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4351 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4354 33aa809d 2019-08-08 stsp return NULL;
4357 33aa809d 2019-08-08 stsp static const struct got_error *
4358 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4359 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4361 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4362 33aa809d 2019-08-08 stsp char *line = NULL;
4363 33aa809d 2019-08-08 stsp size_t linesize = 0;
4364 33aa809d 2019-08-08 stsp ssize_t linelen;
4365 33aa809d 2019-08-08 stsp int resp = ' ';
4366 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4368 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4370 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4372 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4373 33aa809d 2019-08-08 stsp a->action);
4375 33aa809d 2019-08-08 stsp return err;
4376 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4377 33aa809d 2019-08-08 stsp if (linelen == -1) {
4378 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4379 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4380 33aa809d 2019-08-08 stsp return NULL;
4382 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4384 33aa809d 2019-08-08 stsp *nl = '\0';
4385 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4386 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4387 33aa809d 2019-08-08 stsp printf("y\n");
4388 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4389 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4390 33aa809d 2019-08-08 stsp printf("n\n");
4391 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4392 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4393 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4394 33aa809d 2019-08-08 stsp printf("q\n");
4396 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4397 33aa809d 2019-08-08 stsp free(line);
4398 33aa809d 2019-08-08 stsp return NULL;
4401 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4402 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4403 33aa809d 2019-08-08 stsp a->action);
4405 33aa809d 2019-08-08 stsp return err;
4406 33aa809d 2019-08-08 stsp resp = getchar();
4407 33aa809d 2019-08-08 stsp if (resp == '\n')
4408 33aa809d 2019-08-08 stsp resp = getchar();
4409 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4410 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4411 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4412 33aa809d 2019-08-08 stsp resp = ' ';
4414 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4415 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4416 33aa809d 2019-08-08 stsp resp = ' ';
4420 33aa809d 2019-08-08 stsp if (resp == 'y')
4421 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4422 33aa809d 2019-08-08 stsp else if (resp == 'n')
4423 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4424 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4425 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4427 1ee397ad 2019-07-12 stsp return NULL;
4431 a129376b 2019-03-28 stsp static const struct got_error *
4432 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4434 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4435 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4436 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4437 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4438 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4439 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4440 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4441 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4442 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4443 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4445 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4447 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4448 a129376b 2019-03-28 stsp switch (ch) {
4450 33aa809d 2019-08-08 stsp pflag = 1;
4453 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4456 0f6d7415 2019-08-08 stsp can_recurse = 1;
4459 a129376b 2019-03-28 stsp usage_revert();
4460 a129376b 2019-03-28 stsp /* NOTREACHED */
4464 a129376b 2019-03-28 stsp argc -= optind;
4465 a129376b 2019-03-28 stsp argv += optind;
4467 43012d58 2019-07-14 stsp #ifndef PROFILE
4468 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4469 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4470 43012d58 2019-07-14 stsp err(1, "pledge");
4472 e20a8b6f 2019-06-04 stsp if (argc < 1)
4473 a129376b 2019-03-28 stsp usage_revert();
4474 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4475 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4477 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4478 a129376b 2019-03-28 stsp if (cwd == NULL) {
4479 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4480 a129376b 2019-03-28 stsp goto done;
4482 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4483 2ec1f75b 2019-03-26 stsp if (error)
4484 2ec1f75b 2019-03-26 stsp goto done;
4486 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4488 a129376b 2019-03-28 stsp if (error != NULL)
4489 a129376b 2019-03-28 stsp goto done;
4491 33aa809d 2019-08-08 stsp if (patch_script_path) {
4492 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4493 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4494 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4495 33aa809d 2019-08-08 stsp patch_script_path);
4496 33aa809d 2019-08-08 stsp goto done;
4499 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4500 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4501 a129376b 2019-03-28 stsp if (error)
4502 a129376b 2019-03-28 stsp goto done;
4504 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4505 6d022e97 2019-08-04 stsp if (error)
4506 6d022e97 2019-08-04 stsp goto done;
4508 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4509 0f6d7415 2019-08-08 stsp char *ondisk_path;
4510 0f6d7415 2019-08-08 stsp struct stat sb;
4511 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4512 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4513 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4514 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4515 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4516 0f6d7415 2019-08-08 stsp goto done;
4518 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4519 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4520 0f6d7415 2019-08-08 stsp free(ondisk_path);
4523 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4524 0f6d7415 2019-08-08 stsp ondisk_path);
4525 0f6d7415 2019-08-08 stsp free(ondisk_path);
4526 0f6d7415 2019-08-08 stsp goto done;
4528 0f6d7415 2019-08-08 stsp free(ondisk_path);
4529 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4530 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4531 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4532 0f6d7415 2019-08-08 stsp goto done;
4537 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4538 33aa809d 2019-08-08 stsp cpa.action = "revert";
4539 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4540 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4541 a129376b 2019-03-28 stsp if (error)
4542 a129376b 2019-03-28 stsp goto done;
4544 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4545 33aa809d 2019-08-08 stsp error == NULL)
4546 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4548 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4549 2ec1f75b 2019-03-26 stsp if (worktree)
4550 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4551 2ec1f75b 2019-03-26 stsp free(path);
4552 d00136be 2019-03-26 stsp free(cwd);
4553 6bad629b 2019-02-04 stsp return error;
4556 c4296144 2019-05-09 stsp __dead static void
4557 c4296144 2019-05-09 stsp usage_commit(void)
4559 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4560 5c1e53bc 2019-07-28 stsp getprogname());
4564 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4565 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4566 e2ba3d07 2019-05-13 stsp const char *editor;
4567 e0870e44 2019-05-13 stsp const char *worktree_path;
4568 76d98825 2019-06-03 stsp const char *branch_name;
4569 314a6357 2019-05-13 stsp const char *repo_path;
4570 e0870e44 2019-05-13 stsp char *logmsg_path;
4574 33ad4cbe 2019-05-12 jcs static const struct got_error *
4575 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4578 76d98825 2019-06-03 stsp char *initial_content = NULL;
4579 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4580 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4581 e0870e44 2019-05-13 stsp char *template = NULL;
4582 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4584 33ad4cbe 2019-05-12 jcs size_t len;
4586 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4587 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4588 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4589 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4590 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4591 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4592 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4593 33ad4cbe 2019-05-12 jcs return NULL;
4596 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4597 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4599 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4600 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4601 76d98825 2019-06-03 stsp a->branch_name) == -1)
4602 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4604 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4606 e0870e44 2019-05-13 stsp goto done;
4608 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4610 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4611 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4612 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4613 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4614 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4618 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4620 76d98825 2019-06-03 stsp free(initial_content);
4621 e0870e44 2019-05-13 stsp free(template);
4623 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4624 3ce1b845 2019-07-15 stsp if (err == NULL) {
4625 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4626 3ce1b845 2019-07-15 stsp if (err) {
4627 3ce1b845 2019-07-15 stsp free(*logmsg);
4628 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4631 33ad4cbe 2019-05-12 jcs return err;
4634 c4296144 2019-05-09 stsp static const struct got_error *
4635 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4637 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4638 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4639 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4640 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4641 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4642 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4643 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4644 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4645 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4646 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4648 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4649 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4651 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4652 c4296144 2019-05-09 stsp switch (ch) {
4654 c4296144 2019-05-09 stsp logmsg = optarg;
4657 c4296144 2019-05-09 stsp usage_commit();
4658 c4296144 2019-05-09 stsp /* NOTREACHED */
4662 c4296144 2019-05-09 stsp argc -= optind;
4663 c4296144 2019-05-09 stsp argv += optind;
4665 43012d58 2019-07-14 stsp #ifndef PROFILE
4666 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4667 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4668 43012d58 2019-07-14 stsp err(1, "pledge");
4670 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4671 c4296144 2019-05-09 stsp if (cwd == NULL) {
4672 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4673 c4296144 2019-05-09 stsp goto done;
4675 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4676 7d5807f4 2019-07-11 stsp if (error)
4677 7d5807f4 2019-07-11 stsp goto done;
4679 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4680 c4296144 2019-05-09 stsp if (error)
4681 a698f62e 2019-07-25 stsp goto done;
4682 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4683 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4684 c4296144 2019-05-09 stsp goto done;
4687 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4688 916f288c 2019-07-30 stsp worktree);
4689 5c1e53bc 2019-07-28 stsp if (error)
4690 5c1e53bc 2019-07-28 stsp goto done;
4692 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4693 c9956ddf 2019-09-08 stsp if (error)
4694 c9956ddf 2019-09-08 stsp goto done;
4695 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4696 c9956ddf 2019-09-08 stsp gitconfig_path);
4697 c4296144 2019-05-09 stsp if (error != NULL)
4698 c4296144 2019-05-09 stsp goto done;
4700 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4701 aba9c984 2019-09-08 stsp if (error)
4702 aba9c984 2019-09-08 stsp return error;
4705 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4706 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4708 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4709 314a6357 2019-05-13 stsp error = get_editor(&editor);
4711 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4712 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4713 c4296144 2019-05-09 stsp if (error)
4714 c4296144 2019-05-09 stsp goto done;
4716 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4717 087fb88c 2019-08-04 stsp if (error)
4718 087fb88c 2019-08-04 stsp goto done;
4720 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4721 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4722 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4723 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4724 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4725 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4726 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4727 916f288c 2019-07-30 stsp goto done;
4729 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4731 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4732 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4733 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4734 e0870e44 2019-05-13 stsp if (error) {
4735 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4736 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4737 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4738 c4296144 2019-05-09 stsp goto done;
4741 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4742 c4296144 2019-05-09 stsp if (error)
4743 c4296144 2019-05-09 stsp goto done;
4744 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4746 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4747 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4748 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4749 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4750 7266f21f 2019-10-21 stsp error == NULL)
4751 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4752 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4754 c4296144 2019-05-09 stsp got_repo_close(repo);
4755 c4296144 2019-05-09 stsp if (worktree)
4756 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4757 c4296144 2019-05-09 stsp free(cwd);
4758 c4296144 2019-05-09 stsp free(id_str);
4759 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4760 e2ba3d07 2019-05-13 stsp free(editor);
4761 aba9c984 2019-09-08 stsp free(author);
4762 234035bc 2019-06-01 stsp return error;
4765 234035bc 2019-06-01 stsp __dead static void
4766 234035bc 2019-06-01 stsp usage_cherrypick(void)
4768 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4772 234035bc 2019-06-01 stsp static const struct got_error *
4773 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4775 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4776 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4777 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4778 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4779 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4780 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4781 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4782 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4783 234035bc 2019-06-01 stsp int ch, did_something = 0;
4785 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4786 234035bc 2019-06-01 stsp switch (ch) {
4788 234035bc 2019-06-01 stsp usage_cherrypick();
4789 234035bc 2019-06-01 stsp /* NOTREACHED */
4793 234035bc 2019-06-01 stsp argc -= optind;
4794 234035bc 2019-06-01 stsp argv += optind;
4796 43012d58 2019-07-14 stsp #ifndef PROFILE
4797 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4798 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4799 43012d58 2019-07-14 stsp err(1, "pledge");
4801 234035bc 2019-06-01 stsp if (argc != 1)
4802 234035bc 2019-06-01 stsp usage_cherrypick();
4804 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4805 234035bc 2019-06-01 stsp if (cwd == NULL) {
4806 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4807 234035bc 2019-06-01 stsp goto done;
4809 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4810 234035bc 2019-06-01 stsp if (error)
4811 234035bc 2019-06-01 stsp goto done;
4813 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4815 234035bc 2019-06-01 stsp if (error != NULL)
4816 234035bc 2019-06-01 stsp goto done;
4818 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4819 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4820 234035bc 2019-06-01 stsp if (error)
4821 234035bc 2019-06-01 stsp goto done;
4823 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4824 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4825 234035bc 2019-06-01 stsp if (error != NULL) {
4826 234035bc 2019-06-01 stsp struct got_reference *ref;
4827 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4828 234035bc 2019-06-01 stsp goto done;
4829 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4830 234035bc 2019-06-01 stsp if (error != NULL)
4831 234035bc 2019-06-01 stsp goto done;
4832 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4833 234035bc 2019-06-01 stsp got_ref_close(ref);
4834 234035bc 2019-06-01 stsp if (error != NULL)
4835 234035bc 2019-06-01 stsp goto done;
4837 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4838 234035bc 2019-06-01 stsp if (error)
4839 234035bc 2019-06-01 stsp goto done;
4841 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4842 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4843 234035bc 2019-06-01 stsp if (error != NULL)
4844 234035bc 2019-06-01 stsp goto done;
4846 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4847 234035bc 2019-06-01 stsp if (error) {
4848 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4849 234035bc 2019-06-01 stsp goto done;
4850 234035bc 2019-06-01 stsp error = NULL;
4852 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4853 234035bc 2019-06-01 stsp goto done;
4856 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4857 234035bc 2019-06-01 stsp if (error)
4858 234035bc 2019-06-01 stsp goto done;
4859 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4860 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4861 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4863 234035bc 2019-06-01 stsp if (error != NULL)
4864 234035bc 2019-06-01 stsp goto done;
4866 234035bc 2019-06-01 stsp if (did_something)
4867 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4869 234035bc 2019-06-01 stsp if (commit)
4870 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4871 234035bc 2019-06-01 stsp free(commit_id_str);
4872 234035bc 2019-06-01 stsp if (head_ref)
4873 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4874 234035bc 2019-06-01 stsp if (worktree)
4875 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4877 234035bc 2019-06-01 stsp got_repo_close(repo);
4878 c4296144 2019-05-09 stsp return error;
4881 5ef14e63 2019-06-02 stsp __dead static void
4882 5ef14e63 2019-06-02 stsp usage_backout(void)
4884 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4888 5ef14e63 2019-06-02 stsp static const struct got_error *
4889 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4891 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4892 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4893 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4894 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4895 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4896 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4897 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4898 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4899 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4901 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4902 5ef14e63 2019-06-02 stsp switch (ch) {
4904 5ef14e63 2019-06-02 stsp usage_backout();
4905 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4909 5ef14e63 2019-06-02 stsp argc -= optind;
4910 5ef14e63 2019-06-02 stsp argv += optind;
4912 43012d58 2019-07-14 stsp #ifndef PROFILE
4913 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4914 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4915 43012d58 2019-07-14 stsp err(1, "pledge");
4917 5ef14e63 2019-06-02 stsp if (argc != 1)
4918 5ef14e63 2019-06-02 stsp usage_backout();
4920 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4921 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4922 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4923 5ef14e63 2019-06-02 stsp goto done;
4925 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4926 5ef14e63 2019-06-02 stsp if (error)
4927 5ef14e63 2019-06-02 stsp goto done;
4929 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4931 5ef14e63 2019-06-02 stsp if (error != NULL)
4932 5ef14e63 2019-06-02 stsp goto done;
4934 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4935 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4936 5ef14e63 2019-06-02 stsp if (error)
4937 5ef14e63 2019-06-02 stsp goto done;
4939 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4940 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4941 5ef14e63 2019-06-02 stsp if (error != NULL) {
4942 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4943 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4944 5ef14e63 2019-06-02 stsp goto done;
4945 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4946 5ef14e63 2019-06-02 stsp if (error != NULL)
4947 5ef14e63 2019-06-02 stsp goto done;
4948 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4949 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4950 5ef14e63 2019-06-02 stsp if (error != NULL)
4951 5ef14e63 2019-06-02 stsp goto done;
4953 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4954 5ef14e63 2019-06-02 stsp if (error)
4955 5ef14e63 2019-06-02 stsp goto done;
4957 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4958 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4959 5ef14e63 2019-06-02 stsp if (error != NULL)
4960 5ef14e63 2019-06-02 stsp goto done;
4962 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4963 5ef14e63 2019-06-02 stsp if (error)
4964 5ef14e63 2019-06-02 stsp goto done;
4966 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4967 5ef14e63 2019-06-02 stsp if (error)
4968 5ef14e63 2019-06-02 stsp goto done;
4969 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4970 5ef14e63 2019-06-02 stsp if (pid == NULL) {
4971 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
4972 5ef14e63 2019-06-02 stsp goto done;
4975 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4976 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
4977 5ef14e63 2019-06-02 stsp if (error != NULL)
4978 5ef14e63 2019-06-02 stsp goto done;
4980 5ef14e63 2019-06-02 stsp if (did_something)
4981 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
4983 5ef14e63 2019-06-02 stsp if (commit)
4984 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
4985 5ef14e63 2019-06-02 stsp free(commit_id_str);
4986 5ef14e63 2019-06-02 stsp if (head_ref)
4987 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
4988 818c7501 2019-07-11 stsp if (worktree)
4989 818c7501 2019-07-11 stsp got_worktree_close(worktree);
4991 818c7501 2019-07-11 stsp got_repo_close(repo);
4992 818c7501 2019-07-11 stsp return error;
4995 818c7501 2019-07-11 stsp __dead static void
4996 818c7501 2019-07-11 stsp usage_rebase(void)
4998 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4999 af54c8f8 2019-07-11 stsp getprogname());
5004 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5007 0ebf8283 2019-07-24 stsp size_t len;
5009 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5010 0ebf8283 2019-07-24 stsp if (len > limit)
5011 0ebf8283 2019-07-24 stsp len = limit;
5012 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5013 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5015 0ebf8283 2019-07-24 stsp *nl = '\0';
5018 0ebf8283 2019-07-24 stsp static const struct got_error *
5019 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5021 5943eee2 2019-08-13 stsp const struct got_error *err;
5022 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5023 5943eee2 2019-08-13 stsp const char *s;
5025 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5027 5943eee2 2019-08-13 stsp return err;
5029 5943eee2 2019-08-13 stsp s = logmsg0;
5030 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5033 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5034 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5035 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5036 5943eee2 2019-08-13 stsp goto done;
5039 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5041 5943eee2 2019-08-13 stsp free(logmsg0);
5042 5943eee2 2019-08-13 stsp return err;
5045 818c7501 2019-07-11 stsp static const struct got_error *
5046 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5047 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5049 818c7501 2019-07-11 stsp const struct got_error *err;
5050 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5052 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5054 818c7501 2019-07-11 stsp goto done;
5056 ff0d2220 2019-07-11 stsp if (new_id) {
5057 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5059 ff0d2220 2019-07-11 stsp goto done;
5062 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5063 ff0d2220 2019-07-11 stsp if (new_id_str)
5064 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5066 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5068 0ebf8283 2019-07-24 stsp goto done;
5070 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5071 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5073 818c7501 2019-07-11 stsp free(old_id_str);
5074 818c7501 2019-07-11 stsp free(new_id_str);
5075 818c7501 2019-07-11 stsp return err;
5078 1ee397ad 2019-07-12 stsp static const struct got_error *
5079 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5081 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5083 818c7501 2019-07-11 stsp while (path[0] == '/')
5085 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5087 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5088 1ee397ad 2019-07-12 stsp return NULL;
5089 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5090 818c7501 2019-07-11 stsp *rebase_status = status;
5091 1ee397ad 2019-07-12 stsp return NULL;
5094 818c7501 2019-07-11 stsp static const struct got_error *
5095 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5096 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5097 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5099 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5100 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5101 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5104 818c7501 2019-07-11 stsp static const struct got_error *
5105 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5106 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5107 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5108 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5110 ff0d2220 2019-07-11 stsp const struct got_error *error;
5111 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5112 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5114 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5115 ff0d2220 2019-07-11 stsp if (error)
5116 ff0d2220 2019-07-11 stsp return error;
5118 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5119 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5120 ff0d2220 2019-07-11 stsp if (error) {
5121 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5122 ff0d2220 2019-07-11 stsp goto done;
5123 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5125 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5126 ff0d2220 2019-07-11 stsp free(new_commit_id);
5129 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5130 ff0d2220 2019-07-11 stsp return error;
5133 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5134 64c6d990 2019-07-11 stsp const char *path_prefix;
5135 64c6d990 2019-07-11 stsp size_t len;
5136 8ca9bd68 2019-07-25 stsp int errcode;
5139 64c6d990 2019-07-11 stsp static const struct got_error *
5140 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5141 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5142 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5143 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5145 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5147 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5148 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5149 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5151 64c6d990 2019-07-11 stsp return NULL;
5154 64c6d990 2019-07-11 stsp static const struct got_error *
5155 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5156 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5157 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5159 64c6d990 2019-07-11 stsp const struct got_error *err;
5160 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5161 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5162 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5164 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5165 64c6d990 2019-07-11 stsp return NULL;
5167 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5169 64c6d990 2019-07-11 stsp goto done;
5171 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5173 64c6d990 2019-07-11 stsp goto done;
5175 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5176 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5178 64c6d990 2019-07-11 stsp goto done;
5180 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5181 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5183 64c6d990 2019-07-11 stsp goto done;
5185 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5186 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5187 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5188 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5189 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5190 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5191 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5193 64c6d990 2019-07-11 stsp if (tree1)
5194 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5195 64c6d990 2019-07-11 stsp if (tree2)
5196 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5197 64c6d990 2019-07-11 stsp if (commit)
5198 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5199 64c6d990 2019-07-11 stsp if (parent_commit)
5200 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5201 64c6d990 2019-07-11 stsp return err;
5204 ff0d2220 2019-07-11 stsp static const struct got_error *
5205 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5206 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5207 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5208 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5209 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5211 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5212 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5213 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5214 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5215 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5217 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5219 af61c510 2019-07-19 stsp return err;
5221 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5222 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5224 af61c510 2019-07-19 stsp goto done;
5225 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5226 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
5227 af61c510 2019-07-19 stsp if (err) {
5228 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5229 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5230 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5231 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5232 af61c510 2019-07-19 stsp "been reached?!?");
5233 af61c510 2019-07-19 stsp goto done;
5234 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5235 af61c510 2019-07-19 stsp goto done;
5236 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
5237 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5239 af61c510 2019-07-19 stsp goto done;
5241 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5242 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5244 af61c510 2019-07-19 stsp goto done;
5246 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5248 af61c510 2019-07-19 stsp goto done;
5249 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5250 af61c510 2019-07-19 stsp commit_id = parent_id;
5254 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5255 af61c510 2019-07-19 stsp return err;
5258 af61c510 2019-07-19 stsp static const struct got_error *
5259 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5261 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5262 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5263 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5264 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5265 818c7501 2019-07-11 stsp char *cwd = NULL;
5266 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5267 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5268 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5269 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5270 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5271 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5272 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5273 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5274 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5275 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5276 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5277 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5279 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5280 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5282 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5283 818c7501 2019-07-11 stsp switch (ch) {
5285 818c7501 2019-07-11 stsp abort_rebase = 1;
5288 818c7501 2019-07-11 stsp continue_rebase = 1;
5291 818c7501 2019-07-11 stsp usage_rebase();
5292 818c7501 2019-07-11 stsp /* NOTREACHED */
5296 818c7501 2019-07-11 stsp argc -= optind;
5297 818c7501 2019-07-11 stsp argv += optind;
5299 43012d58 2019-07-14 stsp #ifndef PROFILE
5300 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5301 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5302 43012d58 2019-07-14 stsp err(1, "pledge");
5304 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5305 818c7501 2019-07-11 stsp usage_rebase();
5306 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5307 818c7501 2019-07-11 stsp if (argc != 0)
5308 818c7501 2019-07-11 stsp usage_rebase();
5309 818c7501 2019-07-11 stsp } else if (argc != 1)
5310 818c7501 2019-07-11 stsp usage_rebase();
5312 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5313 818c7501 2019-07-11 stsp if (cwd == NULL) {
5314 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5315 818c7501 2019-07-11 stsp goto done;
5317 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5318 818c7501 2019-07-11 stsp if (error)
5319 818c7501 2019-07-11 stsp goto done;
5321 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5323 818c7501 2019-07-11 stsp if (error != NULL)
5324 818c7501 2019-07-11 stsp goto done;
5326 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5327 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5328 818c7501 2019-07-11 stsp if (error)
5329 818c7501 2019-07-11 stsp goto done;
5331 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5332 818c7501 2019-07-11 stsp if (error)
5333 818c7501 2019-07-11 stsp goto done;
5335 f6794adc 2019-07-23 stsp if (abort_rebase) {
5336 818c7501 2019-07-11 stsp int did_something;
5337 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5338 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5339 f6794adc 2019-07-23 stsp goto done;
5341 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5342 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5343 3e3a69f1 2019-07-25 stsp worktree, repo);
5344 818c7501 2019-07-11 stsp if (error)
5345 818c7501 2019-07-11 stsp goto done;
5346 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5347 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5348 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5349 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5350 818c7501 2019-07-11 stsp if (error)
5351 818c7501 2019-07-11 stsp goto done;
5352 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5353 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5356 818c7501 2019-07-11 stsp if (continue_rebase) {
5357 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5358 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5359 f6794adc 2019-07-23 stsp goto done;
5361 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5362 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5363 3e3a69f1 2019-07-25 stsp worktree, repo);
5364 818c7501 2019-07-11 stsp if (error)
5365 818c7501 2019-07-11 stsp goto done;
5367 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5368 01757395 2019-07-12 stsp resume_commit_id, repo);
5369 818c7501 2019-07-11 stsp if (error)
5370 818c7501 2019-07-11 stsp goto done;
5372 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5373 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5374 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5375 818c7501 2019-07-11 stsp goto done;
5378 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5379 818c7501 2019-07-11 stsp if (error != NULL)
5380 818c7501 2019-07-11 stsp goto done;
5383 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5384 ff0d2220 2019-07-11 stsp if (error)
5385 ff0d2220 2019-07-11 stsp goto done;
5387 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5388 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5390 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5391 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5392 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5393 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5394 818c7501 2019-07-11 stsp if (error)
5395 818c7501 2019-07-11 stsp goto done;
5396 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5397 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5398 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5399 818c7501 2019-07-11 stsp "with work tree's branch");
5400 818c7501 2019-07-11 stsp goto done;
5403 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5404 a51a74b3 2019-07-27 stsp if (error) {
5405 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5406 a51a74b3 2019-07-27 stsp goto done;
5407 a51a74b3 2019-07-27 stsp error = NULL;
5409 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5410 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5411 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5412 a51a74b3 2019-07-27 stsp goto done;
5414 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5415 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5416 818c7501 2019-07-11 stsp if (error)
5417 818c7501 2019-07-11 stsp goto done;
5420 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5421 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5422 818c7501 2019-07-11 stsp if (error)
5423 818c7501 2019-07-11 stsp goto done;
5425 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5426 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5427 fc66b545 2019-08-12 stsp if (pid == NULL) {
5428 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5429 fc66b545 2019-08-12 stsp int did_something;
5430 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5431 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5432 fc66b545 2019-08-12 stsp &did_something);
5433 fc66b545 2019-08-12 stsp if (error)
5434 fc66b545 2019-08-12 stsp goto done;
5435 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5436 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5438 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5439 fc66b545 2019-08-12 stsp goto done;
5441 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5442 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5443 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5444 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5445 818c7501 2019-07-11 stsp commit = NULL;
5446 818c7501 2019-07-11 stsp if (error)
5447 818c7501 2019-07-11 stsp goto done;
5449 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5450 ff0d2220 2019-07-11 stsp if (continue_rebase)
5451 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5452 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5454 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5455 818c7501 2019-07-11 stsp goto done;
5458 818c7501 2019-07-11 stsp pid = NULL;
5459 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5460 818c7501 2019-07-11 stsp commit_id = qid->id;
5461 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5462 818c7501 2019-07-11 stsp pid = qid;
5464 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5465 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5466 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5467 818c7501 2019-07-11 stsp if (error)
5468 818c7501 2019-07-11 stsp goto done;
5470 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5471 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5475 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5476 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5477 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5478 818c7501 2019-07-11 stsp if (error)
5479 818c7501 2019-07-11 stsp goto done;
5482 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5483 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5484 818c7501 2019-07-11 stsp if (error)
5485 818c7501 2019-07-11 stsp goto done;
5486 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5487 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5489 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5490 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5492 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5493 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5494 818c7501 2019-07-11 stsp free(resume_commit_id);
5495 818c7501 2019-07-11 stsp free(yca_id);
5496 818c7501 2019-07-11 stsp if (commit)
5497 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5498 818c7501 2019-07-11 stsp if (branch)
5499 818c7501 2019-07-11 stsp got_ref_close(branch);
5500 818c7501 2019-07-11 stsp if (new_base_branch)
5501 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5502 818c7501 2019-07-11 stsp if (tmp_branch)
5503 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5504 5ef14e63 2019-06-02 stsp if (worktree)
5505 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5507 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5508 5ef14e63 2019-06-02 stsp return error;
5511 0ebf8283 2019-07-24 stsp __dead static void
5512 0ebf8283 2019-07-24 stsp usage_histedit(void)
5514 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5515 0ebf8283 2019-07-24 stsp getprogname());
5519 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5520 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5521 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5522 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5523 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5525 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5526 0ebf8283 2019-07-24 stsp unsigned char code;
5527 0ebf8283 2019-07-24 stsp const char *name;
5528 0ebf8283 2019-07-24 stsp const char *desc;
5529 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5530 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5531 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5532 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5533 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5534 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5535 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5538 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5539 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5540 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5541 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5542 0ebf8283 2019-07-24 stsp char *logmsg;
5544 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5546 0ebf8283 2019-07-24 stsp static const struct got_error *
5547 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5548 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5550 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5551 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5552 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5555 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5557 0ebf8283 2019-07-24 stsp goto done;
5559 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5561 0ebf8283 2019-07-24 stsp goto done;
5563 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5565 0ebf8283 2019-07-24 stsp goto done;
5567 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5568 0ebf8283 2019-07-24 stsp if (n < 0)
5569 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5571 0ebf8283 2019-07-24 stsp if (commit)
5572 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5573 0ebf8283 2019-07-24 stsp free(id_str);
5574 0ebf8283 2019-07-24 stsp free(logmsg);
5575 0ebf8283 2019-07-24 stsp return err;
5578 0ebf8283 2019-07-24 stsp static const struct got_error *
5579 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5580 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5582 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5583 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5585 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5586 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5588 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5589 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5595 0ebf8283 2019-07-24 stsp return err;
5598 0ebf8283 2019-07-24 stsp static const struct got_error *
5599 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5601 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5604 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5605 0ebf8283 2019-07-24 stsp if (n < 0)
5606 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5608 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5609 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5610 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5611 0ebf8283 2019-07-24 stsp cmd->desc);
5612 0ebf8283 2019-07-24 stsp if (n < 0) {
5613 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5617 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5618 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5619 0ebf8283 2019-07-24 stsp if (n < 0)
5620 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5621 0ebf8283 2019-07-24 stsp return err;
5624 0ebf8283 2019-07-24 stsp static const struct got_error *
5625 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5627 0ebf8283 2019-07-24 stsp static char msg[42];
5630 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5632 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5633 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5635 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5638 0ebf8283 2019-07-24 stsp static const struct got_error *
5639 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5640 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5642 0ebf8283 2019-07-24 stsp const struct got_error *err;
5643 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5644 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5646 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5648 0ebf8283 2019-07-24 stsp return err;
5650 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5652 0ebf8283 2019-07-24 stsp goto done;
5654 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5656 5943eee2 2019-08-13 stsp goto done;
5657 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5658 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5659 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5660 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5661 0ebf8283 2019-07-24 stsp goto done;
5664 0ebf8283 2019-07-24 stsp if (folded_commit)
5665 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5666 0ebf8283 2019-07-24 stsp free(id_str);
5667 5943eee2 2019-08-13 stsp free(folded_logmsg);
5668 0ebf8283 2019-07-24 stsp return err;
5671 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5672 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5674 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5676 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5677 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5678 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5679 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5680 3f9de99f 2019-07-24 stsp folded = prev;
5681 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5684 0ebf8283 2019-07-24 stsp return folded;
5687 0ebf8283 2019-07-24 stsp static const struct got_error *
5688 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5689 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5691 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5692 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5693 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5694 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5696 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5698 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5700 0ebf8283 2019-07-24 stsp return err;
5702 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5703 0ebf8283 2019-07-24 stsp if (folded) {
5704 0ebf8283 2019-07-24 stsp while (folded != hle) {
5705 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5706 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5709 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5710 0ebf8283 2019-07-24 stsp logmsg, repo);
5712 0ebf8283 2019-07-24 stsp goto done;
5713 0ebf8283 2019-07-24 stsp free(logmsg);
5714 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5715 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5719 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5721 0ebf8283 2019-07-24 stsp goto done;
5722 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5724 5943eee2 2019-08-13 stsp goto done;
5725 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5726 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5727 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5728 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5729 0ebf8283 2019-07-24 stsp goto done;
5731 0ebf8283 2019-07-24 stsp free(logmsg);
5732 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5734 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5736 0ebf8283 2019-07-24 stsp goto done;
5738 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5740 0ebf8283 2019-07-24 stsp goto done;
5742 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5743 0ebf8283 2019-07-24 stsp close(fd);
5745 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5747 0ebf8283 2019-07-24 stsp goto done;
5749 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5750 0ebf8283 2019-07-24 stsp if (err) {
5751 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5752 0ebf8283 2019-07-24 stsp goto done;
5753 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5756 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5757 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5758 0ebf8283 2019-07-24 stsp free(logmsg_path);
5759 0ebf8283 2019-07-24 stsp free(logmsg);
5760 5943eee2 2019-08-13 stsp free(orig_logmsg);
5761 0ebf8283 2019-07-24 stsp free(editor);
5762 0ebf8283 2019-07-24 stsp if (commit)
5763 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5764 0ebf8283 2019-07-24 stsp return err;
5767 0ebf8283 2019-07-24 stsp static const struct got_error *
5768 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5769 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5771 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5772 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5773 0ebf8283 2019-07-24 stsp size_t size;
5774 0ebf8283 2019-07-24 stsp ssize_t len;
5775 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5776 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5777 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5778 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5780 0ebf8283 2019-07-24 stsp for (;;) {
5781 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5782 0ebf8283 2019-07-24 stsp if (len == -1) {
5783 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5784 0ebf8283 2019-07-24 stsp if (feof(f))
5786 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5787 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5792 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5794 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5795 0ebf8283 2019-07-24 stsp free(line);
5796 0ebf8283 2019-07-24 stsp line = NULL;
5799 0ebf8283 2019-07-24 stsp cmd = NULL;
5800 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5801 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5802 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5803 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5804 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5807 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5812 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5813 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5816 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5818 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5819 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5820 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5823 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5824 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5828 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5829 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5830 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5834 0ebf8283 2019-07-24 stsp free(line);
5835 0ebf8283 2019-07-24 stsp line = NULL;
5839 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5841 0ebf8283 2019-07-24 stsp *end = '\0';
5843 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5844 0ebf8283 2019-07-24 stsp if (err) {
5845 0ebf8283 2019-07-24 stsp /* override error code */
5846 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5850 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
5851 0ebf8283 2019-07-24 stsp if (hle == NULL) {
5852 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
5855 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5856 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
5857 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
5858 0ebf8283 2019-07-24 stsp commit_id = NULL;
5859 0ebf8283 2019-07-24 stsp free(line);
5860 0ebf8283 2019-07-24 stsp line = NULL;
5861 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5864 0ebf8283 2019-07-24 stsp free(line);
5865 0ebf8283 2019-07-24 stsp free(commit_id);
5866 0ebf8283 2019-07-24 stsp return err;
5869 0ebf8283 2019-07-24 stsp static const struct got_error *
5870 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5871 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5873 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5874 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5875 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5876 bfce7f83 2019-07-27 stsp static char msg[80];
5877 bfce7f83 2019-07-27 stsp char *id_str;
5879 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5880 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5881 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5882 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5883 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5885 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5886 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5887 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5890 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5891 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5893 bfce7f83 2019-07-27 stsp return err;
5894 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5895 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5896 bfce7f83 2019-07-27 stsp free(id_str);
5897 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5901 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5902 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5903 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5904 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5906 bfce7f83 2019-07-27 stsp return NULL;
5909 bfce7f83 2019-07-27 stsp static const struct got_error *
5910 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5911 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5912 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5914 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5915 0ebf8283 2019-07-24 stsp char *editor;
5916 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5918 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5920 0ebf8283 2019-07-24 stsp return err;
5922 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5923 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5924 0ebf8283 2019-07-24 stsp goto done;
5927 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5928 0ebf8283 2019-07-24 stsp if (f == NULL) {
5929 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5930 0ebf8283 2019-07-24 stsp goto done;
5932 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5934 bfce7f83 2019-07-27 stsp goto done;
5936 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5938 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5939 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5940 0ebf8283 2019-07-24 stsp free(editor);
5941 0ebf8283 2019-07-24 stsp return err;
5944 0ebf8283 2019-07-24 stsp static const struct got_error *
5945 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5946 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5948 0ebf8283 2019-07-24 stsp static const struct got_error *
5949 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5950 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5952 0ebf8283 2019-07-24 stsp const struct got_error *err;
5953 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5954 0ebf8283 2019-07-24 stsp char *path = NULL;
5956 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
5958 0ebf8283 2019-07-24 stsp return err;
5960 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
5962 0ebf8283 2019-07-24 stsp goto done;
5964 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
5966 0ebf8283 2019-07-24 stsp goto done;
5968 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
5969 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5970 0ebf8283 2019-07-24 stsp goto done;
5974 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
5975 0ebf8283 2019-07-24 stsp if (err) {
5976 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5977 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5978 0ebf8283 2019-07-24 stsp goto done;
5979 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
5980 0ebf8283 2019-07-24 stsp commits, path, repo);
5983 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5984 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5985 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
5986 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
5987 0ebf8283 2019-07-24 stsp free(path);
5988 0ebf8283 2019-07-24 stsp return err;
5991 0ebf8283 2019-07-24 stsp static const struct got_error *
5992 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
5993 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5995 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5996 0ebf8283 2019-07-24 stsp char *path = NULL;
5997 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5998 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5999 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6001 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6003 0ebf8283 2019-07-24 stsp return err;
6005 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6006 0ebf8283 2019-07-24 stsp if (f == NULL) {
6007 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6008 0ebf8283 2019-07-24 stsp goto done;
6010 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6011 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6016 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6017 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6018 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6019 0ebf8283 2019-07-24 stsp if (n < 0) {
6020 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6026 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6027 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6028 0ebf8283 2019-07-24 stsp free(path);
6029 0ebf8283 2019-07-24 stsp if (commit)
6030 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6031 0ebf8283 2019-07-24 stsp return err;
6035 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6037 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6039 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6040 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6041 bfce7f83 2019-07-27 stsp free(hle);
6045 0ebf8283 2019-07-24 stsp static const struct got_error *
6046 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6047 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6049 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6050 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6052 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6053 0ebf8283 2019-07-24 stsp if (f == NULL) {
6054 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6055 0ebf8283 2019-07-24 stsp goto done;
6058 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6060 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6061 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6062 0ebf8283 2019-07-24 stsp return err;
6065 0ebf8283 2019-07-24 stsp static const struct got_error *
6066 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6067 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6068 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6070 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6071 0ebf8283 2019-07-24 stsp int resp = ' ';
6073 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6074 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6075 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6076 0ebf8283 2019-07-24 stsp resp = getchar();
6077 a61a4414 2019-08-07 stsp if (resp == '\n')
6078 a61a4414 2019-08-07 stsp resp = getchar();
6079 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6080 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6081 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6083 426ebf2e 2019-07-25 stsp if (err) {
6084 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6085 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6087 bfce7f83 2019-07-27 stsp prev_err = err;
6088 426ebf2e 2019-07-25 stsp resp = ' ';
6092 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6093 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6094 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6095 0ebf8283 2019-07-24 stsp commits, repo);
6096 426ebf2e 2019-07-25 stsp if (err) {
6097 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6098 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6100 bfce7f83 2019-07-27 stsp prev_err = err;
6101 426ebf2e 2019-07-25 stsp resp = ' ';
6105 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6106 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6109 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6112 0ebf8283 2019-07-24 stsp return err;
6115 0ebf8283 2019-07-24 stsp static const struct got_error *
6116 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6117 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6118 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6120 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6121 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6122 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6123 3e3a69f1 2019-07-25 stsp branch, repo);
6126 0ebf8283 2019-07-24 stsp static const struct got_error *
6127 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6128 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6130 0ebf8283 2019-07-24 stsp const struct got_error *err;
6131 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6133 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6135 0ebf8283 2019-07-24 stsp goto done;
6137 0ebf8283 2019-07-24 stsp if (new_id) {
6138 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6140 0ebf8283 2019-07-24 stsp goto done;
6143 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6144 0ebf8283 2019-07-24 stsp if (new_id_str)
6145 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6147 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6148 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6149 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6150 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6151 0ebf8283 2019-07-24 stsp goto done;
6153 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6155 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6157 0ebf8283 2019-07-24 stsp goto done;
6160 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6161 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6162 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6163 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6164 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6166 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6167 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6168 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6176 0ebf8283 2019-07-24 stsp free(old_id_str);
6177 0ebf8283 2019-07-24 stsp free(new_id_str);
6178 0ebf8283 2019-07-24 stsp return err;
6181 0ebf8283 2019-07-24 stsp static const struct got_error *
6182 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6183 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6184 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6185 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6187 0ebf8283 2019-07-24 stsp const struct got_error *err;
6188 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6189 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6191 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6192 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6193 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6195 0ebf8283 2019-07-24 stsp return err;
6198 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6200 0ebf8283 2019-07-24 stsp return err;
6202 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6203 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6204 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6205 0ebf8283 2019-07-24 stsp if (err) {
6206 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6207 0ebf8283 2019-07-24 stsp goto done;
6208 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6210 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6211 0ebf8283 2019-07-24 stsp free(new_commit_id);
6214 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6215 0ebf8283 2019-07-24 stsp return err;
6218 0ebf8283 2019-07-24 stsp static const struct got_error *
6219 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6220 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6222 0ebf8283 2019-07-24 stsp const struct got_error *error;
6223 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6225 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6227 0ebf8283 2019-07-24 stsp if (error)
6228 0ebf8283 2019-07-24 stsp return error;
6230 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6231 0ebf8283 2019-07-24 stsp if (error)
6232 0ebf8283 2019-07-24 stsp return error;
6234 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6235 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6236 0ebf8283 2019-07-24 stsp return error;
6239 0ebf8283 2019-07-24 stsp static const struct got_error *
6240 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6242 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6243 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6244 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6245 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6246 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6247 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6248 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6249 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6250 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6251 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6252 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6253 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6254 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6255 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6256 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6257 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6258 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6259 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6260 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6261 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6262 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6264 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6265 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6266 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6268 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6269 0ebf8283 2019-07-24 stsp switch (ch) {
6271 0ebf8283 2019-07-24 stsp abort_edit = 1;
6274 0ebf8283 2019-07-24 stsp continue_edit = 1;
6277 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6280 0ebf8283 2019-07-24 stsp usage_histedit();
6281 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6285 0ebf8283 2019-07-24 stsp argc -= optind;
6286 0ebf8283 2019-07-24 stsp argv += optind;
6288 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6289 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6290 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6291 0ebf8283 2019-07-24 stsp err(1, "pledge");
6293 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6294 0ebf8283 2019-07-24 stsp usage_histedit();
6295 0ebf8283 2019-07-24 stsp if (argc != 0)
6296 0ebf8283 2019-07-24 stsp usage_histedit();
6299 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6300 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6301 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6302 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6303 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6304 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6307 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6308 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6309 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6310 0ebf8283 2019-07-24 stsp goto done;
6312 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6313 0ebf8283 2019-07-24 stsp if (error)
6314 0ebf8283 2019-07-24 stsp goto done;
6316 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6318 0ebf8283 2019-07-24 stsp if (error != NULL)
6319 0ebf8283 2019-07-24 stsp goto done;
6321 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6322 0ebf8283 2019-07-24 stsp if (error)
6323 0ebf8283 2019-07-24 stsp goto done;
6324 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6325 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6326 0ebf8283 2019-07-24 stsp goto done;
6329 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6330 0ebf8283 2019-07-24 stsp if (error)
6331 0ebf8283 2019-07-24 stsp goto done;
6333 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6334 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6335 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6336 3e3a69f1 2019-07-25 stsp worktree, repo);
6337 0ebf8283 2019-07-24 stsp if (error)
6338 0ebf8283 2019-07-24 stsp goto done;
6339 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6340 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6341 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6342 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6343 0ebf8283 2019-07-24 stsp if (error)
6344 0ebf8283 2019-07-24 stsp goto done;
6345 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6346 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6347 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6348 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6349 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6350 0ebf8283 2019-07-24 stsp goto done;
6353 0ebf8283 2019-07-24 stsp if (continue_edit) {
6354 0ebf8283 2019-07-24 stsp char *path;
6356 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6357 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6358 0ebf8283 2019-07-24 stsp goto done;
6361 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6362 0ebf8283 2019-07-24 stsp if (error)
6363 0ebf8283 2019-07-24 stsp goto done;
6365 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6366 0ebf8283 2019-07-24 stsp free(path);
6367 0ebf8283 2019-07-24 stsp if (error)
6368 0ebf8283 2019-07-24 stsp goto done;
6370 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6371 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6372 3e3a69f1 2019-07-25 stsp worktree, repo);
6373 0ebf8283 2019-07-24 stsp if (error)
6374 0ebf8283 2019-07-24 stsp goto done;
6376 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6377 0ebf8283 2019-07-24 stsp if (error)
6378 0ebf8283 2019-07-24 stsp goto done;
6380 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6381 0ebf8283 2019-07-24 stsp head_commit_id);
6382 0ebf8283 2019-07-24 stsp if (error)
6383 0ebf8283 2019-07-24 stsp goto done;
6384 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6385 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6386 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6387 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6388 3aac7cf7 2019-07-25 stsp goto done;
6390 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6391 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6392 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6393 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6394 0ebf8283 2019-07-24 stsp commit = NULL;
6395 0ebf8283 2019-07-24 stsp if (error)
6396 0ebf8283 2019-07-24 stsp goto done;
6398 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6399 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6400 0ebf8283 2019-07-24 stsp goto done;
6403 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6404 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6405 0ebf8283 2019-07-24 stsp if (error != NULL)
6406 0ebf8283 2019-07-24 stsp goto done;
6408 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6409 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6410 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6411 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6412 c7d20a3f 2019-07-30 stsp goto done;
6415 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6416 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6417 bfce7f83 2019-07-27 stsp branch = NULL;
6418 0ebf8283 2019-07-24 stsp if (error)
6419 0ebf8283 2019-07-24 stsp goto done;
6421 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6422 0ebf8283 2019-07-24 stsp head_commit_id);
6423 0ebf8283 2019-07-24 stsp if (error)
6424 0ebf8283 2019-07-24 stsp goto done;
6425 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6426 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6427 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6428 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6429 3aac7cf7 2019-07-25 stsp goto done;
6431 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6432 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6433 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6434 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6435 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6436 0ebf8283 2019-07-24 stsp commit = NULL;
6437 0ebf8283 2019-07-24 stsp if (error)
6438 0ebf8283 2019-07-24 stsp goto done;
6440 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6441 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6442 bfce7f83 2019-07-27 stsp if (error)
6443 bfce7f83 2019-07-27 stsp goto done;
6445 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6446 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6447 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6448 6ba2bea2 2019-07-27 stsp if (error) {
6449 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6450 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6451 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6452 0ebf8283 2019-07-24 stsp goto done;
6455 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6457 6ba2bea2 2019-07-27 stsp if (error) {
6458 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6459 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6460 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6461 0ebf8283 2019-07-24 stsp goto done;
6466 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6468 6ba2bea2 2019-07-27 stsp if (error) {
6469 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6470 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6471 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6472 0ebf8283 2019-07-24 stsp goto done;
6477 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6478 4ec14e60 2019-08-28 hiltjo if (error)
6479 0ebf8283 2019-07-24 stsp goto done;
6481 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6482 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6483 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6484 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6487 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6488 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6489 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6490 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6493 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6494 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6496 0ebf8283 2019-07-24 stsp if (error)
6497 0ebf8283 2019-07-24 stsp goto done;
6501 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6502 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6503 0ebf8283 2019-07-24 stsp if (error)
6504 0ebf8283 2019-07-24 stsp goto done;
6508 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6509 0ebf8283 2019-07-24 stsp hle->commit_id);
6510 0ebf8283 2019-07-24 stsp if (error)
6511 0ebf8283 2019-07-24 stsp goto done;
6512 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6513 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6515 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6516 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6517 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6518 0ebf8283 2019-07-24 stsp if (error)
6519 0ebf8283 2019-07-24 stsp goto done;
6520 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6521 0ebf8283 2019-07-24 stsp commit = NULL;
6523 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6524 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6528 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6529 0ebf8283 2019-07-24 stsp char *id_str;
6530 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6531 0ebf8283 2019-07-24 stsp if (error)
6532 0ebf8283 2019-07-24 stsp goto done;
6533 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6535 0ebf8283 2019-07-24 stsp free(id_str);
6536 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6537 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6538 3e3a69f1 2019-07-25 stsp fileindex);
6539 0ebf8283 2019-07-24 stsp goto done;
6542 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6543 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6544 0ebf8283 2019-07-24 stsp if (error)
6545 0ebf8283 2019-07-24 stsp goto done;
6549 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6550 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6551 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6552 0ebf8283 2019-07-24 stsp if (error)
6553 0ebf8283 2019-07-24 stsp goto done;
6556 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6557 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6558 0ebf8283 2019-07-24 stsp if (error)
6559 0ebf8283 2019-07-24 stsp goto done;
6560 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6561 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6563 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6564 3e3a69f1 2019-07-25 stsp branch, repo);
6566 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6567 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6568 0ebf8283 2019-07-24 stsp free(head_commit_id);
6569 0ebf8283 2019-07-24 stsp free(base_commit_id);
6570 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6571 0ebf8283 2019-07-24 stsp if (commit)
6572 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6573 0ebf8283 2019-07-24 stsp if (branch)
6574 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6575 0ebf8283 2019-07-24 stsp if (tmp_branch)
6576 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6577 0ebf8283 2019-07-24 stsp if (worktree)
6578 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6580 2822a352 2019-10-15 stsp got_repo_close(repo);
6581 2822a352 2019-10-15 stsp return error;
6584 2822a352 2019-10-15 stsp __dead static void
6585 2822a352 2019-10-15 stsp usage_integrate(void)
6587 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6591 2822a352 2019-10-15 stsp static const struct got_error *
6592 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6594 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6595 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6596 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6597 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6598 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6599 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6600 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6601 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6602 2822a352 2019-10-15 stsp int ch, did_something = 0;
6604 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6605 2822a352 2019-10-15 stsp switch (ch) {
6607 2822a352 2019-10-15 stsp usage_integrate();
6608 2822a352 2019-10-15 stsp /* NOTREACHED */
6612 2822a352 2019-10-15 stsp argc -= optind;
6613 2822a352 2019-10-15 stsp argv += optind;
6615 2822a352 2019-10-15 stsp if (argc != 1)
6616 2822a352 2019-10-15 stsp usage_integrate();
6617 2822a352 2019-10-15 stsp branch_arg = argv[0];
6619 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6620 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6621 2822a352 2019-10-15 stsp err(1, "pledge");
6623 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6624 2822a352 2019-10-15 stsp if (cwd == NULL) {
6625 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6626 2822a352 2019-10-15 stsp goto done;
6629 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6630 2822a352 2019-10-15 stsp if (error)
6631 2822a352 2019-10-15 stsp goto done;
6633 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6634 2822a352 2019-10-15 stsp if (error)
6635 2822a352 2019-10-15 stsp goto done;
6637 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6639 2822a352 2019-10-15 stsp if (error != NULL)
6640 2822a352 2019-10-15 stsp goto done;
6642 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6643 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6644 2822a352 2019-10-15 stsp if (error)
6645 2822a352 2019-10-15 stsp goto done;
6647 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6648 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6649 2822a352 2019-10-15 stsp goto done;
6652 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6653 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6654 2822a352 2019-10-15 stsp if (error)
6655 2822a352 2019-10-15 stsp goto done;
6657 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6658 2822a352 2019-10-15 stsp if (refname == NULL) {
6659 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6660 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6661 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6662 2822a352 2019-10-15 stsp goto done;
6664 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6665 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6666 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6667 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6668 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6669 2822a352 2019-10-15 stsp goto done;
6672 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6673 2822a352 2019-10-15 stsp if (error)
6674 2822a352 2019-10-15 stsp goto done;
6676 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6677 2822a352 2019-10-15 stsp if (error)
6678 2822a352 2019-10-15 stsp goto done;
6680 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6681 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6682 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6683 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6684 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6685 2822a352 2019-10-15 stsp goto done;
6688 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6689 2822a352 2019-10-15 stsp if (error) {
6690 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6691 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6692 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6693 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6694 2822a352 2019-10-15 stsp goto done;
6697 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
6698 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6699 2822a352 2019-10-15 stsp check_cancelled, NULL);
6700 2822a352 2019-10-15 stsp if (error)
6701 2822a352 2019-10-15 stsp goto done;
6703 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6706 715dc77e 2019-08-03 stsp got_repo_close(repo);
6707 2822a352 2019-10-15 stsp if (worktree)
6708 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6709 2822a352 2019-10-15 stsp free(cwd);
6710 2822a352 2019-10-15 stsp free(base_commit_id);
6711 2822a352 2019-10-15 stsp free(commit_id);
6712 2822a352 2019-10-15 stsp free(refname);
6713 2822a352 2019-10-15 stsp free(base_refname);
6714 715dc77e 2019-08-03 stsp return error;
6717 715dc77e 2019-08-03 stsp __dead static void
6718 715dc77e 2019-08-03 stsp usage_stage(void)
6720 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6721 f3055044 2019-08-07 stsp "[file-path ...]\n",
6722 715dc77e 2019-08-03 stsp getprogname());
6726 715dc77e 2019-08-03 stsp static const struct got_error *
6727 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6728 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6729 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6731 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6732 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6734 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6735 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6736 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6737 408b4ebc 2019-08-03 stsp return NULL;
6739 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6740 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6741 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6743 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6745 408b4ebc 2019-08-03 stsp return err;
6747 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6748 408b4ebc 2019-08-03 stsp free(id_str);
6749 dc424a06 2019-08-07 stsp return NULL;
6752 dc424a06 2019-08-07 stsp static const struct got_error *
6753 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6755 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6756 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6757 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6758 715dc77e 2019-08-03 stsp char *cwd = NULL;
6759 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6760 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6761 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6762 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6763 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6764 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6766 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6768 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6769 715dc77e 2019-08-03 stsp switch (ch) {
6771 408b4ebc 2019-08-03 stsp list_stage = 1;
6774 dc424a06 2019-08-07 stsp pflag = 1;
6777 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6780 715dc77e 2019-08-03 stsp usage_stage();
6781 715dc77e 2019-08-03 stsp /* NOTREACHED */
6785 715dc77e 2019-08-03 stsp argc -= optind;
6786 715dc77e 2019-08-03 stsp argv += optind;
6788 715dc77e 2019-08-03 stsp #ifndef PROFILE
6789 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6790 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6791 715dc77e 2019-08-03 stsp err(1, "pledge");
6793 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6794 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6795 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6796 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6798 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6799 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6800 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6801 715dc77e 2019-08-03 stsp goto done;
6804 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6805 715dc77e 2019-08-03 stsp if (error)
6806 715dc77e 2019-08-03 stsp goto done;
6808 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6810 715dc77e 2019-08-03 stsp if (error != NULL)
6811 715dc77e 2019-08-03 stsp goto done;
6813 dc424a06 2019-08-07 stsp if (patch_script_path) {
6814 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6815 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6816 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6817 dc424a06 2019-08-07 stsp patch_script_path);
6818 dc424a06 2019-08-07 stsp goto done;
6821 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6822 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6823 715dc77e 2019-08-03 stsp if (error)
6824 715dc77e 2019-08-03 stsp goto done;
6826 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6827 6d022e97 2019-08-04 stsp if (error)
6828 6d022e97 2019-08-04 stsp goto done;
6830 6d022e97 2019-08-04 stsp if (list_stage)
6831 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
6832 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
6834 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6835 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
6836 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
6837 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
6838 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6841 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6842 2e1f37b0 2019-08-08 stsp error == NULL)
6843 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6845 ad493afc 2019-08-03 stsp got_repo_close(repo);
6846 ad493afc 2019-08-03 stsp if (worktree)
6847 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
6848 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6849 ad493afc 2019-08-03 stsp free((char *)pe->path);
6850 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
6851 ad493afc 2019-08-03 stsp free(cwd);
6852 ad493afc 2019-08-03 stsp return error;
6855 ad493afc 2019-08-03 stsp __dead static void
6856 ad493afc 2019-08-03 stsp usage_unstage(void)
6858 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6859 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
6860 ad493afc 2019-08-03 stsp getprogname());
6865 ad493afc 2019-08-03 stsp static const struct got_error *
6866 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
6868 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
6869 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
6870 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
6871 ad493afc 2019-08-03 stsp char *cwd = NULL;
6872 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
6873 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6874 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
6875 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
6876 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6877 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6879 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
6881 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
6882 ad493afc 2019-08-03 stsp switch (ch) {
6884 2e1f37b0 2019-08-08 stsp pflag = 1;
6887 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
6890 ad493afc 2019-08-03 stsp usage_unstage();
6891 ad493afc 2019-08-03 stsp /* NOTREACHED */
6895 ad493afc 2019-08-03 stsp argc -= optind;
6896 ad493afc 2019-08-03 stsp argv += optind;
6898 ad493afc 2019-08-03 stsp #ifndef PROFILE
6899 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6900 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
6901 ad493afc 2019-08-03 stsp err(1, "pledge");
6903 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
6904 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6906 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
6907 ad493afc 2019-08-03 stsp if (cwd == NULL) {
6908 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
6909 ad493afc 2019-08-03 stsp goto done;
6912 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6913 ad493afc 2019-08-03 stsp if (error)
6914 ad493afc 2019-08-03 stsp goto done;
6916 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6918 ad493afc 2019-08-03 stsp if (error != NULL)
6919 ad493afc 2019-08-03 stsp goto done;
6921 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
6922 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6923 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
6924 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
6925 2e1f37b0 2019-08-08 stsp patch_script_path);
6926 2e1f37b0 2019-08-08 stsp goto done;
6930 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6931 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
6932 ad493afc 2019-08-03 stsp if (error)
6933 ad493afc 2019-08-03 stsp goto done;
6935 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6936 ad493afc 2019-08-03 stsp if (error)
6937 ad493afc 2019-08-03 stsp goto done;
6939 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6940 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
6941 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
6942 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6944 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6945 2e1f37b0 2019-08-08 stsp error == NULL)
6946 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6948 0ebf8283 2019-07-24 stsp got_repo_close(repo);
6949 715dc77e 2019-08-03 stsp if (worktree)
6950 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
6951 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6952 715dc77e 2019-08-03 stsp free((char *)pe->path);
6953 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
6954 715dc77e 2019-08-03 stsp free(cwd);
6955 01073a5d 2019-08-22 stsp return error;
6958 01073a5d 2019-08-22 stsp __dead static void
6959 01073a5d 2019-08-22 stsp usage_cat(void)
6961 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6962 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
6966 01073a5d 2019-08-22 stsp static const struct got_error *
6967 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6969 01073a5d 2019-08-22 stsp const struct got_error *err;
6970 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
6972 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
6974 01073a5d 2019-08-22 stsp return err;
6976 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6977 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
6978 01073a5d 2019-08-22 stsp return err;
6981 01073a5d 2019-08-22 stsp static const struct got_error *
6982 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6984 01073a5d 2019-08-22 stsp const struct got_error *err;
6985 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
6986 56e0773d 2019-11-28 stsp int nentries, i;
6988 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
6990 01073a5d 2019-08-22 stsp return err;
6992 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
6993 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
6994 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6995 01073a5d 2019-08-22 stsp char *id_str;
6996 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
6998 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
6999 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7002 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7003 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7004 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7005 01073a5d 2019-08-22 stsp free(id_str);
7008 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7009 01073a5d 2019-08-22 stsp return err;
7012 01073a5d 2019-08-22 stsp static const struct got_error *
7013 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7015 01073a5d 2019-08-22 stsp const struct got_error *err;
7016 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7017 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7018 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7019 01073a5d 2019-08-22 stsp char *id_str = NULL;
7020 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7022 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7024 01073a5d 2019-08-22 stsp return err;
7026 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7028 01073a5d 2019-08-22 stsp goto done;
7030 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7031 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7032 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7033 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7034 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7035 01073a5d 2019-08-22 stsp char *pid_str;
7036 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7038 01073a5d 2019-08-22 stsp goto done;
7039 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7040 01073a5d 2019-08-22 stsp free(pid_str);
7042 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7043 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7044 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7046 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7047 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7048 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7050 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7051 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7052 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7054 01073a5d 2019-08-22 stsp free(id_str);
7055 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7056 01073a5d 2019-08-22 stsp return err;
7059 01073a5d 2019-08-22 stsp static const struct got_error *
7060 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7062 01073a5d 2019-08-22 stsp const struct got_error *err;
7063 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7064 01073a5d 2019-08-22 stsp char *id_str = NULL;
7065 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7067 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7069 01073a5d 2019-08-22 stsp return err;
7071 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7073 01073a5d 2019-08-22 stsp goto done;
7075 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7077 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7078 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7079 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7080 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7082 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7083 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7084 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7086 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7087 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7088 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7090 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7091 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7092 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7098 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7099 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7101 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7102 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7103 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7105 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7106 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7107 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7109 01073a5d 2019-08-22 stsp free(id_str);
7110 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7111 01073a5d 2019-08-22 stsp return err;
7114 01073a5d 2019-08-22 stsp static const struct got_error *
7115 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7117 01073a5d 2019-08-22 stsp const struct got_error *error;
7118 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7119 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7120 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7121 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7122 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7123 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7125 01073a5d 2019-08-22 stsp #ifndef PROFILE
7126 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7127 01073a5d 2019-08-22 stsp NULL) == -1)
7128 01073a5d 2019-08-22 stsp err(1, "pledge");
7131 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7132 01073a5d 2019-08-22 stsp switch (ch) {
7134 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7137 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7138 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7139 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7141 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7144 896e9b6f 2019-08-26 stsp force_path = 1;
7147 01073a5d 2019-08-22 stsp usage_cat();
7148 01073a5d 2019-08-22 stsp /* NOTREACHED */
7152 01073a5d 2019-08-22 stsp argc -= optind;
7153 01073a5d 2019-08-22 stsp argv += optind;
7155 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7156 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7157 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7158 01073a5d 2019-08-22 stsp goto done;
7160 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7161 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7162 01073a5d 2019-08-22 stsp goto done;
7163 01073a5d 2019-08-22 stsp if (worktree) {
7164 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7165 01073a5d 2019-08-22 stsp repo_path = strdup(
7166 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7167 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7168 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7169 01073a5d 2019-08-22 stsp goto done;
7174 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7175 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7176 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7177 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7180 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7181 01073a5d 2019-08-22 stsp free(repo_path);
7182 01073a5d 2019-08-22 stsp if (error != NULL)
7183 01073a5d 2019-08-22 stsp goto done;
7185 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7186 896e9b6f 2019-08-26 stsp if (error)
7187 896e9b6f 2019-08-26 stsp goto done;
7189 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7190 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7191 896e9b6f 2019-08-26 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7192 01073a5d 2019-08-22 stsp if (error)
7193 01073a5d 2019-08-22 stsp goto done;
7195 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7196 896e9b6f 2019-08-26 stsp if (force_path) {
7197 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7199 896e9b6f 2019-08-26 stsp if (error)
7202 896e9b6f 2019-08-26 stsp error = match_object_id(&id, &label, argv[i],
7203 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7204 896e9b6f 2019-08-26 stsp if (error) {
7205 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7206 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7208 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7209 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7210 896e9b6f 2019-08-26 stsp if (error)
7215 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7216 01073a5d 2019-08-22 stsp if (error)
7219 01073a5d 2019-08-22 stsp switch (obj_type) {
7220 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7221 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7223 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7224 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7226 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7227 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7229 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7230 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7233 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7236 01073a5d 2019-08-22 stsp if (error)
7238 01073a5d 2019-08-22 stsp free(label);
7239 01073a5d 2019-08-22 stsp label = NULL;
7241 01073a5d 2019-08-22 stsp id = NULL;
7245 01073a5d 2019-08-22 stsp free(label);
7247 896e9b6f 2019-08-26 stsp free(commit_id);
7248 01073a5d 2019-08-22 stsp if (worktree)
7249 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7250 01073a5d 2019-08-22 stsp if (repo) {
7251 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7252 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7253 01073a5d 2019-08-22 stsp if (error == NULL)
7254 01073a5d 2019-08-22 stsp error = repo_error;
7256 0ebf8283 2019-07-24 stsp return error;