2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 404c43c4 2018-06-21 stsp #include "got_blame.h"
54 63219cd2 2019-01-04 stsp #include "got_privsep.h"
55 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 5c860e29 2018-03-12 stsp #ifndef nitems
58 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
65 99437157 2018-11-11 stsp catch_sigint(int signo)
67 99437157 2018-11-11 stsp sigint_received = 1;
71 99437157 2018-11-11 stsp catch_sigpipe(int signo)
73 99437157 2018-11-11 stsp sigpipe_received = 1;
77 8cfb4057 2019-07-09 stsp struct got_cmd {
78 5c860e29 2018-03-12 stsp const char *cmd_name;
79 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
80 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
81 97b3a7be 2019-07-09 stsp const char *cmd_alias;
84 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
85 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
86 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
90 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
91 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
92 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
93 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
94 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
95 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
96 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
97 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
98 d00136be 2019-03-26 stsp __dead static void usage_add(void);
99 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
100 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
101 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
102 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
103 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
104 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
105 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
106 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
107 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
108 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
109 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
111 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
112 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
113 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
114 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
115 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
116 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
118 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
119 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
121 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
122 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
123 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
124 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
125 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
126 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
127 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
128 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
129 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
130 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
131 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
132 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
133 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
134 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
135 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
137 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
138 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
139 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
140 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
141 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
142 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
143 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
144 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
145 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
146 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
147 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
148 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
149 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
150 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
151 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
152 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
153 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
154 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
155 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
157 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
158 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
159 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
160 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
161 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
162 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
165 ce5b7c56 2019-07-09 stsp static void
166 ce5b7c56 2019-07-09 stsp list_commands(void)
170 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
171 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
172 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
173 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
175 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
179 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
181 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
182 5c860e29 2018-03-12 stsp unsigned int i;
184 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
185 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
186 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
187 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
190 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
192 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
193 5c860e29 2018-03-12 stsp switch (ch) {
201 ce5b7c56 2019-07-09 stsp usage(hflag);
202 5c860e29 2018-03-12 stsp /* NOTREACHED */
206 5c860e29 2018-03-12 stsp argc -= optind;
207 5c860e29 2018-03-12 stsp argv += optind;
208 1e70621d 2018-03-27 stsp optind = 0;
210 53ccebc2 2019-07-30 stsp if (Vflag) {
211 53ccebc2 2019-07-30 stsp got_version_print_str();
215 5c860e29 2018-03-12 stsp if (argc <= 0)
216 ce5b7c56 2019-07-09 stsp usage(hflag);
218 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
219 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
221 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
222 d7d4f210 2018-03-12 stsp const struct got_error *error;
224 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
226 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
231 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
233 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
234 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
235 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
236 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
237 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
238 70015d7a 2019-11-08 stsp !(sigint_received &&
239 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
247 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
248 ce5b7c56 2019-07-09 stsp list_commands();
252 4ed7e80c 2018-05-20 stsp __dead static void
253 ce5b7c56 2019-07-09 stsp usage(int hflag)
255 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
256 53ccebc2 2019-07-30 stsp getprogname());
258 ce5b7c56 2019-07-09 stsp list_commands();
262 0266afb7 2019-01-04 stsp static const struct got_error *
263 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
265 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
266 e2ba3d07 2019-05-13 stsp const char *editor;
268 8920fa04 2019-08-18 stsp *abspath = NULL;
270 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
271 e2ba3d07 2019-05-13 stsp if (editor == NULL)
272 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
274 0ee7065d 2019-05-13 stsp if (editor) {
275 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
277 0ee7065d 2019-05-13 stsp return err;
280 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
281 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
282 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
283 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
286 e2ba3d07 2019-05-13 stsp return NULL;
289 e2ba3d07 2019-05-13 stsp static const struct got_error *
290 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
291 c530dc23 2019-07-23 stsp const char *worktree_path)
293 163ce85a 2019-05-13 stsp const struct got_error *err;
295 37c06ea4 2019-07-15 stsp #ifdef PROFILE
296 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
297 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
299 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
302 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
305 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
309 163ce85a 2019-05-13 stsp if (err != NULL)
310 163ce85a 2019-05-13 stsp return err;
312 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
313 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
315 0266afb7 2019-01-04 stsp return NULL;
318 2c7829a4 2019-06-17 stsp __dead static void
319 2c7829a4 2019-06-17 stsp usage_init(void)
321 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
325 2c7829a4 2019-06-17 stsp static const struct got_error *
326 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
328 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
329 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
332 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
333 2c7829a4 2019-06-17 stsp switch (ch) {
335 2c7829a4 2019-06-17 stsp usage_init();
336 2c7829a4 2019-06-17 stsp /* NOTREACHED */
340 2c7829a4 2019-06-17 stsp argc -= optind;
341 2c7829a4 2019-06-17 stsp argv += optind;
343 2c7829a4 2019-06-17 stsp #ifndef PROFILE
344 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 2c7829a4 2019-06-17 stsp err(1, "pledge");
347 2c7829a4 2019-06-17 stsp if (argc != 1)
348 bc20e173 2019-06-17 stsp usage_init();
350 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
351 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
352 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
354 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
356 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
357 2c7829a4 2019-06-17 stsp if (error &&
358 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
361 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
365 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
367 3ce1b845 2019-07-15 stsp free(repo_path);
368 3ce1b845 2019-07-15 stsp return error;
371 3ce1b845 2019-07-15 stsp __dead static void
372 3ce1b845 2019-07-15 stsp usage_import(void)
374 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
380 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
383 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
384 3ce1b845 2019-07-15 stsp int st = -1;
386 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
387 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
388 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
390 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
392 3ce1b845 2019-07-15 stsp goto doneediting;
394 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
395 3ce1b845 2019-07-15 stsp _exit(127);
398 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
399 3ce1b845 2019-07-15 stsp if (errno != EINTR)
402 3ce1b845 2019-07-15 stsp doneediting:
403 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
404 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
405 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
407 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
408 3ce1b845 2019-07-15 stsp errno = EINTR;
412 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
415 3ce1b845 2019-07-15 stsp static const struct got_error *
416 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 3ce1b845 2019-07-15 stsp const char *initial_content)
419 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
420 3ce1b845 2019-07-15 stsp char buf[1024];
421 3ce1b845 2019-07-15 stsp struct stat st, st2;
423 3ce1b845 2019-07-15 stsp int content_changed = 0;
424 3ce1b845 2019-07-15 stsp size_t len;
426 3ce1b845 2019-07-15 stsp *logmsg = NULL;
428 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
429 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
431 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
432 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
434 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
435 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
437 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
441 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
442 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
444 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
447 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
448 3ce1b845 2019-07-15 stsp if (fp == NULL) {
449 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
452 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
453 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
454 3ce1b845 2019-07-15 stsp content_changed = 1;
455 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
457 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
459 3ce1b845 2019-07-15 stsp fclose(fp);
461 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
466 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
467 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
471 3ce1b845 2019-07-15 stsp free(*logmsg);
472 3ce1b845 2019-07-15 stsp *logmsg = NULL;
474 3ce1b845 2019-07-15 stsp return err;
477 3ce1b845 2019-07-15 stsp static const struct got_error *
478 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
481 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
482 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
485 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
486 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
487 3ce1b845 2019-07-15 stsp branch_name) == -1)
488 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
490 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
491 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
495 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
498 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 3ce1b845 2019-07-15 stsp free(initial_content);
501 3ce1b845 2019-07-15 stsp return err;
504 3ce1b845 2019-07-15 stsp static const struct got_error *
505 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
507 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
508 3ce1b845 2019-07-15 stsp return NULL;
511 3ce1b845 2019-07-15 stsp static const struct got_error *
512 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
514 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
515 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
517 84792843 2019-08-09 stsp *author = NULL;
519 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
520 c9956ddf 2019-09-08 stsp email = got_repo_get_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 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
524 aba9c984 2019-09-08 stsp return NULL;
527 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
528 84792843 2019-08-09 stsp if (got_author == NULL) {
529 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
530 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
531 c9956ddf 2019-09-08 stsp if (name && email) {
532 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
533 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
534 c9956ddf 2019-09-08 stsp return NULL;
536 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
537 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
540 aba9c984 2019-09-08 stsp *author = strdup(got_author);
541 aba9c984 2019-09-08 stsp if (*author == NULL)
542 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
545 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
546 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
548 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
549 84792843 2019-08-09 stsp got_author++;
550 aba9c984 2019-09-08 stsp if (*got_author != '<') {
551 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
554 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
555 84792843 2019-08-09 stsp got_author++;
556 aba9c984 2019-09-08 stsp if (*got_author != '@') {
557 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
560 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
561 84792843 2019-08-09 stsp got_author++;
562 84792843 2019-08-09 stsp if (*got_author != '>')
563 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
566 aba9c984 2019-09-08 stsp free(*author);
567 aba9c984 2019-09-08 stsp *author = NULL;
569 aba9c984 2019-09-08 stsp return err;
572 c9956ddf 2019-09-08 stsp static const struct got_error *
573 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
575 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
577 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
578 c9956ddf 2019-09-08 stsp if (homedir) {
579 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
583 c9956ddf 2019-09-08 stsp return NULL;
586 84792843 2019-08-09 stsp static const struct got_error *
587 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
589 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
590 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
593 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
595 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
598 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
599 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
600 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
602 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
604 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
605 3ce1b845 2019-07-15 stsp switch (ch) {
607 3ce1b845 2019-07-15 stsp branch_name = optarg;
610 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
611 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
612 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
617 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
618 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
619 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
625 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
627 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
633 b2b65d18 2019-08-22 stsp usage_import();
634 3ce1b845 2019-07-15 stsp /* NOTREACHED */
638 3ce1b845 2019-07-15 stsp argc -= optind;
639 3ce1b845 2019-07-15 stsp argv += optind;
641 3ce1b845 2019-07-15 stsp #ifndef PROFILE
642 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 3ce1b845 2019-07-15 stsp NULL) == -1)
645 3ce1b845 2019-07-15 stsp err(1, "pledge");
647 3ce1b845 2019-07-15 stsp if (argc != 1)
648 3ce1b845 2019-07-15 stsp usage_import();
650 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
651 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
652 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
653 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
655 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
656 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
659 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
663 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
665 aba9c984 2019-09-08 stsp return error;
668 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
669 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
670 e560b7e0 2019-11-28 stsp * an unintended typo.
672 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
673 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
675 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
680 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
681 3ce1b845 2019-07-15 stsp if (error) {
682 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
685 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 3ce1b845 2019-07-15 stsp "import target branch already exists");
690 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
691 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
692 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
695 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
698 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
699 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
701 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
702 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
705 8e158b01 2019-09-22 stsp free(logmsg);
706 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
707 ef293bdd 2019-10-21 stsp path_dir, refname);
708 ef293bdd 2019-10-21 stsp if (error) {
709 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
711 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
716 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
717 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
718 ef293bdd 2019-10-21 stsp if (logmsg_path)
719 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
723 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 ef293bdd 2019-10-21 stsp if (error) {
725 ef293bdd 2019-10-21 stsp if (logmsg_path)
726 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
730 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
732 ef293bdd 2019-10-21 stsp if (error) {
733 ef293bdd 2019-10-21 stsp if (logmsg_path)
734 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
738 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 ef293bdd 2019-10-21 stsp if (error) {
740 ef293bdd 2019-10-21 stsp if (logmsg_path)
741 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
745 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
746 ef293bdd 2019-10-21 stsp if (error) {
747 ef293bdd 2019-10-21 stsp if (logmsg_path)
748 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
752 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
753 ef293bdd 2019-10-21 stsp if (error) {
754 ef293bdd 2019-10-21 stsp if (logmsg_path)
755 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
759 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
760 3ce1b845 2019-07-15 stsp if (error) {
761 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
762 ef293bdd 2019-10-21 stsp if (logmsg_path)
763 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
767 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
768 3ce1b845 2019-07-15 stsp branch_ref);
769 ef293bdd 2019-10-21 stsp if (error) {
770 ef293bdd 2019-10-21 stsp if (logmsg_path)
771 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
775 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
776 ef293bdd 2019-10-21 stsp if (error) {
777 ef293bdd 2019-10-21 stsp if (logmsg_path)
778 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
783 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
784 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
786 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
787 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
788 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
789 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
791 8e158b01 2019-09-22 stsp free(logmsg);
792 ef293bdd 2019-10-21 stsp free(logmsg_path);
793 2c7829a4 2019-06-17 stsp free(repo_path);
794 3ce1b845 2019-07-15 stsp free(editor);
795 3ce1b845 2019-07-15 stsp free(refname);
796 3ce1b845 2019-07-15 stsp free(new_commit_id);
797 3ce1b845 2019-07-15 stsp free(id_str);
798 aba9c984 2019-09-08 stsp free(author);
799 c9956ddf 2019-09-08 stsp free(gitconfig_path);
800 3ce1b845 2019-07-15 stsp if (branch_ref)
801 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
802 3ce1b845 2019-07-15 stsp if (head_ref)
803 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
804 2c7829a4 2019-06-17 stsp return error;
807 93658fb9 2020-03-18 stsp __dead static void
808 93658fb9 2020-03-18 stsp usage_clone(void)
810 93658fb9 2020-03-18 stsp fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
814 4ed7e80c 2018-05-20 stsp __dead static void
815 c09a553d 2018-03-12 stsp usage_checkout(void)
817 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
822 7f47418f 2019-12-20 stsp static void
823 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
825 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
826 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
827 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
828 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
829 7f47418f 2019-12-20 stsp getprogname());
832 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
833 7f47418f 2019-12-20 stsp const char *worktree_path;
834 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
837 1ee397ad 2019-07-12 stsp static const struct got_error *
838 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
840 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
842 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
843 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
844 7f47418f 2019-12-20 stsp return NULL;
846 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
847 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
848 1ee397ad 2019-07-12 stsp return NULL;
851 92a684f4 2018-03-12 stsp while (path[0] == '/')
854 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
855 1ee397ad 2019-07-12 stsp return NULL;
858 99437157 2018-11-11 stsp static const struct got_error *
859 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
861 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
862 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
863 99437157 2018-11-11 stsp return NULL;
866 8069f636 2019-01-12 stsp static const struct got_error *
867 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
868 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 3aef623b 2019-10-15 stsp struct got_repository *repo)
871 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
872 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
874 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
877 36a38700 2019-05-10 stsp return err;
879 d5bea539 2019-05-13 stsp if (yca_id == NULL)
880 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
883 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
884 d5bea539 2019-05-13 stsp * and the work tree's base commit.
886 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
888 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
896 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
897 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
898 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
900 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
901 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
903 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
905 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
907 d5bea539 2019-05-13 stsp free(yca_id);
908 d5bea539 2019-05-13 stsp return NULL;
911 a367ff0f 2019-05-14 stsp static const struct got_error *
912 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
913 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
914 a51a74b3 2019-07-27 stsp struct got_repository *repo)
916 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
917 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
918 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
919 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
921 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
925 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
926 a51a74b3 2019-07-27 stsp is_same_branch = 1;
929 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
930 a51a74b3 2019-07-27 stsp is_same_branch = 1;
934 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
938 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
944 a367ff0f 2019-05-14 stsp struct got_object_id *id;
945 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
946 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
948 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
949 a367ff0f 2019-05-14 stsp err = NULL;
954 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
956 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
957 a367ff0f 2019-05-14 stsp is_same_branch = 1;
964 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
965 a367ff0f 2019-05-14 stsp free(head_commit_id);
966 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
967 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
968 a367ff0f 2019-05-14 stsp return err;
971 93658fb9 2020-03-18 stsp static const struct got_error *
972 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
974 09838ffc 2020-03-18 stsp const struct got_error *err = NULL;
975 09838ffc 2020-03-18 stsp const char *uri, *branch_filter, *dirname;
976 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
977 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
978 bb64b798 2020-03-18 stsp const char *repo_path;
979 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
980 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
981 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
982 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
983 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
985 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
986 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
988 93658fb9 2020-03-18 stsp while ((ch = getopt(argc, argv, "b:")) != -1) {
989 93658fb9 2020-03-18 stsp switch (ch) {
991 93658fb9 2020-03-18 stsp branch_filter = optarg;
994 93658fb9 2020-03-18 stsp usage_clone();
998 93658fb9 2020-03-18 stsp argc -= optind;
999 93658fb9 2020-03-18 stsp argv += optind;
1000 93658fb9 2020-03-18 stsp uri = argv[0];
1001 93658fb9 2020-03-18 stsp if(argc == 1)
1002 93658fb9 2020-03-18 stsp dirname = NULL;
1003 93658fb9 2020-03-18 stsp else if(argc == 2)
1004 93658fb9 2020-03-18 stsp dirname = argv[1];
1006 93658fb9 2020-03-18 stsp usage_clone();
1008 09838ffc 2020-03-18 stsp err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1009 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1011 20eb36d0 2020-03-18 stsp goto done;
1013 20eb36d0 2020-03-18 stsp err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1015 09838ffc 2020-03-18 stsp goto done;
1017 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1018 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1019 bb64b798 2020-03-18 stsp err = got_error_from_errno("asprintf");
1020 bb64b798 2020-03-18 stsp goto done;
1022 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1024 bb64b798 2020-03-18 stsp repo_path = dirname;
1026 bb64b798 2020-03-18 stsp err = got_path_mkdir(repo_path);
1028 bb64b798 2020-03-18 stsp goto done;
1030 bb64b798 2020-03-18 stsp err = got_repo_init(repo_path);
1031 bb64b798 2020-03-18 stsp if (err != NULL)
1032 bb64b798 2020-03-18 stsp goto done;
1034 bb64b798 2020-03-18 stsp err = got_repo_open(&repo, repo_path, NULL);
1036 bb64b798 2020-03-18 stsp goto done;
1038 07e52fce 2020-03-18 stsp err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1041 d9b4d0c0 2020-03-18 stsp goto done;
1043 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, pack_hash);
1045 d9b4d0c0 2020-03-18 stsp goto done;
1046 d9b4d0c0 2020-03-18 stsp printf("Fetched %s.pack\n", id_str);
1047 d9b4d0c0 2020-03-18 stsp free(id_str);
1049 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1050 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1051 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1052 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1053 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1055 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, id);
1057 d9b4d0c0 2020-03-18 stsp goto done;
1059 d9b4d0c0 2020-03-18 stsp err = got_ref_alloc(&ref, refname, id);
1060 d9b4d0c0 2020-03-18 stsp if (err) {
1061 d9b4d0c0 2020-03-18 stsp free(id_str);
1062 d9b4d0c0 2020-03-18 stsp goto done;
1065 d9b4d0c0 2020-03-18 stsp printf("%s: %s\n", got_ref_get_name(ref), id_str);
1066 d9b4d0c0 2020-03-18 stsp free(id_str);
1067 d9b4d0c0 2020-03-18 stsp err = got_ref_write(ref, repo);
1068 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1070 d9b4d0c0 2020-03-18 stsp goto done;
1073 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1074 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1075 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1076 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1077 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1079 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1082 d9b4d0c0 2020-03-18 stsp err = got_ref_open(&target_ref, repo, target, 0);
1083 d9b4d0c0 2020-03-18 stsp if (err) {
1084 d9b4d0c0 2020-03-18 stsp if (err->code == GOT_ERR_NOT_REF)
1086 d9b4d0c0 2020-03-18 stsp goto done;
1089 d9b4d0c0 2020-03-18 stsp err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1090 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1092 d9b4d0c0 2020-03-18 stsp goto done;
1094 d9b4d0c0 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1095 d9b4d0c0 2020-03-18 stsp got_ref_get_symref_target(symref));
1097 d9b4d0c0 2020-03-18 stsp err = got_ref_write(symref, repo);
1098 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1103 20eb36d0 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1104 20eb36d0 2020-03-18 stsp err = got_error_from_errno("close");
1106 bb64b798 2020-03-18 stsp got_repo_close(repo);
1107 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1108 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1109 d9b4d0c0 2020-03-18 stsp free(pe->data);
1111 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1112 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1113 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1114 d9b4d0c0 2020-03-18 stsp free(pe->data);
1116 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1117 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1118 09838ffc 2020-03-18 stsp free(proto);
1119 09838ffc 2020-03-18 stsp free(host);
1120 09838ffc 2020-03-18 stsp free(port);
1121 09838ffc 2020-03-18 stsp free(server_path);
1122 09838ffc 2020-03-18 stsp free(repo_name);
1123 bb64b798 2020-03-18 stsp free(default_destdir);
1124 09838ffc 2020-03-18 stsp return err;
1127 4ed7e80c 2018-05-20 stsp static const struct got_error *
1128 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1130 4b6c9460 2020-03-05 stsp static char msg[512];
1131 4b6c9460 2020-03-05 stsp const char *branch_name;
1133 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1134 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1136 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1138 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1139 4b6c9460 2020-03-05 stsp branch_name += 11;
1141 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1142 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1143 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1144 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1145 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1146 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1148 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1151 4b6c9460 2020-03-05 stsp static const struct got_error *
1152 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1154 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1155 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1156 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1157 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1158 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1159 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1160 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1161 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1162 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1163 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1164 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1165 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1167 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1169 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1170 0bb8a95e 2018-03-12 stsp switch (ch) {
1172 08573d5b 2019-05-14 stsp branch_name = optarg;
1175 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1176 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1177 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1180 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1183 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1186 2deda0b9 2019-03-07 stsp usage_checkout();
1187 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1191 0bb8a95e 2018-03-12 stsp argc -= optind;
1192 0bb8a95e 2018-03-12 stsp argv += optind;
1194 6715a751 2018-03-16 stsp #ifndef PROFILE
1195 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1196 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1197 c09a553d 2018-03-12 stsp err(1, "pledge");
1199 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1200 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1201 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1202 76089277 2018-04-01 stsp if (repo_path == NULL)
1203 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1204 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1205 76089277 2018-04-01 stsp if (cwd == NULL) {
1206 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1207 76089277 2018-04-01 stsp goto done;
1209 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1210 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1211 230a42bd 2019-05-11 jcs if (base == NULL) {
1212 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1213 230a42bd 2019-05-11 jcs path_prefix);
1217 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1218 230a42bd 2019-05-11 jcs if (base == NULL) {
1219 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1220 230a42bd 2019-05-11 jcs repo_path);
1224 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1225 c09a553d 2018-03-12 stsp if (dotgit)
1226 c09a553d 2018-03-12 stsp *dotgit = '\0';
1227 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1228 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1229 c09a553d 2018-03-12 stsp free(cwd);
1230 76089277 2018-04-01 stsp goto done;
1232 c09a553d 2018-03-12 stsp free(cwd);
1233 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1234 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1235 76089277 2018-04-01 stsp if (repo_path == NULL) {
1236 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1237 76089277 2018-04-01 stsp goto done;
1239 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1240 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1241 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1242 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1244 b4b3a7dd 2019-07-22 stsp goto done;
1246 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1247 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1248 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1249 b4b3a7dd 2019-07-22 stsp goto done;
1253 c09a553d 2018-03-12 stsp usage_checkout();
1255 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1256 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1258 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1259 c09a553d 2018-03-12 stsp if (error != NULL)
1260 c02c541e 2019-03-29 stsp goto done;
1262 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1263 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1264 c530dc23 2019-07-23 stsp if (error) {
1265 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1266 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1267 c530dc23 2019-07-23 stsp goto done;
1268 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1269 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1270 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1271 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1272 c530dc23 2019-07-23 stsp goto done;
1276 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1277 c02c541e 2019-03-29 stsp if (error)
1278 c09a553d 2018-03-12 stsp goto done;
1280 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1281 c09a553d 2018-03-12 stsp if (error != NULL)
1282 c09a553d 2018-03-12 stsp goto done;
1284 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1285 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1286 c09a553d 2018-03-12 stsp goto done;
1288 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1289 c09a553d 2018-03-12 stsp if (error != NULL)
1290 c09a553d 2018-03-12 stsp goto done;
1292 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1293 e5dc7198 2018-12-29 stsp path_prefix);
1294 e5dc7198 2018-12-29 stsp if (error != NULL)
1295 e5dc7198 2018-12-29 stsp goto done;
1296 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1297 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1298 49520a32 2018-12-29 stsp goto done;
1301 8069f636 2019-01-12 stsp if (commit_id_str) {
1302 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1303 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1304 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1305 30837e32 2019-07-25 stsp if (error)
1306 8069f636 2019-01-12 stsp goto done;
1307 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1308 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1309 8069f636 2019-01-12 stsp if (error != NULL) {
1310 8069f636 2019-01-12 stsp free(commit_id);
1311 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1312 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1313 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1315 8069f636 2019-01-12 stsp goto done;
1317 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1318 4b6c9460 2020-03-05 stsp if (error) {
1319 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1320 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1321 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1323 45d344f6 2019-05-14 stsp goto done;
1325 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1326 8069f636 2019-01-12 stsp commit_id);
1327 8069f636 2019-01-12 stsp free(commit_id);
1328 8069f636 2019-01-12 stsp if (error)
1329 8069f636 2019-01-12 stsp goto done;
1332 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1333 f2ea84fa 2019-07-27 stsp if (error)
1334 f2ea84fa 2019-07-27 stsp goto done;
1335 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1336 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1337 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1338 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1339 c09a553d 2018-03-12 stsp if (error != NULL)
1340 c09a553d 2018-03-12 stsp goto done;
1342 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1343 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1344 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1346 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1347 8069f636 2019-01-12 stsp free(commit_id_str);
1348 76089277 2018-04-01 stsp free(repo_path);
1349 507dc3bb 2018-12-29 stsp free(worktree_path);
1350 507dc3bb 2018-12-29 stsp return error;
1353 507dc3bb 2018-12-29 stsp __dead static void
1354 507dc3bb 2018-12-29 stsp usage_update(void)
1356 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1357 507dc3bb 2018-12-29 stsp getprogname());
1361 1ee397ad 2019-07-12 stsp static const struct got_error *
1362 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1364 784955db 2019-01-12 stsp int *did_something = arg;
1366 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1367 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1368 1ee397ad 2019-07-12 stsp return NULL;
1370 1545c615 2019-02-10 stsp *did_something = 1;
1372 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1373 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1374 1ee397ad 2019-07-12 stsp return NULL;
1376 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1378 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1379 1ee397ad 2019-07-12 stsp return NULL;
1382 be7061eb 2018-12-30 stsp static const struct got_error *
1383 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1384 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1385 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1387 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1388 a1fb16d8 2019-05-24 stsp char *base_id_str;
1389 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1391 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1392 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1393 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1394 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1395 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1396 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1397 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1400 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1401 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1402 a1fb16d8 2019-05-24 stsp if (err) {
1403 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1404 a1fb16d8 2019-05-24 stsp return err;
1405 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1407 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1408 a1fb16d8 2019-05-24 stsp return NULL;
1410 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1411 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1412 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1414 a1fb16d8 2019-05-24 stsp return err;
1415 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1416 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1417 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1418 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1419 0ebf8283 2019-07-24 stsp return NULL;
1422 0ebf8283 2019-07-24 stsp static const struct got_error *
1423 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1425 0ebf8283 2019-07-24 stsp const struct got_error *err;
1426 0ebf8283 2019-07-24 stsp int in_progress;
1428 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1430 0ebf8283 2019-07-24 stsp return err;
1431 0ebf8283 2019-07-24 stsp if (in_progress)
1432 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1434 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1436 0ebf8283 2019-07-24 stsp return err;
1437 0ebf8283 2019-07-24 stsp if (in_progress)
1438 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1440 a1fb16d8 2019-05-24 stsp return NULL;
1443 a5edda0a 2019-07-27 stsp static const struct got_error *
1444 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1445 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1447 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1448 a5edda0a 2019-07-27 stsp char *path;
1451 a5edda0a 2019-07-27 stsp if (argc == 0) {
1452 a5edda0a 2019-07-27 stsp path = strdup("");
1453 a5edda0a 2019-07-27 stsp if (path == NULL)
1454 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1455 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1458 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1459 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1462 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1463 a5edda0a 2019-07-27 stsp if (err) {
1464 a5edda0a 2019-07-27 stsp free(path);
1469 a5edda0a 2019-07-27 stsp return err;
1472 a1fb16d8 2019-05-24 stsp static const struct got_error *
1473 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1475 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1476 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1477 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1478 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1479 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1480 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1481 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1482 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1483 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1484 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1485 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1487 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1489 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1490 507dc3bb 2018-12-29 stsp switch (ch) {
1492 024e9686 2019-05-14 stsp branch_name = optarg;
1495 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1496 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1497 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1500 2deda0b9 2019-03-07 stsp usage_update();
1501 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1505 507dc3bb 2018-12-29 stsp argc -= optind;
1506 507dc3bb 2018-12-29 stsp argv += optind;
1508 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1509 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1510 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1511 507dc3bb 2018-12-29 stsp err(1, "pledge");
1513 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1514 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1515 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1516 c4cdcb68 2019-04-03 stsp goto done;
1518 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1519 7d5807f4 2019-07-11 stsp if (error)
1520 7d5807f4 2019-07-11 stsp goto done;
1522 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1523 f2ea84fa 2019-07-27 stsp if (error)
1524 f2ea84fa 2019-07-27 stsp goto done;
1526 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1528 507dc3bb 2018-12-29 stsp if (error != NULL)
1529 507dc3bb 2018-12-29 stsp goto done;
1531 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1532 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1533 087fb88c 2019-08-04 stsp if (error)
1534 087fb88c 2019-08-04 stsp goto done;
1536 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1537 0266afb7 2019-01-04 stsp if (error)
1538 0266afb7 2019-01-04 stsp goto done;
1540 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1541 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1542 024e9686 2019-05-14 stsp if (error != NULL)
1543 024e9686 2019-05-14 stsp goto done;
1544 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1545 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1546 9c4b8182 2019-01-02 stsp if (error != NULL)
1547 9c4b8182 2019-01-02 stsp goto done;
1548 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1549 507dc3bb 2018-12-29 stsp if (error != NULL)
1550 507dc3bb 2018-12-29 stsp goto done;
1552 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1553 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1554 04f57cb3 2019-07-25 stsp free(commit_id_str);
1555 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1556 30837e32 2019-07-25 stsp if (error)
1557 a297e751 2019-07-11 stsp goto done;
1558 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1559 a297e751 2019-07-11 stsp if (error)
1560 507dc3bb 2018-12-29 stsp goto done;
1563 a1fb16d8 2019-05-24 stsp if (branch_name) {
1564 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1565 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1566 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1568 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1569 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1570 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1571 024e9686 2019-05-14 stsp goto done;
1573 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1574 024e9686 2019-05-14 stsp if (error)
1575 024e9686 2019-05-14 stsp goto done;
1576 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1578 a367ff0f 2019-05-14 stsp free(head_commit_id);
1579 024e9686 2019-05-14 stsp if (error != NULL)
1580 024e9686 2019-05-14 stsp goto done;
1581 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1582 a367ff0f 2019-05-14 stsp if (error)
1583 a367ff0f 2019-05-14 stsp goto done;
1584 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1585 024e9686 2019-05-14 stsp if (error)
1586 024e9686 2019-05-14 stsp goto done;
1588 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1589 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1590 a367ff0f 2019-05-14 stsp if (error != NULL) {
1591 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1592 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1593 024e9686 2019-05-14 stsp goto done;
1595 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1596 a367ff0f 2019-05-14 stsp if (error)
1597 a367ff0f 2019-05-14 stsp goto done;
1600 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1601 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1602 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1603 507dc3bb 2018-12-29 stsp commit_id);
1604 507dc3bb 2018-12-29 stsp if (error)
1605 507dc3bb 2018-12-29 stsp goto done;
1608 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1609 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1610 507dc3bb 2018-12-29 stsp if (error != NULL)
1611 507dc3bb 2018-12-29 stsp goto done;
1613 784955db 2019-01-12 stsp if (did_something)
1614 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1616 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1618 c09a553d 2018-03-12 stsp free(worktree_path);
1619 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1620 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1621 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1622 507dc3bb 2018-12-29 stsp free(commit_id);
1623 9c4b8182 2019-01-02 stsp free(commit_id_str);
1624 c09a553d 2018-03-12 stsp return error;
1627 f42b1b34 2018-03-12 stsp static const struct got_error *
1628 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1629 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1630 63035f9f 2019-10-06 stsp struct got_repository *repo)
1632 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1633 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1635 44392932 2019-08-25 stsp if (blob_id1) {
1636 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1638 44392932 2019-08-25 stsp goto done;
1641 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1643 44392932 2019-08-25 stsp goto done;
1645 44392932 2019-08-25 stsp while (path[0] == '/')
1647 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1648 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1650 44392932 2019-08-25 stsp if (blob1)
1651 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1652 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1653 44392932 2019-08-25 stsp return err;
1656 44392932 2019-08-25 stsp static const struct got_error *
1657 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1658 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1659 63035f9f 2019-10-06 stsp struct got_repository *repo)
1661 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1662 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1663 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1665 44392932 2019-08-25 stsp if (tree_id1) {
1666 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1668 44392932 2019-08-25 stsp goto done;
1671 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1673 0f2b3dca 2018-12-22 stsp goto done;
1675 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1676 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1677 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1678 44392932 2019-08-25 stsp while (path[0] == '/')
1680 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1681 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1683 79109fed 2018-03-27 stsp if (tree1)
1684 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1685 366e0a5f 2019-10-10 stsp if (tree2)
1686 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1687 44392932 2019-08-25 stsp return err;
1690 44392932 2019-08-25 stsp static const struct got_error *
1691 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1692 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1694 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1695 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1696 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1697 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1698 44392932 2019-08-25 stsp struct got_object_qid *qid;
1700 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1701 44392932 2019-08-25 stsp if (qid != NULL) {
1702 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1705 44392932 2019-08-25 stsp return err;
1708 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1709 44392932 2019-08-25 stsp int obj_type;
1710 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1712 44392932 2019-08-25 stsp goto done;
1713 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1714 44392932 2019-08-25 stsp if (err) {
1715 44392932 2019-08-25 stsp free(obj_id2);
1716 44392932 2019-08-25 stsp goto done;
1718 44392932 2019-08-25 stsp if (pcommit) {
1719 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1720 44392932 2019-08-25 stsp qid->id, path);
1721 44392932 2019-08-25 stsp if (err) {
1722 44392932 2019-08-25 stsp free(obj_id2);
1723 44392932 2019-08-25 stsp goto done;
1725 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1726 44392932 2019-08-25 stsp if (err) {
1727 44392932 2019-08-25 stsp free(obj_id2);
1728 44392932 2019-08-25 stsp goto done;
1731 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1732 44392932 2019-08-25 stsp if (err) {
1733 44392932 2019-08-25 stsp free(obj_id2);
1734 44392932 2019-08-25 stsp goto done;
1736 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1737 44392932 2019-08-25 stsp switch (obj_type) {
1738 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1739 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1742 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1743 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1747 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1750 44392932 2019-08-25 stsp free(obj_id1);
1751 44392932 2019-08-25 stsp free(obj_id2);
1753 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1754 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1756 44392932 2019-08-25 stsp goto done;
1757 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1758 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1760 44392932 2019-08-25 stsp goto done;
1761 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1762 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1765 0f2b3dca 2018-12-22 stsp free(id_str1);
1766 0f2b3dca 2018-12-22 stsp free(id_str2);
1767 44392932 2019-08-25 stsp if (pcommit)
1768 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1769 79109fed 2018-03-27 stsp return err;
1772 4bb494d5 2018-06-16 stsp static char *
1773 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1775 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1776 09867e48 2019-08-13 stsp char *p, *s;
1778 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1779 09867e48 2019-08-13 stsp if (tm == NULL)
1780 09867e48 2019-08-13 stsp return NULL;
1781 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1782 09867e48 2019-08-13 stsp if (s == NULL)
1783 09867e48 2019-08-13 stsp return NULL;
1784 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1786 6c281f94 2018-06-11 stsp *p = '\0';
1790 6841bf13 2019-11-29 kn static const struct got_error *
1791 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1792 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1794 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1795 6841bf13 2019-11-29 kn regmatch_t regmatch;
1796 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1798 6841bf13 2019-11-29 kn *have_match = 0;
1800 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1804 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1808 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1809 6841bf13 2019-11-29 kn *have_match = 1;
1811 6841bf13 2019-11-29 kn free(id_str);
1812 6841bf13 2019-11-29 kn free(logmsg);
1816 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1818 79109fed 2018-03-27 stsp static const struct got_error *
1819 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1820 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1821 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1823 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1824 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1825 6c281f94 2018-06-11 stsp char datebuf[26];
1826 45d799e2 2018-12-23 stsp time_t committer_time;
1827 45d799e2 2018-12-23 stsp const char *author, *committer;
1828 199a4027 2019-02-02 stsp char *refs_str = NULL;
1829 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1831 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1833 199a4027 2019-02-02 stsp const char *name;
1834 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1837 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1838 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1840 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1841 199a4027 2019-02-02 stsp name += 5;
1842 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1844 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1845 e34f9ed6 2019-02-02 stsp name += 6;
1846 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1847 141c2bff 2019-02-04 stsp name += 8;
1848 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1849 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1850 5d844a1e 2019-08-13 stsp if (err) {
1851 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1852 5d844a1e 2019-08-13 stsp return err;
1853 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1854 5d844a1e 2019-08-13 stsp err = NULL;
1855 5d844a1e 2019-08-13 stsp tag = NULL;
1858 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1859 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1861 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1862 a436ad14 2019-08-13 stsp if (cmp != 0)
1864 199a4027 2019-02-02 stsp s = refs_str;
1865 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1866 199a4027 2019-02-02 stsp name) == -1) {
1867 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1869 34ca4898 2019-08-13 stsp return err;
1873 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1875 8bf5b3c9 2018-03-17 stsp return err;
1877 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1878 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1879 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1880 832c249c 2018-06-10 stsp free(id_str);
1881 d3d493d7 2019-02-21 stsp id_str = NULL;
1882 d3d493d7 2019-02-21 stsp free(refs_str);
1883 d3d493d7 2019-02-21 stsp refs_str = NULL;
1884 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1885 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1886 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1887 09867e48 2019-08-13 stsp if (datestr)
1888 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1889 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1890 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1891 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1892 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1893 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1894 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1895 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1896 3fe1abad 2018-06-10 stsp int n = 1;
1897 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1898 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1899 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1901 a0603db2 2018-06-10 stsp return err;
1902 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1903 a0603db2 2018-06-10 stsp free(id_str);
1907 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1909 5943eee2 2019-08-13 stsp return err;
1911 621015ac 2018-07-23 stsp logmsg = logmsg0;
1913 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1915 832c249c 2018-06-10 stsp printf(" %s\n", line);
1916 832c249c 2018-06-10 stsp } while (line);
1917 621015ac 2018-07-23 stsp free(logmsg0);
1919 971751ac 2018-03-27 stsp if (show_patch) {
1920 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1921 971751ac 2018-03-27 stsp if (err == 0)
1922 971751ac 2018-03-27 stsp printf("\n");
1925 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1926 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1927 07862c20 2018-09-15 stsp return err;
1930 f42b1b34 2018-03-12 stsp static const struct got_error *
1931 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1932 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1933 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
1934 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1936 f42b1b34 2018-03-12 stsp const struct got_error *err;
1937 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1938 6841bf13 2019-11-29 kn regex_t regex;
1939 6841bf13 2019-11-29 kn int have_match;
1941 6841bf13 2019-11-29 kn if (search_pattern &&
1942 6841bf13 2019-11-29 kn regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1943 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1945 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
1947 f42b1b34 2018-03-12 stsp return err;
1948 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1949 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1951 fcc85cad 2018-07-22 stsp goto done;
1953 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1954 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1956 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1959 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1960 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1961 372ccdbb 2018-06-10 stsp if (err) {
1962 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1963 9ba79e04 2018-06-11 stsp err = NULL;
1966 b43fbaa0 2018-06-11 stsp if (id == NULL)
1969 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1973 6841bf13 2019-11-29 kn if (search_pattern) {
1974 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
1976 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1979 6841bf13 2019-11-29 kn if (have_match == 0) {
1980 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1985 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1986 44392932 2019-08-25 stsp diff_context, refs);
1987 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1988 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1992 6841bf13 2019-11-29 kn if (search_pattern)
1993 6841bf13 2019-11-29 kn regfree(®ex);
1994 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1995 f42b1b34 2018-03-12 stsp return err;
1998 4ed7e80c 2018-05-20 stsp __dead static void
1999 6f3d1eb0 2018-03-12 stsp usage_log(void)
2001 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2002 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2006 b1ebc001 2019-08-13 stsp static int
2007 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2009 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2010 b1ebc001 2019-08-13 stsp long long n;
2011 b1ebc001 2019-08-13 stsp const char *errstr;
2013 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2014 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2016 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2017 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2022 4ed7e80c 2018-05-20 stsp static const struct got_error *
2023 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2025 f42b1b34 2018-03-12 stsp const struct got_error *error;
2026 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2027 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2028 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2029 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2030 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2031 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2032 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2033 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2034 64a96a6d 2018-04-01 stsp const char *errstr;
2035 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2037 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2039 6715a751 2018-03-16 stsp #ifndef PROFILE
2040 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2043 f42b1b34 2018-03-12 stsp err(1, "pledge");
2046 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2048 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2049 79109fed 2018-03-27 stsp switch (ch) {
2051 79109fed 2018-03-27 stsp show_patch = 1;
2054 d142fc45 2018-04-01 stsp start_commit = optarg;
2057 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2059 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2060 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2063 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2064 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2065 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2068 48c8c60d 2020-01-27 stsp log_branches = 1;
2071 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2072 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2073 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2075 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2078 6841bf13 2019-11-29 kn search_pattern = optarg;
2081 2deda0b9 2019-03-07 stsp usage_log();
2082 79109fed 2018-03-27 stsp /* NOTREACHED */
2086 79109fed 2018-03-27 stsp argc -= optind;
2087 79109fed 2018-03-27 stsp argv += optind;
2089 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2090 dc1edbfa 2019-11-29 kn diff_context = 3;
2091 dc1edbfa 2019-11-29 kn else if (!show_patch)
2092 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2094 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2095 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2096 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2097 04ca23f4 2018-07-16 stsp goto done;
2100 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2101 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2102 cffc0aa4 2019-02-05 stsp goto done;
2103 cffc0aa4 2019-02-05 stsp error = NULL;
2105 e7301579 2019-03-18 stsp if (argc == 0) {
2106 cbd1af7a 2019-03-18 stsp path = strdup("");
2107 e7301579 2019-03-18 stsp if (path == NULL) {
2108 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2109 cbd1af7a 2019-03-18 stsp goto done;
2111 e7301579 2019-03-18 stsp } else if (argc == 1) {
2112 e7301579 2019-03-18 stsp if (worktree) {
2113 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2115 e7301579 2019-03-18 stsp if (error)
2116 e7301579 2019-03-18 stsp goto done;
2118 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2119 e7301579 2019-03-18 stsp if (path == NULL) {
2120 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2121 e7301579 2019-03-18 stsp goto done;
2125 cbd1af7a 2019-03-18 stsp usage_log();
2127 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2128 5486daa2 2019-05-11 stsp repo_path = worktree ?
2129 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2131 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2132 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2133 cffc0aa4 2019-02-05 stsp goto done;
2136 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2137 d7d4f210 2018-03-12 stsp if (error != NULL)
2138 04ca23f4 2018-07-16 stsp goto done;
2140 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2141 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2142 c02c541e 2019-03-29 stsp if (error)
2143 c02c541e 2019-03-29 stsp goto done;
2145 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2146 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2147 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2148 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2149 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2150 3235492e 2018-04-01 stsp if (error != NULL)
2151 3235492e 2018-04-01 stsp return error;
2152 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2153 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2154 3235492e 2018-04-01 stsp if (error != NULL)
2155 3235492e 2018-04-01 stsp return error;
2156 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2158 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2159 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2160 3235492e 2018-04-01 stsp if (error == NULL) {
2161 1caad61b 2019-02-01 stsp int obj_type;
2162 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2163 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2164 d1f2edc9 2018-06-13 stsp if (error != NULL)
2165 1caad61b 2019-02-01 stsp goto done;
2166 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2167 1caad61b 2019-02-01 stsp if (error != NULL)
2168 1caad61b 2019-02-01 stsp goto done;
2169 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2170 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2171 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2172 1caad61b 2019-02-01 stsp if (error != NULL)
2173 1caad61b 2019-02-01 stsp goto done;
2174 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2175 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2176 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2177 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2178 1caad61b 2019-02-01 stsp goto done;
2181 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2182 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2183 1caad61b 2019-02-01 stsp if (id == NULL)
2184 638f9024 2019-05-13 stsp error = got_error_from_errno(
2185 230a42bd 2019-05-11 jcs "got_object_id_dup");
2186 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2187 1caad61b 2019-02-01 stsp if (error)
2188 1caad61b 2019-02-01 stsp goto done;
2189 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2190 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2191 1caad61b 2019-02-01 stsp goto done;
2193 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2194 d1f2edc9 2018-06-13 stsp if (error != NULL)
2195 1caad61b 2019-02-01 stsp goto done;
2197 15a94983 2018-12-23 stsp if (commit == NULL) {
2198 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2199 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2200 d1f2edc9 2018-06-13 stsp if (error != NULL)
2201 d1f2edc9 2018-06-13 stsp return error;
2204 d7d4f210 2018-03-12 stsp if (error != NULL)
2205 04ca23f4 2018-07-16 stsp goto done;
2207 deeabeae 2019-08-27 stsp if (worktree) {
2208 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2210 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2211 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2212 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2213 deeabeae 2019-08-27 stsp goto done;
2215 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2218 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2219 04ca23f4 2018-07-16 stsp if (error != NULL)
2220 04ca23f4 2018-07-16 stsp goto done;
2221 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2222 04ca23f4 2018-07-16 stsp free(path);
2223 04ca23f4 2018-07-16 stsp path = in_repo_path;
2226 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2227 199a4027 2019-02-02 stsp if (error)
2228 199a4027 2019-02-02 stsp goto done;
2230 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2231 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2233 04ca23f4 2018-07-16 stsp free(path);
2234 04ca23f4 2018-07-16 stsp free(repo_path);
2235 04ca23f4 2018-07-16 stsp free(cwd);
2237 cffc0aa4 2019-02-05 stsp if (worktree)
2238 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2239 ad242220 2018-09-08 stsp if (repo) {
2240 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2241 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2242 ad242220 2018-09-08 stsp if (error == NULL)
2243 ad242220 2018-09-08 stsp error = repo_error;
2245 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2246 8bf5b3c9 2018-03-17 stsp return error;
2249 4ed7e80c 2018-05-20 stsp __dead static void
2250 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2252 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2253 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2257 b72f483a 2019-02-05 stsp struct print_diff_arg {
2258 b72f483a 2019-02-05 stsp struct got_repository *repo;
2259 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2260 b72f483a 2019-02-05 stsp int diff_context;
2261 3fc0c068 2019-02-10 stsp const char *id_str;
2262 3fc0c068 2019-02-10 stsp int header_shown;
2263 98eaaa12 2019-08-03 stsp int diff_staged;
2264 63035f9f 2019-10-06 stsp int ignore_whitespace;
2267 4ed7e80c 2018-05-20 stsp static const struct got_error *
2268 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2269 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2270 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2271 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2273 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2274 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2275 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2276 12463d8b 2019-12-13 stsp int fd = -1;
2277 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2278 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2279 b72f483a 2019-02-05 stsp struct stat sb;
2281 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2282 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2283 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2284 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2285 98eaaa12 2019-08-03 stsp return NULL;
2287 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2288 98eaaa12 2019-08-03 stsp return NULL;
2289 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2290 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2291 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2292 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2293 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2294 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2295 98eaaa12 2019-08-03 stsp return NULL;
2298 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2299 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2300 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2301 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2302 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2305 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2306 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2307 98eaaa12 2019-08-03 stsp switch (staged_status) {
2308 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2309 98eaaa12 2019-08-03 stsp label1 = path;
2310 98eaaa12 2019-08-03 stsp label2 = path;
2312 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2313 98eaaa12 2019-08-03 stsp label2 = path;
2315 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2316 98eaaa12 2019-08-03 stsp label1 = path;
2319 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2321 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2322 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2323 63035f9f 2019-10-06 stsp a->repo, stdout);
2326 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2327 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2328 4ce46740 2019-08-08 stsp char *id_str;
2329 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2332 4ce46740 2019-08-08 stsp goto done;
2333 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2335 4ce46740 2019-08-08 stsp goto done;
2336 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2337 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2338 4ce46740 2019-08-08 stsp free(id_str);
2339 4ce46740 2019-08-08 stsp goto done;
2341 4ce46740 2019-08-08 stsp free(id_str);
2342 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2343 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2345 4ce46740 2019-08-08 stsp goto done;
2348 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2349 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2350 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2351 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2352 2ec1f75b 2019-03-26 stsp goto done;
2355 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2356 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2357 12463d8b 2019-12-13 stsp if (fd == -1) {
2358 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2359 12463d8b 2019-12-13 stsp goto done;
2362 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2363 12463d8b 2019-12-13 stsp if (fd == -1) {
2364 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2365 12463d8b 2019-12-13 stsp goto done;
2368 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2369 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2370 2ec1f75b 2019-03-26 stsp goto done;
2372 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2373 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2374 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2375 2ec1f75b 2019-03-26 stsp goto done;
2379 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2381 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2382 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2384 b72f483a 2019-02-05 stsp if (blob1)
2385 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2386 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2387 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2388 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2389 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2390 b72f483a 2019-02-05 stsp free(abspath);
2391 927df6b7 2019-02-10 stsp return err;
2394 927df6b7 2019-02-10 stsp static const struct got_error *
2395 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2397 b00d56cd 2018-04-01 stsp const struct got_error *error;
2398 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2399 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2400 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2401 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2402 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2403 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2404 15a94983 2018-12-23 stsp int type1, type2;
2405 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2406 c0cc5c62 2018-10-18 stsp const char *errstr;
2407 927df6b7 2019-02-10 stsp char *path = NULL;
2409 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2410 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2411 25eccc22 2019-01-04 stsp NULL) == -1)
2412 b00d56cd 2018-04-01 stsp err(1, "pledge");
2415 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2416 b00d56cd 2018-04-01 stsp switch (ch) {
2418 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2420 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2421 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2424 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2425 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2426 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2428 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2431 98eaaa12 2019-08-03 stsp diff_staged = 1;
2434 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2437 2deda0b9 2019-03-07 stsp usage_diff();
2438 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2442 b00d56cd 2018-04-01 stsp argc -= optind;
2443 b00d56cd 2018-04-01 stsp argv += optind;
2445 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2446 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2447 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2448 b72f483a 2019-02-05 stsp goto done;
2450 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2451 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2452 927df6b7 2019-02-10 stsp goto done;
2453 927df6b7 2019-02-10 stsp if (argc <= 1) {
2454 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2455 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2456 927df6b7 2019-02-10 stsp goto done;
2458 b72f483a 2019-02-05 stsp if (repo_path)
2460 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2461 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2462 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2463 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2464 927df6b7 2019-02-10 stsp goto done;
2466 927df6b7 2019-02-10 stsp if (argc == 1) {
2467 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2469 927df6b7 2019-02-10 stsp if (error)
2470 927df6b7 2019-02-10 stsp goto done;
2472 927df6b7 2019-02-10 stsp path = strdup("");
2473 927df6b7 2019-02-10 stsp if (path == NULL) {
2474 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2475 927df6b7 2019-02-10 stsp goto done;
2478 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2479 98eaaa12 2019-08-03 stsp if (diff_staged)
2480 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2481 98eaaa12 2019-08-03 stsp "objects in repository");
2482 15a94983 2018-12-23 stsp id_str1 = argv[0];
2483 15a94983 2018-12-23 stsp id_str2 = argv[1];
2484 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2485 30db809c 2019-06-05 stsp repo_path =
2486 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2487 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2488 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2489 30db809c 2019-06-05 stsp goto done;
2493 b00d56cd 2018-04-01 stsp usage_diff();
2495 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2496 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2497 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2498 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2501 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2502 76089277 2018-04-01 stsp free(repo_path);
2503 b00d56cd 2018-04-01 stsp if (error != NULL)
2504 b00d56cd 2018-04-01 stsp goto done;
2506 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2507 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2508 c02c541e 2019-03-29 stsp if (error)
2509 c02c541e 2019-03-29 stsp goto done;
2511 30db809c 2019-06-05 stsp if (argc <= 1) {
2512 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2513 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2514 b72f483a 2019-02-05 stsp char *id_str;
2516 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2518 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2519 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2520 b72f483a 2019-02-05 stsp if (error)
2521 b72f483a 2019-02-05 stsp goto done;
2522 b72f483a 2019-02-05 stsp arg.repo = repo;
2523 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2524 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2525 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2526 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2527 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2528 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2530 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2531 72ea6654 2019-07-27 stsp if (error)
2532 72ea6654 2019-07-27 stsp goto done;
2534 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2535 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2536 b72f483a 2019-02-05 stsp free(id_str);
2537 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2538 b72f483a 2019-02-05 stsp goto done;
2541 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2542 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2543 0adb7196 2019-08-11 stsp if (error)
2544 0adb7196 2019-08-11 stsp goto done;
2546 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2547 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2548 0adb7196 2019-08-11 stsp if (error)
2549 0adb7196 2019-08-11 stsp goto done;
2551 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2552 15a94983 2018-12-23 stsp if (error)
2553 15a94983 2018-12-23 stsp goto done;
2555 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2556 15a94983 2018-12-23 stsp if (error)
2557 15a94983 2018-12-23 stsp goto done;
2559 15a94983 2018-12-23 stsp if (type1 != type2) {
2560 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2561 b00d56cd 2018-04-01 stsp goto done;
2564 15a94983 2018-12-23 stsp switch (type1) {
2565 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2566 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2567 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2569 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2570 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2571 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2573 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2574 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2575 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2576 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2579 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2582 5e70831e 2019-05-28 stsp free(label1);
2583 5e70831e 2019-05-28 stsp free(label2);
2584 15a94983 2018-12-23 stsp free(id1);
2585 15a94983 2018-12-23 stsp free(id2);
2586 927df6b7 2019-02-10 stsp free(path);
2587 b72f483a 2019-02-05 stsp if (worktree)
2588 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2589 ad242220 2018-09-08 stsp if (repo) {
2590 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2591 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2592 ad242220 2018-09-08 stsp if (error == NULL)
2593 ad242220 2018-09-08 stsp error = repo_error;
2595 b00d56cd 2018-04-01 stsp return error;
2598 404c43c4 2018-06-21 stsp __dead static void
2599 404c43c4 2018-06-21 stsp usage_blame(void)
2601 9270e621 2019-02-05 stsp fprintf(stderr,
2602 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2603 404c43c4 2018-06-21 stsp getprogname());
2607 28315671 2019-08-14 stsp struct blame_line {
2608 28315671 2019-08-14 stsp int annotated;
2609 28315671 2019-08-14 stsp char *id_str;
2610 82f6abb8 2019-08-14 stsp char *committer;
2611 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2614 28315671 2019-08-14 stsp struct blame_cb_args {
2615 28315671 2019-08-14 stsp struct blame_line *lines;
2616 28315671 2019-08-14 stsp int nlines;
2617 7ef28ff8 2019-08-14 stsp int nlines_prec;
2618 28315671 2019-08-14 stsp int lineno_cur;
2619 28315671 2019-08-14 stsp off_t *line_offsets;
2621 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2624 404c43c4 2018-06-21 stsp static const struct got_error *
2625 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2627 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2628 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2629 28315671 2019-08-14 stsp struct blame_line *bline;
2630 82f6abb8 2019-08-14 stsp char *line = NULL;
2631 28315671 2019-08-14 stsp size_t linesize = 0;
2632 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2633 28315671 2019-08-14 stsp off_t offset;
2634 bcb49d15 2019-08-14 stsp struct tm tm;
2635 bcb49d15 2019-08-14 stsp time_t committer_time;
2637 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2638 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2639 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2641 28315671 2019-08-14 stsp if (sigint_received)
2642 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2644 2839f8b9 2019-08-18 stsp if (lineno == -1)
2645 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2647 28315671 2019-08-14 stsp /* Annotate this line. */
2648 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2649 28315671 2019-08-14 stsp if (bline->annotated)
2650 28315671 2019-08-14 stsp return NULL;
2651 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2653 28315671 2019-08-14 stsp return err;
2655 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2657 82f6abb8 2019-08-14 stsp goto done;
2659 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2660 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2661 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2662 bcb49d15 2019-08-14 stsp goto done;
2665 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2666 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2667 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2668 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2669 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2670 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2671 82f6abb8 2019-08-14 stsp goto done;
2673 28315671 2019-08-14 stsp bline->annotated = 1;
2675 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2676 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2677 28315671 2019-08-14 stsp if (!bline->annotated)
2678 82f6abb8 2019-08-14 stsp goto done;
2680 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2681 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2682 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2683 82f6abb8 2019-08-14 stsp goto done;
2686 28315671 2019-08-14 stsp while (bline->annotated) {
2687 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2688 82f6abb8 2019-08-14 stsp size_t len;
2690 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2691 28315671 2019-08-14 stsp if (ferror(a->f))
2692 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2696 82f6abb8 2019-08-14 stsp committer = bline->committer;
2697 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2698 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2699 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2700 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2702 82f6abb8 2019-08-14 stsp *at = '\0';
2703 82f6abb8 2019-08-14 stsp len = strlen(committer);
2704 82f6abb8 2019-08-14 stsp if (len >= 9)
2705 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2707 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2709 28315671 2019-08-14 stsp *nl = '\0';
2710 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2711 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2713 28315671 2019-08-14 stsp a->lineno_cur++;
2714 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2717 82f6abb8 2019-08-14 stsp if (commit)
2718 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2719 28315671 2019-08-14 stsp free(line);
2720 28315671 2019-08-14 stsp return err;
2723 28315671 2019-08-14 stsp static const struct got_error *
2724 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2726 404c43c4 2018-06-21 stsp const struct got_error *error;
2727 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2728 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2729 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2730 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2731 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2732 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2733 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2734 28315671 2019-08-14 stsp struct blame_cb_args bca;
2735 28315671 2019-08-14 stsp int ch, obj_type, i;
2736 b02560ec 2019-08-19 stsp size_t filesize;
2738 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2740 404c43c4 2018-06-21 stsp #ifndef PROFILE
2741 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2742 36e2fb66 2019-01-04 stsp NULL) == -1)
2743 404c43c4 2018-06-21 stsp err(1, "pledge");
2746 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2747 404c43c4 2018-06-21 stsp switch (ch) {
2749 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2752 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2753 66bea077 2018-08-02 stsp if (repo_path == NULL)
2754 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2756 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2759 2deda0b9 2019-03-07 stsp usage_blame();
2760 404c43c4 2018-06-21 stsp /* NOTREACHED */
2764 404c43c4 2018-06-21 stsp argc -= optind;
2765 404c43c4 2018-06-21 stsp argv += optind;
2767 a39318fd 2018-08-02 stsp if (argc == 1)
2768 404c43c4 2018-06-21 stsp path = argv[0];
2770 404c43c4 2018-06-21 stsp usage_blame();
2772 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2773 66bea077 2018-08-02 stsp if (cwd == NULL) {
2774 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2775 66bea077 2018-08-02 stsp goto done;
2777 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2778 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2779 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2780 66bea077 2018-08-02 stsp goto done;
2782 0c06baac 2019-02-05 stsp error = NULL;
2783 0c06baac 2019-02-05 stsp if (worktree) {
2784 0c06baac 2019-02-05 stsp repo_path =
2785 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2786 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2787 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2788 0c06baac 2019-02-05 stsp if (error)
2789 0c06baac 2019-02-05 stsp goto done;
2791 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2792 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2793 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2794 0c06baac 2019-02-05 stsp goto done;
2799 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2800 c02c541e 2019-03-29 stsp if (error != NULL)
2801 36e2fb66 2019-01-04 stsp goto done;
2803 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2804 c02c541e 2019-03-29 stsp if (error)
2805 404c43c4 2018-06-21 stsp goto done;
2807 0c06baac 2019-02-05 stsp if (worktree) {
2808 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2809 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2810 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2811 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2812 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2813 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2814 6efaaa2d 2019-02-05 stsp path) == -1) {
2815 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2816 0c06baac 2019-02-05 stsp goto done;
2818 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2821 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2823 0c06baac 2019-02-05 stsp if (error)
2824 66bea077 2018-08-02 stsp goto done;
2826 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2827 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2828 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2829 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2830 404c43c4 2018-06-21 stsp if (error != NULL)
2831 66bea077 2018-08-02 stsp goto done;
2832 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2833 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2834 404c43c4 2018-06-21 stsp if (error != NULL)
2835 66bea077 2018-08-02 stsp goto done;
2837 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2838 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2839 30837e32 2019-07-25 stsp if (error)
2840 66bea077 2018-08-02 stsp goto done;
2843 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2844 28315671 2019-08-14 stsp if (error)
2845 28315671 2019-08-14 stsp goto done;
2847 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2848 28315671 2019-08-14 stsp if (error)
2849 28315671 2019-08-14 stsp goto done;
2851 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2852 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2853 28315671 2019-08-14 stsp goto done;
2856 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2857 28315671 2019-08-14 stsp if (error)
2858 28315671 2019-08-14 stsp goto done;
2859 28315671 2019-08-14 stsp bca.f = got_opentemp();
2860 28315671 2019-08-14 stsp if (bca.f == NULL) {
2861 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2862 28315671 2019-08-14 stsp goto done;
2864 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2865 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2866 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2867 28315671 2019-08-14 stsp goto done;
2869 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2870 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2871 b02560ec 2019-08-19 stsp bca.nlines--;
2873 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2874 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2875 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2876 28315671 2019-08-14 stsp goto done;
2878 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2879 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2880 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2881 7ef28ff8 2019-08-14 stsp while (i > 0) {
2883 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2885 82f6abb8 2019-08-14 stsp bca.repo = repo;
2887 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2888 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2890 66bea077 2018-08-02 stsp free(in_repo_path);
2891 66bea077 2018-08-02 stsp free(repo_path);
2892 66bea077 2018-08-02 stsp free(cwd);
2893 404c43c4 2018-06-21 stsp free(commit_id);
2894 28315671 2019-08-14 stsp free(obj_id);
2896 28315671 2019-08-14 stsp got_object_blob_close(blob);
2897 0c06baac 2019-02-05 stsp if (worktree)
2898 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2899 ad242220 2018-09-08 stsp if (repo) {
2900 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2901 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2902 ad242220 2018-09-08 stsp if (error == NULL)
2903 ad242220 2018-09-08 stsp error = repo_error;
2905 3affba96 2019-09-22 stsp if (bca.lines) {
2906 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2907 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2908 3affba96 2019-09-22 stsp free(bline->id_str);
2909 3affba96 2019-09-22 stsp free(bline->committer);
2911 3affba96 2019-09-22 stsp free(bca.lines);
2913 28315671 2019-08-14 stsp free(bca.line_offsets);
2914 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2915 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2916 404c43c4 2018-06-21 stsp return error;
2919 5de5890b 2018-10-18 stsp __dead static void
2920 5de5890b 2018-10-18 stsp usage_tree(void)
2922 c1669e2e 2019-01-09 stsp fprintf(stderr,
2923 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2924 5de5890b 2018-10-18 stsp getprogname());
2928 c1669e2e 2019-01-09 stsp static void
2929 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2930 c1669e2e 2019-01-09 stsp const char *root_path)
2932 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2933 848d6979 2019-08-12 stsp const char *modestr = "";
2934 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2936 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2937 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2940 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2941 63c5ca5d 2019-08-24 stsp modestr = "$";
2942 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2943 848d6979 2019-08-12 stsp modestr = "@";
2944 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2945 848d6979 2019-08-12 stsp modestr = "/";
2946 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2947 848d6979 2019-08-12 stsp modestr = "*";
2949 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2950 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2953 5de5890b 2018-10-18 stsp static const struct got_error *
2954 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2955 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2956 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2958 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2959 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2960 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2961 56e0773d 2019-11-28 stsp int nentries, i;
2963 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2965 5de5890b 2018-10-18 stsp goto done;
2967 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2969 5de5890b 2018-10-18 stsp goto done;
2970 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2971 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2972 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2973 5de5890b 2018-10-18 stsp char *id = NULL;
2975 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2978 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2979 5de5890b 2018-10-18 stsp if (show_ids) {
2980 5de5890b 2018-10-18 stsp char *id_str;
2981 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2982 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2984 5de5890b 2018-10-18 stsp goto done;
2985 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2986 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2987 5de5890b 2018-10-18 stsp free(id_str);
2988 5de5890b 2018-10-18 stsp goto done;
2990 5de5890b 2018-10-18 stsp free(id_str);
2992 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2995 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2996 c1669e2e 2019-01-09 stsp char *child_path;
2997 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2998 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2999 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3000 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3001 c1669e2e 2019-01-09 stsp goto done;
3003 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3004 c1669e2e 2019-01-09 stsp root_path, repo);
3005 c1669e2e 2019-01-09 stsp free(child_path);
3007 c1669e2e 2019-01-09 stsp goto done;
3012 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3013 5de5890b 2018-10-18 stsp free(tree_id);
3014 5de5890b 2018-10-18 stsp return err;
3017 5de5890b 2018-10-18 stsp static const struct got_error *
3018 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3020 5de5890b 2018-10-18 stsp const struct got_error *error;
3021 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3022 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3023 7a2c19d6 2019-02-05 stsp const char *path;
3024 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3025 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3026 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3027 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3030 5de5890b 2018-10-18 stsp #ifndef PROFILE
3031 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3032 0f8d269b 2019-01-04 stsp NULL) == -1)
3033 5de5890b 2018-10-18 stsp err(1, "pledge");
3036 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3037 5de5890b 2018-10-18 stsp switch (ch) {
3039 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3042 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3043 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3044 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3046 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3049 5de5890b 2018-10-18 stsp show_ids = 1;
3052 c1669e2e 2019-01-09 stsp recurse = 1;
3055 2deda0b9 2019-03-07 stsp usage_tree();
3056 5de5890b 2018-10-18 stsp /* NOTREACHED */
3060 5de5890b 2018-10-18 stsp argc -= optind;
3061 5de5890b 2018-10-18 stsp argv += optind;
3063 5de5890b 2018-10-18 stsp if (argc == 1)
3064 5de5890b 2018-10-18 stsp path = argv[0];
3065 5de5890b 2018-10-18 stsp else if (argc > 1)
3066 5de5890b 2018-10-18 stsp usage_tree();
3068 7a2c19d6 2019-02-05 stsp path = NULL;
3070 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3071 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3072 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3073 9bf7a39b 2019-02-05 stsp goto done;
3075 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3076 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3077 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3078 7a2c19d6 2019-02-05 stsp goto done;
3080 7a2c19d6 2019-02-05 stsp error = NULL;
3081 7a2c19d6 2019-02-05 stsp if (worktree) {
3082 7a2c19d6 2019-02-05 stsp repo_path =
3083 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3084 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3085 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3086 7a2c19d6 2019-02-05 stsp if (error)
3087 7a2c19d6 2019-02-05 stsp goto done;
3089 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3090 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3091 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3092 7a2c19d6 2019-02-05 stsp goto done;
3097 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3098 5de5890b 2018-10-18 stsp if (error != NULL)
3099 c02c541e 2019-03-29 stsp goto done;
3101 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3102 c02c541e 2019-03-29 stsp if (error)
3103 5de5890b 2018-10-18 stsp goto done;
3105 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3106 9bf7a39b 2019-02-05 stsp if (worktree) {
3107 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3108 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3109 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3110 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3111 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3112 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3113 9bf7a39b 2019-02-05 stsp goto done;
3115 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3117 9bf7a39b 2019-02-05 stsp if (error)
3118 9bf7a39b 2019-02-05 stsp goto done;
3120 9bf7a39b 2019-02-05 stsp path = "/";
3122 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3123 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3124 9bf7a39b 2019-02-05 stsp if (error != NULL)
3125 9bf7a39b 2019-02-05 stsp goto done;
3128 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3129 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3130 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3131 5de5890b 2018-10-18 stsp if (error != NULL)
3132 5de5890b 2018-10-18 stsp goto done;
3133 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3134 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3135 5de5890b 2018-10-18 stsp if (error != NULL)
3136 5de5890b 2018-10-18 stsp goto done;
3138 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3139 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3140 30837e32 2019-07-25 stsp if (error)
3141 5de5890b 2018-10-18 stsp goto done;
3144 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3145 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3147 5de5890b 2018-10-18 stsp free(in_repo_path);
3148 5de5890b 2018-10-18 stsp free(repo_path);
3149 5de5890b 2018-10-18 stsp free(cwd);
3150 5de5890b 2018-10-18 stsp free(commit_id);
3151 7a2c19d6 2019-02-05 stsp if (worktree)
3152 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3153 5de5890b 2018-10-18 stsp if (repo) {
3154 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3155 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3156 5de5890b 2018-10-18 stsp if (error == NULL)
3157 5de5890b 2018-10-18 stsp error = repo_error;
3159 5de5890b 2018-10-18 stsp return error;
3162 6bad629b 2019-02-04 stsp __dead static void
3163 6bad629b 2019-02-04 stsp usage_status(void)
3165 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3169 b72f483a 2019-02-05 stsp static const struct got_error *
3170 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3171 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3172 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3173 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3175 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3176 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3177 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3178 b72f483a 2019-02-05 stsp return NULL;
3181 6bad629b 2019-02-04 stsp static const struct got_error *
3182 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3184 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3185 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3186 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3187 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3188 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3189 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3192 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3194 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3195 6bad629b 2019-02-04 stsp switch (ch) {
3197 2deda0b9 2019-03-07 stsp usage_status();
3198 6bad629b 2019-02-04 stsp /* NOTREACHED */
3202 6bad629b 2019-02-04 stsp argc -= optind;
3203 6bad629b 2019-02-04 stsp argv += optind;
3205 6bad629b 2019-02-04 stsp #ifndef PROFILE
3206 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3207 6bad629b 2019-02-04 stsp NULL) == -1)
3208 6bad629b 2019-02-04 stsp err(1, "pledge");
3210 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3211 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3212 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3213 927df6b7 2019-02-10 stsp goto done;
3216 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3217 927df6b7 2019-02-10 stsp if (error != NULL)
3218 a5edda0a 2019-07-27 stsp goto done;
3220 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3222 6bad629b 2019-02-04 stsp if (error != NULL)
3223 6bad629b 2019-02-04 stsp goto done;
3225 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3226 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3227 087fb88c 2019-08-04 stsp if (error)
3228 087fb88c 2019-08-04 stsp goto done;
3230 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3231 6bad629b 2019-02-04 stsp if (error)
3232 6bad629b 2019-02-04 stsp goto done;
3234 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3235 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3237 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3238 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3239 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3240 927df6b7 2019-02-10 stsp free(cwd);
3241 d0eebce4 2019-03-11 stsp return error;
3244 d0eebce4 2019-03-11 stsp __dead static void
3245 d0eebce4 2019-03-11 stsp usage_ref(void)
3247 d0eebce4 2019-03-11 stsp fprintf(stderr,
3248 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3249 d0eebce4 2019-03-11 stsp getprogname());
3253 d0eebce4 2019-03-11 stsp static const struct got_error *
3254 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3256 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3257 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3258 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3260 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3261 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3263 d0eebce4 2019-03-11 stsp return err;
3265 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3266 d0eebce4 2019-03-11 stsp char *refstr;
3267 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3268 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3269 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3270 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3271 d0eebce4 2019-03-11 stsp free(refstr);
3274 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3275 d0eebce4 2019-03-11 stsp return NULL;
3278 d0eebce4 2019-03-11 stsp static const struct got_error *
3279 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3281 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3282 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3284 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3286 d0eebce4 2019-03-11 stsp return err;
3288 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3289 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3290 d0eebce4 2019-03-11 stsp return err;
3293 d0eebce4 2019-03-11 stsp static const struct got_error *
3294 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3296 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3297 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3298 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3301 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3302 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3303 d1644381 2019-07-14 stsp * an unintended typo.
3305 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3306 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3308 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3310 d83d9d5c 2019-05-13 stsp if (err) {
3311 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3313 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3314 d83d9d5c 2019-05-13 stsp return err;
3315 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3317 d83d9d5c 2019-05-13 stsp return err;
3318 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3319 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3321 d83d9d5c 2019-05-13 stsp return err;
3324 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3326 d0eebce4 2019-03-11 stsp goto done;
3328 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3331 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3333 d1c1ae5f 2019-08-12 stsp return err;
3336 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3337 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3339 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3340 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3341 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3344 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3345 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3346 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3348 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3349 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3351 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3353 d1c1ae5f 2019-08-12 stsp return err;
3355 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3357 d1c1ae5f 2019-08-12 stsp goto done;
3359 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3361 d1c1ae5f 2019-08-12 stsp if (target_ref)
3362 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3364 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3365 d0eebce4 2019-03-11 stsp return err;
3368 d0eebce4 2019-03-11 stsp static const struct got_error *
3369 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3371 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3372 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3373 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3374 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3375 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3376 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3378 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3379 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3380 d0eebce4 2019-03-11 stsp switch (ch) {
3382 d0eebce4 2019-03-11 stsp delref = optarg;
3385 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3386 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3387 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3389 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3392 d0eebce4 2019-03-11 stsp do_list = 1;
3395 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3398 d0eebce4 2019-03-11 stsp usage_ref();
3399 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3403 d0eebce4 2019-03-11 stsp if (do_list && delref)
3404 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3406 d0eebce4 2019-03-11 stsp argc -= optind;
3407 d0eebce4 2019-03-11 stsp argv += optind;
3409 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3410 d1c1ae5f 2019-08-12 stsp if (create_symref)
3411 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3412 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3413 d0eebce4 2019-03-11 stsp if (argc > 0)
3414 d0eebce4 2019-03-11 stsp usage_ref();
3415 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3416 d0eebce4 2019-03-11 stsp usage_ref();
3418 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3419 e0b57350 2019-03-12 stsp if (do_list) {
3420 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3421 e0b57350 2019-03-12 stsp NULL) == -1)
3422 e0b57350 2019-03-12 stsp err(1, "pledge");
3424 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3425 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3426 e0b57350 2019-03-12 stsp err(1, "pledge");
3429 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3430 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3431 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3432 d0eebce4 2019-03-11 stsp goto done;
3435 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3436 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3437 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3438 d0eebce4 2019-03-11 stsp goto done;
3440 d0eebce4 2019-03-11 stsp error = NULL;
3441 d0eebce4 2019-03-11 stsp if (worktree) {
3442 d0eebce4 2019-03-11 stsp repo_path =
3443 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3444 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3445 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3446 d0eebce4 2019-03-11 stsp if (error)
3447 d0eebce4 2019-03-11 stsp goto done;
3449 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3450 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3451 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3452 d0eebce4 2019-03-11 stsp goto done;
3457 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3458 d0eebce4 2019-03-11 stsp if (error != NULL)
3459 d0eebce4 2019-03-11 stsp goto done;
3461 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3462 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3463 c02c541e 2019-03-29 stsp if (error)
3464 c02c541e 2019-03-29 stsp goto done;
3466 d0eebce4 2019-03-11 stsp if (do_list)
3467 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3468 d0eebce4 2019-03-11 stsp else if (delref)
3469 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3470 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3471 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3473 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3476 4e759de4 2019-06-26 stsp got_repo_close(repo);
3477 4e759de4 2019-06-26 stsp if (worktree)
3478 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3479 4e759de4 2019-06-26 stsp free(cwd);
3480 4e759de4 2019-06-26 stsp free(repo_path);
3481 4e759de4 2019-06-26 stsp return error;
3484 4e759de4 2019-06-26 stsp __dead static void
3485 4e759de4 2019-06-26 stsp usage_branch(void)
3487 4e759de4 2019-06-26 stsp fprintf(stderr,
3488 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3489 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3493 4e759de4 2019-06-26 stsp static const struct got_error *
3494 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3495 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3497 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3498 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3499 4e99b47e 2019-10-04 stsp char *refstr;
3501 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3502 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3503 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3504 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3506 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3508 4e99b47e 2019-10-04 stsp return err;
3509 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3510 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3511 4e99b47e 2019-10-04 stsp marker = "* ";
3513 4e99b47e 2019-10-04 stsp marker = "~ ";
3517 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3518 4e99b47e 2019-10-04 stsp refname += 11;
3519 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3520 4e99b47e 2019-10-04 stsp refname += 18;
3522 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3523 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3524 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3526 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3527 4e99b47e 2019-10-04 stsp free(refstr);
3528 4e99b47e 2019-10-04 stsp return NULL;
3531 4e99b47e 2019-10-04 stsp static const struct got_error *
3532 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3534 ad89fa31 2019-10-04 stsp const char *refname;
3536 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3537 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3539 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3541 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3542 ad89fa31 2019-10-04 stsp refname += 11;
3543 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3544 ad89fa31 2019-10-04 stsp refname += 18;
3546 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3548 ad89fa31 2019-10-04 stsp return NULL;
3551 ad89fa31 2019-10-04 stsp static const struct got_error *
3552 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3554 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3555 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3556 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3557 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3558 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3560 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3562 4e99b47e 2019-10-04 stsp if (worktree) {
3563 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3564 4e99b47e 2019-10-04 stsp worktree);
3566 4e99b47e 2019-10-04 stsp return err;
3568 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3569 4e99b47e 2019-10-04 stsp worktree);
3571 4e99b47e 2019-10-04 stsp return err;
3573 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3574 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3575 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3577 4e99b47e 2019-10-04 stsp return err;
3578 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3579 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3583 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3584 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3586 4e759de4 2019-06-26 stsp return err;
3588 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3589 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3591 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3592 4e759de4 2019-06-26 stsp return NULL;
3595 4e759de4 2019-06-26 stsp static const struct got_error *
3596 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3597 45cd4e47 2019-08-25 stsp const char *branch_name)
3599 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3600 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3601 4e759de4 2019-06-26 stsp char *refname;
3603 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3604 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3606 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3608 4e759de4 2019-06-26 stsp goto done;
3610 45cd4e47 2019-08-25 stsp if (worktree &&
3611 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3612 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3613 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3614 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3615 45cd4e47 2019-08-25 stsp goto done;
3618 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3621 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3622 4e759de4 2019-06-26 stsp free(refname);
3623 4e759de4 2019-06-26 stsp return err;
3626 4e759de4 2019-06-26 stsp static const struct got_error *
3627 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3628 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3630 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3631 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3632 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3635 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3636 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3637 d3f84d51 2019-07-11 stsp * an unintended typo.
3639 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3640 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3642 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3643 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3644 4e759de4 2019-06-26 stsp goto done;
3647 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3648 4e759de4 2019-06-26 stsp if (err == NULL) {
3649 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3650 4e759de4 2019-06-26 stsp goto done;
3651 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3652 4e759de4 2019-06-26 stsp goto done;
3654 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3656 4e759de4 2019-06-26 stsp goto done;
3658 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3661 4e759de4 2019-06-26 stsp got_ref_close(ref);
3662 4e759de4 2019-06-26 stsp free(base_refname);
3663 4e759de4 2019-06-26 stsp free(refname);
3664 4e759de4 2019-06-26 stsp return err;
3667 4e759de4 2019-06-26 stsp static const struct got_error *
3668 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3670 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3671 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3672 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3673 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3674 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3675 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3676 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3677 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3678 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3679 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3680 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3682 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3684 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3685 4e759de4 2019-06-26 stsp switch (ch) {
3687 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3690 4e759de4 2019-06-26 stsp delref = optarg;
3693 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3694 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3695 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3697 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3700 4e759de4 2019-06-26 stsp do_list = 1;
3703 da76fce2 2020-02-24 stsp do_update = 0;
3706 4e759de4 2019-06-26 stsp usage_branch();
3707 4e759de4 2019-06-26 stsp /* NOTREACHED */
3711 4e759de4 2019-06-26 stsp if (do_list && delref)
3712 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3714 4e759de4 2019-06-26 stsp argc -= optind;
3715 4e759de4 2019-06-26 stsp argv += optind;
3717 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3718 ad89fa31 2019-10-04 stsp do_show = 1;
3720 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3721 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3723 4e759de4 2019-06-26 stsp if (do_list || delref) {
3724 4e759de4 2019-06-26 stsp if (argc > 0)
3725 4e759de4 2019-06-26 stsp usage_branch();
3726 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3727 4e759de4 2019-06-26 stsp usage_branch();
3729 4e759de4 2019-06-26 stsp #ifndef PROFILE
3730 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3731 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3732 4e759de4 2019-06-26 stsp NULL) == -1)
3733 4e759de4 2019-06-26 stsp err(1, "pledge");
3735 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3736 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3737 4e759de4 2019-06-26 stsp err(1, "pledge");
3740 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3741 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3742 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3743 4e759de4 2019-06-26 stsp goto done;
3746 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3747 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3748 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3749 4e759de4 2019-06-26 stsp goto done;
3751 4e759de4 2019-06-26 stsp error = NULL;
3752 4e759de4 2019-06-26 stsp if (worktree) {
3753 4e759de4 2019-06-26 stsp repo_path =
3754 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3755 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3756 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3757 4e759de4 2019-06-26 stsp if (error)
3758 4e759de4 2019-06-26 stsp goto done;
3760 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3761 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3762 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3763 4e759de4 2019-06-26 stsp goto done;
3768 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3769 4e759de4 2019-06-26 stsp if (error != NULL)
3770 4e759de4 2019-06-26 stsp goto done;
3772 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3773 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3774 4e759de4 2019-06-26 stsp if (error)
3775 4e759de4 2019-06-26 stsp goto done;
3777 ad89fa31 2019-10-04 stsp if (do_show)
3778 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3779 ad89fa31 2019-10-04 stsp else if (do_list)
3780 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3781 4e759de4 2019-06-26 stsp else if (delref)
3782 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3784 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3785 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3786 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3787 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3788 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3789 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3790 a74f7e83 2019-11-10 stsp if (error)
3791 a74f7e83 2019-11-10 stsp goto done;
3792 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3793 da76fce2 2020-02-24 stsp if (error)
3794 da76fce2 2020-02-24 stsp goto done;
3795 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3796 da76fce2 2020-02-24 stsp int did_something = 0;
3797 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3799 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3800 da76fce2 2020-02-24 stsp if (error)
3801 da76fce2 2020-02-24 stsp goto done;
3802 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3803 da76fce2 2020-02-24 stsp worktree);
3804 da76fce2 2020-02-24 stsp if (error)
3805 da76fce2 2020-02-24 stsp goto done;
3806 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3808 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3809 da76fce2 2020-02-24 stsp goto done;
3811 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
3812 da76fce2 2020-02-24 stsp free(branch_refname);
3813 da76fce2 2020-02-24 stsp if (error)
3814 da76fce2 2020-02-24 stsp goto done;
3815 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
3817 da76fce2 2020-02-24 stsp if (error)
3818 da76fce2 2020-02-24 stsp goto done;
3819 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3820 da76fce2 2020-02-24 stsp commit_id);
3821 da76fce2 2020-02-24 stsp if (error)
3822 da76fce2 2020-02-24 stsp goto done;
3823 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
3824 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
3825 da76fce2 2020-02-24 stsp check_cancelled, NULL);
3826 da76fce2 2020-02-24 stsp if (error)
3827 da76fce2 2020-02-24 stsp goto done;
3828 da76fce2 2020-02-24 stsp if (did_something)
3829 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
3834 da76fce2 2020-02-24 stsp got_ref_close(ref);
3836 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3837 d0eebce4 2019-03-11 stsp if (worktree)
3838 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3839 d0eebce4 2019-03-11 stsp free(cwd);
3840 d0eebce4 2019-03-11 stsp free(repo_path);
3841 da76fce2 2020-02-24 stsp free(commit_id);
3842 da76fce2 2020-02-24 stsp free(commit_id_str);
3843 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
3844 da76fce2 2020-02-24 stsp free((char *)pe->path);
3845 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
3846 d00136be 2019-03-26 stsp return error;
3850 d00136be 2019-03-26 stsp __dead static void
3851 8e7bd50a 2019-08-22 stsp usage_tag(void)
3853 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3854 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
3855 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
3860 b8bad2ba 2019-08-23 stsp static const struct got_error *
3861 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3863 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3864 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3865 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3866 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3867 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3869 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3870 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3871 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3872 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3874 b8bad2ba 2019-08-23 stsp return err;
3875 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3878 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3881 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3882 b8bad2ba 2019-08-23 stsp free(re_id);
3885 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3886 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3889 b8bad2ba 2019-08-23 stsp while (se) {
3890 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3893 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3894 b8bad2ba 2019-08-23 stsp free(se_id);
3897 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3898 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3900 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3901 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3903 b8bad2ba 2019-08-23 stsp return err;
3904 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3907 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3912 b8bad2ba 2019-08-23 stsp return err;
3916 b8bad2ba 2019-08-23 stsp static const struct got_error *
3917 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3919 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3920 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3921 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3923 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3925 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3927 8e7bd50a 2019-08-22 stsp return err;
3929 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3930 8e7bd50a 2019-08-22 stsp const char *refname;
3931 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3932 8e7bd50a 2019-08-22 stsp char datebuf[26];
3933 d4efa91b 2020-01-14 stsp const char *tagger;
3934 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3935 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3936 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3937 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3939 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3940 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3942 8e7bd50a 2019-08-22 stsp refname += 10;
3943 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3944 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3945 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3948 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3949 8e7bd50a 2019-08-22 stsp free(refstr);
3951 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3954 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3955 d4efa91b 2020-01-14 stsp if (err) {
3956 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3960 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3961 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
3962 d4efa91b 2020-01-14 stsp if (err) {
3966 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
3967 d4efa91b 2020-01-14 stsp tagger_time =
3968 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
3969 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
3975 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
3976 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3977 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
3978 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
3982 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
3983 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3984 2417344c 2019-08-23 stsp if (datestr)
3985 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3986 d4efa91b 2020-01-14 stsp if (commit)
3987 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3989 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
3990 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
3991 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3994 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
3995 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3998 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
3999 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4002 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4003 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4010 8e7bd50a 2019-08-22 stsp free(id_str);
4011 d4efa91b 2020-01-14 stsp if (commit) {
4012 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4015 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4017 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4018 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4019 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4020 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4025 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4027 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4029 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4030 8e7bd50a 2019-08-22 stsp } while (line);
4031 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4034 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4035 8e7bd50a 2019-08-22 stsp return NULL;
4038 8e7bd50a 2019-08-22 stsp static const struct got_error *
4039 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4040 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4042 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4043 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4044 f372d5cd 2019-10-21 stsp char *editor = NULL;
4045 8e7bd50a 2019-08-22 stsp int fd = -1;
4047 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4048 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4049 8e7bd50a 2019-08-22 stsp goto done;
4052 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4053 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4054 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4055 8e7bd50a 2019-08-22 stsp goto done;
4058 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4060 8e7bd50a 2019-08-22 stsp goto done;
4062 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4063 8e7bd50a 2019-08-22 stsp close(fd);
4065 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4067 8e7bd50a 2019-08-22 stsp goto done;
4068 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4070 8e7bd50a 2019-08-22 stsp free(initial_content);
4071 8e7bd50a 2019-08-22 stsp free(template);
4072 8e7bd50a 2019-08-22 stsp free(editor);
4074 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4075 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4076 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4077 8e7bd50a 2019-08-22 stsp if (err) {
4078 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4079 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4082 8e7bd50a 2019-08-22 stsp return err;
4085 8e7bd50a 2019-08-22 stsp static const struct got_error *
4086 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4087 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4089 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4090 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4091 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4092 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4093 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4094 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4095 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4098 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4099 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4100 8e7bd50a 2019-08-22 stsp * an unintended typo.
4102 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4103 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4105 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4107 8e7bd50a 2019-08-22 stsp return err;
4109 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4110 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4112 8e7bd50a 2019-08-22 stsp goto done;
4114 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4116 8e7bd50a 2019-08-22 stsp goto done;
4118 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4119 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4120 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4121 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4122 8e7bd50a 2019-08-22 stsp goto done;
4124 8e7bd50a 2019-08-22 stsp tag_name += 10;
4125 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4126 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4127 8e7bd50a 2019-08-22 stsp goto done;
4130 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4131 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4132 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4133 8e7bd50a 2019-08-22 stsp goto done;
4134 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4135 8e7bd50a 2019-08-22 stsp goto done;
4137 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4138 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4139 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4140 f372d5cd 2019-10-21 stsp if (err) {
4141 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4142 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4143 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4144 8e7bd50a 2019-08-22 stsp goto done;
4148 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4149 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4150 f372d5cd 2019-10-21 stsp if (err) {
4151 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4152 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4153 8e7bd50a 2019-08-22 stsp goto done;
4156 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4157 f372d5cd 2019-10-21 stsp if (err) {
4158 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4159 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4160 8e7bd50a 2019-08-22 stsp goto done;
4163 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4164 f372d5cd 2019-10-21 stsp if (err) {
4165 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4166 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4167 f372d5cd 2019-10-21 stsp goto done;
4170 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4171 f372d5cd 2019-10-21 stsp if (err) {
4172 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4173 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4174 f372d5cd 2019-10-21 stsp goto done;
4176 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4178 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4179 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4180 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4181 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4182 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4183 f372d5cd 2019-10-21 stsp free(tag_id_str);
4185 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4186 8e7bd50a 2019-08-22 stsp free(commit_id);
4187 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4188 8e7bd50a 2019-08-22 stsp free(refname);
4189 8e7bd50a 2019-08-22 stsp free(tagmsg);
4190 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4191 aba9c984 2019-09-08 stsp free(tagger);
4192 8e7bd50a 2019-08-22 stsp return err;
4195 8e7bd50a 2019-08-22 stsp static const struct got_error *
4196 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4198 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4199 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4200 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4201 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4202 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4203 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4204 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4206 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4207 8e7bd50a 2019-08-22 stsp switch (ch) {
4209 80106605 2020-02-24 stsp commit_id_arg = optarg;
4212 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4215 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4216 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4217 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4219 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4222 8e7bd50a 2019-08-22 stsp do_list = 1;
4225 8e7bd50a 2019-08-22 stsp usage_tag();
4226 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4230 8e7bd50a 2019-08-22 stsp argc -= optind;
4231 8e7bd50a 2019-08-22 stsp argv += optind;
4233 8e7bd50a 2019-08-22 stsp if (do_list) {
4234 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4235 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4236 8e7bd50a 2019-08-22 stsp if (tagmsg)
4237 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4238 8e7bd50a 2019-08-22 stsp if (argc > 0)
4239 8e7bd50a 2019-08-22 stsp usage_tag();
4240 80106605 2020-02-24 stsp } else if (argc != 1)
4241 8e7bd50a 2019-08-22 stsp usage_tag();
4243 a2887370 2019-08-23 stsp tag_name = argv[0];
4245 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4246 8e7bd50a 2019-08-22 stsp if (do_list) {
4247 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4248 8e7bd50a 2019-08-22 stsp NULL) == -1)
4249 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4251 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4252 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4253 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4256 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4257 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4258 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4259 8e7bd50a 2019-08-22 stsp goto done;
4262 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4263 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4264 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4265 8e7bd50a 2019-08-22 stsp goto done;
4267 8e7bd50a 2019-08-22 stsp error = NULL;
4268 8e7bd50a 2019-08-22 stsp if (worktree) {
4269 8e7bd50a 2019-08-22 stsp repo_path =
4270 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4271 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4272 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4273 8e7bd50a 2019-08-22 stsp if (error)
4274 8e7bd50a 2019-08-22 stsp goto done;
4276 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4277 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4278 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4279 8e7bd50a 2019-08-22 stsp goto done;
4284 8e7bd50a 2019-08-22 stsp if (do_list) {
4285 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4286 c9956ddf 2019-09-08 stsp if (error != NULL)
4287 c9956ddf 2019-09-08 stsp goto done;
4288 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4289 8e7bd50a 2019-08-22 stsp if (error)
4290 8e7bd50a 2019-08-22 stsp goto done;
4291 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4293 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4294 c9956ddf 2019-09-08 stsp if (error)
4295 c9956ddf 2019-09-08 stsp goto done;
4296 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4297 c9956ddf 2019-09-08 stsp if (error != NULL)
4298 c9956ddf 2019-09-08 stsp goto done;
4300 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4301 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4302 8e7bd50a 2019-08-22 stsp if (error)
4303 8e7bd50a 2019-08-22 stsp goto done;
4306 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4307 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4308 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4309 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4310 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4311 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4312 8e7bd50a 2019-08-22 stsp if (error)
4313 8e7bd50a 2019-08-22 stsp goto done;
4314 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4315 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4316 8e7bd50a 2019-08-22 stsp if (error)
4317 8e7bd50a 2019-08-22 stsp goto done;
4318 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4319 8e7bd50a 2019-08-22 stsp free(commit_id);
4320 8e7bd50a 2019-08-22 stsp if (error)
4321 8e7bd50a 2019-08-22 stsp goto done;
4324 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4325 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4329 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4330 8e7bd50a 2019-08-22 stsp if (worktree)
4331 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4332 8e7bd50a 2019-08-22 stsp free(cwd);
4333 8e7bd50a 2019-08-22 stsp free(repo_path);
4334 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4335 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4336 8e7bd50a 2019-08-22 stsp return error;
4339 8e7bd50a 2019-08-22 stsp __dead static void
4340 d00136be 2019-03-26 stsp usage_add(void)
4342 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4343 022fae89 2019-12-06 tracey getprogname());
4347 d00136be 2019-03-26 stsp static const struct got_error *
4348 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4350 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4352 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4353 4e68cba3 2019-11-23 stsp return NULL;
4356 4e68cba3 2019-11-23 stsp static const struct got_error *
4357 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4359 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4360 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4361 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4362 1dd54920 2019-05-11 stsp char *cwd = NULL;
4363 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4364 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4365 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4367 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4369 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4370 d00136be 2019-03-26 stsp switch (ch) {
4371 022fae89 2019-12-06 tracey case 'I':
4372 022fae89 2019-12-06 tracey no_ignores = 1;
4375 4e68cba3 2019-11-23 stsp can_recurse = 1;
4378 d00136be 2019-03-26 stsp usage_add();
4379 d00136be 2019-03-26 stsp /* NOTREACHED */
4383 d00136be 2019-03-26 stsp argc -= optind;
4384 d00136be 2019-03-26 stsp argv += optind;
4386 43012d58 2019-07-14 stsp #ifndef PROFILE
4387 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4388 43012d58 2019-07-14 stsp NULL) == -1)
4389 43012d58 2019-07-14 stsp err(1, "pledge");
4391 723c305c 2019-05-11 jcs if (argc < 1)
4392 d00136be 2019-03-26 stsp usage_add();
4394 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4395 d00136be 2019-03-26 stsp if (cwd == NULL) {
4396 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4397 d00136be 2019-03-26 stsp goto done;
4400 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4401 d00136be 2019-03-26 stsp if (error)
4402 d00136be 2019-03-26 stsp goto done;
4404 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4406 031a5338 2019-03-26 stsp if (error != NULL)
4407 031a5338 2019-03-26 stsp goto done;
4409 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4410 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4411 d00136be 2019-03-26 stsp if (error)
4412 d00136be 2019-03-26 stsp goto done;
4414 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4415 6d022e97 2019-08-04 stsp if (error)
4416 022fae89 2019-12-06 tracey goto done;
4418 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4419 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4420 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4421 6d022e97 2019-08-04 stsp goto done;
4425 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4426 4e68cba3 2019-11-23 stsp char *ondisk_path;
4427 4e68cba3 2019-11-23 stsp struct stat sb;
4428 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4429 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4430 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4431 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4432 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4433 4e68cba3 2019-11-23 stsp goto done;
4435 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4436 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4437 4e68cba3 2019-11-23 stsp free(ondisk_path);
4440 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4441 4e68cba3 2019-11-23 stsp ondisk_path);
4442 4e68cba3 2019-11-23 stsp free(ondisk_path);
4443 4e68cba3 2019-11-23 stsp goto done;
4445 4e68cba3 2019-11-23 stsp free(ondisk_path);
4446 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4447 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4448 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4449 4e68cba3 2019-11-23 stsp goto done;
4454 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4455 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4458 031a5338 2019-03-26 stsp got_repo_close(repo);
4459 d00136be 2019-03-26 stsp if (worktree)
4460 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4461 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4462 1dd54920 2019-05-11 stsp free((char *)pe->path);
4463 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4464 2ec1f75b 2019-03-26 stsp free(cwd);
4465 2ec1f75b 2019-03-26 stsp return error;
4468 2ec1f75b 2019-03-26 stsp __dead static void
4469 648e4ef7 2019-07-09 stsp usage_remove(void)
4471 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4472 f2a9dc41 2019-12-13 tracey getprogname());
4476 2a06fe5f 2019-08-24 stsp static const struct got_error *
4477 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4478 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4480 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4482 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4483 2a06fe5f 2019-08-24 stsp return NULL;
4484 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4485 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4486 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4487 2a06fe5f 2019-08-24 stsp return NULL;
4490 2ec1f75b 2019-03-26 stsp static const struct got_error *
4491 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4493 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4494 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4495 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4496 17ed4618 2019-06-02 stsp char *cwd = NULL;
4497 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4498 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4499 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4501 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4503 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4504 2ec1f75b 2019-03-26 stsp switch (ch) {
4506 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4508 70e3e7f5 2019-12-13 tracey case 'k':
4509 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4511 f2a9dc41 2019-12-13 tracey case 'R':
4512 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4515 f2a9dc41 2019-12-13 tracey usage_remove();
4516 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4520 2ec1f75b 2019-03-26 stsp argc -= optind;
4521 2ec1f75b 2019-03-26 stsp argv += optind;
4523 43012d58 2019-07-14 stsp #ifndef PROFILE
4524 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4525 43012d58 2019-07-14 stsp NULL) == -1)
4526 43012d58 2019-07-14 stsp err(1, "pledge");
4528 17ed4618 2019-06-02 stsp if (argc < 1)
4529 648e4ef7 2019-07-09 stsp usage_remove();
4531 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4532 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4533 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4534 2ec1f75b 2019-03-26 stsp goto done;
4536 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4537 2ec1f75b 2019-03-26 stsp if (error)
4538 2ec1f75b 2019-03-26 stsp goto done;
4540 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4543 2ec1f75b 2019-03-26 stsp goto done;
4545 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4546 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4547 2ec1f75b 2019-03-26 stsp if (error)
4548 2ec1f75b 2019-03-26 stsp goto done;
4550 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4551 6d022e97 2019-08-04 stsp if (error)
4552 6d022e97 2019-08-04 stsp goto done;
4554 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4555 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4556 f2a9dc41 2019-12-13 tracey struct stat sb;
4557 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4558 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4559 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4560 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4561 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4562 f2a9dc41 2019-12-13 tracey goto done;
4564 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4565 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4566 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4567 f2a9dc41 2019-12-13 tracey continue;
4569 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4570 f2a9dc41 2019-12-13 tracey ondisk_path);
4571 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4572 f2a9dc41 2019-12-13 tracey goto done;
4574 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4575 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4576 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4577 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4578 f2a9dc41 2019-12-13 tracey goto done;
4583 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4584 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4587 a129376b 2019-03-28 stsp got_repo_close(repo);
4588 a129376b 2019-03-28 stsp if (worktree)
4589 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4590 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4591 17ed4618 2019-06-02 stsp free((char *)pe->path);
4592 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4593 a129376b 2019-03-28 stsp free(cwd);
4594 a129376b 2019-03-28 stsp return error;
4597 a129376b 2019-03-28 stsp __dead static void
4598 a129376b 2019-03-28 stsp usage_revert(void)
4600 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4601 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4605 1ee397ad 2019-07-12 stsp static const struct got_error *
4606 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4608 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4609 fb9704af 2020-01-27 stsp return NULL;
4611 a129376b 2019-03-28 stsp while (path[0] == '/')
4613 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4614 33aa809d 2019-08-08 stsp return NULL;
4617 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4618 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4619 33aa809d 2019-08-08 stsp const char *action;
4622 33aa809d 2019-08-08 stsp static const struct got_error *
4623 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4624 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4626 33aa809d 2019-08-08 stsp char *line = NULL;
4627 33aa809d 2019-08-08 stsp size_t linesize = 0;
4628 33aa809d 2019-08-08 stsp ssize_t linelen;
4630 33aa809d 2019-08-08 stsp switch (status) {
4631 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4632 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4634 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4635 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4637 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4638 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4639 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4640 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4641 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4642 33aa809d 2019-08-08 stsp printf("%s", line);
4643 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4644 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4645 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4646 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4647 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4650 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4653 33aa809d 2019-08-08 stsp return NULL;
4656 33aa809d 2019-08-08 stsp static const struct got_error *
4657 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4658 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4660 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4661 33aa809d 2019-08-08 stsp char *line = NULL;
4662 33aa809d 2019-08-08 stsp size_t linesize = 0;
4663 33aa809d 2019-08-08 stsp ssize_t linelen;
4664 33aa809d 2019-08-08 stsp int resp = ' ';
4665 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4667 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4669 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4671 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4672 33aa809d 2019-08-08 stsp a->action);
4674 33aa809d 2019-08-08 stsp return err;
4675 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4676 33aa809d 2019-08-08 stsp if (linelen == -1) {
4677 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4678 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4679 33aa809d 2019-08-08 stsp return NULL;
4681 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4683 33aa809d 2019-08-08 stsp *nl = '\0';
4684 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4685 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4686 33aa809d 2019-08-08 stsp printf("y\n");
4687 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4688 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4689 33aa809d 2019-08-08 stsp printf("n\n");
4690 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4691 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4692 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4693 33aa809d 2019-08-08 stsp printf("q\n");
4695 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4696 33aa809d 2019-08-08 stsp free(line);
4697 33aa809d 2019-08-08 stsp return NULL;
4700 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4701 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4702 33aa809d 2019-08-08 stsp a->action);
4704 33aa809d 2019-08-08 stsp return err;
4705 33aa809d 2019-08-08 stsp resp = getchar();
4706 33aa809d 2019-08-08 stsp if (resp == '\n')
4707 33aa809d 2019-08-08 stsp resp = getchar();
4708 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4709 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4710 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4711 33aa809d 2019-08-08 stsp resp = ' ';
4713 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4714 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4715 33aa809d 2019-08-08 stsp resp = ' ';
4719 33aa809d 2019-08-08 stsp if (resp == 'y')
4720 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4721 33aa809d 2019-08-08 stsp else if (resp == 'n')
4722 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4723 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4724 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4726 1ee397ad 2019-07-12 stsp return NULL;
4730 a129376b 2019-03-28 stsp static const struct got_error *
4731 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4733 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4734 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4735 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4736 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4737 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4738 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4739 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4740 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4741 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4742 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4744 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4746 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4747 a129376b 2019-03-28 stsp switch (ch) {
4749 33aa809d 2019-08-08 stsp pflag = 1;
4752 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4755 0f6d7415 2019-08-08 stsp can_recurse = 1;
4758 a129376b 2019-03-28 stsp usage_revert();
4759 a129376b 2019-03-28 stsp /* NOTREACHED */
4763 a129376b 2019-03-28 stsp argc -= optind;
4764 a129376b 2019-03-28 stsp argv += optind;
4766 43012d58 2019-07-14 stsp #ifndef PROFILE
4767 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4768 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4769 43012d58 2019-07-14 stsp err(1, "pledge");
4771 e20a8b6f 2019-06-04 stsp if (argc < 1)
4772 a129376b 2019-03-28 stsp usage_revert();
4773 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4774 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4776 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4777 a129376b 2019-03-28 stsp if (cwd == NULL) {
4778 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4779 a129376b 2019-03-28 stsp goto done;
4781 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4782 2ec1f75b 2019-03-26 stsp if (error)
4783 2ec1f75b 2019-03-26 stsp goto done;
4785 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4787 a129376b 2019-03-28 stsp if (error != NULL)
4788 a129376b 2019-03-28 stsp goto done;
4790 33aa809d 2019-08-08 stsp if (patch_script_path) {
4791 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4792 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4793 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4794 33aa809d 2019-08-08 stsp patch_script_path);
4795 33aa809d 2019-08-08 stsp goto done;
4798 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4799 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4800 a129376b 2019-03-28 stsp if (error)
4801 a129376b 2019-03-28 stsp goto done;
4803 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4804 6d022e97 2019-08-04 stsp if (error)
4805 6d022e97 2019-08-04 stsp goto done;
4807 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4808 0f6d7415 2019-08-08 stsp char *ondisk_path;
4809 0f6d7415 2019-08-08 stsp struct stat sb;
4810 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4811 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4812 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4813 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4814 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4815 0f6d7415 2019-08-08 stsp goto done;
4817 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4818 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4819 0f6d7415 2019-08-08 stsp free(ondisk_path);
4822 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4823 0f6d7415 2019-08-08 stsp ondisk_path);
4824 0f6d7415 2019-08-08 stsp free(ondisk_path);
4825 0f6d7415 2019-08-08 stsp goto done;
4827 0f6d7415 2019-08-08 stsp free(ondisk_path);
4828 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4829 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4830 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4831 0f6d7415 2019-08-08 stsp goto done;
4836 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4837 33aa809d 2019-08-08 stsp cpa.action = "revert";
4838 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4839 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4841 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4842 33aa809d 2019-08-08 stsp error == NULL)
4843 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4845 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4846 2ec1f75b 2019-03-26 stsp if (worktree)
4847 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4848 2ec1f75b 2019-03-26 stsp free(path);
4849 d00136be 2019-03-26 stsp free(cwd);
4850 6bad629b 2019-02-04 stsp return error;
4853 c4296144 2019-05-09 stsp __dead static void
4854 c4296144 2019-05-09 stsp usage_commit(void)
4856 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4857 5c1e53bc 2019-07-28 stsp getprogname());
4861 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4862 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4863 e2ba3d07 2019-05-13 stsp const char *editor;
4864 e0870e44 2019-05-13 stsp const char *worktree_path;
4865 76d98825 2019-06-03 stsp const char *branch_name;
4866 314a6357 2019-05-13 stsp const char *repo_path;
4867 e0870e44 2019-05-13 stsp char *logmsg_path;
4871 33ad4cbe 2019-05-12 jcs static const struct got_error *
4872 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4875 76d98825 2019-06-03 stsp char *initial_content = NULL;
4876 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4877 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4878 e0870e44 2019-05-13 stsp char *template = NULL;
4879 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4881 33ad4cbe 2019-05-12 jcs size_t len;
4883 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4884 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4885 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4886 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4887 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4888 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4889 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4890 33ad4cbe 2019-05-12 jcs return NULL;
4893 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4894 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4896 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4897 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4898 76d98825 2019-06-03 stsp a->branch_name) == -1)
4899 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4901 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4903 e0870e44 2019-05-13 stsp goto done;
4905 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4907 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4908 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4909 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4910 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4911 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4915 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4917 76d98825 2019-06-03 stsp free(initial_content);
4918 e0870e44 2019-05-13 stsp free(template);
4920 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4921 3ce1b845 2019-07-15 stsp if (err == NULL) {
4922 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4923 3ce1b845 2019-07-15 stsp if (err) {
4924 3ce1b845 2019-07-15 stsp free(*logmsg);
4925 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4928 33ad4cbe 2019-05-12 jcs return err;
4931 c4296144 2019-05-09 stsp static const struct got_error *
4932 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4934 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4935 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4936 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4937 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4938 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4939 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4940 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4941 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4942 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4943 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4945 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4946 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4948 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4949 c4296144 2019-05-09 stsp switch (ch) {
4951 c4296144 2019-05-09 stsp logmsg = optarg;
4954 c4296144 2019-05-09 stsp usage_commit();
4955 c4296144 2019-05-09 stsp /* NOTREACHED */
4959 c4296144 2019-05-09 stsp argc -= optind;
4960 c4296144 2019-05-09 stsp argv += optind;
4962 43012d58 2019-07-14 stsp #ifndef PROFILE
4963 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4964 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4965 43012d58 2019-07-14 stsp err(1, "pledge");
4967 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4968 c4296144 2019-05-09 stsp if (cwd == NULL) {
4969 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4970 c4296144 2019-05-09 stsp goto done;
4972 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4973 7d5807f4 2019-07-11 stsp if (error)
4974 7d5807f4 2019-07-11 stsp goto done;
4976 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4977 c4296144 2019-05-09 stsp if (error)
4978 a698f62e 2019-07-25 stsp goto done;
4979 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4980 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4981 c4296144 2019-05-09 stsp goto done;
4984 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4985 916f288c 2019-07-30 stsp worktree);
4986 5c1e53bc 2019-07-28 stsp if (error)
4987 5c1e53bc 2019-07-28 stsp goto done;
4989 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4990 c9956ddf 2019-09-08 stsp if (error)
4991 c9956ddf 2019-09-08 stsp goto done;
4992 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4993 c9956ddf 2019-09-08 stsp gitconfig_path);
4994 c4296144 2019-05-09 stsp if (error != NULL)
4995 c4296144 2019-05-09 stsp goto done;
4997 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4998 aba9c984 2019-09-08 stsp if (error)
4999 aba9c984 2019-09-08 stsp return error;
5002 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5003 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5005 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5006 314a6357 2019-05-13 stsp error = get_editor(&editor);
5008 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5009 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5010 c4296144 2019-05-09 stsp if (error)
5011 c4296144 2019-05-09 stsp goto done;
5013 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5014 087fb88c 2019-08-04 stsp if (error)
5015 087fb88c 2019-08-04 stsp goto done;
5017 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5018 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5019 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5020 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5021 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5022 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5023 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5024 916f288c 2019-07-30 stsp goto done;
5026 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5028 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5029 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5030 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5031 e0870e44 2019-05-13 stsp if (error) {
5032 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5033 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5034 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5035 c4296144 2019-05-09 stsp goto done;
5038 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5039 c4296144 2019-05-09 stsp if (error)
5040 c4296144 2019-05-09 stsp goto done;
5041 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5043 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5044 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5045 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5046 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5047 7266f21f 2019-10-21 stsp error == NULL)
5048 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5049 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5051 c4296144 2019-05-09 stsp got_repo_close(repo);
5052 c4296144 2019-05-09 stsp if (worktree)
5053 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5054 c4296144 2019-05-09 stsp free(cwd);
5055 c4296144 2019-05-09 stsp free(id_str);
5056 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5057 e2ba3d07 2019-05-13 stsp free(editor);
5058 aba9c984 2019-09-08 stsp free(author);
5059 234035bc 2019-06-01 stsp return error;
5062 234035bc 2019-06-01 stsp __dead static void
5063 234035bc 2019-06-01 stsp usage_cherrypick(void)
5065 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5069 234035bc 2019-06-01 stsp static const struct got_error *
5070 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5072 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5073 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5074 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5075 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5076 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5077 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5078 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5079 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5080 234035bc 2019-06-01 stsp int ch, did_something = 0;
5082 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5083 234035bc 2019-06-01 stsp switch (ch) {
5085 234035bc 2019-06-01 stsp usage_cherrypick();
5086 234035bc 2019-06-01 stsp /* NOTREACHED */
5090 234035bc 2019-06-01 stsp argc -= optind;
5091 234035bc 2019-06-01 stsp argv += optind;
5093 43012d58 2019-07-14 stsp #ifndef PROFILE
5094 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5095 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5096 43012d58 2019-07-14 stsp err(1, "pledge");
5098 234035bc 2019-06-01 stsp if (argc != 1)
5099 234035bc 2019-06-01 stsp usage_cherrypick();
5101 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5102 234035bc 2019-06-01 stsp if (cwd == NULL) {
5103 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5104 234035bc 2019-06-01 stsp goto done;
5106 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5107 234035bc 2019-06-01 stsp if (error)
5108 234035bc 2019-06-01 stsp goto done;
5110 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5112 234035bc 2019-06-01 stsp if (error != NULL)
5113 234035bc 2019-06-01 stsp goto done;
5115 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5116 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5117 234035bc 2019-06-01 stsp if (error)
5118 234035bc 2019-06-01 stsp goto done;
5120 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5121 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5122 234035bc 2019-06-01 stsp if (error != NULL) {
5123 234035bc 2019-06-01 stsp struct got_reference *ref;
5124 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5125 234035bc 2019-06-01 stsp goto done;
5126 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5127 234035bc 2019-06-01 stsp if (error != NULL)
5128 234035bc 2019-06-01 stsp goto done;
5129 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5130 234035bc 2019-06-01 stsp got_ref_close(ref);
5131 234035bc 2019-06-01 stsp if (error != NULL)
5132 234035bc 2019-06-01 stsp goto done;
5134 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5135 234035bc 2019-06-01 stsp if (error)
5136 234035bc 2019-06-01 stsp goto done;
5138 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5139 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5140 234035bc 2019-06-01 stsp if (error != NULL)
5141 234035bc 2019-06-01 stsp goto done;
5143 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5144 234035bc 2019-06-01 stsp if (error) {
5145 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5146 234035bc 2019-06-01 stsp goto done;
5147 234035bc 2019-06-01 stsp error = NULL;
5149 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5150 234035bc 2019-06-01 stsp goto done;
5153 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5154 234035bc 2019-06-01 stsp if (error)
5155 234035bc 2019-06-01 stsp goto done;
5156 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5157 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5158 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5160 234035bc 2019-06-01 stsp if (error != NULL)
5161 234035bc 2019-06-01 stsp goto done;
5163 234035bc 2019-06-01 stsp if (did_something)
5164 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5166 234035bc 2019-06-01 stsp if (commit)
5167 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5168 234035bc 2019-06-01 stsp free(commit_id_str);
5169 234035bc 2019-06-01 stsp if (head_ref)
5170 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5171 234035bc 2019-06-01 stsp if (worktree)
5172 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5174 234035bc 2019-06-01 stsp got_repo_close(repo);
5175 c4296144 2019-05-09 stsp return error;
5178 5ef14e63 2019-06-02 stsp __dead static void
5179 5ef14e63 2019-06-02 stsp usage_backout(void)
5181 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5185 5ef14e63 2019-06-02 stsp static const struct got_error *
5186 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5188 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5189 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5190 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5191 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5192 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5193 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5194 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5195 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5196 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5198 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5199 5ef14e63 2019-06-02 stsp switch (ch) {
5201 5ef14e63 2019-06-02 stsp usage_backout();
5202 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5206 5ef14e63 2019-06-02 stsp argc -= optind;
5207 5ef14e63 2019-06-02 stsp argv += optind;
5209 43012d58 2019-07-14 stsp #ifndef PROFILE
5210 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5211 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5212 43012d58 2019-07-14 stsp err(1, "pledge");
5214 5ef14e63 2019-06-02 stsp if (argc != 1)
5215 5ef14e63 2019-06-02 stsp usage_backout();
5217 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5218 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5219 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5220 5ef14e63 2019-06-02 stsp goto done;
5222 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5223 5ef14e63 2019-06-02 stsp if (error)
5224 5ef14e63 2019-06-02 stsp goto done;
5226 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5228 5ef14e63 2019-06-02 stsp if (error != NULL)
5229 5ef14e63 2019-06-02 stsp goto done;
5231 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5232 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5233 5ef14e63 2019-06-02 stsp if (error)
5234 5ef14e63 2019-06-02 stsp goto done;
5236 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5237 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5238 5ef14e63 2019-06-02 stsp if (error != NULL) {
5239 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5240 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5241 5ef14e63 2019-06-02 stsp goto done;
5242 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5243 5ef14e63 2019-06-02 stsp if (error != NULL)
5244 5ef14e63 2019-06-02 stsp goto done;
5245 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5246 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5247 5ef14e63 2019-06-02 stsp if (error != NULL)
5248 5ef14e63 2019-06-02 stsp goto done;
5250 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5251 5ef14e63 2019-06-02 stsp if (error)
5252 5ef14e63 2019-06-02 stsp goto done;
5254 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5255 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5256 5ef14e63 2019-06-02 stsp if (error != NULL)
5257 5ef14e63 2019-06-02 stsp goto done;
5259 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5260 5ef14e63 2019-06-02 stsp if (error)
5261 5ef14e63 2019-06-02 stsp goto done;
5263 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5264 5ef14e63 2019-06-02 stsp if (error)
5265 5ef14e63 2019-06-02 stsp goto done;
5266 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5267 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5268 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5269 5ef14e63 2019-06-02 stsp goto done;
5272 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5273 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5274 5ef14e63 2019-06-02 stsp if (error != NULL)
5275 5ef14e63 2019-06-02 stsp goto done;
5277 5ef14e63 2019-06-02 stsp if (did_something)
5278 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5280 5ef14e63 2019-06-02 stsp if (commit)
5281 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5282 5ef14e63 2019-06-02 stsp free(commit_id_str);
5283 5ef14e63 2019-06-02 stsp if (head_ref)
5284 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5285 818c7501 2019-07-11 stsp if (worktree)
5286 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5288 818c7501 2019-07-11 stsp got_repo_close(repo);
5289 818c7501 2019-07-11 stsp return error;
5292 818c7501 2019-07-11 stsp __dead static void
5293 818c7501 2019-07-11 stsp usage_rebase(void)
5295 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5296 af54c8f8 2019-07-11 stsp getprogname());
5301 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5304 0ebf8283 2019-07-24 stsp size_t len;
5306 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5307 0ebf8283 2019-07-24 stsp if (len > limit)
5308 0ebf8283 2019-07-24 stsp len = limit;
5309 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5310 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5312 0ebf8283 2019-07-24 stsp *nl = '\0';
5315 0ebf8283 2019-07-24 stsp static const struct got_error *
5316 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5318 5943eee2 2019-08-13 stsp const struct got_error *err;
5319 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5320 5943eee2 2019-08-13 stsp const char *s;
5322 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5324 5943eee2 2019-08-13 stsp return err;
5326 5943eee2 2019-08-13 stsp s = logmsg0;
5327 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5330 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5331 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5332 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5333 5943eee2 2019-08-13 stsp goto done;
5336 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5338 5943eee2 2019-08-13 stsp free(logmsg0);
5339 a0ea4fc0 2020-02-28 stsp return err;
5342 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5343 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5345 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5346 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5347 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5349 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5351 a0ea4fc0 2020-02-28 stsp return err;
5353 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5355 a0ea4fc0 2020-02-28 stsp goto done;
5357 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5359 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5361 a0ea4fc0 2020-02-28 stsp goto done;
5363 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5365 a0ea4fc0 2020-02-28 stsp free(id_str);
5366 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5367 a0ea4fc0 2020-02-28 stsp free(logmsg);
5368 5943eee2 2019-08-13 stsp return err;
5371 818c7501 2019-07-11 stsp static const struct got_error *
5372 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5373 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5375 818c7501 2019-07-11 stsp const struct got_error *err;
5376 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5378 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5380 818c7501 2019-07-11 stsp goto done;
5382 ff0d2220 2019-07-11 stsp if (new_id) {
5383 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5385 ff0d2220 2019-07-11 stsp goto done;
5388 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5389 ff0d2220 2019-07-11 stsp if (new_id_str)
5390 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5392 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5394 0ebf8283 2019-07-24 stsp goto done;
5396 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5397 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5399 818c7501 2019-07-11 stsp free(old_id_str);
5400 818c7501 2019-07-11 stsp free(new_id_str);
5401 272a1371 2020-02-28 stsp free(logmsg);
5402 818c7501 2019-07-11 stsp return err;
5405 1ee397ad 2019-07-12 stsp static const struct got_error *
5406 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5408 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5410 818c7501 2019-07-11 stsp while (path[0] == '/')
5412 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5414 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5415 1ee397ad 2019-07-12 stsp return NULL;
5416 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5417 818c7501 2019-07-11 stsp *rebase_status = status;
5418 1ee397ad 2019-07-12 stsp return NULL;
5421 818c7501 2019-07-11 stsp static const struct got_error *
5422 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5423 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5424 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5426 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5427 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5428 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5431 818c7501 2019-07-11 stsp static const struct got_error *
5432 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5433 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5434 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5435 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5437 ff0d2220 2019-07-11 stsp const struct got_error *error;
5438 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5439 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5441 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5442 ff0d2220 2019-07-11 stsp if (error)
5443 ff0d2220 2019-07-11 stsp return error;
5445 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5446 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5447 ff0d2220 2019-07-11 stsp if (error) {
5448 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5449 ff0d2220 2019-07-11 stsp goto done;
5450 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5452 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5453 ff0d2220 2019-07-11 stsp free(new_commit_id);
5456 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5457 ff0d2220 2019-07-11 stsp return error;
5460 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5461 64c6d990 2019-07-11 stsp const char *path_prefix;
5462 64c6d990 2019-07-11 stsp size_t len;
5463 8ca9bd68 2019-07-25 stsp int errcode;
5466 64c6d990 2019-07-11 stsp static const struct got_error *
5467 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5468 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5469 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5470 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5472 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5474 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5475 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5476 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5478 64c6d990 2019-07-11 stsp return NULL;
5481 64c6d990 2019-07-11 stsp static const struct got_error *
5482 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5483 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5484 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5486 64c6d990 2019-07-11 stsp const struct got_error *err;
5487 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5488 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5489 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5491 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5492 64c6d990 2019-07-11 stsp return NULL;
5494 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5496 64c6d990 2019-07-11 stsp goto done;
5498 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5500 64c6d990 2019-07-11 stsp goto done;
5502 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5503 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5505 64c6d990 2019-07-11 stsp goto done;
5507 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5508 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5510 64c6d990 2019-07-11 stsp goto done;
5512 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5513 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5514 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5515 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5516 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5517 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5518 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5520 64c6d990 2019-07-11 stsp if (tree1)
5521 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5522 64c6d990 2019-07-11 stsp if (tree2)
5523 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5524 64c6d990 2019-07-11 stsp if (commit)
5525 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5526 64c6d990 2019-07-11 stsp if (parent_commit)
5527 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5528 64c6d990 2019-07-11 stsp return err;
5531 ff0d2220 2019-07-11 stsp static const struct got_error *
5532 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5533 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5534 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5535 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5536 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5538 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5539 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5540 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5541 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5542 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5544 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5546 af61c510 2019-07-19 stsp return err;
5548 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5549 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5551 af61c510 2019-07-19 stsp goto done;
5552 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5553 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5554 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5555 af61c510 2019-07-19 stsp if (err) {
5556 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5557 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5558 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5559 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5560 af61c510 2019-07-19 stsp "been reached?!?");
5562 ee780d5c 2020-01-04 stsp goto done;
5564 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5565 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5567 af61c510 2019-07-19 stsp goto done;
5569 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5571 af61c510 2019-07-19 stsp goto done;
5572 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5573 af61c510 2019-07-19 stsp commit_id = parent_id;
5577 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5578 af61c510 2019-07-19 stsp return err;
5581 af61c510 2019-07-19 stsp static const struct got_error *
5582 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5584 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5585 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5586 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5587 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5588 818c7501 2019-07-11 stsp char *cwd = NULL;
5589 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5590 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5591 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5592 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5593 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5594 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5595 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5596 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5597 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5598 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5599 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5600 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5601 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5603 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5604 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5606 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5607 818c7501 2019-07-11 stsp switch (ch) {
5609 818c7501 2019-07-11 stsp abort_rebase = 1;
5612 818c7501 2019-07-11 stsp continue_rebase = 1;
5615 818c7501 2019-07-11 stsp usage_rebase();
5616 818c7501 2019-07-11 stsp /* NOTREACHED */
5620 818c7501 2019-07-11 stsp argc -= optind;
5621 818c7501 2019-07-11 stsp argv += optind;
5623 43012d58 2019-07-14 stsp #ifndef PROFILE
5624 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5625 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5626 43012d58 2019-07-14 stsp err(1, "pledge");
5628 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5629 818c7501 2019-07-11 stsp usage_rebase();
5630 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5631 818c7501 2019-07-11 stsp if (argc != 0)
5632 818c7501 2019-07-11 stsp usage_rebase();
5633 818c7501 2019-07-11 stsp } else if (argc != 1)
5634 818c7501 2019-07-11 stsp usage_rebase();
5636 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5637 818c7501 2019-07-11 stsp if (cwd == NULL) {
5638 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5639 818c7501 2019-07-11 stsp goto done;
5641 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5642 818c7501 2019-07-11 stsp if (error)
5643 818c7501 2019-07-11 stsp goto done;
5645 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5647 818c7501 2019-07-11 stsp if (error != NULL)
5648 818c7501 2019-07-11 stsp goto done;
5650 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5651 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5652 7ef62c4e 2020-02-24 stsp if (error)
5653 7ef62c4e 2020-02-24 stsp goto done;
5655 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5656 7ef62c4e 2020-02-24 stsp worktree);
5657 818c7501 2019-07-11 stsp if (error)
5658 7ef62c4e 2020-02-24 stsp goto done;
5659 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5660 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5661 818c7501 2019-07-11 stsp goto done;
5664 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5665 818c7501 2019-07-11 stsp if (error)
5666 818c7501 2019-07-11 stsp goto done;
5668 f6794adc 2019-07-23 stsp if (abort_rebase) {
5669 818c7501 2019-07-11 stsp int did_something;
5670 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5671 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5672 f6794adc 2019-07-23 stsp goto done;
5674 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5675 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5676 3e3a69f1 2019-07-25 stsp worktree, repo);
5677 818c7501 2019-07-11 stsp if (error)
5678 818c7501 2019-07-11 stsp goto done;
5679 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5680 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5681 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5682 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5683 818c7501 2019-07-11 stsp if (error)
5684 818c7501 2019-07-11 stsp goto done;
5685 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5686 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5689 818c7501 2019-07-11 stsp if (continue_rebase) {
5690 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5691 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5692 f6794adc 2019-07-23 stsp goto done;
5694 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5695 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5696 3e3a69f1 2019-07-25 stsp worktree, repo);
5697 818c7501 2019-07-11 stsp if (error)
5698 818c7501 2019-07-11 stsp goto done;
5700 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5701 01757395 2019-07-12 stsp resume_commit_id, repo);
5702 818c7501 2019-07-11 stsp if (error)
5703 818c7501 2019-07-11 stsp goto done;
5705 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5706 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5707 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5708 818c7501 2019-07-11 stsp goto done;
5711 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5712 818c7501 2019-07-11 stsp if (error != NULL)
5713 818c7501 2019-07-11 stsp goto done;
5716 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5717 ff0d2220 2019-07-11 stsp if (error)
5718 ff0d2220 2019-07-11 stsp goto done;
5720 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5721 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5723 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5724 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5725 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5726 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5727 818c7501 2019-07-11 stsp if (error)
5728 818c7501 2019-07-11 stsp goto done;
5729 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5730 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5731 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5732 818c7501 2019-07-11 stsp "with work tree's branch");
5733 818c7501 2019-07-11 stsp goto done;
5736 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5737 a51a74b3 2019-07-27 stsp if (error) {
5738 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5739 a51a74b3 2019-07-27 stsp goto done;
5740 a51a74b3 2019-07-27 stsp error = NULL;
5742 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5743 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5744 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5745 a51a74b3 2019-07-27 stsp goto done;
5747 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5748 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5749 818c7501 2019-07-11 stsp if (error)
5750 818c7501 2019-07-11 stsp goto done;
5753 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5754 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5755 818c7501 2019-07-11 stsp if (error)
5756 818c7501 2019-07-11 stsp goto done;
5758 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5759 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5760 fc66b545 2019-08-12 stsp if (pid == NULL) {
5761 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5762 fc66b545 2019-08-12 stsp int did_something;
5763 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5764 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5765 fc66b545 2019-08-12 stsp &did_something);
5766 fc66b545 2019-08-12 stsp if (error)
5767 fc66b545 2019-08-12 stsp goto done;
5768 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5769 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5771 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5772 fc66b545 2019-08-12 stsp goto done;
5774 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5775 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5776 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5777 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5778 818c7501 2019-07-11 stsp commit = NULL;
5779 818c7501 2019-07-11 stsp if (error)
5780 818c7501 2019-07-11 stsp goto done;
5782 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5783 38b0338b 2019-11-29 stsp if (continue_rebase) {
5784 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5785 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5786 38b0338b 2019-11-29 stsp goto done;
5788 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5789 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5790 38b0338b 2019-11-29 stsp char *id_str;
5791 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5792 38b0338b 2019-11-29 stsp new_base_branch);
5793 38b0338b 2019-11-29 stsp if (error)
5794 38b0338b 2019-11-29 stsp goto done;
5795 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5796 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5797 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5798 38b0338b 2019-11-29 stsp free(id_str);
5799 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5800 38b0338b 2019-11-29 stsp new_head_commit_id);
5801 38b0338b 2019-11-29 stsp if (error)
5802 38b0338b 2019-11-29 stsp goto done;
5806 818c7501 2019-07-11 stsp pid = NULL;
5807 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5808 818c7501 2019-07-11 stsp commit_id = qid->id;
5809 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5810 818c7501 2019-07-11 stsp pid = qid;
5812 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5813 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5814 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5815 818c7501 2019-07-11 stsp if (error)
5816 818c7501 2019-07-11 stsp goto done;
5818 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5819 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
5820 a0ea4fc0 2020-02-28 stsp if (error)
5821 a0ea4fc0 2020-02-28 stsp goto done;
5822 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5826 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5827 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5828 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5829 818c7501 2019-07-11 stsp if (error)
5830 818c7501 2019-07-11 stsp goto done;
5833 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5834 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5835 818c7501 2019-07-11 stsp if (error)
5836 818c7501 2019-07-11 stsp goto done;
5837 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5838 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5840 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5841 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5843 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5844 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5845 818c7501 2019-07-11 stsp free(resume_commit_id);
5846 818c7501 2019-07-11 stsp free(yca_id);
5847 818c7501 2019-07-11 stsp if (commit)
5848 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5849 818c7501 2019-07-11 stsp if (branch)
5850 818c7501 2019-07-11 stsp got_ref_close(branch);
5851 818c7501 2019-07-11 stsp if (new_base_branch)
5852 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5853 818c7501 2019-07-11 stsp if (tmp_branch)
5854 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5855 5ef14e63 2019-06-02 stsp if (worktree)
5856 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5858 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5859 5ef14e63 2019-06-02 stsp return error;
5862 0ebf8283 2019-07-24 stsp __dead static void
5863 0ebf8283 2019-07-24 stsp usage_histedit(void)
5865 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5866 0ebf8283 2019-07-24 stsp getprogname());
5870 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5871 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5872 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5873 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5874 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5876 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5877 0ebf8283 2019-07-24 stsp unsigned char code;
5878 0ebf8283 2019-07-24 stsp const char *name;
5879 0ebf8283 2019-07-24 stsp const char *desc;
5880 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5881 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5882 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5883 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5884 82997472 2020-01-29 stsp "be used" },
5885 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5886 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5887 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5890 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5891 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5892 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5893 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5894 0ebf8283 2019-07-24 stsp char *logmsg;
5896 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5898 0ebf8283 2019-07-24 stsp static const struct got_error *
5899 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5900 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5902 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5903 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5904 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5907 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5909 0ebf8283 2019-07-24 stsp goto done;
5911 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5913 0ebf8283 2019-07-24 stsp goto done;
5915 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5917 0ebf8283 2019-07-24 stsp goto done;
5919 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5920 0ebf8283 2019-07-24 stsp if (n < 0)
5921 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5923 0ebf8283 2019-07-24 stsp if (commit)
5924 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5925 0ebf8283 2019-07-24 stsp free(id_str);
5926 0ebf8283 2019-07-24 stsp free(logmsg);
5927 0ebf8283 2019-07-24 stsp return err;
5930 0ebf8283 2019-07-24 stsp static const struct got_error *
5931 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
5932 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
5934 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5935 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5937 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5938 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5940 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5941 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5945 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
5946 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5947 083957f4 2020-02-24 stsp if (n < 0) {
5948 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
5954 0ebf8283 2019-07-24 stsp return err;
5957 0ebf8283 2019-07-24 stsp static const struct got_error *
5958 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
5959 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
5961 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5963 514f2ffe 2020-01-29 stsp char *id_str;
5964 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
5966 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
5967 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
5969 514f2ffe 2020-01-29 stsp return err;
5971 514f2ffe 2020-01-29 stsp n = fprintf(f,
5972 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
5973 514f2ffe 2020-01-29 stsp "# commit %s\n"
5974 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
5975 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
5976 514f2ffe 2020-01-29 stsp if (n < 0) {
5977 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5978 514f2ffe 2020-01-29 stsp goto done;
5981 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5982 514f2ffe 2020-01-29 stsp if (n < 0) {
5983 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5984 514f2ffe 2020-01-29 stsp goto done;
5987 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5988 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5989 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5990 0ebf8283 2019-07-24 stsp cmd->desc);
5991 0ebf8283 2019-07-24 stsp if (n < 0) {
5992 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5997 514f2ffe 2020-01-29 stsp free(id_str);
5998 0ebf8283 2019-07-24 stsp return err;
6001 0ebf8283 2019-07-24 stsp static const struct got_error *
6002 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6004 0ebf8283 2019-07-24 stsp static char msg[42];
6007 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6009 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6010 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6012 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6015 0ebf8283 2019-07-24 stsp static const struct got_error *
6016 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6017 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6019 0ebf8283 2019-07-24 stsp const struct got_error *err;
6020 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6021 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6023 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6025 0ebf8283 2019-07-24 stsp return err;
6027 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6029 0ebf8283 2019-07-24 stsp goto done;
6031 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6033 5943eee2 2019-08-13 stsp goto done;
6034 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6035 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6036 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6037 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6040 0ebf8283 2019-07-24 stsp if (folded_commit)
6041 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6042 0ebf8283 2019-07-24 stsp free(id_str);
6043 5943eee2 2019-08-13 stsp free(folded_logmsg);
6044 0ebf8283 2019-07-24 stsp return err;
6047 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6048 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6050 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6052 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6053 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6054 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6055 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6056 3f9de99f 2019-07-24 stsp folded = prev;
6057 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6060 0ebf8283 2019-07-24 stsp return folded;
6063 0ebf8283 2019-07-24 stsp static const struct got_error *
6064 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6065 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6067 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6068 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6069 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6070 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6072 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6074 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6076 0ebf8283 2019-07-24 stsp return err;
6078 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6079 0ebf8283 2019-07-24 stsp if (folded) {
6080 0ebf8283 2019-07-24 stsp while (folded != hle) {
6081 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6082 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6085 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6086 0ebf8283 2019-07-24 stsp logmsg, repo);
6088 0ebf8283 2019-07-24 stsp goto done;
6089 0ebf8283 2019-07-24 stsp free(logmsg);
6090 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6091 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6095 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6097 0ebf8283 2019-07-24 stsp goto done;
6098 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6100 5943eee2 2019-08-13 stsp goto done;
6101 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6102 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6103 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6104 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6105 0ebf8283 2019-07-24 stsp goto done;
6107 0ebf8283 2019-07-24 stsp free(logmsg);
6108 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6110 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6112 0ebf8283 2019-07-24 stsp goto done;
6114 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6115 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6117 0ebf8283 2019-07-24 stsp goto done;
6119 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6120 0ebf8283 2019-07-24 stsp close(fd);
6122 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6124 0ebf8283 2019-07-24 stsp goto done;
6126 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6127 0ebf8283 2019-07-24 stsp if (err) {
6128 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6129 0ebf8283 2019-07-24 stsp goto done;
6130 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6133 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6134 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6135 0ebf8283 2019-07-24 stsp free(logmsg_path);
6136 0ebf8283 2019-07-24 stsp free(logmsg);
6137 5943eee2 2019-08-13 stsp free(orig_logmsg);
6138 0ebf8283 2019-07-24 stsp free(editor);
6139 0ebf8283 2019-07-24 stsp if (commit)
6140 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6141 0ebf8283 2019-07-24 stsp return err;
6144 0ebf8283 2019-07-24 stsp static const struct got_error *
6145 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6146 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6148 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6149 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6150 0ebf8283 2019-07-24 stsp size_t size;
6151 0ebf8283 2019-07-24 stsp ssize_t len;
6152 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6153 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6154 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6155 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6157 0ebf8283 2019-07-24 stsp for (;;) {
6158 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6159 0ebf8283 2019-07-24 stsp if (len == -1) {
6160 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6161 0ebf8283 2019-07-24 stsp if (feof(f))
6163 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6164 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6169 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6171 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6172 0ebf8283 2019-07-24 stsp free(line);
6173 0ebf8283 2019-07-24 stsp line = NULL;
6176 0ebf8283 2019-07-24 stsp cmd = NULL;
6177 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6178 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6179 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6180 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6181 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6184 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6189 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6190 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6193 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6195 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6196 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6197 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6200 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6201 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6205 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6206 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6207 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6211 0ebf8283 2019-07-24 stsp free(line);
6212 0ebf8283 2019-07-24 stsp line = NULL;
6216 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6218 0ebf8283 2019-07-24 stsp *end = '\0';
6220 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6221 0ebf8283 2019-07-24 stsp if (err) {
6222 0ebf8283 2019-07-24 stsp /* override error code */
6223 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6227 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6228 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6229 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6232 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6233 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6234 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6235 0ebf8283 2019-07-24 stsp commit_id = NULL;
6236 0ebf8283 2019-07-24 stsp free(line);
6237 0ebf8283 2019-07-24 stsp line = NULL;
6238 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6241 0ebf8283 2019-07-24 stsp free(line);
6242 0ebf8283 2019-07-24 stsp free(commit_id);
6243 0ebf8283 2019-07-24 stsp return err;
6246 0ebf8283 2019-07-24 stsp static const struct got_error *
6247 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6248 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6250 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6251 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6252 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6253 5b87815e 2020-03-05 stsp static char msg[92];
6254 bfce7f83 2019-07-27 stsp char *id_str;
6256 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6257 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6258 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6259 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6260 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6262 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6263 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6264 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6265 5b87815e 2020-03-05 stsp if (hle == hle2)
6267 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6268 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6270 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6272 5b87815e 2020-03-05 stsp return err;
6273 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6274 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6275 5b87815e 2020-03-05 stsp free(id_str);
6276 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6280 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6281 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6282 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6285 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6286 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6288 bfce7f83 2019-07-27 stsp return err;
6289 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6290 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6291 bfce7f83 2019-07-27 stsp free(id_str);
6292 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6296 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6297 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6298 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6299 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6301 bfce7f83 2019-07-27 stsp return NULL;
6304 bfce7f83 2019-07-27 stsp static const struct got_error *
6305 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6306 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6307 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6309 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6310 0ebf8283 2019-07-24 stsp char *editor;
6311 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6313 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6315 0ebf8283 2019-07-24 stsp return err;
6317 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6318 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6319 0ebf8283 2019-07-24 stsp goto done;
6322 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6323 0ebf8283 2019-07-24 stsp if (f == NULL) {
6324 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6325 0ebf8283 2019-07-24 stsp goto done;
6327 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6329 bfce7f83 2019-07-27 stsp goto done;
6331 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6333 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6334 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6335 0ebf8283 2019-07-24 stsp free(editor);
6336 0ebf8283 2019-07-24 stsp return err;
6339 0ebf8283 2019-07-24 stsp static const struct got_error *
6340 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6341 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6342 514f2ffe 2020-01-29 stsp struct got_repository *);
6344 0ebf8283 2019-07-24 stsp static const struct got_error *
6345 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6346 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6347 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6349 0ebf8283 2019-07-24 stsp const struct got_error *err;
6350 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6351 0ebf8283 2019-07-24 stsp char *path = NULL;
6353 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6355 0ebf8283 2019-07-24 stsp return err;
6357 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6359 0ebf8283 2019-07-24 stsp goto done;
6361 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6363 0ebf8283 2019-07-24 stsp goto done;
6365 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6366 083957f4 2020-02-24 stsp rewind(f);
6367 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6369 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6370 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6371 0ebf8283 2019-07-24 stsp goto done;
6374 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6375 083957f4 2020-02-24 stsp if (err) {
6376 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6377 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6378 083957f4 2020-02-24 stsp goto done;
6379 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6380 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6384 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6385 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6386 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6387 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6388 0ebf8283 2019-07-24 stsp free(path);
6389 0ebf8283 2019-07-24 stsp return err;
6392 0ebf8283 2019-07-24 stsp static const struct got_error *
6393 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6394 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6396 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6397 0ebf8283 2019-07-24 stsp char *path = NULL;
6398 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6399 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6400 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6402 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6404 0ebf8283 2019-07-24 stsp return err;
6406 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6407 0ebf8283 2019-07-24 stsp if (f == NULL) {
6408 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6409 0ebf8283 2019-07-24 stsp goto done;
6411 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6412 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6417 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6418 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6419 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6420 0ebf8283 2019-07-24 stsp if (n < 0) {
6421 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6427 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6428 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6429 0ebf8283 2019-07-24 stsp free(path);
6430 0ebf8283 2019-07-24 stsp if (commit)
6431 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6432 0ebf8283 2019-07-24 stsp return err;
6436 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6438 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6440 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6441 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6442 bfce7f83 2019-07-27 stsp free(hle);
6446 0ebf8283 2019-07-24 stsp static const struct got_error *
6447 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6448 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6450 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6451 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6453 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6454 0ebf8283 2019-07-24 stsp if (f == NULL) {
6455 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6456 0ebf8283 2019-07-24 stsp goto done;
6459 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6461 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6462 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6463 0ebf8283 2019-07-24 stsp return err;
6466 0ebf8283 2019-07-24 stsp static const struct got_error *
6467 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6468 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6469 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6471 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6472 0ebf8283 2019-07-24 stsp int resp = ' ';
6474 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6475 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6476 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6477 0ebf8283 2019-07-24 stsp resp = getchar();
6478 a61a4414 2019-08-07 stsp if (resp == '\n')
6479 a61a4414 2019-08-07 stsp resp = getchar();
6480 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6481 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6482 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6484 426ebf2e 2019-07-25 stsp if (err) {
6485 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6486 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6488 bfce7f83 2019-07-27 stsp prev_err = err;
6489 426ebf2e 2019-07-25 stsp resp = ' ';
6493 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6494 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6495 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6496 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6497 426ebf2e 2019-07-25 stsp if (err) {
6498 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6499 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6501 bfce7f83 2019-07-27 stsp prev_err = err;
6502 426ebf2e 2019-07-25 stsp resp = ' ';
6506 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6507 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6510 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6513 0ebf8283 2019-07-24 stsp return err;
6516 0ebf8283 2019-07-24 stsp static const struct got_error *
6517 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6518 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6519 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6521 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6522 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6523 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6524 3e3a69f1 2019-07-25 stsp branch, repo);
6527 0ebf8283 2019-07-24 stsp static const struct got_error *
6528 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6529 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6531 0ebf8283 2019-07-24 stsp const struct got_error *err;
6532 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6534 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6536 0ebf8283 2019-07-24 stsp goto done;
6538 0ebf8283 2019-07-24 stsp if (new_id) {
6539 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6541 0ebf8283 2019-07-24 stsp goto done;
6544 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6545 0ebf8283 2019-07-24 stsp if (new_id_str)
6546 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6548 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6549 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6550 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6551 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6552 0ebf8283 2019-07-24 stsp goto done;
6554 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6556 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6558 0ebf8283 2019-07-24 stsp goto done;
6561 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6562 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6563 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6564 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6565 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6567 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6568 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6569 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6576 0ebf8283 2019-07-24 stsp free(old_id_str);
6577 0ebf8283 2019-07-24 stsp free(new_id_str);
6578 0ebf8283 2019-07-24 stsp return err;
6581 0ebf8283 2019-07-24 stsp static const struct got_error *
6582 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6583 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6584 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6585 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6587 0ebf8283 2019-07-24 stsp const struct got_error *err;
6588 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6589 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6591 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6592 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6593 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6595 0ebf8283 2019-07-24 stsp return err;
6598 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6600 0ebf8283 2019-07-24 stsp return err;
6602 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6603 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6604 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6605 0ebf8283 2019-07-24 stsp if (err) {
6606 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6607 0ebf8283 2019-07-24 stsp goto done;
6608 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6610 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6611 0ebf8283 2019-07-24 stsp free(new_commit_id);
6614 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6615 0ebf8283 2019-07-24 stsp return err;
6618 0ebf8283 2019-07-24 stsp static const struct got_error *
6619 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6620 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6622 0ebf8283 2019-07-24 stsp const struct got_error *error;
6623 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6625 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6627 0ebf8283 2019-07-24 stsp if (error)
6628 0ebf8283 2019-07-24 stsp return error;
6630 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6631 0ebf8283 2019-07-24 stsp if (error)
6632 0ebf8283 2019-07-24 stsp return error;
6634 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6635 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6636 0ebf8283 2019-07-24 stsp return error;
6639 ab20a43a 2020-01-29 stsp static const struct got_error *
6640 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6641 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6642 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6643 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6645 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6647 ab20a43a 2020-01-29 stsp switch (status) {
6648 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6649 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6650 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6651 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6652 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6653 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6658 ab20a43a 2020-01-29 stsp switch (staged_status) {
6659 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6660 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6661 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6662 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6663 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6668 ab20a43a 2020-01-29 stsp return NULL;
6671 0ebf8283 2019-07-24 stsp static const struct got_error *
6672 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6674 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6675 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6676 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6677 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6678 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6679 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6680 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6681 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6682 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6683 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6684 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6685 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6686 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6687 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6688 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6689 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6690 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6691 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6692 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6693 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6694 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6695 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6697 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6698 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6699 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6701 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6702 0ebf8283 2019-07-24 stsp switch (ch) {
6704 0ebf8283 2019-07-24 stsp abort_edit = 1;
6707 0ebf8283 2019-07-24 stsp continue_edit = 1;
6710 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6713 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6716 0ebf8283 2019-07-24 stsp usage_histedit();
6717 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6721 0ebf8283 2019-07-24 stsp argc -= optind;
6722 0ebf8283 2019-07-24 stsp argv += optind;
6724 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6725 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6726 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6727 0ebf8283 2019-07-24 stsp err(1, "pledge");
6729 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6730 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6731 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6732 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6733 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6734 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6735 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6736 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6737 0ebf8283 2019-07-24 stsp if (argc != 0)
6738 0ebf8283 2019-07-24 stsp usage_histedit();
6741 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6742 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6743 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6744 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6745 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6746 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6749 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6750 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6751 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6752 0ebf8283 2019-07-24 stsp goto done;
6754 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6755 0ebf8283 2019-07-24 stsp if (error)
6756 0ebf8283 2019-07-24 stsp goto done;
6758 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6760 0ebf8283 2019-07-24 stsp if (error != NULL)
6761 0ebf8283 2019-07-24 stsp goto done;
6763 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6764 0ebf8283 2019-07-24 stsp if (error)
6765 0ebf8283 2019-07-24 stsp goto done;
6766 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6767 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6768 0ebf8283 2019-07-24 stsp goto done;
6771 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6772 0ebf8283 2019-07-24 stsp if (error)
6773 0ebf8283 2019-07-24 stsp goto done;
6775 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6776 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6777 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6778 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6779 083957f4 2020-02-24 stsp "before the -m option can be used");
6780 083957f4 2020-02-24 stsp goto done;
6783 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6784 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6785 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6786 3e3a69f1 2019-07-25 stsp worktree, repo);
6787 0ebf8283 2019-07-24 stsp if (error)
6788 0ebf8283 2019-07-24 stsp goto done;
6789 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6790 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6791 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6792 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6793 0ebf8283 2019-07-24 stsp if (error)
6794 0ebf8283 2019-07-24 stsp goto done;
6795 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6796 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6797 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6798 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6799 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6800 0ebf8283 2019-07-24 stsp goto done;
6803 0ebf8283 2019-07-24 stsp if (continue_edit) {
6804 0ebf8283 2019-07-24 stsp char *path;
6806 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6807 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6808 0ebf8283 2019-07-24 stsp goto done;
6811 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6812 0ebf8283 2019-07-24 stsp if (error)
6813 0ebf8283 2019-07-24 stsp goto done;
6815 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6816 0ebf8283 2019-07-24 stsp free(path);
6817 0ebf8283 2019-07-24 stsp if (error)
6818 0ebf8283 2019-07-24 stsp goto done;
6820 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6821 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6822 3e3a69f1 2019-07-25 stsp worktree, repo);
6823 0ebf8283 2019-07-24 stsp if (error)
6824 0ebf8283 2019-07-24 stsp goto done;
6826 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6827 0ebf8283 2019-07-24 stsp if (error)
6828 0ebf8283 2019-07-24 stsp goto done;
6830 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6831 0ebf8283 2019-07-24 stsp head_commit_id);
6832 0ebf8283 2019-07-24 stsp if (error)
6833 0ebf8283 2019-07-24 stsp goto done;
6834 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6835 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6836 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6837 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6838 3aac7cf7 2019-07-25 stsp goto done;
6840 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6841 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6842 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6843 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6844 0ebf8283 2019-07-24 stsp commit = NULL;
6845 0ebf8283 2019-07-24 stsp if (error)
6846 0ebf8283 2019-07-24 stsp goto done;
6848 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6849 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6850 0ebf8283 2019-07-24 stsp goto done;
6853 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6854 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6855 0ebf8283 2019-07-24 stsp if (error != NULL)
6856 0ebf8283 2019-07-24 stsp goto done;
6858 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6859 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6860 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6861 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6862 c7d20a3f 2019-07-30 stsp goto done;
6865 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6866 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6867 bfce7f83 2019-07-27 stsp branch = NULL;
6868 0ebf8283 2019-07-24 stsp if (error)
6869 0ebf8283 2019-07-24 stsp goto done;
6871 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6872 0ebf8283 2019-07-24 stsp head_commit_id);
6873 0ebf8283 2019-07-24 stsp if (error)
6874 0ebf8283 2019-07-24 stsp goto done;
6875 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6876 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6877 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6878 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6879 3aac7cf7 2019-07-25 stsp goto done;
6881 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6882 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6883 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6884 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6885 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6886 0ebf8283 2019-07-24 stsp commit = NULL;
6887 0ebf8283 2019-07-24 stsp if (error)
6888 0ebf8283 2019-07-24 stsp goto done;
6890 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
6891 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6892 c5996fff 2020-01-29 stsp goto done;
6895 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6896 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6897 bfce7f83 2019-07-27 stsp if (error)
6898 bfce7f83 2019-07-27 stsp goto done;
6900 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6901 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6902 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6903 6ba2bea2 2019-07-27 stsp if (error) {
6904 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6905 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6906 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6907 0ebf8283 2019-07-24 stsp goto done;
6910 514f2ffe 2020-01-29 stsp const char *branch_name;
6911 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
6912 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6913 514f2ffe 2020-01-29 stsp branch_name += 11;
6914 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6915 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
6916 6ba2bea2 2019-07-27 stsp if (error) {
6917 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6918 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6919 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6920 0ebf8283 2019-07-24 stsp goto done;
6925 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6927 6ba2bea2 2019-07-27 stsp if (error) {
6928 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6929 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6930 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6931 0ebf8283 2019-07-24 stsp goto done;
6936 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6937 4ec14e60 2019-08-28 hiltjo if (error)
6938 0ebf8283 2019-07-24 stsp goto done;
6940 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6941 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6942 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6943 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6946 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6947 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6948 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6949 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6951 ab20a43a 2020-01-29 stsp if (error)
6952 ab20a43a 2020-01-29 stsp goto done;
6954 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
6955 ab20a43a 2020-01-29 stsp int have_changes = 0;
6957 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
6958 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
6959 ab20a43a 2020-01-29 stsp if (error)
6960 ab20a43a 2020-01-29 stsp goto done;
6961 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
6962 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
6963 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
6964 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
6965 ab20a43a 2020-01-29 stsp if (error) {
6966 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
6967 ab20a43a 2020-01-29 stsp goto done;
6968 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
6969 ab20a43a 2020-01-29 stsp goto done;
6971 ab20a43a 2020-01-29 stsp if (have_changes) {
6972 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
6973 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
6974 ab20a43a 2020-01-29 stsp if (error)
6975 ab20a43a 2020-01-29 stsp goto done;
6977 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
6978 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
6979 ab20a43a 2020-01-29 stsp if (error)
6980 ab20a43a 2020-01-29 stsp goto done;
6981 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
6982 ab20a43a 2020-01-29 stsp hle, NULL);
6983 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
6984 ab20a43a 2020-01-29 stsp commit = NULL;
6985 ab20a43a 2020-01-29 stsp if (error)
6986 ab20a43a 2020-01-29 stsp goto done;
6992 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6993 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6994 0ebf8283 2019-07-24 stsp if (error)
6995 0ebf8283 2019-07-24 stsp goto done;
6999 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7000 0ebf8283 2019-07-24 stsp hle->commit_id);
7001 0ebf8283 2019-07-24 stsp if (error)
7002 0ebf8283 2019-07-24 stsp goto done;
7003 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7004 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7006 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7007 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7008 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7009 0ebf8283 2019-07-24 stsp if (error)
7010 0ebf8283 2019-07-24 stsp goto done;
7011 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7012 0ebf8283 2019-07-24 stsp commit = NULL;
7014 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7015 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7017 a0ea4fc0 2020-02-28 stsp if (error)
7018 a0ea4fc0 2020-02-28 stsp goto done;
7019 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7023 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7024 0ebf8283 2019-07-24 stsp char *id_str;
7025 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7026 0ebf8283 2019-07-24 stsp if (error)
7027 0ebf8283 2019-07-24 stsp goto done;
7028 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7030 0ebf8283 2019-07-24 stsp free(id_str);
7031 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7032 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7033 3e3a69f1 2019-07-25 stsp fileindex);
7034 0ebf8283 2019-07-24 stsp goto done;
7037 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7038 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7039 0ebf8283 2019-07-24 stsp if (error)
7040 0ebf8283 2019-07-24 stsp goto done;
7044 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7045 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7046 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7047 0ebf8283 2019-07-24 stsp if (error)
7048 0ebf8283 2019-07-24 stsp goto done;
7051 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7052 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7053 0ebf8283 2019-07-24 stsp if (error)
7054 0ebf8283 2019-07-24 stsp goto done;
7055 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7056 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7058 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7059 3e3a69f1 2019-07-25 stsp branch, repo);
7061 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7062 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7063 0ebf8283 2019-07-24 stsp free(head_commit_id);
7064 0ebf8283 2019-07-24 stsp free(base_commit_id);
7065 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7066 0ebf8283 2019-07-24 stsp if (commit)
7067 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7068 0ebf8283 2019-07-24 stsp if (branch)
7069 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7070 0ebf8283 2019-07-24 stsp if (tmp_branch)
7071 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7072 0ebf8283 2019-07-24 stsp if (worktree)
7073 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7075 2822a352 2019-10-15 stsp got_repo_close(repo);
7076 2822a352 2019-10-15 stsp return error;
7079 2822a352 2019-10-15 stsp __dead static void
7080 2822a352 2019-10-15 stsp usage_integrate(void)
7082 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7086 2822a352 2019-10-15 stsp static const struct got_error *
7087 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7089 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7090 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7091 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7092 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7093 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7094 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7095 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7096 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7097 2822a352 2019-10-15 stsp int ch, did_something = 0;
7099 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7100 2822a352 2019-10-15 stsp switch (ch) {
7102 2822a352 2019-10-15 stsp usage_integrate();
7103 2822a352 2019-10-15 stsp /* NOTREACHED */
7107 2822a352 2019-10-15 stsp argc -= optind;
7108 2822a352 2019-10-15 stsp argv += optind;
7110 2822a352 2019-10-15 stsp if (argc != 1)
7111 2822a352 2019-10-15 stsp usage_integrate();
7112 2822a352 2019-10-15 stsp branch_arg = argv[0];
7114 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7115 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7116 2822a352 2019-10-15 stsp err(1, "pledge");
7118 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7119 2822a352 2019-10-15 stsp if (cwd == NULL) {
7120 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7121 2822a352 2019-10-15 stsp goto done;
7124 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7125 2822a352 2019-10-15 stsp if (error)
7126 2822a352 2019-10-15 stsp goto done;
7128 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7129 2822a352 2019-10-15 stsp if (error)
7130 2822a352 2019-10-15 stsp goto done;
7132 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7134 2822a352 2019-10-15 stsp if (error != NULL)
7135 2822a352 2019-10-15 stsp goto done;
7137 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7138 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7139 2822a352 2019-10-15 stsp if (error)
7140 2822a352 2019-10-15 stsp goto done;
7142 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7143 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7144 2822a352 2019-10-15 stsp goto done;
7147 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7148 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7149 2822a352 2019-10-15 stsp if (error)
7150 2822a352 2019-10-15 stsp goto done;
7152 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7153 2822a352 2019-10-15 stsp if (refname == NULL) {
7154 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7155 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7156 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7157 2822a352 2019-10-15 stsp goto done;
7159 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7160 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7161 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7162 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7163 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7164 2822a352 2019-10-15 stsp goto done;
7167 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7168 2822a352 2019-10-15 stsp if (error)
7169 2822a352 2019-10-15 stsp goto done;
7171 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7172 2822a352 2019-10-15 stsp if (error)
7173 2822a352 2019-10-15 stsp goto done;
7175 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7176 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7177 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7178 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7179 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7180 2822a352 2019-10-15 stsp goto done;
7183 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7184 2822a352 2019-10-15 stsp if (error) {
7185 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7186 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7187 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7188 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7189 2822a352 2019-10-15 stsp goto done;
7192 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7193 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7194 2822a352 2019-10-15 stsp check_cancelled, NULL);
7195 2822a352 2019-10-15 stsp if (error)
7196 2822a352 2019-10-15 stsp goto done;
7198 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7201 715dc77e 2019-08-03 stsp got_repo_close(repo);
7202 2822a352 2019-10-15 stsp if (worktree)
7203 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7204 2822a352 2019-10-15 stsp free(cwd);
7205 2822a352 2019-10-15 stsp free(base_commit_id);
7206 2822a352 2019-10-15 stsp free(commit_id);
7207 2822a352 2019-10-15 stsp free(refname);
7208 2822a352 2019-10-15 stsp free(base_refname);
7209 715dc77e 2019-08-03 stsp return error;
7212 715dc77e 2019-08-03 stsp __dead static void
7213 715dc77e 2019-08-03 stsp usage_stage(void)
7215 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7216 f3055044 2019-08-07 stsp "[file-path ...]\n",
7217 715dc77e 2019-08-03 stsp getprogname());
7221 715dc77e 2019-08-03 stsp static const struct got_error *
7222 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7223 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7224 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7225 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7227 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7228 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7230 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7231 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7232 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7233 408b4ebc 2019-08-03 stsp return NULL;
7235 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7236 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7237 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7239 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7241 408b4ebc 2019-08-03 stsp return err;
7243 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7244 408b4ebc 2019-08-03 stsp free(id_str);
7245 dc424a06 2019-08-07 stsp return NULL;
7248 dc424a06 2019-08-07 stsp static const struct got_error *
7249 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7251 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7252 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7253 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7254 715dc77e 2019-08-03 stsp char *cwd = NULL;
7255 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7256 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7257 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7258 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7259 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7260 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7262 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7264 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7265 715dc77e 2019-08-03 stsp switch (ch) {
7267 408b4ebc 2019-08-03 stsp list_stage = 1;
7270 dc424a06 2019-08-07 stsp pflag = 1;
7273 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7276 715dc77e 2019-08-03 stsp usage_stage();
7277 715dc77e 2019-08-03 stsp /* NOTREACHED */
7281 715dc77e 2019-08-03 stsp argc -= optind;
7282 715dc77e 2019-08-03 stsp argv += optind;
7284 715dc77e 2019-08-03 stsp #ifndef PROFILE
7285 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7286 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7287 715dc77e 2019-08-03 stsp err(1, "pledge");
7289 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7290 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7291 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7292 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7294 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7295 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7296 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7297 715dc77e 2019-08-03 stsp goto done;
7300 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7301 715dc77e 2019-08-03 stsp if (error)
7302 715dc77e 2019-08-03 stsp goto done;
7304 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7306 715dc77e 2019-08-03 stsp if (error != NULL)
7307 715dc77e 2019-08-03 stsp goto done;
7309 dc424a06 2019-08-07 stsp if (patch_script_path) {
7310 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7311 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7312 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7313 dc424a06 2019-08-07 stsp patch_script_path);
7314 dc424a06 2019-08-07 stsp goto done;
7317 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7318 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7319 715dc77e 2019-08-03 stsp if (error)
7320 715dc77e 2019-08-03 stsp goto done;
7322 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7323 6d022e97 2019-08-04 stsp if (error)
7324 6d022e97 2019-08-04 stsp goto done;
7326 6d022e97 2019-08-04 stsp if (list_stage)
7327 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7328 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7330 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7331 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7332 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7333 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7334 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7337 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7338 2e1f37b0 2019-08-08 stsp error == NULL)
7339 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7341 ad493afc 2019-08-03 stsp got_repo_close(repo);
7342 ad493afc 2019-08-03 stsp if (worktree)
7343 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7344 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7345 ad493afc 2019-08-03 stsp free((char *)pe->path);
7346 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7347 ad493afc 2019-08-03 stsp free(cwd);
7348 ad493afc 2019-08-03 stsp return error;
7351 ad493afc 2019-08-03 stsp __dead static void
7352 ad493afc 2019-08-03 stsp usage_unstage(void)
7354 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7355 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7356 ad493afc 2019-08-03 stsp getprogname());
7361 ad493afc 2019-08-03 stsp static const struct got_error *
7362 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7364 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7365 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7366 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7367 ad493afc 2019-08-03 stsp char *cwd = NULL;
7368 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7369 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7370 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7371 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7372 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7373 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7375 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7377 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7378 ad493afc 2019-08-03 stsp switch (ch) {
7380 2e1f37b0 2019-08-08 stsp pflag = 1;
7383 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7386 ad493afc 2019-08-03 stsp usage_unstage();
7387 ad493afc 2019-08-03 stsp /* NOTREACHED */
7391 ad493afc 2019-08-03 stsp argc -= optind;
7392 ad493afc 2019-08-03 stsp argv += optind;
7394 ad493afc 2019-08-03 stsp #ifndef PROFILE
7395 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7396 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7397 ad493afc 2019-08-03 stsp err(1, "pledge");
7399 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7400 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7402 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7403 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7404 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7405 ad493afc 2019-08-03 stsp goto done;
7408 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7409 ad493afc 2019-08-03 stsp if (error)
7410 ad493afc 2019-08-03 stsp goto done;
7412 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7414 ad493afc 2019-08-03 stsp if (error != NULL)
7415 ad493afc 2019-08-03 stsp goto done;
7417 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7418 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7419 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7420 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7421 2e1f37b0 2019-08-08 stsp patch_script_path);
7422 2e1f37b0 2019-08-08 stsp goto done;
7426 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7427 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7428 ad493afc 2019-08-03 stsp if (error)
7429 ad493afc 2019-08-03 stsp goto done;
7431 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7432 ad493afc 2019-08-03 stsp if (error)
7433 ad493afc 2019-08-03 stsp goto done;
7435 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7436 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7437 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7438 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7440 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7441 2e1f37b0 2019-08-08 stsp error == NULL)
7442 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7444 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7445 715dc77e 2019-08-03 stsp if (worktree)
7446 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7447 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7448 715dc77e 2019-08-03 stsp free((char *)pe->path);
7449 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7450 715dc77e 2019-08-03 stsp free(cwd);
7451 01073a5d 2019-08-22 stsp return error;
7454 01073a5d 2019-08-22 stsp __dead static void
7455 01073a5d 2019-08-22 stsp usage_cat(void)
7457 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7458 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7462 01073a5d 2019-08-22 stsp static const struct got_error *
7463 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7465 01073a5d 2019-08-22 stsp const struct got_error *err;
7466 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7468 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7470 01073a5d 2019-08-22 stsp return err;
7472 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7473 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7474 01073a5d 2019-08-22 stsp return err;
7477 01073a5d 2019-08-22 stsp static const struct got_error *
7478 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7480 01073a5d 2019-08-22 stsp const struct got_error *err;
7481 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7482 56e0773d 2019-11-28 stsp int nentries, i;
7484 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7486 01073a5d 2019-08-22 stsp return err;
7488 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7489 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7490 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7491 01073a5d 2019-08-22 stsp char *id_str;
7492 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7494 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7495 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7498 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7499 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7500 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7501 01073a5d 2019-08-22 stsp free(id_str);
7504 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7505 01073a5d 2019-08-22 stsp return err;
7508 01073a5d 2019-08-22 stsp static const struct got_error *
7509 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7511 01073a5d 2019-08-22 stsp const struct got_error *err;
7512 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7513 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7514 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7515 01073a5d 2019-08-22 stsp char *id_str = NULL;
7516 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7518 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7520 01073a5d 2019-08-22 stsp return err;
7522 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7524 01073a5d 2019-08-22 stsp goto done;
7526 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7527 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7528 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7529 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7530 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7531 01073a5d 2019-08-22 stsp char *pid_str;
7532 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7534 01073a5d 2019-08-22 stsp goto done;
7535 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7536 01073a5d 2019-08-22 stsp free(pid_str);
7538 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7539 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7540 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7542 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7543 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7544 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7546 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7547 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7548 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7550 01073a5d 2019-08-22 stsp free(id_str);
7551 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7552 01073a5d 2019-08-22 stsp return err;
7555 01073a5d 2019-08-22 stsp static const struct got_error *
7556 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7558 01073a5d 2019-08-22 stsp const struct got_error *err;
7559 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7560 01073a5d 2019-08-22 stsp char *id_str = NULL;
7561 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7563 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7565 01073a5d 2019-08-22 stsp return err;
7567 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7569 01073a5d 2019-08-22 stsp goto done;
7571 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7573 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7574 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7575 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7576 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7578 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7579 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7580 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7582 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7583 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7584 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7586 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7587 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7588 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7594 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7595 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7597 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7598 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7599 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7601 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7602 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7603 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7605 01073a5d 2019-08-22 stsp free(id_str);
7606 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7607 01073a5d 2019-08-22 stsp return err;
7610 01073a5d 2019-08-22 stsp static const struct got_error *
7611 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7613 01073a5d 2019-08-22 stsp const struct got_error *error;
7614 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7615 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7616 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7617 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7618 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7619 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7621 01073a5d 2019-08-22 stsp #ifndef PROFILE
7622 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7623 01073a5d 2019-08-22 stsp NULL) == -1)
7624 01073a5d 2019-08-22 stsp err(1, "pledge");
7627 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7628 01073a5d 2019-08-22 stsp switch (ch) {
7630 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7633 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7634 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7635 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7637 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7640 896e9b6f 2019-08-26 stsp force_path = 1;
7643 01073a5d 2019-08-22 stsp usage_cat();
7644 01073a5d 2019-08-22 stsp /* NOTREACHED */
7648 01073a5d 2019-08-22 stsp argc -= optind;
7649 01073a5d 2019-08-22 stsp argv += optind;
7651 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7652 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7653 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7654 01073a5d 2019-08-22 stsp goto done;
7656 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7657 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7658 01073a5d 2019-08-22 stsp goto done;
7659 01073a5d 2019-08-22 stsp if (worktree) {
7660 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7661 01073a5d 2019-08-22 stsp repo_path = strdup(
7662 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7663 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7664 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7665 01073a5d 2019-08-22 stsp goto done;
7670 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7671 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7672 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7673 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7676 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7677 01073a5d 2019-08-22 stsp free(repo_path);
7678 01073a5d 2019-08-22 stsp if (error != NULL)
7679 01073a5d 2019-08-22 stsp goto done;
7681 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7682 896e9b6f 2019-08-26 stsp if (error)
7683 896e9b6f 2019-08-26 stsp goto done;
7685 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7686 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7687 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7688 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7689 01073a5d 2019-08-22 stsp if (error)
7690 01073a5d 2019-08-22 stsp goto done;
7692 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7693 896e9b6f 2019-08-26 stsp if (force_path) {
7694 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7696 896e9b6f 2019-08-26 stsp if (error)
7699 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7700 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7701 896e9b6f 2019-08-26 stsp if (error) {
7702 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7703 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7705 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7706 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7707 896e9b6f 2019-08-26 stsp if (error)
7712 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7713 01073a5d 2019-08-22 stsp if (error)
7716 01073a5d 2019-08-22 stsp switch (obj_type) {
7717 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7718 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7720 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7721 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7723 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7724 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7726 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7727 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7730 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7733 01073a5d 2019-08-22 stsp if (error)
7735 01073a5d 2019-08-22 stsp free(label);
7736 01073a5d 2019-08-22 stsp label = NULL;
7738 01073a5d 2019-08-22 stsp id = NULL;
7741 01073a5d 2019-08-22 stsp free(label);
7743 896e9b6f 2019-08-26 stsp free(commit_id);
7744 01073a5d 2019-08-22 stsp if (worktree)
7745 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7746 01073a5d 2019-08-22 stsp if (repo) {
7747 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7748 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7749 01073a5d 2019-08-22 stsp if (error == NULL)
7750 01073a5d 2019-08-22 stsp error = repo_error;
7752 0ebf8283 2019-07-24 stsp return error;