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>
41 d2cdc636 2020-03-18 stsp #include <util.h>
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
58 5c860e29 2018-03-12 stsp #ifndef nitems
59 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
66 99437157 2018-11-11 stsp catch_sigint(int signo)
68 99437157 2018-11-11 stsp sigint_received = 1;
72 99437157 2018-11-11 stsp catch_sigpipe(int signo)
74 99437157 2018-11-11 stsp sigpipe_received = 1;
78 8cfb4057 2019-07-09 stsp struct got_cmd {
79 5c860e29 2018-03-12 stsp const char *cmd_name;
80 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
82 97b3a7be 2019-07-09 stsp const char *cmd_alias;
85 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
86 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
87 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
90 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
91 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
94 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
95 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
96 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
97 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
98 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
99 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
100 d00136be 2019-03-26 stsp __dead static void usage_add(void);
101 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
102 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
103 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
104 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
105 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
106 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
107 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
108 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
109 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
110 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
111 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
114 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
115 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
116 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
118 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
121 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
122 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
124 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
125 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
126 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
127 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
128 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
129 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
130 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
131 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
132 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
133 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
134 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
135 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
136 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
137 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
138 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
140 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
141 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
142 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
143 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
144 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
145 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
146 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
147 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
148 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
149 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
150 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
151 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
152 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
153 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
154 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
155 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
156 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
157 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
158 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
159 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
161 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
162 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
163 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
164 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
165 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
166 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
169 ce5b7c56 2019-07-09 stsp static void
170 ce5b7c56 2019-07-09 stsp list_commands(void)
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
175 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
176 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
177 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
179 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
183 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
185 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
186 5c860e29 2018-03-12 stsp unsigned int i;
188 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
189 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
190 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
191 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
194 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
196 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 5c860e29 2018-03-12 stsp switch (ch) {
205 ce5b7c56 2019-07-09 stsp usage(hflag);
206 5c860e29 2018-03-12 stsp /* NOTREACHED */
210 5c860e29 2018-03-12 stsp argc -= optind;
211 5c860e29 2018-03-12 stsp argv += optind;
212 1e70621d 2018-03-27 stsp optind = 0;
214 53ccebc2 2019-07-30 stsp if (Vflag) {
215 53ccebc2 2019-07-30 stsp got_version_print_str();
219 5c860e29 2018-03-12 stsp if (argc <= 0)
220 ce5b7c56 2019-07-09 stsp usage(hflag);
222 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
223 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
225 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
226 d7d4f210 2018-03-12 stsp const struct got_error *error;
228 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
230 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
235 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
237 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
238 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
239 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
240 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
241 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 70015d7a 2019-11-08 stsp !(sigint_received &&
243 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
251 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 ce5b7c56 2019-07-09 stsp list_commands();
256 4ed7e80c 2018-05-20 stsp __dead static void
257 ce5b7c56 2019-07-09 stsp usage(int hflag)
259 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 53ccebc2 2019-07-30 stsp getprogname());
262 ce5b7c56 2019-07-09 stsp list_commands();
266 0266afb7 2019-01-04 stsp static const struct got_error *
267 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
269 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
270 e2ba3d07 2019-05-13 stsp const char *editor;
272 8920fa04 2019-08-18 stsp *abspath = NULL;
274 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
275 e2ba3d07 2019-05-13 stsp if (editor == NULL)
276 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
278 0ee7065d 2019-05-13 stsp if (editor) {
279 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
281 0ee7065d 2019-05-13 stsp return err;
284 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
285 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
286 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
287 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
290 e2ba3d07 2019-05-13 stsp return NULL;
293 e2ba3d07 2019-05-13 stsp static const struct got_error *
294 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
295 c530dc23 2019-07-23 stsp const char *worktree_path)
297 163ce85a 2019-05-13 stsp const struct got_error *err;
299 37c06ea4 2019-07-15 stsp #ifdef PROFILE
300 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
301 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
303 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
306 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
309 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
313 163ce85a 2019-05-13 stsp if (err != NULL)
314 163ce85a 2019-05-13 stsp return err;
316 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
319 0266afb7 2019-01-04 stsp return NULL;
322 2c7829a4 2019-06-17 stsp __dead static void
323 2c7829a4 2019-06-17 stsp usage_init(void)
325 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
329 2c7829a4 2019-06-17 stsp static const struct got_error *
330 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
332 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
333 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
336 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
337 2c7829a4 2019-06-17 stsp switch (ch) {
339 2c7829a4 2019-06-17 stsp usage_init();
340 2c7829a4 2019-06-17 stsp /* NOTREACHED */
344 2c7829a4 2019-06-17 stsp argc -= optind;
345 2c7829a4 2019-06-17 stsp argv += optind;
347 2c7829a4 2019-06-17 stsp #ifndef PROFILE
348 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 2c7829a4 2019-06-17 stsp err(1, "pledge");
351 2c7829a4 2019-06-17 stsp if (argc != 1)
352 bc20e173 2019-06-17 stsp usage_init();
354 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
355 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
356 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
358 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
360 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
361 2c7829a4 2019-06-17 stsp if (error &&
362 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
365 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
369 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
371 3ce1b845 2019-07-15 stsp free(repo_path);
372 3ce1b845 2019-07-15 stsp return error;
375 3ce1b845 2019-07-15 stsp __dead static void
376 3ce1b845 2019-07-15 stsp usage_import(void)
378 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
384 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
387 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
388 3ce1b845 2019-07-15 stsp int st = -1;
390 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
391 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
392 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
394 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
396 3ce1b845 2019-07-15 stsp goto doneediting;
398 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
399 3ce1b845 2019-07-15 stsp _exit(127);
402 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
403 3ce1b845 2019-07-15 stsp if (errno != EINTR)
406 3ce1b845 2019-07-15 stsp doneediting:
407 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
408 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
409 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
411 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
412 3ce1b845 2019-07-15 stsp errno = EINTR;
416 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
419 3ce1b845 2019-07-15 stsp static const struct got_error *
420 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 3ce1b845 2019-07-15 stsp const char *initial_content)
423 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
424 3ce1b845 2019-07-15 stsp char buf[1024];
425 3ce1b845 2019-07-15 stsp struct stat st, st2;
427 3ce1b845 2019-07-15 stsp int content_changed = 0;
428 3ce1b845 2019-07-15 stsp size_t len;
430 3ce1b845 2019-07-15 stsp *logmsg = NULL;
432 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
435 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
438 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
439 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
441 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
445 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
446 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
447 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
448 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
451 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
452 3ce1b845 2019-07-15 stsp if (fp == NULL) {
453 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
456 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
457 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
458 3ce1b845 2019-07-15 stsp content_changed = 1;
459 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
461 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
463 3ce1b845 2019-07-15 stsp fclose(fp);
465 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
470 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
471 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
475 3ce1b845 2019-07-15 stsp free(*logmsg);
476 3ce1b845 2019-07-15 stsp *logmsg = NULL;
478 3ce1b845 2019-07-15 stsp return err;
481 3ce1b845 2019-07-15 stsp static const struct got_error *
482 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
485 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
486 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
489 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
490 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
491 3ce1b845 2019-07-15 stsp branch_name) == -1)
492 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
494 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
495 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
499 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
502 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
504 3ce1b845 2019-07-15 stsp free(initial_content);
505 3ce1b845 2019-07-15 stsp return err;
508 3ce1b845 2019-07-15 stsp static const struct got_error *
509 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
511 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
512 3ce1b845 2019-07-15 stsp return NULL;
515 3ce1b845 2019-07-15 stsp static const struct got_error *
516 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
518 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
519 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
521 84792843 2019-08-09 stsp *author = NULL;
523 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
524 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
525 c9956ddf 2019-09-08 stsp if (name && email) {
526 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
527 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
528 aba9c984 2019-09-08 stsp return NULL;
531 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
532 84792843 2019-08-09 stsp if (got_author == NULL) {
533 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
534 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
535 c9956ddf 2019-09-08 stsp if (name && email) {
536 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
537 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
538 c9956ddf 2019-09-08 stsp return NULL;
540 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
541 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 aba9c984 2019-09-08 stsp *author = strdup(got_author);
545 aba9c984 2019-09-08 stsp if (*author == NULL)
546 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
549 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
550 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
552 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
553 84792843 2019-08-09 stsp got_author++;
554 aba9c984 2019-09-08 stsp if (*got_author != '<') {
555 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
559 84792843 2019-08-09 stsp got_author++;
560 aba9c984 2019-09-08 stsp if (*got_author != '@') {
561 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
565 84792843 2019-08-09 stsp got_author++;
566 84792843 2019-08-09 stsp if (*got_author != '>')
567 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
570 aba9c984 2019-09-08 stsp free(*author);
571 aba9c984 2019-09-08 stsp *author = NULL;
573 aba9c984 2019-09-08 stsp return err;
576 c9956ddf 2019-09-08 stsp static const struct got_error *
577 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
579 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
581 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
582 c9956ddf 2019-09-08 stsp if (homedir) {
583 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
587 c9956ddf 2019-09-08 stsp return NULL;
590 84792843 2019-08-09 stsp static const struct got_error *
591 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
593 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
594 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
597 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
599 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
602 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
603 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
604 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
606 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
608 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 3ce1b845 2019-07-15 stsp switch (ch) {
611 3ce1b845 2019-07-15 stsp branch_name = optarg;
614 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
615 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
616 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
621 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
622 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
623 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
629 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
631 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
637 b2b65d18 2019-08-22 stsp usage_import();
638 3ce1b845 2019-07-15 stsp /* NOTREACHED */
642 3ce1b845 2019-07-15 stsp argc -= optind;
643 3ce1b845 2019-07-15 stsp argv += optind;
645 3ce1b845 2019-07-15 stsp #ifndef PROFILE
646 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
648 3ce1b845 2019-07-15 stsp NULL) == -1)
649 3ce1b845 2019-07-15 stsp err(1, "pledge");
651 3ce1b845 2019-07-15 stsp if (argc != 1)
652 3ce1b845 2019-07-15 stsp usage_import();
654 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
655 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
656 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
657 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
659 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
660 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
663 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
667 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
669 aba9c984 2019-09-08 stsp return error;
672 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
673 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
674 e560b7e0 2019-11-28 stsp * an unintended typo.
676 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
677 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
684 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
685 3ce1b845 2019-07-15 stsp if (error) {
686 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
689 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 3ce1b845 2019-07-15 stsp "import target branch already exists");
694 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
695 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
696 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
699 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
702 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
703 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
705 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
706 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
709 8e158b01 2019-09-22 stsp free(logmsg);
710 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 ef293bdd 2019-10-21 stsp path_dir, refname);
712 ef293bdd 2019-10-21 stsp if (error) {
713 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
715 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
720 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
721 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
722 ef293bdd 2019-10-21 stsp if (logmsg_path)
723 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
727 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 ef293bdd 2019-10-21 stsp if (error) {
729 ef293bdd 2019-10-21 stsp if (logmsg_path)
730 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
734 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
736 ef293bdd 2019-10-21 stsp if (error) {
737 ef293bdd 2019-10-21 stsp if (logmsg_path)
738 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
742 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 ef293bdd 2019-10-21 stsp if (error) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
749 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (logmsg_path)
752 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
756 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
757 ef293bdd 2019-10-21 stsp if (error) {
758 ef293bdd 2019-10-21 stsp if (logmsg_path)
759 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
763 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 3ce1b845 2019-07-15 stsp if (error) {
765 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
766 ef293bdd 2019-10-21 stsp if (logmsg_path)
767 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
771 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 3ce1b845 2019-07-15 stsp branch_ref);
773 ef293bdd 2019-10-21 stsp if (error) {
774 ef293bdd 2019-10-21 stsp if (logmsg_path)
775 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
779 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
780 ef293bdd 2019-10-21 stsp if (error) {
781 ef293bdd 2019-10-21 stsp if (logmsg_path)
782 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
787 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
788 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
790 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
791 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
792 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
793 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
795 8e158b01 2019-09-22 stsp free(logmsg);
796 ef293bdd 2019-10-21 stsp free(logmsg_path);
797 2c7829a4 2019-06-17 stsp free(repo_path);
798 3ce1b845 2019-07-15 stsp free(editor);
799 3ce1b845 2019-07-15 stsp free(refname);
800 3ce1b845 2019-07-15 stsp free(new_commit_id);
801 3ce1b845 2019-07-15 stsp free(id_str);
802 aba9c984 2019-09-08 stsp free(author);
803 c9956ddf 2019-09-08 stsp free(gitconfig_path);
804 3ce1b845 2019-07-15 stsp if (branch_ref)
805 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
806 3ce1b845 2019-07-15 stsp if (head_ref)
807 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
808 2c7829a4 2019-06-17 stsp return error;
811 93658fb9 2020-03-18 stsp __dead static void
812 93658fb9 2020-03-18 stsp usage_clone(void)
814 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
815 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
819 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
820 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
821 892ac3b6 2020-03-18 stsp int last_p_indexed;
822 892ac3b6 2020-03-18 stsp int last_p_resolved;
823 68999b92 2020-03-18 stsp int verbosity;
826 93658fb9 2020-03-18 stsp static const struct got_error *
827 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
828 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
831 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
832 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
833 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
835 68999b92 2020-03-18 stsp if (a->verbosity < 0)
836 68999b92 2020-03-18 stsp return NULL;
838 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
839 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
840 892ac3b6 2020-03-18 stsp fflush(stdout);
841 12d1281e 2020-03-19 stsp return NULL;
844 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
845 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
847 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 892ac3b6 2020-03-18 stsp print_size = 1;
849 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
850 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
853 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
854 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
855 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
856 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
857 892ac3b6 2020-03-18 stsp print_indexed = 1;
858 892ac3b6 2020-03-18 stsp print_size = 1;
861 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
862 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
863 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
864 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
865 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
866 892ac3b6 2020-03-18 stsp print_resolved = 1;
867 892ac3b6 2020-03-18 stsp print_indexed = 1;
868 892ac3b6 2020-03-18 stsp print_size = 1;
873 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
874 892ac3b6 2020-03-18 stsp printf("\r");
875 892ac3b6 2020-03-18 stsp if (print_size)
876 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 d715f13e 2020-03-19 stsp if (print_indexed)
878 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
879 d715f13e 2020-03-19 stsp if (print_resolved)
880 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
881 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
882 892ac3b6 2020-03-18 stsp fflush(stdout);
884 531c3985 2020-03-18 stsp return NULL;
887 531c3985 2020-03-18 stsp static const struct got_error *
888 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
889 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
891 4ba14133 2020-03-20 stsp const struct got_error *err;
892 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
894 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
896 4ba14133 2020-03-20 stsp return err;
898 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
899 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
900 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
901 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
902 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
904 4ba14133 2020-03-20 stsp return err;
907 4ba14133 2020-03-20 stsp static const struct got_error *
908 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
909 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
911 41b0de12 2020-03-21 stsp const struct got_error *err;
912 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
914 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
915 41b0de12 2020-03-21 stsp const char *refname = pe->path;
916 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
918 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
921 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
922 41b0de12 2020-03-21 stsp const char *refname = pe->path;
923 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
924 41b0de12 2020-03-21 stsp char *id_str;
926 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
928 41b0de12 2020-03-21 stsp return err;
929 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
930 41b0de12 2020-03-21 stsp free(id_str);
933 41b0de12 2020-03-21 stsp return NULL;
936 6338a6a1 2020-03-21 stsp static const struct got_error *
937 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
938 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
940 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
941 6338a6a1 2020-03-21 stsp struct got_reference *ref;
942 6338a6a1 2020-03-21 stsp char *id_str;
944 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
946 6338a6a1 2020-03-21 stsp return err;
948 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
952 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
953 6338a6a1 2020-03-21 stsp got_ref_close(ref);
955 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
956 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
958 6338a6a1 2020-03-21 stsp free(id_str);
959 6338a6a1 2020-03-21 stsp return err;
963 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
965 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
967 0e4002ca 2020-03-21 stsp refname += 5;
970 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
971 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
973 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
975 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
978 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
979 0e4002ca 2020-03-21 stsp wanted_ref += 5;
981 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
982 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
985 0e4002ca 2020-03-21 stsp /* Allow exact match. */
986 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
990 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
992 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
994 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
995 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1002 41b0de12 2020-03-21 stsp static const struct got_error *
1003 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1004 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1006 0e4002ca 2020-03-21 stsp const struct got_error *err;
1007 0e4002ca 2020-03-21 stsp char *remote_refname;
1009 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1010 0e4002ca 2020-03-21 stsp refname += 5;
1012 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1013 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1014 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1016 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1017 0e4002ca 2020-03-21 stsp free(remote_refname);
1018 0e4002ca 2020-03-21 stsp return err;
1021 0e4002ca 2020-03-21 stsp static const struct got_error *
1022 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1024 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1025 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1026 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1027 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1028 bb64b798 2020-03-18 stsp const char *repo_path;
1029 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1031 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1032 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1033 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1034 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1035 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1036 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1037 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
1038 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
1039 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
1040 b46f3e71 2020-03-18 stsp ssize_t n;
1041 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1042 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1043 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1045 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1046 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1047 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1048 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1050 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1051 93658fb9 2020-03-18 stsp switch (ch) {
1053 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1056 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1057 4ba14133 2020-03-20 stsp optarg, NULL);
1058 4ba14133 2020-03-20 stsp if (error)
1059 4ba14133 2020-03-20 stsp return error;
1062 41b0de12 2020-03-21 stsp list_refs_only = 1;
1065 469dd726 2020-03-20 stsp mirror_references = 1;
1068 68999b92 2020-03-18 stsp if (verbosity < 0)
1069 68999b92 2020-03-18 stsp verbosity = 0;
1070 68999b92 2020-03-18 stsp else if (verbosity < 3)
1071 68999b92 2020-03-18 stsp verbosity++;
1074 68999b92 2020-03-18 stsp verbosity = -1;
1077 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1078 0e4002ca 2020-03-21 stsp optarg, NULL);
1079 0e4002ca 2020-03-21 stsp if (error)
1080 0e4002ca 2020-03-21 stsp return error;
1083 93658fb9 2020-03-18 stsp usage_clone();
1087 93658fb9 2020-03-18 stsp argc -= optind;
1088 93658fb9 2020-03-18 stsp argv += optind;
1090 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1091 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1092 41b0de12 2020-03-21 stsp if (list_refs_only) {
1093 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1094 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1095 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1096 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1097 41b0de12 2020-03-21 stsp if (mirror_references)
1098 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1099 41b0de12 2020-03-21 stsp if (verbosity == -1)
1100 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1101 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1102 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1105 93658fb9 2020-03-18 stsp uri = argv[0];
1107 9df6f38b 2020-03-18 stsp if (argc == 1)
1108 93658fb9 2020-03-18 stsp dirname = NULL;
1109 9df6f38b 2020-03-18 stsp else if (argc == 2)
1110 93658fb9 2020-03-18 stsp dirname = argv[1];
1112 93658fb9 2020-03-18 stsp usage_clone();
1114 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1115 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1116 39c64a6a 2020-03-18 stsp if (error)
1117 09f63084 2020-03-20 stsp goto done;
1119 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1120 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1121 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1122 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1123 09838ffc 2020-03-18 stsp goto done;
1126 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1127 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1128 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1129 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1130 39c64a6a 2020-03-18 stsp err(1, "pledge");
1132 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1133 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1134 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1135 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1136 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1137 39c64a6a 2020-03-18 stsp err(1, "pledge");
1139 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1140 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1141 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1142 39c64a6a 2020-03-18 stsp goto done;
1144 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1145 39c64a6a 2020-03-18 stsp goto done;
1147 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1148 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1149 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1150 bb64b798 2020-03-18 stsp goto done;
1152 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1154 bb64b798 2020-03-18 stsp repo_path = dirname;
1156 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1157 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1158 41b0de12 2020-03-21 stsp if (error)
1159 41b0de12 2020-03-21 stsp goto done;
1161 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1162 41b0de12 2020-03-21 stsp if (error)
1163 41b0de12 2020-03-21 stsp goto done;
1164 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1165 41b0de12 2020-03-21 stsp if (error)
1166 41b0de12 2020-03-21 stsp goto done;
1169 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1170 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1171 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1172 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1173 ee448f5f 2020-03-18 stsp goto done;
1176 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1177 ee448f5f 2020-03-18 stsp if (error)
1178 ee448f5f 2020-03-18 stsp goto done;
1180 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1181 9c52365f 2020-03-21 stsp server_path, verbosity);
1182 39c64a6a 2020-03-18 stsp if (error)
1183 bb64b798 2020-03-18 stsp goto done;
1185 68999b92 2020-03-18 stsp if (verbosity >= 0)
1186 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1187 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1189 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1190 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1191 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1192 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1193 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1194 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1195 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1196 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1197 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1198 39c64a6a 2020-03-18 stsp if (error)
1199 d9b4d0c0 2020-03-18 stsp goto done;
1201 41b0de12 2020-03-21 stsp if (list_refs_only) {
1202 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1203 41b0de12 2020-03-21 stsp goto done;
1206 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1207 39c64a6a 2020-03-18 stsp if (error)
1208 d9b4d0c0 2020-03-18 stsp goto done;
1209 68999b92 2020-03-18 stsp if (verbosity >= 0)
1210 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1211 d9b4d0c0 2020-03-18 stsp free(id_str);
1213 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1214 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1215 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1216 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1217 7ebc0570 2020-03-18 stsp char *remote_refname;
1219 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1220 0e4002ca 2020-03-21 stsp !mirror_references) {
1221 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1222 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1223 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1224 0e4002ca 2020-03-21 stsp if (error)
1225 0e4002ca 2020-03-21 stsp goto done;
1229 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1230 39c64a6a 2020-03-18 stsp if (error)
1231 d9b4d0c0 2020-03-18 stsp goto done;
1233 469dd726 2020-03-20 stsp if (mirror_references)
1236 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1239 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1240 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1241 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1242 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1243 7ebc0570 2020-03-18 stsp goto done;
1245 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1246 f298ae0f 2020-03-25 stsp free(remote_refname);
1247 39c64a6a 2020-03-18 stsp if (error)
1248 d9b4d0c0 2020-03-18 stsp goto done;
1251 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1253 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1254 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1255 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1256 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1258 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1261 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1262 39c64a6a 2020-03-18 stsp if (error) {
1263 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1264 55330abe 2020-03-20 stsp error = NULL;
1267 d9b4d0c0 2020-03-18 stsp goto done;
1270 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1271 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1272 f298ae0f 2020-03-25 stsp if (error)
1273 f298ae0f 2020-03-25 stsp goto done;
1275 f298ae0f 2020-03-25 stsp if (mirror_references)
1278 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1281 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1282 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1283 f298ae0f 2020-03-25 stsp refname) == -1) {
1284 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1285 f298ae0f 2020-03-25 stsp goto done;
1287 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1288 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1289 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1290 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1291 f298ae0f 2020-03-25 stsp free(remote_refname);
1292 f298ae0f 2020-03-25 stsp goto done;
1294 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1295 f298ae0f 2020-03-25 stsp if (error) {
1296 f298ae0f 2020-03-25 stsp free(remote_refname);
1297 f298ae0f 2020-03-25 stsp free(remote_target);
1298 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1299 f298ae0f 2020-03-25 stsp error = NULL;
1302 f298ae0f 2020-03-25 stsp goto done;
1304 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1305 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1306 f298ae0f 2020-03-25 stsp free(remote_refname);
1307 f298ae0f 2020-03-25 stsp free(remote_target);
1308 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1309 39c64a6a 2020-03-18 stsp if (error)
1310 d9b4d0c0 2020-03-18 stsp goto done;
1312 4ba14133 2020-03-20 stsp if (pe == NULL) {
1314 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1315 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1316 4ba14133 2020-03-20 stsp * which could be fetched instead.
1318 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1319 4ba14133 2020-03-20 stsp const char *target = pe->path;
1320 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1322 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1323 4ba14133 2020-03-20 stsp if (error) {
1324 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1325 4ba14133 2020-03-20 stsp error = NULL;
1328 4ba14133 2020-03-20 stsp goto done;
1331 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1332 f298ae0f 2020-03-25 stsp verbosity, repo);
1333 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1334 4ba14133 2020-03-20 stsp if (error)
1335 4ba14133 2020-03-20 stsp goto done;
1340 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1341 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1342 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1343 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1344 659e7fbd 2020-03-20 stsp goto done;
1346 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1347 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1348 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1349 659e7fbd 2020-03-20 stsp goto done;
1351 659e7fbd 2020-03-20 stsp if (mirror_references) {
1352 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1353 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1354 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1355 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1356 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1357 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1358 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1359 659e7fbd 2020-03-20 stsp goto done;
1361 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1362 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1363 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1364 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1365 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1366 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1367 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1368 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1369 659e7fbd 2020-03-20 stsp goto done;
1372 659e7fbd 2020-03-20 stsp const char *branchname;
1375 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1376 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1378 659e7fbd 2020-03-20 stsp if (head_symref) {
1379 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1380 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1381 659e7fbd 2020-03-20 stsp branchname += 11;
1383 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1384 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1385 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1386 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1387 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1388 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1389 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1390 659e7fbd 2020-03-20 stsp branchname) == -1) {
1391 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1392 659e7fbd 2020-03-20 stsp goto done;
1395 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1396 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1397 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1398 659e7fbd 2020-03-20 stsp goto done;
1401 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1402 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1403 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1405 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1406 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1407 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1408 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1409 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1411 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1412 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1413 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1414 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1416 bb64b798 2020-03-18 stsp got_repo_close(repo);
1417 659e7fbd 2020-03-20 stsp if (head_symref)
1418 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1419 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1420 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1421 d9b4d0c0 2020-03-18 stsp free(pe->data);
1423 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1424 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1425 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1426 d9b4d0c0 2020-03-18 stsp free(pe->data);
1428 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1429 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1430 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1431 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1432 09838ffc 2020-03-18 stsp free(proto);
1433 09838ffc 2020-03-18 stsp free(host);
1434 09838ffc 2020-03-18 stsp free(port);
1435 09838ffc 2020-03-18 stsp free(server_path);
1436 09838ffc 2020-03-18 stsp free(repo_name);
1437 bb64b798 2020-03-18 stsp free(default_destdir);
1438 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1439 b46f3e71 2020-03-18 stsp free(git_url);
1440 39c64a6a 2020-03-18 stsp return error;
1443 7848a0e1 2020-03-19 stsp static const struct got_error *
1444 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1445 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1447 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1448 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1449 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1451 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1453 7848a0e1 2020-03-19 stsp goto done;
1455 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1456 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1457 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1459 88609724 2020-03-21 stsp goto done;
1460 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1461 88609724 2020-03-21 stsp goto done;
1462 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1463 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1464 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1466 db6d8ad8 2020-03-21 stsp goto done;
1469 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1470 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1471 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1472 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1473 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1475 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1477 7848a0e1 2020-03-19 stsp goto done;
1478 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1480 e8a967e0 2020-03-21 stsp goto done;
1482 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1484 7848a0e1 2020-03-19 stsp goto done;
1485 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1486 6338a6a1 2020-03-21 stsp goto done;
1488 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1490 6338a6a1 2020-03-21 stsp goto done;
1491 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1493 6338a6a1 2020-03-21 stsp goto done;
1496 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1497 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1498 6338a6a1 2020-03-21 stsp new_id_str);
1500 7848a0e1 2020-03-19 stsp free(old_id);
1501 7848a0e1 2020-03-19 stsp free(new_id_str);
1502 7848a0e1 2020-03-19 stsp return err;
1505 f1bcca34 2020-03-25 stsp static const struct got_error *
1506 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1507 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1509 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1510 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1512 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1514 f1bcca34 2020-03-25 stsp return err;
1516 f1bcca34 2020-03-25 stsp if (strcmp(got_ref_get_symref_target(symref),
1517 f1bcca34 2020-03-25 stsp got_ref_get_name(target_ref)) == 0)
1518 f1bcca34 2020-03-25 stsp goto done;
1520 f1bcca34 2020-03-25 stsp err = got_ref_change_symref(symref, got_ref_get_name(target_ref));
1522 f1bcca34 2020-03-25 stsp goto done;
1524 f1bcca34 2020-03-25 stsp err = got_ref_write(symref, repo);
1526 f1bcca34 2020-03-25 stsp goto done;
1528 f1bcca34 2020-03-25 stsp if (verbosity >= 0)
1529 f1bcca34 2020-03-25 stsp printf("Updated reference %s: %s\n", got_ref_get_name(symref),
1530 f1bcca34 2020-03-25 stsp got_ref_get_symref_target(symref));
1532 f1bcca34 2020-03-25 stsp unlock_err = got_ref_unlock(symref);
1533 f1bcca34 2020-03-25 stsp if (unlock_err && err == NULL)
1534 f1bcca34 2020-03-25 stsp err = unlock_err;
1535 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1536 f1bcca34 2020-03-25 stsp return err;
1537 f1bcca34 2020-03-25 stsp return NULL;
1540 2ab43947 2020-03-18 stsp __dead static void
1541 7848a0e1 2020-03-19 stsp usage_fetch(void)
1543 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1544 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1545 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1546 13f12b09 2020-03-21 stsp getprogname());
1550 7848a0e1 2020-03-19 stsp static const struct got_error *
1551 f21ec2f0 2020-03-21 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1552 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1554 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1555 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1556 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1557 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1558 f21ec2f0 2020-03-21 stsp struct got_object_id *id;
1559 f21ec2f0 2020-03-21 stsp char *id_str;
1561 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1563 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1565 f21ec2f0 2020-03-21 stsp return err;
1567 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1568 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1570 f21ec2f0 2020-03-21 stsp if (strncmp(refname, "refs/heads/", 11) != 0 &&
1571 f21ec2f0 2020-03-21 stsp strncmp(refname, "refs/tags/", 10) != 0)
1574 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1575 f21ec2f0 2020-03-21 stsp if (strcmp(refname, pe->path) == 0)
1578 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1581 f21ec2f0 2020-03-21 stsp err = got_ref_resolve(&id, repo, re->ref);
1584 f21ec2f0 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1589 f21ec2f0 2020-03-21 stsp free(id_str);
1590 f21ec2f0 2020-03-21 stsp err = got_ref_delete(re->ref, repo);
1593 6338a6a1 2020-03-21 stsp if (verbosity >= 0) {
1594 6338a6a1 2020-03-21 stsp printf("Deleted reference %s: %s\n",
1595 6338a6a1 2020-03-21 stsp got_ref_get_name(re->ref), id_str);
1599 0e4002ca 2020-03-21 stsp return err;
1602 0e4002ca 2020-03-21 stsp static const struct got_error *
1603 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1604 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1606 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1607 0e4002ca 2020-03-21 stsp char *remote_refname;
1608 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1610 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1611 0e4002ca 2020-03-21 stsp refname += 5;
1613 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1614 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1615 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1617 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1618 0e4002ca 2020-03-21 stsp if (err) {
1619 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1620 0e4002ca 2020-03-21 stsp goto done;
1621 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1623 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1624 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1625 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1626 9f142382 2020-03-21 stsp err = unlock_err;
1627 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1630 0e4002ca 2020-03-21 stsp free(remote_refname);
1631 f21ec2f0 2020-03-21 stsp return err;
1634 f21ec2f0 2020-03-21 stsp static const struct got_error *
1635 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1637 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1638 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1639 7848a0e1 2020-03-19 stsp const char *remote_name;
1640 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1641 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1642 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1643 7848a0e1 2020-03-19 stsp int nremotes;
1644 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1645 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1646 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1647 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1648 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1649 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1650 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1651 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1652 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1653 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1654 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1656 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1657 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1658 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1659 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1661 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1662 7848a0e1 2020-03-19 stsp switch (ch) {
1664 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1667 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1668 4ba14133 2020-03-20 stsp optarg, NULL);
1669 4ba14133 2020-03-20 stsp if (error)
1670 4ba14133 2020-03-20 stsp return error;
1673 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1676 41b0de12 2020-03-21 stsp list_refs_only = 1;
1679 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1680 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1681 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1683 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1686 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1689 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1690 7848a0e1 2020-03-19 stsp verbosity = 0;
1691 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1692 7848a0e1 2020-03-19 stsp verbosity++;
1695 7848a0e1 2020-03-19 stsp verbosity = -1;
1698 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1699 0e4002ca 2020-03-21 stsp optarg, NULL);
1700 0e4002ca 2020-03-21 stsp if (error)
1701 0e4002ca 2020-03-21 stsp return error;
1704 7848a0e1 2020-03-19 stsp usage_fetch();
1708 7848a0e1 2020-03-19 stsp argc -= optind;
1709 7848a0e1 2020-03-19 stsp argv += optind;
1711 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1712 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1713 41b0de12 2020-03-21 stsp if (list_refs_only) {
1714 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1715 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1716 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1717 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1718 f21ec2f0 2020-03-21 stsp if (delete_refs)
1719 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1720 41b0de12 2020-03-21 stsp if (verbosity == -1)
1721 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1724 7848a0e1 2020-03-19 stsp if (argc == 0)
1725 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1726 7848a0e1 2020-03-19 stsp else if (argc == 1)
1727 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1729 7848a0e1 2020-03-19 stsp usage_fetch();
1731 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1732 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1733 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1734 7848a0e1 2020-03-19 stsp goto done;
1737 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1738 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1739 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1740 7848a0e1 2020-03-19 stsp goto done;
1742 7848a0e1 2020-03-19 stsp error = NULL;
1743 7848a0e1 2020-03-19 stsp if (worktree) {
1744 7848a0e1 2020-03-19 stsp repo_path =
1745 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1746 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1747 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1748 7848a0e1 2020-03-19 stsp if (error)
1749 7848a0e1 2020-03-19 stsp goto done;
1751 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1752 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1753 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1754 7848a0e1 2020-03-19 stsp goto done;
1759 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1760 7848a0e1 2020-03-19 stsp if (error)
1761 7848a0e1 2020-03-19 stsp goto done;
1763 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1764 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1765 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1766 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1769 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1770 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1771 7848a0e1 2020-03-19 stsp goto done;
1774 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1775 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1776 7848a0e1 2020-03-19 stsp if (error)
1777 7848a0e1 2020-03-19 stsp goto done;
1779 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1780 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1781 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1782 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1783 7848a0e1 2020-03-19 stsp err(1, "pledge");
1785 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1786 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1787 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1788 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1789 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1790 7848a0e1 2020-03-19 stsp err(1, "pledge");
1792 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1793 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1794 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1795 7848a0e1 2020-03-19 stsp goto done;
1797 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1798 7848a0e1 2020-03-19 stsp goto done;
1801 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1802 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1803 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1804 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1805 7848a0e1 2020-03-19 stsp goto done;
1808 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1809 7848a0e1 2020-03-19 stsp if (error)
1810 7848a0e1 2020-03-19 stsp goto done;
1812 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1813 9c52365f 2020-03-21 stsp server_path, verbosity);
1814 7848a0e1 2020-03-19 stsp if (error)
1815 7848a0e1 2020-03-19 stsp goto done;
1817 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1818 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1819 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1821 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1822 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1823 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1824 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1825 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1826 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1827 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1828 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1829 7848a0e1 2020-03-19 stsp if (error)
1830 7848a0e1 2020-03-19 stsp goto done;
1832 41b0de12 2020-03-21 stsp if (list_refs_only) {
1833 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1834 41b0de12 2020-03-21 stsp goto done;
1837 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1838 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1839 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1840 f21ec2f0 2020-03-21 stsp if (delete_refs)
1841 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1842 7848a0e1 2020-03-19 stsp goto done;
1845 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1846 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1847 984065c8 2020-03-19 stsp if (error)
1848 984065c8 2020-03-19 stsp goto done;
1849 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1850 984065c8 2020-03-19 stsp free(id_str);
1851 984065c8 2020-03-19 stsp id_str = NULL;
1854 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1855 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1856 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1857 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1858 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1859 7848a0e1 2020-03-19 stsp char *remote_refname;
1861 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1862 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
1863 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
1864 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
1865 0e4002ca 2020-03-21 stsp if (error)
1866 0e4002ca 2020-03-21 stsp goto done;
1870 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1871 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1872 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1873 7848a0e1 2020-03-19 stsp if (error) {
1874 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1875 7848a0e1 2020-03-19 stsp goto done;
1876 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1878 7848a0e1 2020-03-19 stsp if (error)
1879 7848a0e1 2020-03-19 stsp goto done;
1881 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1882 db6d8ad8 2020-03-21 stsp verbosity, repo);
1883 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1884 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1885 9f142382 2020-03-21 stsp error = unlock_err;
1886 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1887 7848a0e1 2020-03-19 stsp if (error)
1888 7848a0e1 2020-03-19 stsp goto done;
1890 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1891 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1892 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1893 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1894 7848a0e1 2020-03-19 stsp goto done;
1897 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
1898 7848a0e1 2020-03-19 stsp if (error) {
1899 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1900 7848a0e1 2020-03-19 stsp goto done;
1901 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1902 688f11b3 2020-03-21 stsp verbosity, repo);
1903 7848a0e1 2020-03-19 stsp if (error)
1904 7848a0e1 2020-03-19 stsp goto done;
1906 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1907 db6d8ad8 2020-03-21 stsp verbosity, repo);
1908 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1909 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1910 9f142382 2020-03-21 stsp error = unlock_err;
1911 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1912 7848a0e1 2020-03-19 stsp if (error)
1913 7848a0e1 2020-03-19 stsp goto done;
1916 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
1917 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1918 2ec30c80 2020-03-20 stsp if (error) {
1919 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
1920 2ec30c80 2020-03-20 stsp goto done;
1921 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1923 2ec30c80 2020-03-20 stsp if (error)
1924 2ec30c80 2020-03-20 stsp goto done;
1926 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1927 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1928 9f142382 2020-03-21 stsp error = unlock_err;
1929 2ec30c80 2020-03-20 stsp got_ref_close(ref);
1933 f1bcca34 2020-03-25 stsp if (delete_refs) {
1934 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1935 f1bcca34 2020-03-25 stsp if (error)
1936 f1bcca34 2020-03-25 stsp goto done;
1939 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
1940 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
1941 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1942 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
1943 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
1944 f1bcca34 2020-03-25 stsp const char *target = pe->data;
1945 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1947 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1950 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1953 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1954 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
1955 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
1956 f1bcca34 2020-03-25 stsp goto done;
1958 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
1959 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
1960 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
1961 f1bcca34 2020-03-25 stsp free(remote_refname);
1962 f1bcca34 2020-03-25 stsp goto done;
1965 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
1967 f1bcca34 2020-03-25 stsp if (error) {
1968 f1bcca34 2020-03-25 stsp free(remote_refname);
1969 f1bcca34 2020-03-25 stsp free(remote_target);
1970 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1971 f1bcca34 2020-03-25 stsp error = NULL;
1974 f1bcca34 2020-03-25 stsp goto done;
1976 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
1977 f1bcca34 2020-03-25 stsp verbosity, repo);
1978 f1bcca34 2020-03-25 stsp free(remote_refname);
1979 f1bcca34 2020-03-25 stsp free(remote_target);
1980 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
1981 f1bcca34 2020-03-25 stsp if (error)
1982 f1bcca34 2020-03-25 stsp goto done;
1986 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1987 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1988 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1989 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1990 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1992 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1993 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1995 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1996 7848a0e1 2020-03-19 stsp if (worktree)
1997 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1998 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1999 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2000 7848a0e1 2020-03-19 stsp free(pe->data);
2002 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2003 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2004 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2005 7848a0e1 2020-03-19 stsp free(pe->data);
2007 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2008 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2009 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2010 7848a0e1 2020-03-19 stsp free(id_str);
2011 7848a0e1 2020-03-19 stsp free(cwd);
2012 7848a0e1 2020-03-19 stsp free(repo_path);
2013 7848a0e1 2020-03-19 stsp free(pack_hash);
2014 7848a0e1 2020-03-19 stsp free(proto);
2015 7848a0e1 2020-03-19 stsp free(host);
2016 7848a0e1 2020-03-19 stsp free(port);
2017 7848a0e1 2020-03-19 stsp free(server_path);
2018 7848a0e1 2020-03-19 stsp free(repo_name);
2019 7848a0e1 2020-03-19 stsp return error;
2023 7848a0e1 2020-03-19 stsp __dead static void
2024 2ab43947 2020-03-18 stsp usage_checkout(void)
2026 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2027 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2031 2ab43947 2020-03-18 stsp static void
2032 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2034 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2035 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2036 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2037 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2038 2ab43947 2020-03-18 stsp getprogname());
2041 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2042 2ab43947 2020-03-18 stsp const char *worktree_path;
2043 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2046 2ab43947 2020-03-18 stsp static const struct got_error *
2047 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2049 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2051 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2052 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2053 2ab43947 2020-03-18 stsp return NULL;
2055 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2056 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2057 2ab43947 2020-03-18 stsp return NULL;
2060 2ab43947 2020-03-18 stsp while (path[0] == '/')
2063 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2064 2ab43947 2020-03-18 stsp return NULL;
2067 2ab43947 2020-03-18 stsp static const struct got_error *
2068 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2070 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2071 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2072 2ab43947 2020-03-18 stsp return NULL;
2075 2ab43947 2020-03-18 stsp static const struct got_error *
2076 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2077 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2078 2ab43947 2020-03-18 stsp struct got_repository *repo)
2080 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2081 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2083 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2084 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2086 2ab43947 2020-03-18 stsp return err;
2088 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2089 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2092 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2093 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2095 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2097 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2105 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2106 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2107 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2109 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2110 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2111 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2112 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2113 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2114 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2116 2ab43947 2020-03-18 stsp free(yca_id);
2117 2ab43947 2020-03-18 stsp return NULL;
2120 2ab43947 2020-03-18 stsp static const struct got_error *
2121 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2122 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2123 2ab43947 2020-03-18 stsp struct got_repository *repo)
2125 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2126 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2127 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2128 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2130 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2132 2ab43947 2020-03-18 stsp goto done;
2134 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2135 2ab43947 2020-03-18 stsp is_same_branch = 1;
2136 2ab43947 2020-03-18 stsp goto done;
2138 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2139 2ab43947 2020-03-18 stsp is_same_branch = 1;
2140 2ab43947 2020-03-18 stsp goto done;
2143 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2145 2ab43947 2020-03-18 stsp goto done;
2147 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2148 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2150 2ab43947 2020-03-18 stsp goto done;
2152 2ab43947 2020-03-18 stsp for (;;) {
2153 2ab43947 2020-03-18 stsp struct got_object_id *id;
2154 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2155 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2156 2ab43947 2020-03-18 stsp if (err) {
2157 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2158 2ab43947 2020-03-18 stsp err = NULL;
2163 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2165 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2166 2ab43947 2020-03-18 stsp is_same_branch = 1;
2172 2ab43947 2020-03-18 stsp if (graph)
2173 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2174 2ab43947 2020-03-18 stsp free(head_commit_id);
2175 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2176 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2177 2ab43947 2020-03-18 stsp return err;
2180 4ed7e80c 2018-05-20 stsp static const struct got_error *
2181 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2183 4b6c9460 2020-03-05 stsp static char msg[512];
2184 4b6c9460 2020-03-05 stsp const char *branch_name;
2186 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2187 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2189 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2191 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2192 4b6c9460 2020-03-05 stsp branch_name += 11;
2194 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2195 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2196 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2197 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2198 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2199 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2201 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2204 4b6c9460 2020-03-05 stsp static const struct got_error *
2205 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2207 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2208 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2209 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2210 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2211 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2212 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2213 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2214 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2215 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2216 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2217 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2218 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2220 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2222 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2223 0bb8a95e 2018-03-12 stsp switch (ch) {
2225 08573d5b 2019-05-14 stsp branch_name = optarg;
2228 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2229 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2230 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2233 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2236 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2239 2deda0b9 2019-03-07 stsp usage_checkout();
2240 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2244 0bb8a95e 2018-03-12 stsp argc -= optind;
2245 0bb8a95e 2018-03-12 stsp argv += optind;
2247 6715a751 2018-03-16 stsp #ifndef PROFILE
2248 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2249 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2250 c09a553d 2018-03-12 stsp err(1, "pledge");
2252 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2253 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2254 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2255 76089277 2018-04-01 stsp if (repo_path == NULL)
2256 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2257 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2258 76089277 2018-04-01 stsp if (cwd == NULL) {
2259 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2260 76089277 2018-04-01 stsp goto done;
2262 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2263 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2264 230a42bd 2019-05-11 jcs if (base == NULL) {
2265 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2266 230a42bd 2019-05-11 jcs path_prefix);
2270 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2271 230a42bd 2019-05-11 jcs if (base == NULL) {
2272 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2273 230a42bd 2019-05-11 jcs repo_path);
2277 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2278 c09a553d 2018-03-12 stsp if (dotgit)
2279 c09a553d 2018-03-12 stsp *dotgit = '\0';
2280 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2281 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2282 c09a553d 2018-03-12 stsp free(cwd);
2283 76089277 2018-04-01 stsp goto done;
2285 c09a553d 2018-03-12 stsp free(cwd);
2286 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2287 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2288 76089277 2018-04-01 stsp if (repo_path == NULL) {
2289 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2290 76089277 2018-04-01 stsp goto done;
2292 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2293 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2294 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2295 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2297 b4b3a7dd 2019-07-22 stsp goto done;
2299 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2300 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2301 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2302 b4b3a7dd 2019-07-22 stsp goto done;
2306 c09a553d 2018-03-12 stsp usage_checkout();
2308 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2309 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2311 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2312 c09a553d 2018-03-12 stsp if (error != NULL)
2313 c02c541e 2019-03-29 stsp goto done;
2315 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2316 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2317 c530dc23 2019-07-23 stsp if (error) {
2318 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2319 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2320 c530dc23 2019-07-23 stsp goto done;
2321 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2322 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2323 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2324 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2325 c530dc23 2019-07-23 stsp goto done;
2329 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2330 c02c541e 2019-03-29 stsp if (error)
2331 c09a553d 2018-03-12 stsp goto done;
2333 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2334 c09a553d 2018-03-12 stsp if (error != NULL)
2335 c09a553d 2018-03-12 stsp goto done;
2337 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2338 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2339 c09a553d 2018-03-12 stsp goto done;
2341 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2342 c09a553d 2018-03-12 stsp if (error != NULL)
2343 c09a553d 2018-03-12 stsp goto done;
2345 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2346 e5dc7198 2018-12-29 stsp path_prefix);
2347 e5dc7198 2018-12-29 stsp if (error != NULL)
2348 e5dc7198 2018-12-29 stsp goto done;
2349 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2350 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2351 49520a32 2018-12-29 stsp goto done;
2354 8069f636 2019-01-12 stsp if (commit_id_str) {
2355 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2356 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2357 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2358 30837e32 2019-07-25 stsp if (error)
2359 8069f636 2019-01-12 stsp goto done;
2360 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2361 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2362 8069f636 2019-01-12 stsp if (error != NULL) {
2363 8069f636 2019-01-12 stsp free(commit_id);
2364 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2365 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2366 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2368 8069f636 2019-01-12 stsp goto done;
2370 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2371 4b6c9460 2020-03-05 stsp if (error) {
2372 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2373 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2374 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2376 45d344f6 2019-05-14 stsp goto done;
2378 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2379 8069f636 2019-01-12 stsp commit_id);
2380 8069f636 2019-01-12 stsp free(commit_id);
2381 8069f636 2019-01-12 stsp if (error)
2382 8069f636 2019-01-12 stsp goto done;
2385 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2386 f2ea84fa 2019-07-27 stsp if (error)
2387 f2ea84fa 2019-07-27 stsp goto done;
2388 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2389 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2390 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2391 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2392 c09a553d 2018-03-12 stsp if (error != NULL)
2393 c09a553d 2018-03-12 stsp goto done;
2395 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2396 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2397 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2399 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2400 8069f636 2019-01-12 stsp free(commit_id_str);
2401 76089277 2018-04-01 stsp free(repo_path);
2402 507dc3bb 2018-12-29 stsp free(worktree_path);
2403 507dc3bb 2018-12-29 stsp return error;
2406 507dc3bb 2018-12-29 stsp __dead static void
2407 507dc3bb 2018-12-29 stsp usage_update(void)
2409 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2410 507dc3bb 2018-12-29 stsp getprogname());
2414 1ee397ad 2019-07-12 stsp static const struct got_error *
2415 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2417 784955db 2019-01-12 stsp int *did_something = arg;
2419 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2420 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2421 1ee397ad 2019-07-12 stsp return NULL;
2423 1545c615 2019-02-10 stsp *did_something = 1;
2425 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2426 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2427 1ee397ad 2019-07-12 stsp return NULL;
2429 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2431 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2432 1ee397ad 2019-07-12 stsp return NULL;
2435 be7061eb 2018-12-30 stsp static const struct got_error *
2436 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2437 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2438 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2440 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2441 a1fb16d8 2019-05-24 stsp char *base_id_str;
2442 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2444 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2445 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2446 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2447 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2448 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2449 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2450 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2453 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2454 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2455 a1fb16d8 2019-05-24 stsp if (err) {
2456 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2457 a1fb16d8 2019-05-24 stsp return err;
2458 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2460 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2461 a1fb16d8 2019-05-24 stsp return NULL;
2463 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2464 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2465 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2467 a1fb16d8 2019-05-24 stsp return err;
2468 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2469 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2470 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2471 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2472 0ebf8283 2019-07-24 stsp return NULL;
2475 0ebf8283 2019-07-24 stsp static const struct got_error *
2476 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2478 0ebf8283 2019-07-24 stsp const struct got_error *err;
2479 0ebf8283 2019-07-24 stsp int in_progress;
2481 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2483 0ebf8283 2019-07-24 stsp return err;
2484 0ebf8283 2019-07-24 stsp if (in_progress)
2485 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2487 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2489 0ebf8283 2019-07-24 stsp return err;
2490 0ebf8283 2019-07-24 stsp if (in_progress)
2491 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2493 a1fb16d8 2019-05-24 stsp return NULL;
2496 a5edda0a 2019-07-27 stsp static const struct got_error *
2497 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2498 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2500 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2501 a5edda0a 2019-07-27 stsp char *path;
2504 a5edda0a 2019-07-27 stsp if (argc == 0) {
2505 a5edda0a 2019-07-27 stsp path = strdup("");
2506 a5edda0a 2019-07-27 stsp if (path == NULL)
2507 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2508 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2511 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2512 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2515 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2516 a5edda0a 2019-07-27 stsp if (err) {
2517 a5edda0a 2019-07-27 stsp free(path);
2522 a5edda0a 2019-07-27 stsp return err;
2525 a1fb16d8 2019-05-24 stsp static const struct got_error *
2526 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2528 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2529 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2530 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2531 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2532 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2533 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2534 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2535 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2536 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2537 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2538 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2540 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2542 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2543 507dc3bb 2018-12-29 stsp switch (ch) {
2545 024e9686 2019-05-14 stsp branch_name = optarg;
2548 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2549 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2550 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2553 2deda0b9 2019-03-07 stsp usage_update();
2554 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2558 507dc3bb 2018-12-29 stsp argc -= optind;
2559 507dc3bb 2018-12-29 stsp argv += optind;
2561 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2562 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2563 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2564 507dc3bb 2018-12-29 stsp err(1, "pledge");
2566 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2567 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2568 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2569 c4cdcb68 2019-04-03 stsp goto done;
2571 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2572 7d5807f4 2019-07-11 stsp if (error)
2573 7d5807f4 2019-07-11 stsp goto done;
2575 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2576 f2ea84fa 2019-07-27 stsp if (error)
2577 f2ea84fa 2019-07-27 stsp goto done;
2579 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2581 507dc3bb 2018-12-29 stsp if (error != NULL)
2582 507dc3bb 2018-12-29 stsp goto done;
2584 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2585 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2586 087fb88c 2019-08-04 stsp if (error)
2587 087fb88c 2019-08-04 stsp goto done;
2589 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2590 0266afb7 2019-01-04 stsp if (error)
2591 0266afb7 2019-01-04 stsp goto done;
2593 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2594 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2595 024e9686 2019-05-14 stsp if (error != NULL)
2596 024e9686 2019-05-14 stsp goto done;
2597 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2598 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2599 9c4b8182 2019-01-02 stsp if (error != NULL)
2600 9c4b8182 2019-01-02 stsp goto done;
2601 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2602 507dc3bb 2018-12-29 stsp if (error != NULL)
2603 507dc3bb 2018-12-29 stsp goto done;
2605 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2606 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2607 04f57cb3 2019-07-25 stsp free(commit_id_str);
2608 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2609 30837e32 2019-07-25 stsp if (error)
2610 a297e751 2019-07-11 stsp goto done;
2611 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2612 a297e751 2019-07-11 stsp if (error)
2613 507dc3bb 2018-12-29 stsp goto done;
2616 a1fb16d8 2019-05-24 stsp if (branch_name) {
2617 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2618 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2619 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2621 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2622 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2623 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2624 024e9686 2019-05-14 stsp goto done;
2626 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2627 024e9686 2019-05-14 stsp if (error)
2628 024e9686 2019-05-14 stsp goto done;
2629 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2631 a367ff0f 2019-05-14 stsp free(head_commit_id);
2632 024e9686 2019-05-14 stsp if (error != NULL)
2633 024e9686 2019-05-14 stsp goto done;
2634 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2635 a367ff0f 2019-05-14 stsp if (error)
2636 a367ff0f 2019-05-14 stsp goto done;
2637 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2638 024e9686 2019-05-14 stsp if (error)
2639 024e9686 2019-05-14 stsp goto done;
2641 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2642 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2643 a367ff0f 2019-05-14 stsp if (error != NULL) {
2644 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2645 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2646 024e9686 2019-05-14 stsp goto done;
2648 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2649 a367ff0f 2019-05-14 stsp if (error)
2650 a367ff0f 2019-05-14 stsp goto done;
2653 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2654 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2655 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2656 507dc3bb 2018-12-29 stsp commit_id);
2657 507dc3bb 2018-12-29 stsp if (error)
2658 507dc3bb 2018-12-29 stsp goto done;
2661 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2662 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2663 507dc3bb 2018-12-29 stsp if (error != NULL)
2664 507dc3bb 2018-12-29 stsp goto done;
2666 784955db 2019-01-12 stsp if (did_something)
2667 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2669 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2671 c09a553d 2018-03-12 stsp free(worktree_path);
2672 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2673 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2674 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2675 507dc3bb 2018-12-29 stsp free(commit_id);
2676 9c4b8182 2019-01-02 stsp free(commit_id_str);
2677 c09a553d 2018-03-12 stsp return error;
2680 f42b1b34 2018-03-12 stsp static const struct got_error *
2681 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2682 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2683 63035f9f 2019-10-06 stsp struct got_repository *repo)
2685 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2686 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2688 44392932 2019-08-25 stsp if (blob_id1) {
2689 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2691 44392932 2019-08-25 stsp goto done;
2694 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2696 44392932 2019-08-25 stsp goto done;
2698 44392932 2019-08-25 stsp while (path[0] == '/')
2700 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2701 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2703 44392932 2019-08-25 stsp if (blob1)
2704 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2705 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2706 44392932 2019-08-25 stsp return err;
2709 44392932 2019-08-25 stsp static const struct got_error *
2710 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2711 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2712 63035f9f 2019-10-06 stsp struct got_repository *repo)
2714 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2715 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2716 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2718 44392932 2019-08-25 stsp if (tree_id1) {
2719 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2721 44392932 2019-08-25 stsp goto done;
2724 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2726 0f2b3dca 2018-12-22 stsp goto done;
2728 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2729 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2730 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2731 44392932 2019-08-25 stsp while (path[0] == '/')
2733 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2734 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2736 79109fed 2018-03-27 stsp if (tree1)
2737 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2738 366e0a5f 2019-10-10 stsp if (tree2)
2739 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2740 44392932 2019-08-25 stsp return err;
2743 44392932 2019-08-25 stsp static const struct got_error *
2744 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2745 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2747 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2748 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2749 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2750 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2751 44392932 2019-08-25 stsp struct got_object_qid *qid;
2753 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2754 44392932 2019-08-25 stsp if (qid != NULL) {
2755 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2758 44392932 2019-08-25 stsp return err;
2761 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2762 44392932 2019-08-25 stsp int obj_type;
2763 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2765 44392932 2019-08-25 stsp goto done;
2766 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2767 44392932 2019-08-25 stsp if (err) {
2768 44392932 2019-08-25 stsp free(obj_id2);
2769 44392932 2019-08-25 stsp goto done;
2771 44392932 2019-08-25 stsp if (pcommit) {
2772 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2773 44392932 2019-08-25 stsp qid->id, path);
2774 44392932 2019-08-25 stsp if (err) {
2775 44392932 2019-08-25 stsp free(obj_id2);
2776 44392932 2019-08-25 stsp goto done;
2778 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2779 44392932 2019-08-25 stsp if (err) {
2780 44392932 2019-08-25 stsp free(obj_id2);
2781 44392932 2019-08-25 stsp goto done;
2784 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2785 44392932 2019-08-25 stsp if (err) {
2786 44392932 2019-08-25 stsp free(obj_id2);
2787 44392932 2019-08-25 stsp goto done;
2789 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2790 44392932 2019-08-25 stsp switch (obj_type) {
2791 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2792 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2795 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2796 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2800 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2803 44392932 2019-08-25 stsp free(obj_id1);
2804 44392932 2019-08-25 stsp free(obj_id2);
2806 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2807 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2809 44392932 2019-08-25 stsp goto done;
2810 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2811 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2813 44392932 2019-08-25 stsp goto done;
2814 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2815 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2818 0f2b3dca 2018-12-22 stsp free(id_str1);
2819 0f2b3dca 2018-12-22 stsp free(id_str2);
2820 44392932 2019-08-25 stsp if (pcommit)
2821 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2822 79109fed 2018-03-27 stsp return err;
2825 4bb494d5 2018-06-16 stsp static char *
2826 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2828 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2829 09867e48 2019-08-13 stsp char *p, *s;
2831 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2832 09867e48 2019-08-13 stsp if (tm == NULL)
2833 09867e48 2019-08-13 stsp return NULL;
2834 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2835 09867e48 2019-08-13 stsp if (s == NULL)
2836 09867e48 2019-08-13 stsp return NULL;
2837 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2839 6c281f94 2018-06-11 stsp *p = '\0';
2843 6841bf13 2019-11-29 kn static const struct got_error *
2844 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2845 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2847 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2848 6841bf13 2019-11-29 kn regmatch_t regmatch;
2849 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2851 6841bf13 2019-11-29 kn *have_match = 0;
2853 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2857 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2861 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2862 6841bf13 2019-11-29 kn *have_match = 1;
2864 6841bf13 2019-11-29 kn free(id_str);
2865 6841bf13 2019-11-29 kn free(logmsg);
2869 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2871 79109fed 2018-03-27 stsp static const struct got_error *
2872 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2873 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2874 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2876 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2877 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2878 6c281f94 2018-06-11 stsp char datebuf[26];
2879 45d799e2 2018-12-23 stsp time_t committer_time;
2880 45d799e2 2018-12-23 stsp const char *author, *committer;
2881 199a4027 2019-02-02 stsp char *refs_str = NULL;
2882 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2884 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2886 199a4027 2019-02-02 stsp const char *name;
2887 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2890 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2891 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2893 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2894 199a4027 2019-02-02 stsp name += 5;
2895 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2897 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2898 e34f9ed6 2019-02-02 stsp name += 6;
2899 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2900 141c2bff 2019-02-04 stsp name += 8;
2901 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2902 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2903 5d844a1e 2019-08-13 stsp if (err) {
2904 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2905 5d844a1e 2019-08-13 stsp return err;
2906 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2907 5d844a1e 2019-08-13 stsp err = NULL;
2908 5d844a1e 2019-08-13 stsp tag = NULL;
2911 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2912 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2914 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2915 a436ad14 2019-08-13 stsp if (cmp != 0)
2917 199a4027 2019-02-02 stsp s = refs_str;
2918 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2919 199a4027 2019-02-02 stsp name) == -1) {
2920 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2922 34ca4898 2019-08-13 stsp return err;
2926 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2928 8bf5b3c9 2018-03-17 stsp return err;
2930 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2931 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2932 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2933 832c249c 2018-06-10 stsp free(id_str);
2934 d3d493d7 2019-02-21 stsp id_str = NULL;
2935 d3d493d7 2019-02-21 stsp free(refs_str);
2936 d3d493d7 2019-02-21 stsp refs_str = NULL;
2937 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2938 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2939 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2940 09867e48 2019-08-13 stsp if (datestr)
2941 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2942 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2943 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2944 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2945 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2946 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2947 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2948 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2949 3fe1abad 2018-06-10 stsp int n = 1;
2950 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2951 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2952 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2954 a0603db2 2018-06-10 stsp return err;
2955 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2956 a0603db2 2018-06-10 stsp free(id_str);
2960 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2962 5943eee2 2019-08-13 stsp return err;
2964 621015ac 2018-07-23 stsp logmsg = logmsg0;
2966 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2968 832c249c 2018-06-10 stsp printf(" %s\n", line);
2969 832c249c 2018-06-10 stsp } while (line);
2970 621015ac 2018-07-23 stsp free(logmsg0);
2972 971751ac 2018-03-27 stsp if (show_patch) {
2973 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2974 971751ac 2018-03-27 stsp if (err == 0)
2975 971751ac 2018-03-27 stsp printf("\n");
2978 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2979 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2980 07862c20 2018-09-15 stsp return err;
2983 f42b1b34 2018-03-12 stsp static const struct got_error *
2984 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2985 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2986 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2987 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2989 f42b1b34 2018-03-12 stsp const struct got_error *err;
2990 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2991 6841bf13 2019-11-29 kn regex_t regex;
2992 6841bf13 2019-11-29 kn int have_match;
2994 6841bf13 2019-11-29 kn if (search_pattern &&
2995 6841bf13 2019-11-29 kn regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2996 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2998 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3000 f42b1b34 2018-03-12 stsp return err;
3001 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3002 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3004 fcc85cad 2018-07-22 stsp goto done;
3006 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
3007 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3009 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3012 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3013 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3014 372ccdbb 2018-06-10 stsp if (err) {
3015 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3016 9ba79e04 2018-06-11 stsp err = NULL;
3019 b43fbaa0 2018-06-11 stsp if (id == NULL)
3022 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3026 6841bf13 2019-11-29 kn if (search_pattern) {
3027 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3029 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3032 6841bf13 2019-11-29 kn if (have_match == 0) {
3033 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3038 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
3039 44392932 2019-08-25 stsp diff_context, refs);
3040 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
3041 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
3045 6841bf13 2019-11-29 kn if (search_pattern)
3046 6841bf13 2019-11-29 kn regfree(®ex);
3047 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3048 f42b1b34 2018-03-12 stsp return err;
3051 4ed7e80c 2018-05-20 stsp __dead static void
3052 6f3d1eb0 2018-03-12 stsp usage_log(void)
3054 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
3055 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
3059 b1ebc001 2019-08-13 stsp static int
3060 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3062 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3063 b1ebc001 2019-08-13 stsp long long n;
3064 b1ebc001 2019-08-13 stsp const char *errstr;
3066 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3067 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3069 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3070 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3075 4ed7e80c 2018-05-20 stsp static const struct got_error *
3076 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3078 f42b1b34 2018-03-12 stsp const struct got_error *error;
3079 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3080 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3081 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
3082 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
3083 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3084 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
3085 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3086 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
3087 64a96a6d 2018-04-01 stsp const char *errstr;
3088 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3090 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3092 6715a751 2018-03-16 stsp #ifndef PROFILE
3093 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3096 f42b1b34 2018-03-12 stsp err(1, "pledge");
3099 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3101 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
3102 79109fed 2018-03-27 stsp switch (ch) {
3104 79109fed 2018-03-27 stsp show_patch = 1;
3107 d142fc45 2018-04-01 stsp start_commit = optarg;
3110 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3112 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3113 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3116 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3117 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3118 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3121 48c8c60d 2020-01-27 stsp log_branches = 1;
3124 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3125 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3126 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3128 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3131 6841bf13 2019-11-29 kn search_pattern = optarg;
3134 2deda0b9 2019-03-07 stsp usage_log();
3135 79109fed 2018-03-27 stsp /* NOTREACHED */
3139 79109fed 2018-03-27 stsp argc -= optind;
3140 79109fed 2018-03-27 stsp argv += optind;
3142 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3143 dc1edbfa 2019-11-29 kn diff_context = 3;
3144 dc1edbfa 2019-11-29 kn else if (!show_patch)
3145 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3147 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3148 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3149 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3150 04ca23f4 2018-07-16 stsp goto done;
3153 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3154 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3155 cffc0aa4 2019-02-05 stsp goto done;
3156 cffc0aa4 2019-02-05 stsp error = NULL;
3158 e7301579 2019-03-18 stsp if (argc == 0) {
3159 cbd1af7a 2019-03-18 stsp path = strdup("");
3160 e7301579 2019-03-18 stsp if (path == NULL) {
3161 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3162 cbd1af7a 2019-03-18 stsp goto done;
3164 e7301579 2019-03-18 stsp } else if (argc == 1) {
3165 e7301579 2019-03-18 stsp if (worktree) {
3166 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3168 e7301579 2019-03-18 stsp if (error)
3169 e7301579 2019-03-18 stsp goto done;
3171 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3172 e7301579 2019-03-18 stsp if (path == NULL) {
3173 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3174 e7301579 2019-03-18 stsp goto done;
3178 cbd1af7a 2019-03-18 stsp usage_log();
3180 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3181 5486daa2 2019-05-11 stsp repo_path = worktree ?
3182 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3184 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3185 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3186 cffc0aa4 2019-02-05 stsp goto done;
3189 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3190 d7d4f210 2018-03-12 stsp if (error != NULL)
3191 04ca23f4 2018-07-16 stsp goto done;
3193 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3194 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3195 c02c541e 2019-03-29 stsp if (error)
3196 c02c541e 2019-03-29 stsp goto done;
3198 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3199 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3200 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3201 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3202 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3203 3235492e 2018-04-01 stsp if (error != NULL)
3204 3235492e 2018-04-01 stsp return error;
3205 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
3206 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3207 3235492e 2018-04-01 stsp if (error != NULL)
3208 3235492e 2018-04-01 stsp return error;
3209 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3211 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
3212 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3213 3235492e 2018-04-01 stsp if (error == NULL) {
3214 1caad61b 2019-02-01 stsp int obj_type;
3215 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
3216 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
3217 d1f2edc9 2018-06-13 stsp if (error != NULL)
3218 1caad61b 2019-02-01 stsp goto done;
3219 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
3220 1caad61b 2019-02-01 stsp if (error != NULL)
3221 1caad61b 2019-02-01 stsp goto done;
3222 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3223 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
3224 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
3225 1caad61b 2019-02-01 stsp if (error != NULL)
3226 1caad61b 2019-02-01 stsp goto done;
3227 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
3228 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
3229 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3230 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3231 1caad61b 2019-02-01 stsp goto done;
3234 1caad61b 2019-02-01 stsp id = got_object_id_dup(
3235 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
3236 1caad61b 2019-02-01 stsp if (id == NULL)
3237 638f9024 2019-05-13 stsp error = got_error_from_errno(
3238 230a42bd 2019-05-11 jcs "got_object_id_dup");
3239 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3240 1caad61b 2019-02-01 stsp if (error)
3241 1caad61b 2019-02-01 stsp goto done;
3242 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3243 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3244 1caad61b 2019-02-01 stsp goto done;
3246 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3247 d1f2edc9 2018-06-13 stsp if (error != NULL)
3248 1caad61b 2019-02-01 stsp goto done;
3250 15a94983 2018-12-23 stsp if (commit == NULL) {
3251 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
3252 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
3253 d1f2edc9 2018-06-13 stsp if (error != NULL)
3254 d1f2edc9 2018-06-13 stsp return error;
3257 d7d4f210 2018-03-12 stsp if (error != NULL)
3258 04ca23f4 2018-07-16 stsp goto done;
3260 deeabeae 2019-08-27 stsp if (worktree) {
3261 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3263 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3264 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3265 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3266 deeabeae 2019-08-27 stsp goto done;
3268 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3271 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3272 04ca23f4 2018-07-16 stsp if (error != NULL)
3273 04ca23f4 2018-07-16 stsp goto done;
3274 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3275 04ca23f4 2018-07-16 stsp free(path);
3276 04ca23f4 2018-07-16 stsp path = in_repo_path;
3279 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3280 199a4027 2019-02-02 stsp if (error)
3281 199a4027 2019-02-02 stsp goto done;
3283 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
3284 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
3286 04ca23f4 2018-07-16 stsp free(path);
3287 04ca23f4 2018-07-16 stsp free(repo_path);
3288 04ca23f4 2018-07-16 stsp free(cwd);
3290 cffc0aa4 2019-02-05 stsp if (worktree)
3291 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3292 ad242220 2018-09-08 stsp if (repo) {
3293 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3294 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3295 ad242220 2018-09-08 stsp if (error == NULL)
3296 ad242220 2018-09-08 stsp error = repo_error;
3298 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3299 8bf5b3c9 2018-03-17 stsp return error;
3302 4ed7e80c 2018-05-20 stsp __dead static void
3303 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3305 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3306 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3310 b72f483a 2019-02-05 stsp struct print_diff_arg {
3311 b72f483a 2019-02-05 stsp struct got_repository *repo;
3312 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3313 b72f483a 2019-02-05 stsp int diff_context;
3314 3fc0c068 2019-02-10 stsp const char *id_str;
3315 3fc0c068 2019-02-10 stsp int header_shown;
3316 98eaaa12 2019-08-03 stsp int diff_staged;
3317 63035f9f 2019-10-06 stsp int ignore_whitespace;
3320 4ed7e80c 2018-05-20 stsp static const struct got_error *
3321 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3322 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3323 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3324 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3326 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3327 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3328 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3329 12463d8b 2019-12-13 stsp int fd = -1;
3330 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3331 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3332 b72f483a 2019-02-05 stsp struct stat sb;
3334 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3335 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3336 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3337 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3338 98eaaa12 2019-08-03 stsp return NULL;
3340 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3341 98eaaa12 2019-08-03 stsp return NULL;
3342 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3343 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3344 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3345 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3346 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3347 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3348 98eaaa12 2019-08-03 stsp return NULL;
3351 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3352 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3353 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3354 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3355 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3358 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3359 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3360 98eaaa12 2019-08-03 stsp switch (staged_status) {
3361 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3362 98eaaa12 2019-08-03 stsp label1 = path;
3363 98eaaa12 2019-08-03 stsp label2 = path;
3365 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3366 98eaaa12 2019-08-03 stsp label2 = path;
3368 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3369 98eaaa12 2019-08-03 stsp label1 = path;
3372 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3374 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3375 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3376 63035f9f 2019-10-06 stsp a->repo, stdout);
3379 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3380 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3381 4ce46740 2019-08-08 stsp char *id_str;
3382 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3385 4ce46740 2019-08-08 stsp goto done;
3386 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3388 4ce46740 2019-08-08 stsp goto done;
3389 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3390 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3391 4ce46740 2019-08-08 stsp free(id_str);
3392 4ce46740 2019-08-08 stsp goto done;
3394 4ce46740 2019-08-08 stsp free(id_str);
3395 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3396 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3398 4ce46740 2019-08-08 stsp goto done;
3401 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3402 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3403 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3404 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3405 2ec1f75b 2019-03-26 stsp goto done;
3408 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3409 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3410 12463d8b 2019-12-13 stsp if (fd == -1) {
3411 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3412 12463d8b 2019-12-13 stsp goto done;
3415 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3416 12463d8b 2019-12-13 stsp if (fd == -1) {
3417 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3418 12463d8b 2019-12-13 stsp goto done;
3421 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3422 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3423 2ec1f75b 2019-03-26 stsp goto done;
3425 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3426 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3427 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3428 2ec1f75b 2019-03-26 stsp goto done;
3432 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3434 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3435 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3437 b72f483a 2019-02-05 stsp if (blob1)
3438 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3439 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3440 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3441 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3442 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3443 b72f483a 2019-02-05 stsp free(abspath);
3444 927df6b7 2019-02-10 stsp return err;
3447 927df6b7 2019-02-10 stsp static const struct got_error *
3448 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3450 b00d56cd 2018-04-01 stsp const struct got_error *error;
3451 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3452 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3453 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3454 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3455 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3456 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3457 15a94983 2018-12-23 stsp int type1, type2;
3458 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3459 c0cc5c62 2018-10-18 stsp const char *errstr;
3460 927df6b7 2019-02-10 stsp char *path = NULL;
3462 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3463 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3464 25eccc22 2019-01-04 stsp NULL) == -1)
3465 b00d56cd 2018-04-01 stsp err(1, "pledge");
3468 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3469 b00d56cd 2018-04-01 stsp switch (ch) {
3471 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3473 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3474 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3477 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3478 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3479 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3481 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3484 98eaaa12 2019-08-03 stsp diff_staged = 1;
3487 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3490 2deda0b9 2019-03-07 stsp usage_diff();
3491 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3495 b00d56cd 2018-04-01 stsp argc -= optind;
3496 b00d56cd 2018-04-01 stsp argv += optind;
3498 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3499 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3500 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3501 b72f483a 2019-02-05 stsp goto done;
3503 927df6b7 2019-02-10 stsp if (argc <= 1) {
3504 b72f483a 2019-02-05 stsp if (repo_path)
3506 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3507 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3508 1f03b8da 2020-03-20 stsp if (error)
3509 1f03b8da 2020-03-20 stsp goto done;
3510 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3511 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3512 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3513 927df6b7 2019-02-10 stsp goto done;
3515 927df6b7 2019-02-10 stsp if (argc == 1) {
3516 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3518 927df6b7 2019-02-10 stsp if (error)
3519 927df6b7 2019-02-10 stsp goto done;
3521 927df6b7 2019-02-10 stsp path = strdup("");
3522 927df6b7 2019-02-10 stsp if (path == NULL) {
3523 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3524 927df6b7 2019-02-10 stsp goto done;
3527 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3528 98eaaa12 2019-08-03 stsp if (diff_staged)
3529 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3530 98eaaa12 2019-08-03 stsp "objects in repository");
3531 15a94983 2018-12-23 stsp id_str1 = argv[0];
3532 15a94983 2018-12-23 stsp id_str2 = argv[1];
3533 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3534 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3535 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3536 30db809c 2019-06-05 stsp goto done;
3537 1f03b8da 2020-03-20 stsp if (worktree) {
3538 1f03b8da 2020-03-20 stsp repo_path = strdup(
3539 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3540 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3541 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3542 1f03b8da 2020-03-20 stsp goto done;
3545 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3546 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3547 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3548 1f03b8da 2020-03-20 stsp goto done;
3553 b00d56cd 2018-04-01 stsp usage_diff();
3555 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3556 76089277 2018-04-01 stsp free(repo_path);
3557 b00d56cd 2018-04-01 stsp if (error != NULL)
3558 b00d56cd 2018-04-01 stsp goto done;
3560 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3561 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3562 c02c541e 2019-03-29 stsp if (error)
3563 c02c541e 2019-03-29 stsp goto done;
3565 30db809c 2019-06-05 stsp if (argc <= 1) {
3566 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3567 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3568 b72f483a 2019-02-05 stsp char *id_str;
3570 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3572 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3573 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3574 b72f483a 2019-02-05 stsp if (error)
3575 b72f483a 2019-02-05 stsp goto done;
3576 b72f483a 2019-02-05 stsp arg.repo = repo;
3577 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3578 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3579 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3580 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3581 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3582 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3584 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3585 72ea6654 2019-07-27 stsp if (error)
3586 72ea6654 2019-07-27 stsp goto done;
3588 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3589 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3590 b72f483a 2019-02-05 stsp free(id_str);
3591 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3592 b72f483a 2019-02-05 stsp goto done;
3595 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3596 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3597 0adb7196 2019-08-11 stsp if (error)
3598 0adb7196 2019-08-11 stsp goto done;
3600 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3601 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3602 0adb7196 2019-08-11 stsp if (error)
3603 0adb7196 2019-08-11 stsp goto done;
3605 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3606 15a94983 2018-12-23 stsp if (error)
3607 15a94983 2018-12-23 stsp goto done;
3609 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3610 15a94983 2018-12-23 stsp if (error)
3611 15a94983 2018-12-23 stsp goto done;
3613 15a94983 2018-12-23 stsp if (type1 != type2) {
3614 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3615 b00d56cd 2018-04-01 stsp goto done;
3618 15a94983 2018-12-23 stsp switch (type1) {
3619 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3620 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3621 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3623 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3624 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3625 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3627 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3628 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3629 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3630 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3633 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3636 5e70831e 2019-05-28 stsp free(label1);
3637 5e70831e 2019-05-28 stsp free(label2);
3638 15a94983 2018-12-23 stsp free(id1);
3639 15a94983 2018-12-23 stsp free(id2);
3640 927df6b7 2019-02-10 stsp free(path);
3641 b72f483a 2019-02-05 stsp if (worktree)
3642 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3643 ad242220 2018-09-08 stsp if (repo) {
3644 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3645 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3646 ad242220 2018-09-08 stsp if (error == NULL)
3647 ad242220 2018-09-08 stsp error = repo_error;
3649 b00d56cd 2018-04-01 stsp return error;
3652 404c43c4 2018-06-21 stsp __dead static void
3653 404c43c4 2018-06-21 stsp usage_blame(void)
3655 9270e621 2019-02-05 stsp fprintf(stderr,
3656 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3657 404c43c4 2018-06-21 stsp getprogname());
3661 28315671 2019-08-14 stsp struct blame_line {
3662 28315671 2019-08-14 stsp int annotated;
3663 28315671 2019-08-14 stsp char *id_str;
3664 82f6abb8 2019-08-14 stsp char *committer;
3665 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3668 28315671 2019-08-14 stsp struct blame_cb_args {
3669 28315671 2019-08-14 stsp struct blame_line *lines;
3670 28315671 2019-08-14 stsp int nlines;
3671 7ef28ff8 2019-08-14 stsp int nlines_prec;
3672 28315671 2019-08-14 stsp int lineno_cur;
3673 28315671 2019-08-14 stsp off_t *line_offsets;
3675 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3678 404c43c4 2018-06-21 stsp static const struct got_error *
3679 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3681 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3682 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3683 28315671 2019-08-14 stsp struct blame_line *bline;
3684 82f6abb8 2019-08-14 stsp char *line = NULL;
3685 28315671 2019-08-14 stsp size_t linesize = 0;
3686 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3687 28315671 2019-08-14 stsp off_t offset;
3688 bcb49d15 2019-08-14 stsp struct tm tm;
3689 bcb49d15 2019-08-14 stsp time_t committer_time;
3691 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3692 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3693 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3695 28315671 2019-08-14 stsp if (sigint_received)
3696 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3698 2839f8b9 2019-08-18 stsp if (lineno == -1)
3699 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3701 28315671 2019-08-14 stsp /* Annotate this line. */
3702 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3703 28315671 2019-08-14 stsp if (bline->annotated)
3704 28315671 2019-08-14 stsp return NULL;
3705 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3707 28315671 2019-08-14 stsp return err;
3709 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3711 82f6abb8 2019-08-14 stsp goto done;
3713 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3714 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3715 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3716 bcb49d15 2019-08-14 stsp goto done;
3719 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3720 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3721 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3722 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3723 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3724 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3725 82f6abb8 2019-08-14 stsp goto done;
3727 28315671 2019-08-14 stsp bline->annotated = 1;
3729 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3730 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3731 28315671 2019-08-14 stsp if (!bline->annotated)
3732 82f6abb8 2019-08-14 stsp goto done;
3734 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3735 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3736 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3737 82f6abb8 2019-08-14 stsp goto done;
3740 28315671 2019-08-14 stsp while (bline->annotated) {
3741 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3742 82f6abb8 2019-08-14 stsp size_t len;
3744 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3745 28315671 2019-08-14 stsp if (ferror(a->f))
3746 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3750 82f6abb8 2019-08-14 stsp committer = bline->committer;
3751 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3752 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3753 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3754 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3756 82f6abb8 2019-08-14 stsp *at = '\0';
3757 82f6abb8 2019-08-14 stsp len = strlen(committer);
3758 82f6abb8 2019-08-14 stsp if (len >= 9)
3759 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3761 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3763 28315671 2019-08-14 stsp *nl = '\0';
3764 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3765 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3767 28315671 2019-08-14 stsp a->lineno_cur++;
3768 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3771 82f6abb8 2019-08-14 stsp if (commit)
3772 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3773 28315671 2019-08-14 stsp free(line);
3774 28315671 2019-08-14 stsp return err;
3777 28315671 2019-08-14 stsp static const struct got_error *
3778 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3780 404c43c4 2018-06-21 stsp const struct got_error *error;
3781 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3782 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3783 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3784 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3785 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3786 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3787 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3788 28315671 2019-08-14 stsp struct blame_cb_args bca;
3789 28315671 2019-08-14 stsp int ch, obj_type, i;
3790 b02560ec 2019-08-19 stsp size_t filesize;
3792 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3794 404c43c4 2018-06-21 stsp #ifndef PROFILE
3795 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3796 36e2fb66 2019-01-04 stsp NULL) == -1)
3797 404c43c4 2018-06-21 stsp err(1, "pledge");
3800 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3801 404c43c4 2018-06-21 stsp switch (ch) {
3803 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3806 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3807 66bea077 2018-08-02 stsp if (repo_path == NULL)
3808 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3810 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3813 2deda0b9 2019-03-07 stsp usage_blame();
3814 404c43c4 2018-06-21 stsp /* NOTREACHED */
3818 404c43c4 2018-06-21 stsp argc -= optind;
3819 404c43c4 2018-06-21 stsp argv += optind;
3821 a39318fd 2018-08-02 stsp if (argc == 1)
3822 404c43c4 2018-06-21 stsp path = argv[0];
3824 404c43c4 2018-06-21 stsp usage_blame();
3826 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3827 66bea077 2018-08-02 stsp if (cwd == NULL) {
3828 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3829 66bea077 2018-08-02 stsp goto done;
3831 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3832 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3833 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3834 66bea077 2018-08-02 stsp goto done;
3836 0c06baac 2019-02-05 stsp error = NULL;
3837 0c06baac 2019-02-05 stsp if (worktree) {
3838 0c06baac 2019-02-05 stsp repo_path =
3839 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3840 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3841 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3842 1f03b8da 2020-03-20 stsp if (error)
3843 1f03b8da 2020-03-20 stsp goto done;
3846 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3847 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3848 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3849 0c06baac 2019-02-05 stsp goto done;
3854 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3855 c02c541e 2019-03-29 stsp if (error != NULL)
3856 36e2fb66 2019-01-04 stsp goto done;
3858 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3859 c02c541e 2019-03-29 stsp if (error)
3860 404c43c4 2018-06-21 stsp goto done;
3862 0c06baac 2019-02-05 stsp if (worktree) {
3863 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3864 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3865 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3866 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3867 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3868 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3869 6efaaa2d 2019-02-05 stsp path) == -1) {
3870 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3871 0c06baac 2019-02-05 stsp goto done;
3873 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3876 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3878 0c06baac 2019-02-05 stsp if (error)
3879 66bea077 2018-08-02 stsp goto done;
3881 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3882 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3883 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3884 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3885 404c43c4 2018-06-21 stsp if (error != NULL)
3886 66bea077 2018-08-02 stsp goto done;
3887 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3888 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3889 404c43c4 2018-06-21 stsp if (error != NULL)
3890 66bea077 2018-08-02 stsp goto done;
3892 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3893 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3894 30837e32 2019-07-25 stsp if (error)
3895 66bea077 2018-08-02 stsp goto done;
3898 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3899 28315671 2019-08-14 stsp if (error)
3900 28315671 2019-08-14 stsp goto done;
3902 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3903 28315671 2019-08-14 stsp if (error)
3904 28315671 2019-08-14 stsp goto done;
3906 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3907 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3908 28315671 2019-08-14 stsp goto done;
3911 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3912 28315671 2019-08-14 stsp if (error)
3913 28315671 2019-08-14 stsp goto done;
3914 28315671 2019-08-14 stsp bca.f = got_opentemp();
3915 28315671 2019-08-14 stsp if (bca.f == NULL) {
3916 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3917 28315671 2019-08-14 stsp goto done;
3919 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3920 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3921 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3922 28315671 2019-08-14 stsp goto done;
3924 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3925 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3926 b02560ec 2019-08-19 stsp bca.nlines--;
3928 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3929 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3930 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3931 28315671 2019-08-14 stsp goto done;
3933 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3934 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3935 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3936 7ef28ff8 2019-08-14 stsp while (i > 0) {
3938 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3940 82f6abb8 2019-08-14 stsp bca.repo = repo;
3942 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3943 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3945 66bea077 2018-08-02 stsp free(in_repo_path);
3946 66bea077 2018-08-02 stsp free(repo_path);
3947 66bea077 2018-08-02 stsp free(cwd);
3948 404c43c4 2018-06-21 stsp free(commit_id);
3949 28315671 2019-08-14 stsp free(obj_id);
3951 28315671 2019-08-14 stsp got_object_blob_close(blob);
3952 0c06baac 2019-02-05 stsp if (worktree)
3953 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3954 ad242220 2018-09-08 stsp if (repo) {
3955 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3956 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3957 ad242220 2018-09-08 stsp if (error == NULL)
3958 ad242220 2018-09-08 stsp error = repo_error;
3960 3affba96 2019-09-22 stsp if (bca.lines) {
3961 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3962 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3963 3affba96 2019-09-22 stsp free(bline->id_str);
3964 3affba96 2019-09-22 stsp free(bline->committer);
3966 3affba96 2019-09-22 stsp free(bca.lines);
3968 28315671 2019-08-14 stsp free(bca.line_offsets);
3969 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3970 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3971 404c43c4 2018-06-21 stsp return error;
3974 5de5890b 2018-10-18 stsp __dead static void
3975 5de5890b 2018-10-18 stsp usage_tree(void)
3977 c1669e2e 2019-01-09 stsp fprintf(stderr,
3978 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3979 5de5890b 2018-10-18 stsp getprogname());
3983 c1669e2e 2019-01-09 stsp static void
3984 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3985 c1669e2e 2019-01-09 stsp const char *root_path)
3987 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3988 848d6979 2019-08-12 stsp const char *modestr = "";
3989 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3991 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3992 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3995 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3996 63c5ca5d 2019-08-24 stsp modestr = "$";
3997 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3998 848d6979 2019-08-12 stsp modestr = "@";
3999 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4000 848d6979 2019-08-12 stsp modestr = "/";
4001 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4002 848d6979 2019-08-12 stsp modestr = "*";
4004 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
4005 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
4008 5de5890b 2018-10-18 stsp static const struct got_error *
4009 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4010 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4011 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4013 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4014 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4015 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4016 56e0773d 2019-11-28 stsp int nentries, i;
4018 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4020 5de5890b 2018-10-18 stsp goto done;
4022 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4024 5de5890b 2018-10-18 stsp goto done;
4025 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4026 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4027 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4028 5de5890b 2018-10-18 stsp char *id = NULL;
4030 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4033 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4034 5de5890b 2018-10-18 stsp if (show_ids) {
4035 5de5890b 2018-10-18 stsp char *id_str;
4036 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4037 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4039 5de5890b 2018-10-18 stsp goto done;
4040 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4041 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4042 5de5890b 2018-10-18 stsp free(id_str);
4043 5de5890b 2018-10-18 stsp goto done;
4045 5de5890b 2018-10-18 stsp free(id_str);
4047 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
4050 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4051 c1669e2e 2019-01-09 stsp char *child_path;
4052 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4053 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4054 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4055 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4056 c1669e2e 2019-01-09 stsp goto done;
4058 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4059 c1669e2e 2019-01-09 stsp root_path, repo);
4060 c1669e2e 2019-01-09 stsp free(child_path);
4062 c1669e2e 2019-01-09 stsp goto done;
4067 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4068 5de5890b 2018-10-18 stsp free(tree_id);
4069 5de5890b 2018-10-18 stsp return err;
4072 5de5890b 2018-10-18 stsp static const struct got_error *
4073 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4075 5de5890b 2018-10-18 stsp const struct got_error *error;
4076 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4077 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4078 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4079 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4080 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4081 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4082 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4085 5de5890b 2018-10-18 stsp #ifndef PROFILE
4086 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4087 0f8d269b 2019-01-04 stsp NULL) == -1)
4088 5de5890b 2018-10-18 stsp err(1, "pledge");
4091 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4092 5de5890b 2018-10-18 stsp switch (ch) {
4094 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4097 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4098 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4099 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4101 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4104 5de5890b 2018-10-18 stsp show_ids = 1;
4107 c1669e2e 2019-01-09 stsp recurse = 1;
4110 2deda0b9 2019-03-07 stsp usage_tree();
4111 5de5890b 2018-10-18 stsp /* NOTREACHED */
4115 5de5890b 2018-10-18 stsp argc -= optind;
4116 5de5890b 2018-10-18 stsp argv += optind;
4118 5de5890b 2018-10-18 stsp if (argc == 1)
4119 5de5890b 2018-10-18 stsp path = argv[0];
4120 5de5890b 2018-10-18 stsp else if (argc > 1)
4121 5de5890b 2018-10-18 stsp usage_tree();
4123 7a2c19d6 2019-02-05 stsp path = NULL;
4125 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4126 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4127 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4128 9bf7a39b 2019-02-05 stsp goto done;
4130 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4131 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4132 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4133 7a2c19d6 2019-02-05 stsp goto done;
4135 7a2c19d6 2019-02-05 stsp error = NULL;
4136 7a2c19d6 2019-02-05 stsp if (worktree) {
4137 7a2c19d6 2019-02-05 stsp repo_path =
4138 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4139 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4140 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4141 7a2c19d6 2019-02-05 stsp if (error)
4142 7a2c19d6 2019-02-05 stsp goto done;
4144 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4145 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4146 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4147 7a2c19d6 2019-02-05 stsp goto done;
4152 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4153 5de5890b 2018-10-18 stsp if (error != NULL)
4154 c02c541e 2019-03-29 stsp goto done;
4156 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4157 c02c541e 2019-03-29 stsp if (error)
4158 5de5890b 2018-10-18 stsp goto done;
4160 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4161 9bf7a39b 2019-02-05 stsp if (worktree) {
4162 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4163 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4164 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4165 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4166 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4167 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4168 9bf7a39b 2019-02-05 stsp goto done;
4170 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4172 9bf7a39b 2019-02-05 stsp if (error)
4173 9bf7a39b 2019-02-05 stsp goto done;
4175 9bf7a39b 2019-02-05 stsp path = "/";
4177 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4178 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4179 9bf7a39b 2019-02-05 stsp if (error != NULL)
4180 9bf7a39b 2019-02-05 stsp goto done;
4183 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4184 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4185 4e0a20a4 2020-03-23 tracey if (worktree)
4186 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4188 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4189 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4190 5de5890b 2018-10-18 stsp if (error != NULL)
4191 5de5890b 2018-10-18 stsp goto done;
4192 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4193 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4194 5de5890b 2018-10-18 stsp if (error != NULL)
4195 5de5890b 2018-10-18 stsp goto done;
4197 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4198 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4199 30837e32 2019-07-25 stsp if (error)
4200 5de5890b 2018-10-18 stsp goto done;
4203 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4204 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4206 5de5890b 2018-10-18 stsp free(in_repo_path);
4207 5de5890b 2018-10-18 stsp free(repo_path);
4208 5de5890b 2018-10-18 stsp free(cwd);
4209 5de5890b 2018-10-18 stsp free(commit_id);
4210 7a2c19d6 2019-02-05 stsp if (worktree)
4211 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4212 5de5890b 2018-10-18 stsp if (repo) {
4213 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4214 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4215 5de5890b 2018-10-18 stsp if (error == NULL)
4216 5de5890b 2018-10-18 stsp error = repo_error;
4218 5de5890b 2018-10-18 stsp return error;
4221 6bad629b 2019-02-04 stsp __dead static void
4222 6bad629b 2019-02-04 stsp usage_status(void)
4224 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4228 b72f483a 2019-02-05 stsp static const struct got_error *
4229 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4230 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4231 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4232 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4234 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4235 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4236 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4237 b72f483a 2019-02-05 stsp return NULL;
4240 6bad629b 2019-02-04 stsp static const struct got_error *
4241 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4243 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4244 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4245 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4246 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
4247 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4248 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4251 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4253 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4254 6bad629b 2019-02-04 stsp switch (ch) {
4256 2deda0b9 2019-03-07 stsp usage_status();
4257 6bad629b 2019-02-04 stsp /* NOTREACHED */
4261 6bad629b 2019-02-04 stsp argc -= optind;
4262 6bad629b 2019-02-04 stsp argv += optind;
4264 6bad629b 2019-02-04 stsp #ifndef PROFILE
4265 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4266 6bad629b 2019-02-04 stsp NULL) == -1)
4267 6bad629b 2019-02-04 stsp err(1, "pledge");
4269 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4270 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4271 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4272 927df6b7 2019-02-10 stsp goto done;
4275 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4276 927df6b7 2019-02-10 stsp if (error != NULL)
4277 a5edda0a 2019-07-27 stsp goto done;
4279 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4281 6bad629b 2019-02-04 stsp if (error != NULL)
4282 6bad629b 2019-02-04 stsp goto done;
4284 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4285 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4286 087fb88c 2019-08-04 stsp if (error)
4287 087fb88c 2019-08-04 stsp goto done;
4289 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4290 6bad629b 2019-02-04 stsp if (error)
4291 6bad629b 2019-02-04 stsp goto done;
4293 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4294 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4296 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4297 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4298 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4299 927df6b7 2019-02-10 stsp free(cwd);
4300 d0eebce4 2019-03-11 stsp return error;
4303 d0eebce4 2019-03-11 stsp __dead static void
4304 d0eebce4 2019-03-11 stsp usage_ref(void)
4306 d0eebce4 2019-03-11 stsp fprintf(stderr,
4307 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4308 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4309 d0eebce4 2019-03-11 stsp getprogname());
4313 d0eebce4 2019-03-11 stsp static const struct got_error *
4314 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4316 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4317 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4318 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4320 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4321 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4323 d0eebce4 2019-03-11 stsp return err;
4325 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4326 d0eebce4 2019-03-11 stsp char *refstr;
4327 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4328 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4329 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4330 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4331 d0eebce4 2019-03-11 stsp free(refstr);
4334 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4335 d0eebce4 2019-03-11 stsp return NULL;
4338 d0eebce4 2019-03-11 stsp static const struct got_error *
4339 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4341 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4342 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4344 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4346 d0eebce4 2019-03-11 stsp return err;
4348 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4349 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4350 d0eebce4 2019-03-11 stsp return err;
4353 d0eebce4 2019-03-11 stsp static const struct got_error *
4354 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4356 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4357 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4358 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4361 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4362 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4363 d1644381 2019-07-14 stsp * an unintended typo.
4365 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4366 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4368 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4370 d83d9d5c 2019-05-13 stsp if (err) {
4371 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4373 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4374 d83d9d5c 2019-05-13 stsp return err;
4375 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4377 d83d9d5c 2019-05-13 stsp return err;
4378 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4379 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4381 d83d9d5c 2019-05-13 stsp return err;
4384 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4386 d0eebce4 2019-03-11 stsp goto done;
4388 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4391 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4393 d1c1ae5f 2019-08-12 stsp return err;
4396 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4397 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4399 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4400 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4401 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4404 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4405 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4406 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4408 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4409 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4411 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4413 d1c1ae5f 2019-08-12 stsp return err;
4415 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4417 d1c1ae5f 2019-08-12 stsp goto done;
4419 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4421 d1c1ae5f 2019-08-12 stsp if (target_ref)
4422 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4424 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4425 d0eebce4 2019-03-11 stsp return err;
4428 d0eebce4 2019-03-11 stsp static const struct got_error *
4429 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4431 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4432 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4433 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4434 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4435 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4436 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4437 b2070a3f 2020-03-22 stsp char *refname = NULL;
4439 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4440 d0eebce4 2019-03-11 stsp switch (ch) {
4442 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4445 e31abbf2 2020-03-22 stsp do_delete = 1;
4448 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4449 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4450 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4452 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4455 d0eebce4 2019-03-11 stsp do_list = 1;
4458 e31abbf2 2020-03-22 stsp symref_target = optarg;
4461 d0eebce4 2019-03-11 stsp usage_ref();
4462 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4466 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
4467 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
4468 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
4469 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
4470 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
4471 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
4472 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
4473 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
4474 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
4475 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
4476 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
4477 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
4479 d0eebce4 2019-03-11 stsp argc -= optind;
4480 d0eebce4 2019-03-11 stsp argv += optind;
4482 e31abbf2 2020-03-22 stsp if (do_list) {
4483 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
4484 d0eebce4 2019-03-11 stsp usage_ref();
4485 b2070a3f 2020-03-22 stsp if (argc == 1) {
4486 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4487 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4488 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4489 b2070a3f 2020-03-22 stsp goto done;
4493 e31abbf2 2020-03-22 stsp if (argc != 1)
4494 e31abbf2 2020-03-22 stsp usage_ref();
4495 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4496 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4497 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4498 b2070a3f 2020-03-22 stsp goto done;
4502 b2070a3f 2020-03-22 stsp if (refname)
4503 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
4505 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4506 e0b57350 2019-03-12 stsp if (do_list) {
4507 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4508 e0b57350 2019-03-12 stsp NULL) == -1)
4509 e0b57350 2019-03-12 stsp err(1, "pledge");
4511 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4512 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4513 e0b57350 2019-03-12 stsp err(1, "pledge");
4516 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4517 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4518 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4519 d0eebce4 2019-03-11 stsp goto done;
4522 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4523 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4524 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4525 d0eebce4 2019-03-11 stsp goto done;
4527 d0eebce4 2019-03-11 stsp error = NULL;
4528 d0eebce4 2019-03-11 stsp if (worktree) {
4529 d0eebce4 2019-03-11 stsp repo_path =
4530 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4531 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4532 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4533 d0eebce4 2019-03-11 stsp if (error)
4534 d0eebce4 2019-03-11 stsp goto done;
4536 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4537 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4538 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4539 d0eebce4 2019-03-11 stsp goto done;
4544 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4545 d0eebce4 2019-03-11 stsp if (error != NULL)
4546 d0eebce4 2019-03-11 stsp goto done;
4548 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4549 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4550 c02c541e 2019-03-29 stsp if (error)
4551 c02c541e 2019-03-29 stsp goto done;
4553 d0eebce4 2019-03-11 stsp if (do_list)
4554 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
4555 e31abbf2 2020-03-22 stsp else if (do_delete)
4556 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
4557 e31abbf2 2020-03-22 stsp else if (symref_target)
4558 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
4560 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
4561 e31abbf2 2020-03-22 stsp usage_ref();
4562 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
4565 b2070a3f 2020-03-22 stsp free(refname);
4567 4e759de4 2019-06-26 stsp got_repo_close(repo);
4568 4e759de4 2019-06-26 stsp if (worktree)
4569 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4570 4e759de4 2019-06-26 stsp free(cwd);
4571 4e759de4 2019-06-26 stsp free(repo_path);
4572 4e759de4 2019-06-26 stsp return error;
4575 4e759de4 2019-06-26 stsp __dead static void
4576 4e759de4 2019-06-26 stsp usage_branch(void)
4578 4e759de4 2019-06-26 stsp fprintf(stderr,
4579 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4580 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4584 4e759de4 2019-06-26 stsp static const struct got_error *
4585 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4586 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4588 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4589 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4590 4e99b47e 2019-10-04 stsp char *refstr;
4592 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4593 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4594 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4595 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4597 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4599 4e99b47e 2019-10-04 stsp return err;
4600 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4601 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4602 4e99b47e 2019-10-04 stsp marker = "* ";
4604 4e99b47e 2019-10-04 stsp marker = "~ ";
4608 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4609 4e99b47e 2019-10-04 stsp refname += 11;
4610 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4611 4e99b47e 2019-10-04 stsp refname += 18;
4613 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4614 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4615 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4617 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4618 4e99b47e 2019-10-04 stsp free(refstr);
4619 4e99b47e 2019-10-04 stsp return NULL;
4622 4e99b47e 2019-10-04 stsp static const struct got_error *
4623 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4625 ad89fa31 2019-10-04 stsp const char *refname;
4627 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4628 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4630 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4632 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4633 ad89fa31 2019-10-04 stsp refname += 11;
4634 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4635 ad89fa31 2019-10-04 stsp refname += 18;
4637 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4639 ad89fa31 2019-10-04 stsp return NULL;
4642 ad89fa31 2019-10-04 stsp static const struct got_error *
4643 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4645 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4646 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4647 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4648 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4649 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4651 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4653 4e99b47e 2019-10-04 stsp if (worktree) {
4654 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4655 4e99b47e 2019-10-04 stsp worktree);
4657 4e99b47e 2019-10-04 stsp return err;
4659 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4660 4e99b47e 2019-10-04 stsp worktree);
4662 4e99b47e 2019-10-04 stsp return err;
4664 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4665 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4666 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4668 4e99b47e 2019-10-04 stsp return err;
4669 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4670 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4674 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4675 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4677 4e759de4 2019-06-26 stsp return err;
4679 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4680 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4682 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4683 4e759de4 2019-06-26 stsp return NULL;
4686 4e759de4 2019-06-26 stsp static const struct got_error *
4687 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4688 45cd4e47 2019-08-25 stsp const char *branch_name)
4690 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4691 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4692 4e759de4 2019-06-26 stsp char *refname;
4694 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4695 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4697 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4699 4e759de4 2019-06-26 stsp goto done;
4701 45cd4e47 2019-08-25 stsp if (worktree &&
4702 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4703 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4704 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4705 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4706 45cd4e47 2019-08-25 stsp goto done;
4709 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4712 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4713 4e759de4 2019-06-26 stsp free(refname);
4714 4e759de4 2019-06-26 stsp return err;
4717 4e759de4 2019-06-26 stsp static const struct got_error *
4718 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4719 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4721 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4722 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4723 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4726 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4727 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4728 d3f84d51 2019-07-11 stsp * an unintended typo.
4730 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4731 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4733 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4734 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4735 4e759de4 2019-06-26 stsp goto done;
4738 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4739 4e759de4 2019-06-26 stsp if (err == NULL) {
4740 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4741 4e759de4 2019-06-26 stsp goto done;
4742 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4743 4e759de4 2019-06-26 stsp goto done;
4745 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4747 4e759de4 2019-06-26 stsp goto done;
4749 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4752 4e759de4 2019-06-26 stsp got_ref_close(ref);
4753 4e759de4 2019-06-26 stsp free(base_refname);
4754 4e759de4 2019-06-26 stsp free(refname);
4755 4e759de4 2019-06-26 stsp return err;
4758 4e759de4 2019-06-26 stsp static const struct got_error *
4759 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4761 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4762 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4763 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4764 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4765 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4766 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4767 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4768 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4769 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4770 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4771 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4773 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4775 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4776 4e759de4 2019-06-26 stsp switch (ch) {
4778 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4781 4e759de4 2019-06-26 stsp delref = optarg;
4784 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4785 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4786 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4788 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4791 4e759de4 2019-06-26 stsp do_list = 1;
4794 da76fce2 2020-02-24 stsp do_update = 0;
4797 4e759de4 2019-06-26 stsp usage_branch();
4798 4e759de4 2019-06-26 stsp /* NOTREACHED */
4802 4e759de4 2019-06-26 stsp if (do_list && delref)
4803 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
4805 4e759de4 2019-06-26 stsp argc -= optind;
4806 4e759de4 2019-06-26 stsp argv += optind;
4808 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4809 ad89fa31 2019-10-04 stsp do_show = 1;
4811 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4812 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4814 4e759de4 2019-06-26 stsp if (do_list || delref) {
4815 4e759de4 2019-06-26 stsp if (argc > 0)
4816 4e759de4 2019-06-26 stsp usage_branch();
4817 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4818 4e759de4 2019-06-26 stsp usage_branch();
4820 4e759de4 2019-06-26 stsp #ifndef PROFILE
4821 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4822 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4823 4e759de4 2019-06-26 stsp NULL) == -1)
4824 4e759de4 2019-06-26 stsp err(1, "pledge");
4826 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4827 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4828 4e759de4 2019-06-26 stsp err(1, "pledge");
4831 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4832 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4833 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4834 4e759de4 2019-06-26 stsp goto done;
4837 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4838 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4839 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4840 4e759de4 2019-06-26 stsp goto done;
4842 4e759de4 2019-06-26 stsp error = NULL;
4843 4e759de4 2019-06-26 stsp if (worktree) {
4844 4e759de4 2019-06-26 stsp repo_path =
4845 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4846 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4847 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4848 4e759de4 2019-06-26 stsp if (error)
4849 4e759de4 2019-06-26 stsp goto done;
4851 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4852 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4853 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4854 4e759de4 2019-06-26 stsp goto done;
4859 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4860 4e759de4 2019-06-26 stsp if (error != NULL)
4861 4e759de4 2019-06-26 stsp goto done;
4863 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4864 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4865 4e759de4 2019-06-26 stsp if (error)
4866 4e759de4 2019-06-26 stsp goto done;
4868 ad89fa31 2019-10-04 stsp if (do_show)
4869 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4870 ad89fa31 2019-10-04 stsp else if (do_list)
4871 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4872 4e759de4 2019-06-26 stsp else if (delref)
4873 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4875 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4876 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4877 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4878 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4879 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4880 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4881 a74f7e83 2019-11-10 stsp if (error)
4882 a74f7e83 2019-11-10 stsp goto done;
4883 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4884 da76fce2 2020-02-24 stsp if (error)
4885 da76fce2 2020-02-24 stsp goto done;
4886 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4887 da76fce2 2020-02-24 stsp int did_something = 0;
4888 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4890 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4891 da76fce2 2020-02-24 stsp if (error)
4892 da76fce2 2020-02-24 stsp goto done;
4893 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4894 da76fce2 2020-02-24 stsp worktree);
4895 da76fce2 2020-02-24 stsp if (error)
4896 da76fce2 2020-02-24 stsp goto done;
4897 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4899 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4900 da76fce2 2020-02-24 stsp goto done;
4902 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4903 da76fce2 2020-02-24 stsp free(branch_refname);
4904 da76fce2 2020-02-24 stsp if (error)
4905 da76fce2 2020-02-24 stsp goto done;
4906 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4908 da76fce2 2020-02-24 stsp if (error)
4909 da76fce2 2020-02-24 stsp goto done;
4910 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4911 da76fce2 2020-02-24 stsp commit_id);
4912 da76fce2 2020-02-24 stsp if (error)
4913 da76fce2 2020-02-24 stsp goto done;
4914 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4915 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4916 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4917 da76fce2 2020-02-24 stsp if (error)
4918 da76fce2 2020-02-24 stsp goto done;
4919 da76fce2 2020-02-24 stsp if (did_something)
4920 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4925 da76fce2 2020-02-24 stsp got_ref_close(ref);
4927 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4928 d0eebce4 2019-03-11 stsp if (worktree)
4929 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4930 d0eebce4 2019-03-11 stsp free(cwd);
4931 d0eebce4 2019-03-11 stsp free(repo_path);
4932 da76fce2 2020-02-24 stsp free(commit_id);
4933 da76fce2 2020-02-24 stsp free(commit_id_str);
4934 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4935 da76fce2 2020-02-24 stsp free((char *)pe->path);
4936 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4937 d00136be 2019-03-26 stsp return error;
4941 d00136be 2019-03-26 stsp __dead static void
4942 8e7bd50a 2019-08-22 stsp usage_tag(void)
4944 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4945 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4946 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4951 b8bad2ba 2019-08-23 stsp static const struct got_error *
4952 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4954 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4955 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4956 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4957 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4958 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4960 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4961 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4962 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4963 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4965 b8bad2ba 2019-08-23 stsp return err;
4966 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4969 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4972 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4973 b8bad2ba 2019-08-23 stsp free(re_id);
4976 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4977 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4980 b8bad2ba 2019-08-23 stsp while (se) {
4981 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4984 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4985 b8bad2ba 2019-08-23 stsp free(se_id);
4988 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4989 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4991 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4992 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4994 b8bad2ba 2019-08-23 stsp return err;
4995 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4998 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5003 b8bad2ba 2019-08-23 stsp return err;
5007 b8bad2ba 2019-08-23 stsp static const struct got_error *
5008 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5010 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5011 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5012 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5014 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5016 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5018 8e7bd50a 2019-08-22 stsp return err;
5020 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5021 8e7bd50a 2019-08-22 stsp const char *refname;
5022 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5023 8e7bd50a 2019-08-22 stsp char datebuf[26];
5024 d4efa91b 2020-01-14 stsp const char *tagger;
5025 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5026 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5027 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5028 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5030 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5031 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5033 8e7bd50a 2019-08-22 stsp refname += 10;
5034 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5035 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5036 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5039 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5040 8e7bd50a 2019-08-22 stsp free(refstr);
5042 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5045 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5046 d4efa91b 2020-01-14 stsp if (err) {
5047 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5051 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5052 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5053 d4efa91b 2020-01-14 stsp if (err) {
5057 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5058 d4efa91b 2020-01-14 stsp tagger_time =
5059 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5060 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5066 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5067 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5068 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5069 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5073 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5074 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5075 2417344c 2019-08-23 stsp if (datestr)
5076 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5077 d4efa91b 2020-01-14 stsp if (commit)
5078 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5080 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5081 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5082 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5085 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5086 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5089 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5090 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5093 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5094 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5101 8e7bd50a 2019-08-22 stsp free(id_str);
5102 d4efa91b 2020-01-14 stsp if (commit) {
5103 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5106 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5108 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5109 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5110 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5111 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5116 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5118 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5120 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5121 8e7bd50a 2019-08-22 stsp } while (line);
5122 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5125 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5126 8e7bd50a 2019-08-22 stsp return NULL;
5129 8e7bd50a 2019-08-22 stsp static const struct got_error *
5130 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5131 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5133 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5134 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5135 f372d5cd 2019-10-21 stsp char *editor = NULL;
5136 8e7bd50a 2019-08-22 stsp int fd = -1;
5138 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5139 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5140 8e7bd50a 2019-08-22 stsp goto done;
5143 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5144 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
5145 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5146 8e7bd50a 2019-08-22 stsp goto done;
5149 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5151 8e7bd50a 2019-08-22 stsp goto done;
5153 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
5154 8e7bd50a 2019-08-22 stsp close(fd);
5156 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5158 8e7bd50a 2019-08-22 stsp goto done;
5159 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5161 8e7bd50a 2019-08-22 stsp free(initial_content);
5162 8e7bd50a 2019-08-22 stsp free(template);
5163 8e7bd50a 2019-08-22 stsp free(editor);
5165 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5166 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5167 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5168 8e7bd50a 2019-08-22 stsp if (err) {
5169 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5170 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5173 8e7bd50a 2019-08-22 stsp return err;
5176 8e7bd50a 2019-08-22 stsp static const struct got_error *
5177 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
5178 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
5180 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5181 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5182 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5183 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5184 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5185 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5186 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5189 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5190 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5191 8e7bd50a 2019-08-22 stsp * an unintended typo.
5193 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5194 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5196 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
5198 8e7bd50a 2019-08-22 stsp return err;
5200 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5201 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5203 8e7bd50a 2019-08-22 stsp goto done;
5205 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5207 8e7bd50a 2019-08-22 stsp goto done;
5209 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5210 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5211 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5212 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5213 8e7bd50a 2019-08-22 stsp goto done;
5215 8e7bd50a 2019-08-22 stsp tag_name += 10;
5216 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5217 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5218 8e7bd50a 2019-08-22 stsp goto done;
5221 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5222 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5223 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5224 8e7bd50a 2019-08-22 stsp goto done;
5225 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5226 8e7bd50a 2019-08-22 stsp goto done;
5228 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5229 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5230 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5231 f372d5cd 2019-10-21 stsp if (err) {
5232 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5233 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5234 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5235 8e7bd50a 2019-08-22 stsp goto done;
5239 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5240 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5241 f372d5cd 2019-10-21 stsp if (err) {
5242 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5243 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5244 8e7bd50a 2019-08-22 stsp goto done;
5247 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5248 f372d5cd 2019-10-21 stsp if (err) {
5249 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5250 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5251 8e7bd50a 2019-08-22 stsp goto done;
5254 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5255 f372d5cd 2019-10-21 stsp if (err) {
5256 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5257 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5258 f372d5cd 2019-10-21 stsp goto done;
5261 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5262 f372d5cd 2019-10-21 stsp if (err) {
5263 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5264 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5265 f372d5cd 2019-10-21 stsp goto done;
5267 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5269 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5270 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5271 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5272 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5273 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5274 f372d5cd 2019-10-21 stsp free(tag_id_str);
5276 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5277 8e7bd50a 2019-08-22 stsp free(commit_id);
5278 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5279 8e7bd50a 2019-08-22 stsp free(refname);
5280 8e7bd50a 2019-08-22 stsp free(tagmsg);
5281 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5282 aba9c984 2019-09-08 stsp free(tagger);
5283 8e7bd50a 2019-08-22 stsp return err;
5286 8e7bd50a 2019-08-22 stsp static const struct got_error *
5287 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5289 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5290 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5291 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5292 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5293 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5294 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5295 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5297 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5298 8e7bd50a 2019-08-22 stsp switch (ch) {
5300 80106605 2020-02-24 stsp commit_id_arg = optarg;
5303 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5306 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5307 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5308 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5310 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5313 8e7bd50a 2019-08-22 stsp do_list = 1;
5316 8e7bd50a 2019-08-22 stsp usage_tag();
5317 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5321 8e7bd50a 2019-08-22 stsp argc -= optind;
5322 8e7bd50a 2019-08-22 stsp argv += optind;
5324 8e7bd50a 2019-08-22 stsp if (do_list) {
5325 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5327 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5328 8e7bd50a 2019-08-22 stsp if (tagmsg)
5329 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5330 8e7bd50a 2019-08-22 stsp if (argc > 0)
5331 8e7bd50a 2019-08-22 stsp usage_tag();
5332 80106605 2020-02-24 stsp } else if (argc != 1)
5333 8e7bd50a 2019-08-22 stsp usage_tag();
5335 a2887370 2019-08-23 stsp tag_name = argv[0];
5337 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5338 8e7bd50a 2019-08-22 stsp if (do_list) {
5339 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5340 8e7bd50a 2019-08-22 stsp NULL) == -1)
5341 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5343 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5344 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5345 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5348 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5349 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5350 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5351 8e7bd50a 2019-08-22 stsp goto done;
5354 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5355 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5356 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5357 8e7bd50a 2019-08-22 stsp goto done;
5359 8e7bd50a 2019-08-22 stsp error = NULL;
5360 8e7bd50a 2019-08-22 stsp if (worktree) {
5361 8e7bd50a 2019-08-22 stsp repo_path =
5362 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5363 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5364 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5365 8e7bd50a 2019-08-22 stsp if (error)
5366 8e7bd50a 2019-08-22 stsp goto done;
5368 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5369 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5370 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5371 8e7bd50a 2019-08-22 stsp goto done;
5376 8e7bd50a 2019-08-22 stsp if (do_list) {
5377 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5378 c9956ddf 2019-09-08 stsp if (error != NULL)
5379 c9956ddf 2019-09-08 stsp goto done;
5380 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5381 8e7bd50a 2019-08-22 stsp if (error)
5382 8e7bd50a 2019-08-22 stsp goto done;
5383 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5385 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5386 c9956ddf 2019-09-08 stsp if (error)
5387 c9956ddf 2019-09-08 stsp goto done;
5388 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5389 c9956ddf 2019-09-08 stsp if (error != NULL)
5390 c9956ddf 2019-09-08 stsp goto done;
5392 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5393 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5394 8e7bd50a 2019-08-22 stsp if (error)
5395 8e7bd50a 2019-08-22 stsp goto done;
5398 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5399 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5400 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5401 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5402 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5403 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5404 8e7bd50a 2019-08-22 stsp if (error)
5405 8e7bd50a 2019-08-22 stsp goto done;
5406 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5407 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5408 8e7bd50a 2019-08-22 stsp if (error)
5409 8e7bd50a 2019-08-22 stsp goto done;
5410 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5411 8e7bd50a 2019-08-22 stsp free(commit_id);
5412 8e7bd50a 2019-08-22 stsp if (error)
5413 8e7bd50a 2019-08-22 stsp goto done;
5416 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5417 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5421 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5422 8e7bd50a 2019-08-22 stsp if (worktree)
5423 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5424 8e7bd50a 2019-08-22 stsp free(cwd);
5425 8e7bd50a 2019-08-22 stsp free(repo_path);
5426 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5427 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5428 8e7bd50a 2019-08-22 stsp return error;
5431 8e7bd50a 2019-08-22 stsp __dead static void
5432 d00136be 2019-03-26 stsp usage_add(void)
5434 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5435 022fae89 2019-12-06 tracey getprogname());
5439 d00136be 2019-03-26 stsp static const struct got_error *
5440 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5442 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5444 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5445 4e68cba3 2019-11-23 stsp return NULL;
5448 4e68cba3 2019-11-23 stsp static const struct got_error *
5449 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5451 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5452 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5453 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5454 1dd54920 2019-05-11 stsp char *cwd = NULL;
5455 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5456 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5457 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5459 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5461 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5462 d00136be 2019-03-26 stsp switch (ch) {
5463 022fae89 2019-12-06 tracey case 'I':
5464 022fae89 2019-12-06 tracey no_ignores = 1;
5467 4e68cba3 2019-11-23 stsp can_recurse = 1;
5470 d00136be 2019-03-26 stsp usage_add();
5471 d00136be 2019-03-26 stsp /* NOTREACHED */
5475 d00136be 2019-03-26 stsp argc -= optind;
5476 d00136be 2019-03-26 stsp argv += optind;
5478 43012d58 2019-07-14 stsp #ifndef PROFILE
5479 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5480 43012d58 2019-07-14 stsp NULL) == -1)
5481 43012d58 2019-07-14 stsp err(1, "pledge");
5483 723c305c 2019-05-11 jcs if (argc < 1)
5484 d00136be 2019-03-26 stsp usage_add();
5486 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5487 d00136be 2019-03-26 stsp if (cwd == NULL) {
5488 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5489 d00136be 2019-03-26 stsp goto done;
5492 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5493 d00136be 2019-03-26 stsp if (error)
5494 d00136be 2019-03-26 stsp goto done;
5496 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5498 031a5338 2019-03-26 stsp if (error != NULL)
5499 031a5338 2019-03-26 stsp goto done;
5501 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5502 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5503 d00136be 2019-03-26 stsp if (error)
5504 d00136be 2019-03-26 stsp goto done;
5506 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5507 6d022e97 2019-08-04 stsp if (error)
5508 022fae89 2019-12-06 tracey goto done;
5510 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5511 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5512 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5513 6d022e97 2019-08-04 stsp goto done;
5517 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5518 4e68cba3 2019-11-23 stsp char *ondisk_path;
5519 4e68cba3 2019-11-23 stsp struct stat sb;
5520 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5521 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5522 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5523 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5524 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5525 4e68cba3 2019-11-23 stsp goto done;
5527 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5528 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5529 4e68cba3 2019-11-23 stsp free(ondisk_path);
5532 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5533 4e68cba3 2019-11-23 stsp ondisk_path);
5534 4e68cba3 2019-11-23 stsp free(ondisk_path);
5535 4e68cba3 2019-11-23 stsp goto done;
5537 4e68cba3 2019-11-23 stsp free(ondisk_path);
5538 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5539 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5540 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5541 4e68cba3 2019-11-23 stsp goto done;
5546 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5547 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5550 031a5338 2019-03-26 stsp got_repo_close(repo);
5551 d00136be 2019-03-26 stsp if (worktree)
5552 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5553 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5554 1dd54920 2019-05-11 stsp free((char *)pe->path);
5555 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5556 2ec1f75b 2019-03-26 stsp free(cwd);
5557 2ec1f75b 2019-03-26 stsp return error;
5560 2ec1f75b 2019-03-26 stsp __dead static void
5561 648e4ef7 2019-07-09 stsp usage_remove(void)
5563 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5564 f2a9dc41 2019-12-13 tracey getprogname());
5568 2a06fe5f 2019-08-24 stsp static const struct got_error *
5569 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5570 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5572 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5574 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5575 2a06fe5f 2019-08-24 stsp return NULL;
5576 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5577 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5578 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5579 2a06fe5f 2019-08-24 stsp return NULL;
5582 2ec1f75b 2019-03-26 stsp static const struct got_error *
5583 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5585 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5586 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5587 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5588 17ed4618 2019-06-02 stsp char *cwd = NULL;
5589 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5590 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5591 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5593 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5595 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5596 2ec1f75b 2019-03-26 stsp switch (ch) {
5598 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5600 70e3e7f5 2019-12-13 tracey case 'k':
5601 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5603 f2a9dc41 2019-12-13 tracey case 'R':
5604 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5607 f2a9dc41 2019-12-13 tracey usage_remove();
5608 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5612 2ec1f75b 2019-03-26 stsp argc -= optind;
5613 2ec1f75b 2019-03-26 stsp argv += optind;
5615 43012d58 2019-07-14 stsp #ifndef PROFILE
5616 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5617 43012d58 2019-07-14 stsp NULL) == -1)
5618 43012d58 2019-07-14 stsp err(1, "pledge");
5620 17ed4618 2019-06-02 stsp if (argc < 1)
5621 648e4ef7 2019-07-09 stsp usage_remove();
5623 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5624 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5625 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5626 2ec1f75b 2019-03-26 stsp goto done;
5628 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5629 2ec1f75b 2019-03-26 stsp if (error)
5630 2ec1f75b 2019-03-26 stsp goto done;
5632 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5635 2ec1f75b 2019-03-26 stsp goto done;
5637 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5638 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5639 2ec1f75b 2019-03-26 stsp if (error)
5640 2ec1f75b 2019-03-26 stsp goto done;
5642 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5643 6d022e97 2019-08-04 stsp if (error)
5644 6d022e97 2019-08-04 stsp goto done;
5646 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5647 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5648 f2a9dc41 2019-12-13 tracey struct stat sb;
5649 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5650 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5651 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5652 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5653 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5654 f2a9dc41 2019-12-13 tracey goto done;
5656 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5657 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5658 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5659 f2a9dc41 2019-12-13 tracey continue;
5661 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5662 f2a9dc41 2019-12-13 tracey ondisk_path);
5663 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5664 f2a9dc41 2019-12-13 tracey goto done;
5666 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5667 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5668 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5669 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5670 f2a9dc41 2019-12-13 tracey goto done;
5675 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5676 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5679 a129376b 2019-03-28 stsp got_repo_close(repo);
5680 a129376b 2019-03-28 stsp if (worktree)
5681 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5682 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5683 17ed4618 2019-06-02 stsp free((char *)pe->path);
5684 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5685 a129376b 2019-03-28 stsp free(cwd);
5686 a129376b 2019-03-28 stsp return error;
5689 a129376b 2019-03-28 stsp __dead static void
5690 a129376b 2019-03-28 stsp usage_revert(void)
5692 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5693 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5697 1ee397ad 2019-07-12 stsp static const struct got_error *
5698 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5700 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5701 fb9704af 2020-01-27 stsp return NULL;
5703 a129376b 2019-03-28 stsp while (path[0] == '/')
5705 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5706 33aa809d 2019-08-08 stsp return NULL;
5709 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5710 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5711 33aa809d 2019-08-08 stsp const char *action;
5714 33aa809d 2019-08-08 stsp static const struct got_error *
5715 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5716 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5718 33aa809d 2019-08-08 stsp char *line = NULL;
5719 33aa809d 2019-08-08 stsp size_t linesize = 0;
5720 33aa809d 2019-08-08 stsp ssize_t linelen;
5722 33aa809d 2019-08-08 stsp switch (status) {
5723 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5724 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5726 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5727 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5729 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5730 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5731 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5732 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5733 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5734 33aa809d 2019-08-08 stsp printf("%s", line);
5735 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5736 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5737 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5738 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5739 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5742 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5745 33aa809d 2019-08-08 stsp return NULL;
5748 33aa809d 2019-08-08 stsp static const struct got_error *
5749 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5750 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5752 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5753 33aa809d 2019-08-08 stsp char *line = NULL;
5754 33aa809d 2019-08-08 stsp size_t linesize = 0;
5755 33aa809d 2019-08-08 stsp ssize_t linelen;
5756 33aa809d 2019-08-08 stsp int resp = ' ';
5757 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5759 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5761 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5763 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5764 33aa809d 2019-08-08 stsp a->action);
5766 33aa809d 2019-08-08 stsp return err;
5767 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5768 33aa809d 2019-08-08 stsp if (linelen == -1) {
5769 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5770 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5771 33aa809d 2019-08-08 stsp return NULL;
5773 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5775 33aa809d 2019-08-08 stsp *nl = '\0';
5776 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5777 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5778 33aa809d 2019-08-08 stsp printf("y\n");
5779 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5780 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5781 33aa809d 2019-08-08 stsp printf("n\n");
5782 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5783 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5784 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5785 33aa809d 2019-08-08 stsp printf("q\n");
5787 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5788 33aa809d 2019-08-08 stsp free(line);
5789 33aa809d 2019-08-08 stsp return NULL;
5792 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5793 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5794 33aa809d 2019-08-08 stsp a->action);
5796 33aa809d 2019-08-08 stsp return err;
5797 33aa809d 2019-08-08 stsp resp = getchar();
5798 33aa809d 2019-08-08 stsp if (resp == '\n')
5799 33aa809d 2019-08-08 stsp resp = getchar();
5800 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5801 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5802 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5803 33aa809d 2019-08-08 stsp resp = ' ';
5805 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5806 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5807 33aa809d 2019-08-08 stsp resp = ' ';
5811 33aa809d 2019-08-08 stsp if (resp == 'y')
5812 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5813 33aa809d 2019-08-08 stsp else if (resp == 'n')
5814 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5815 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5816 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5818 1ee397ad 2019-07-12 stsp return NULL;
5822 a129376b 2019-03-28 stsp static const struct got_error *
5823 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5825 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5826 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5827 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5828 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5829 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5830 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5831 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5832 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5833 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5834 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5836 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5838 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5839 a129376b 2019-03-28 stsp switch (ch) {
5841 33aa809d 2019-08-08 stsp pflag = 1;
5844 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5847 0f6d7415 2019-08-08 stsp can_recurse = 1;
5850 a129376b 2019-03-28 stsp usage_revert();
5851 a129376b 2019-03-28 stsp /* NOTREACHED */
5855 a129376b 2019-03-28 stsp argc -= optind;
5856 a129376b 2019-03-28 stsp argv += optind;
5858 43012d58 2019-07-14 stsp #ifndef PROFILE
5859 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5860 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5861 43012d58 2019-07-14 stsp err(1, "pledge");
5863 e20a8b6f 2019-06-04 stsp if (argc < 1)
5864 a129376b 2019-03-28 stsp usage_revert();
5865 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5866 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5868 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5869 a129376b 2019-03-28 stsp if (cwd == NULL) {
5870 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5871 a129376b 2019-03-28 stsp goto done;
5873 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5874 2ec1f75b 2019-03-26 stsp if (error)
5875 2ec1f75b 2019-03-26 stsp goto done;
5877 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5879 a129376b 2019-03-28 stsp if (error != NULL)
5880 a129376b 2019-03-28 stsp goto done;
5882 33aa809d 2019-08-08 stsp if (patch_script_path) {
5883 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5884 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5885 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5886 33aa809d 2019-08-08 stsp patch_script_path);
5887 33aa809d 2019-08-08 stsp goto done;
5890 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5891 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5892 a129376b 2019-03-28 stsp if (error)
5893 a129376b 2019-03-28 stsp goto done;
5895 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5896 6d022e97 2019-08-04 stsp if (error)
5897 6d022e97 2019-08-04 stsp goto done;
5899 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5900 0f6d7415 2019-08-08 stsp char *ondisk_path;
5901 0f6d7415 2019-08-08 stsp struct stat sb;
5902 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5903 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5904 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5905 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5906 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5907 0f6d7415 2019-08-08 stsp goto done;
5909 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5910 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5911 0f6d7415 2019-08-08 stsp free(ondisk_path);
5914 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5915 0f6d7415 2019-08-08 stsp ondisk_path);
5916 0f6d7415 2019-08-08 stsp free(ondisk_path);
5917 0f6d7415 2019-08-08 stsp goto done;
5919 0f6d7415 2019-08-08 stsp free(ondisk_path);
5920 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5921 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5922 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5923 0f6d7415 2019-08-08 stsp goto done;
5928 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5929 33aa809d 2019-08-08 stsp cpa.action = "revert";
5930 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5931 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5933 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5934 33aa809d 2019-08-08 stsp error == NULL)
5935 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5937 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5938 2ec1f75b 2019-03-26 stsp if (worktree)
5939 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5940 2ec1f75b 2019-03-26 stsp free(path);
5941 d00136be 2019-03-26 stsp free(cwd);
5942 6bad629b 2019-02-04 stsp return error;
5945 c4296144 2019-05-09 stsp __dead static void
5946 c4296144 2019-05-09 stsp usage_commit(void)
5948 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5949 5c1e53bc 2019-07-28 stsp getprogname());
5953 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5954 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5955 e2ba3d07 2019-05-13 stsp const char *editor;
5956 e0870e44 2019-05-13 stsp const char *worktree_path;
5957 76d98825 2019-06-03 stsp const char *branch_name;
5958 314a6357 2019-05-13 stsp const char *repo_path;
5959 e0870e44 2019-05-13 stsp char *logmsg_path;
5963 33ad4cbe 2019-05-12 jcs static const struct got_error *
5964 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5967 76d98825 2019-06-03 stsp char *initial_content = NULL;
5968 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5969 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5970 e0870e44 2019-05-13 stsp char *template = NULL;
5971 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5973 33ad4cbe 2019-05-12 jcs size_t len;
5975 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5976 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5977 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5978 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5979 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5980 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5981 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5982 33ad4cbe 2019-05-12 jcs return NULL;
5985 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5986 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5988 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5989 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5990 76d98825 2019-06-03 stsp a->branch_name) == -1)
5991 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5993 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5995 e0870e44 2019-05-13 stsp goto done;
5997 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5999 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6000 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6001 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6002 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6003 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6007 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6009 76d98825 2019-06-03 stsp free(initial_content);
6010 e0870e44 2019-05-13 stsp free(template);
6012 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6013 3ce1b845 2019-07-15 stsp if (err == NULL) {
6014 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6015 3ce1b845 2019-07-15 stsp if (err) {
6016 3ce1b845 2019-07-15 stsp free(*logmsg);
6017 3ce1b845 2019-07-15 stsp *logmsg = NULL;
6020 33ad4cbe 2019-05-12 jcs return err;
6023 c4296144 2019-05-09 stsp static const struct got_error *
6024 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6026 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6027 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6028 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6029 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6030 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6031 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6032 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6033 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6034 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6035 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6037 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6038 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6040 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
6041 c4296144 2019-05-09 stsp switch (ch) {
6043 c4296144 2019-05-09 stsp logmsg = optarg;
6046 c4296144 2019-05-09 stsp usage_commit();
6047 c4296144 2019-05-09 stsp /* NOTREACHED */
6051 c4296144 2019-05-09 stsp argc -= optind;
6052 c4296144 2019-05-09 stsp argv += optind;
6054 43012d58 2019-07-14 stsp #ifndef PROFILE
6055 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6056 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6057 43012d58 2019-07-14 stsp err(1, "pledge");
6059 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6060 c4296144 2019-05-09 stsp if (cwd == NULL) {
6061 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6062 c4296144 2019-05-09 stsp goto done;
6064 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6065 7d5807f4 2019-07-11 stsp if (error)
6066 7d5807f4 2019-07-11 stsp goto done;
6068 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6069 c4296144 2019-05-09 stsp if (error)
6070 a698f62e 2019-07-25 stsp goto done;
6071 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6072 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6073 c4296144 2019-05-09 stsp goto done;
6076 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6077 916f288c 2019-07-30 stsp worktree);
6078 5c1e53bc 2019-07-28 stsp if (error)
6079 5c1e53bc 2019-07-28 stsp goto done;
6081 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6082 c9956ddf 2019-09-08 stsp if (error)
6083 c9956ddf 2019-09-08 stsp goto done;
6084 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6085 c9956ddf 2019-09-08 stsp gitconfig_path);
6086 c4296144 2019-05-09 stsp if (error != NULL)
6087 c4296144 2019-05-09 stsp goto done;
6089 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
6090 aba9c984 2019-09-08 stsp if (error)
6091 aba9c984 2019-09-08 stsp return error;
6094 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6095 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6097 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6098 314a6357 2019-05-13 stsp error = get_editor(&editor);
6100 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6101 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6102 c4296144 2019-05-09 stsp if (error)
6103 c4296144 2019-05-09 stsp goto done;
6105 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6106 087fb88c 2019-08-04 stsp if (error)
6107 087fb88c 2019-08-04 stsp goto done;
6109 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6110 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6111 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6112 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6113 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6114 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6115 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6116 916f288c 2019-07-30 stsp goto done;
6118 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6120 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6121 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6122 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6123 e0870e44 2019-05-13 stsp if (error) {
6124 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6125 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6126 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6127 c4296144 2019-05-09 stsp goto done;
6130 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6131 c4296144 2019-05-09 stsp if (error)
6132 c4296144 2019-05-09 stsp goto done;
6133 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6135 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6136 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6137 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6138 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6139 7266f21f 2019-10-21 stsp error == NULL)
6140 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6141 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6143 c4296144 2019-05-09 stsp got_repo_close(repo);
6144 c4296144 2019-05-09 stsp if (worktree)
6145 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6146 c4296144 2019-05-09 stsp free(cwd);
6147 c4296144 2019-05-09 stsp free(id_str);
6148 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6149 e2ba3d07 2019-05-13 stsp free(editor);
6150 aba9c984 2019-09-08 stsp free(author);
6151 234035bc 2019-06-01 stsp return error;
6154 234035bc 2019-06-01 stsp __dead static void
6155 234035bc 2019-06-01 stsp usage_cherrypick(void)
6157 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6161 234035bc 2019-06-01 stsp static const struct got_error *
6162 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6164 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6165 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6166 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6167 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6168 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6169 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6170 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6171 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6172 234035bc 2019-06-01 stsp int ch, did_something = 0;
6174 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6175 234035bc 2019-06-01 stsp switch (ch) {
6177 234035bc 2019-06-01 stsp usage_cherrypick();
6178 234035bc 2019-06-01 stsp /* NOTREACHED */
6182 234035bc 2019-06-01 stsp argc -= optind;
6183 234035bc 2019-06-01 stsp argv += optind;
6185 43012d58 2019-07-14 stsp #ifndef PROFILE
6186 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6187 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6188 43012d58 2019-07-14 stsp err(1, "pledge");
6190 234035bc 2019-06-01 stsp if (argc != 1)
6191 234035bc 2019-06-01 stsp usage_cherrypick();
6193 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6194 234035bc 2019-06-01 stsp if (cwd == NULL) {
6195 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6196 234035bc 2019-06-01 stsp goto done;
6198 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6199 234035bc 2019-06-01 stsp if (error)
6200 234035bc 2019-06-01 stsp goto done;
6202 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6204 234035bc 2019-06-01 stsp if (error != NULL)
6205 234035bc 2019-06-01 stsp goto done;
6207 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6208 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6209 234035bc 2019-06-01 stsp if (error)
6210 234035bc 2019-06-01 stsp goto done;
6212 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6213 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6214 234035bc 2019-06-01 stsp if (error != NULL) {
6215 234035bc 2019-06-01 stsp struct got_reference *ref;
6216 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6217 234035bc 2019-06-01 stsp goto done;
6218 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6219 234035bc 2019-06-01 stsp if (error != NULL)
6220 234035bc 2019-06-01 stsp goto done;
6221 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6222 234035bc 2019-06-01 stsp got_ref_close(ref);
6223 234035bc 2019-06-01 stsp if (error != NULL)
6224 234035bc 2019-06-01 stsp goto done;
6226 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6227 234035bc 2019-06-01 stsp if (error)
6228 234035bc 2019-06-01 stsp goto done;
6230 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6231 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6232 234035bc 2019-06-01 stsp if (error != NULL)
6233 234035bc 2019-06-01 stsp goto done;
6235 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6236 234035bc 2019-06-01 stsp if (error) {
6237 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6238 234035bc 2019-06-01 stsp goto done;
6239 234035bc 2019-06-01 stsp error = NULL;
6241 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6242 234035bc 2019-06-01 stsp goto done;
6245 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6246 234035bc 2019-06-01 stsp if (error)
6247 234035bc 2019-06-01 stsp goto done;
6248 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6249 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6250 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
6252 234035bc 2019-06-01 stsp if (error != NULL)
6253 234035bc 2019-06-01 stsp goto done;
6255 234035bc 2019-06-01 stsp if (did_something)
6256 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6258 234035bc 2019-06-01 stsp if (commit)
6259 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6260 234035bc 2019-06-01 stsp free(commit_id_str);
6261 234035bc 2019-06-01 stsp if (head_ref)
6262 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6263 234035bc 2019-06-01 stsp if (worktree)
6264 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6266 234035bc 2019-06-01 stsp got_repo_close(repo);
6267 c4296144 2019-05-09 stsp return error;
6270 5ef14e63 2019-06-02 stsp __dead static void
6271 5ef14e63 2019-06-02 stsp usage_backout(void)
6273 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6277 5ef14e63 2019-06-02 stsp static const struct got_error *
6278 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6280 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6281 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6282 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6283 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6284 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6285 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6286 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6287 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6288 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
6290 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6291 5ef14e63 2019-06-02 stsp switch (ch) {
6293 5ef14e63 2019-06-02 stsp usage_backout();
6294 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6298 5ef14e63 2019-06-02 stsp argc -= optind;
6299 5ef14e63 2019-06-02 stsp argv += optind;
6301 43012d58 2019-07-14 stsp #ifndef PROFILE
6302 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6303 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6304 43012d58 2019-07-14 stsp err(1, "pledge");
6306 5ef14e63 2019-06-02 stsp if (argc != 1)
6307 5ef14e63 2019-06-02 stsp usage_backout();
6309 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6310 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6311 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6312 5ef14e63 2019-06-02 stsp goto done;
6314 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6315 5ef14e63 2019-06-02 stsp if (error)
6316 5ef14e63 2019-06-02 stsp goto done;
6318 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6320 5ef14e63 2019-06-02 stsp if (error != NULL)
6321 5ef14e63 2019-06-02 stsp goto done;
6323 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6324 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6325 5ef14e63 2019-06-02 stsp if (error)
6326 5ef14e63 2019-06-02 stsp goto done;
6328 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6329 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6330 5ef14e63 2019-06-02 stsp if (error != NULL) {
6331 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6332 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6333 5ef14e63 2019-06-02 stsp goto done;
6334 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6335 5ef14e63 2019-06-02 stsp if (error != NULL)
6336 5ef14e63 2019-06-02 stsp goto done;
6337 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6338 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6339 5ef14e63 2019-06-02 stsp if (error != NULL)
6340 5ef14e63 2019-06-02 stsp goto done;
6342 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6343 5ef14e63 2019-06-02 stsp if (error)
6344 5ef14e63 2019-06-02 stsp goto done;
6346 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6347 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6348 5ef14e63 2019-06-02 stsp if (error != NULL)
6349 5ef14e63 2019-06-02 stsp goto done;
6351 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6352 5ef14e63 2019-06-02 stsp if (error)
6353 5ef14e63 2019-06-02 stsp goto done;
6355 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6356 5ef14e63 2019-06-02 stsp if (error)
6357 5ef14e63 2019-06-02 stsp goto done;
6358 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6359 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6360 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6361 5ef14e63 2019-06-02 stsp goto done;
6364 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6365 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6366 5ef14e63 2019-06-02 stsp if (error != NULL)
6367 5ef14e63 2019-06-02 stsp goto done;
6369 5ef14e63 2019-06-02 stsp if (did_something)
6370 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6372 5ef14e63 2019-06-02 stsp if (commit)
6373 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6374 5ef14e63 2019-06-02 stsp free(commit_id_str);
6375 5ef14e63 2019-06-02 stsp if (head_ref)
6376 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6377 818c7501 2019-07-11 stsp if (worktree)
6378 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6380 818c7501 2019-07-11 stsp got_repo_close(repo);
6381 818c7501 2019-07-11 stsp return error;
6384 818c7501 2019-07-11 stsp __dead static void
6385 818c7501 2019-07-11 stsp usage_rebase(void)
6387 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6388 af54c8f8 2019-07-11 stsp getprogname());
6393 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6396 0ebf8283 2019-07-24 stsp size_t len;
6398 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6399 0ebf8283 2019-07-24 stsp if (len > limit)
6400 0ebf8283 2019-07-24 stsp len = limit;
6401 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6402 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6404 0ebf8283 2019-07-24 stsp *nl = '\0';
6407 0ebf8283 2019-07-24 stsp static const struct got_error *
6408 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6410 5943eee2 2019-08-13 stsp const struct got_error *err;
6411 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6412 5943eee2 2019-08-13 stsp const char *s;
6414 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6416 5943eee2 2019-08-13 stsp return err;
6418 5943eee2 2019-08-13 stsp s = logmsg0;
6419 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6422 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6423 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6424 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6425 5943eee2 2019-08-13 stsp goto done;
6428 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6430 5943eee2 2019-08-13 stsp free(logmsg0);
6431 a0ea4fc0 2020-02-28 stsp return err;
6434 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6435 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6437 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6438 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6439 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6441 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6443 a0ea4fc0 2020-02-28 stsp return err;
6445 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6447 a0ea4fc0 2020-02-28 stsp goto done;
6449 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6451 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6453 a0ea4fc0 2020-02-28 stsp goto done;
6455 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6457 a0ea4fc0 2020-02-28 stsp free(id_str);
6458 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6459 a0ea4fc0 2020-02-28 stsp free(logmsg);
6460 5943eee2 2019-08-13 stsp return err;
6463 818c7501 2019-07-11 stsp static const struct got_error *
6464 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6465 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6467 818c7501 2019-07-11 stsp const struct got_error *err;
6468 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6470 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6472 818c7501 2019-07-11 stsp goto done;
6474 ff0d2220 2019-07-11 stsp if (new_id) {
6475 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6477 ff0d2220 2019-07-11 stsp goto done;
6480 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6481 ff0d2220 2019-07-11 stsp if (new_id_str)
6482 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6484 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6486 0ebf8283 2019-07-24 stsp goto done;
6488 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6489 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6491 818c7501 2019-07-11 stsp free(old_id_str);
6492 818c7501 2019-07-11 stsp free(new_id_str);
6493 272a1371 2020-02-28 stsp free(logmsg);
6494 818c7501 2019-07-11 stsp return err;
6497 1ee397ad 2019-07-12 stsp static const struct got_error *
6498 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6500 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6502 818c7501 2019-07-11 stsp while (path[0] == '/')
6504 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6506 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6507 1ee397ad 2019-07-12 stsp return NULL;
6508 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6509 818c7501 2019-07-11 stsp *rebase_status = status;
6510 1ee397ad 2019-07-12 stsp return NULL;
6513 818c7501 2019-07-11 stsp static const struct got_error *
6514 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6515 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6516 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6518 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6519 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6520 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6523 818c7501 2019-07-11 stsp static const struct got_error *
6524 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6525 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6526 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6527 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6529 ff0d2220 2019-07-11 stsp const struct got_error *error;
6530 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6531 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6533 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6534 ff0d2220 2019-07-11 stsp if (error)
6535 ff0d2220 2019-07-11 stsp return error;
6537 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6538 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6539 ff0d2220 2019-07-11 stsp if (error) {
6540 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6541 ff0d2220 2019-07-11 stsp goto done;
6542 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6544 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6545 ff0d2220 2019-07-11 stsp free(new_commit_id);
6548 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6549 ff0d2220 2019-07-11 stsp return error;
6552 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6553 64c6d990 2019-07-11 stsp const char *path_prefix;
6554 64c6d990 2019-07-11 stsp size_t len;
6555 8ca9bd68 2019-07-25 stsp int errcode;
6558 64c6d990 2019-07-11 stsp static const struct got_error *
6559 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6560 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6561 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6562 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6564 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6566 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6567 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6568 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6570 64c6d990 2019-07-11 stsp return NULL;
6573 64c6d990 2019-07-11 stsp static const struct got_error *
6574 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6575 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6576 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6578 64c6d990 2019-07-11 stsp const struct got_error *err;
6579 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6580 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6581 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6583 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6584 64c6d990 2019-07-11 stsp return NULL;
6586 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6588 64c6d990 2019-07-11 stsp goto done;
6590 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6592 64c6d990 2019-07-11 stsp goto done;
6594 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6595 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6597 64c6d990 2019-07-11 stsp goto done;
6599 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6600 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6602 64c6d990 2019-07-11 stsp goto done;
6604 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6605 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6606 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6607 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6608 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6609 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6610 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6612 64c6d990 2019-07-11 stsp if (tree1)
6613 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6614 64c6d990 2019-07-11 stsp if (tree2)
6615 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6616 64c6d990 2019-07-11 stsp if (commit)
6617 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6618 64c6d990 2019-07-11 stsp if (parent_commit)
6619 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6620 64c6d990 2019-07-11 stsp return err;
6623 ff0d2220 2019-07-11 stsp static const struct got_error *
6624 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6625 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6626 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6627 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6628 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6630 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6631 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6632 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6633 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6634 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6636 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6638 af61c510 2019-07-19 stsp return err;
6640 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6641 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6643 af61c510 2019-07-19 stsp goto done;
6644 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6645 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6646 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6647 af61c510 2019-07-19 stsp if (err) {
6648 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6649 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6650 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6651 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6652 af61c510 2019-07-19 stsp "been reached?!?");
6654 ee780d5c 2020-01-04 stsp goto done;
6656 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6657 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6659 af61c510 2019-07-19 stsp goto done;
6661 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6663 af61c510 2019-07-19 stsp goto done;
6664 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6665 af61c510 2019-07-19 stsp commit_id = parent_id;
6669 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6670 af61c510 2019-07-19 stsp return err;
6673 af61c510 2019-07-19 stsp static const struct got_error *
6674 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6676 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6677 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6678 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6679 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6680 818c7501 2019-07-11 stsp char *cwd = NULL;
6681 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6682 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6683 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6684 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6685 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6686 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6687 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6688 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6689 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6690 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6691 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6692 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6693 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6695 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6696 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6698 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6699 818c7501 2019-07-11 stsp switch (ch) {
6701 818c7501 2019-07-11 stsp abort_rebase = 1;
6704 818c7501 2019-07-11 stsp continue_rebase = 1;
6707 818c7501 2019-07-11 stsp usage_rebase();
6708 818c7501 2019-07-11 stsp /* NOTREACHED */
6712 818c7501 2019-07-11 stsp argc -= optind;
6713 818c7501 2019-07-11 stsp argv += optind;
6715 43012d58 2019-07-14 stsp #ifndef PROFILE
6716 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6717 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6718 43012d58 2019-07-14 stsp err(1, "pledge");
6720 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6721 818c7501 2019-07-11 stsp usage_rebase();
6722 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6723 818c7501 2019-07-11 stsp if (argc != 0)
6724 818c7501 2019-07-11 stsp usage_rebase();
6725 818c7501 2019-07-11 stsp } else if (argc != 1)
6726 818c7501 2019-07-11 stsp usage_rebase();
6728 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6729 818c7501 2019-07-11 stsp if (cwd == NULL) {
6730 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6731 818c7501 2019-07-11 stsp goto done;
6733 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6734 818c7501 2019-07-11 stsp if (error)
6735 818c7501 2019-07-11 stsp goto done;
6737 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6739 818c7501 2019-07-11 stsp if (error != NULL)
6740 818c7501 2019-07-11 stsp goto done;
6742 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6743 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6744 7ef62c4e 2020-02-24 stsp if (error)
6745 7ef62c4e 2020-02-24 stsp goto done;
6747 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6748 7ef62c4e 2020-02-24 stsp worktree);
6749 818c7501 2019-07-11 stsp if (error)
6750 7ef62c4e 2020-02-24 stsp goto done;
6751 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6752 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6753 818c7501 2019-07-11 stsp goto done;
6756 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6757 818c7501 2019-07-11 stsp if (error)
6758 818c7501 2019-07-11 stsp goto done;
6760 f6794adc 2019-07-23 stsp if (abort_rebase) {
6761 818c7501 2019-07-11 stsp int did_something;
6762 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6763 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6764 f6794adc 2019-07-23 stsp goto done;
6766 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6767 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6768 3e3a69f1 2019-07-25 stsp worktree, repo);
6769 818c7501 2019-07-11 stsp if (error)
6770 818c7501 2019-07-11 stsp goto done;
6771 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6772 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6773 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6774 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6775 818c7501 2019-07-11 stsp if (error)
6776 818c7501 2019-07-11 stsp goto done;
6777 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6778 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6781 818c7501 2019-07-11 stsp if (continue_rebase) {
6782 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6783 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6784 f6794adc 2019-07-23 stsp goto done;
6786 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6787 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6788 3e3a69f1 2019-07-25 stsp worktree, repo);
6789 818c7501 2019-07-11 stsp if (error)
6790 818c7501 2019-07-11 stsp goto done;
6792 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6793 01757395 2019-07-12 stsp resume_commit_id, repo);
6794 818c7501 2019-07-11 stsp if (error)
6795 818c7501 2019-07-11 stsp goto done;
6797 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6798 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6799 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6800 818c7501 2019-07-11 stsp goto done;
6803 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6804 818c7501 2019-07-11 stsp if (error != NULL)
6805 818c7501 2019-07-11 stsp goto done;
6808 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6809 ff0d2220 2019-07-11 stsp if (error)
6810 ff0d2220 2019-07-11 stsp goto done;
6812 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6813 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6815 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6816 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6817 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6818 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6819 818c7501 2019-07-11 stsp if (error)
6820 818c7501 2019-07-11 stsp goto done;
6821 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6822 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6823 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6824 818c7501 2019-07-11 stsp "with work tree's branch");
6825 818c7501 2019-07-11 stsp goto done;
6828 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6829 a51a74b3 2019-07-27 stsp if (error) {
6830 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6831 a51a74b3 2019-07-27 stsp goto done;
6832 a51a74b3 2019-07-27 stsp error = NULL;
6834 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6835 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6836 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6837 a51a74b3 2019-07-27 stsp goto done;
6839 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6840 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6841 818c7501 2019-07-11 stsp if (error)
6842 818c7501 2019-07-11 stsp goto done;
6845 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6846 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6847 818c7501 2019-07-11 stsp if (error)
6848 818c7501 2019-07-11 stsp goto done;
6850 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6851 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6852 fc66b545 2019-08-12 stsp if (pid == NULL) {
6853 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6854 fc66b545 2019-08-12 stsp int did_something;
6855 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6856 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6857 fc66b545 2019-08-12 stsp &did_something);
6858 fc66b545 2019-08-12 stsp if (error)
6859 fc66b545 2019-08-12 stsp goto done;
6860 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6861 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6863 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6864 fc66b545 2019-08-12 stsp goto done;
6866 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6867 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6868 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6869 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6870 818c7501 2019-07-11 stsp commit = NULL;
6871 818c7501 2019-07-11 stsp if (error)
6872 818c7501 2019-07-11 stsp goto done;
6874 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6875 38b0338b 2019-11-29 stsp if (continue_rebase) {
6876 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6877 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6878 38b0338b 2019-11-29 stsp goto done;
6880 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6881 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6882 38b0338b 2019-11-29 stsp char *id_str;
6883 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6884 38b0338b 2019-11-29 stsp new_base_branch);
6885 38b0338b 2019-11-29 stsp if (error)
6886 38b0338b 2019-11-29 stsp goto done;
6887 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6888 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6889 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6890 38b0338b 2019-11-29 stsp free(id_str);
6891 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6892 38b0338b 2019-11-29 stsp new_head_commit_id);
6893 38b0338b 2019-11-29 stsp if (error)
6894 38b0338b 2019-11-29 stsp goto done;
6898 818c7501 2019-07-11 stsp pid = NULL;
6899 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6900 818c7501 2019-07-11 stsp commit_id = qid->id;
6901 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6902 818c7501 2019-07-11 stsp pid = qid;
6904 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6905 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6906 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6907 818c7501 2019-07-11 stsp if (error)
6908 818c7501 2019-07-11 stsp goto done;
6910 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6911 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6912 a0ea4fc0 2020-02-28 stsp if (error)
6913 a0ea4fc0 2020-02-28 stsp goto done;
6914 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6918 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6919 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6920 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6921 818c7501 2019-07-11 stsp if (error)
6922 818c7501 2019-07-11 stsp goto done;
6925 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6926 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6927 818c7501 2019-07-11 stsp if (error)
6928 818c7501 2019-07-11 stsp goto done;
6929 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6930 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6932 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6933 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6935 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6936 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6937 818c7501 2019-07-11 stsp free(resume_commit_id);
6938 818c7501 2019-07-11 stsp free(yca_id);
6939 818c7501 2019-07-11 stsp if (commit)
6940 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6941 818c7501 2019-07-11 stsp if (branch)
6942 818c7501 2019-07-11 stsp got_ref_close(branch);
6943 818c7501 2019-07-11 stsp if (new_base_branch)
6944 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6945 818c7501 2019-07-11 stsp if (tmp_branch)
6946 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6947 5ef14e63 2019-06-02 stsp if (worktree)
6948 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6950 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6951 5ef14e63 2019-06-02 stsp return error;
6954 0ebf8283 2019-07-24 stsp __dead static void
6955 0ebf8283 2019-07-24 stsp usage_histedit(void)
6957 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6958 0ebf8283 2019-07-24 stsp getprogname());
6962 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6963 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6964 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6965 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6966 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6968 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6969 0ebf8283 2019-07-24 stsp unsigned char code;
6970 0ebf8283 2019-07-24 stsp const char *name;
6971 0ebf8283 2019-07-24 stsp const char *desc;
6972 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6973 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6974 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6975 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6976 82997472 2020-01-29 stsp "be used" },
6977 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6978 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6979 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6982 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6983 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6984 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6985 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6986 0ebf8283 2019-07-24 stsp char *logmsg;
6988 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6990 0ebf8283 2019-07-24 stsp static const struct got_error *
6991 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6992 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6994 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6995 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6996 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6999 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7001 0ebf8283 2019-07-24 stsp goto done;
7003 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7005 0ebf8283 2019-07-24 stsp goto done;
7007 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7009 0ebf8283 2019-07-24 stsp goto done;
7011 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7012 0ebf8283 2019-07-24 stsp if (n < 0)
7013 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7015 0ebf8283 2019-07-24 stsp if (commit)
7016 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7017 0ebf8283 2019-07-24 stsp free(id_str);
7018 0ebf8283 2019-07-24 stsp free(logmsg);
7019 0ebf8283 2019-07-24 stsp return err;
7022 0ebf8283 2019-07-24 stsp static const struct got_error *
7023 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7024 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7026 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7027 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7029 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7030 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7032 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7033 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7037 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7038 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7039 083957f4 2020-02-24 stsp if (n < 0) {
7040 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7046 0ebf8283 2019-07-24 stsp return err;
7049 0ebf8283 2019-07-24 stsp static const struct got_error *
7050 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7051 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7053 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7055 514f2ffe 2020-01-29 stsp char *id_str;
7056 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7058 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7059 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7061 514f2ffe 2020-01-29 stsp return err;
7063 514f2ffe 2020-01-29 stsp n = fprintf(f,
7064 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7065 514f2ffe 2020-01-29 stsp "# commit %s\n"
7066 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7067 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7068 514f2ffe 2020-01-29 stsp if (n < 0) {
7069 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7070 514f2ffe 2020-01-29 stsp goto done;
7073 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7074 514f2ffe 2020-01-29 stsp if (n < 0) {
7075 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7076 514f2ffe 2020-01-29 stsp goto done;
7079 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7080 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7081 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7082 0ebf8283 2019-07-24 stsp cmd->desc);
7083 0ebf8283 2019-07-24 stsp if (n < 0) {
7084 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7089 514f2ffe 2020-01-29 stsp free(id_str);
7090 0ebf8283 2019-07-24 stsp return err;
7093 0ebf8283 2019-07-24 stsp static const struct got_error *
7094 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7096 0ebf8283 2019-07-24 stsp static char msg[42];
7099 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7101 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7102 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7104 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7107 0ebf8283 2019-07-24 stsp static const struct got_error *
7108 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7109 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7111 0ebf8283 2019-07-24 stsp const struct got_error *err;
7112 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7113 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7115 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7117 0ebf8283 2019-07-24 stsp return err;
7119 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7121 0ebf8283 2019-07-24 stsp goto done;
7123 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7125 5943eee2 2019-08-13 stsp goto done;
7126 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7127 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7128 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7129 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7132 0ebf8283 2019-07-24 stsp if (folded_commit)
7133 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7134 0ebf8283 2019-07-24 stsp free(id_str);
7135 5943eee2 2019-08-13 stsp free(folded_logmsg);
7136 0ebf8283 2019-07-24 stsp return err;
7139 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7140 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7142 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7144 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7145 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7146 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7147 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7148 3f9de99f 2019-07-24 stsp folded = prev;
7149 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7152 0ebf8283 2019-07-24 stsp return folded;
7155 0ebf8283 2019-07-24 stsp static const struct got_error *
7156 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7157 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7159 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7160 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7161 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7162 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7164 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7166 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7168 0ebf8283 2019-07-24 stsp return err;
7170 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7171 0ebf8283 2019-07-24 stsp if (folded) {
7172 0ebf8283 2019-07-24 stsp while (folded != hle) {
7173 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7174 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7177 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7178 0ebf8283 2019-07-24 stsp logmsg, repo);
7180 0ebf8283 2019-07-24 stsp goto done;
7181 0ebf8283 2019-07-24 stsp free(logmsg);
7182 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7183 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7187 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7189 0ebf8283 2019-07-24 stsp goto done;
7190 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7192 5943eee2 2019-08-13 stsp goto done;
7193 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
7194 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7195 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7196 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7197 0ebf8283 2019-07-24 stsp goto done;
7199 0ebf8283 2019-07-24 stsp free(logmsg);
7200 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7202 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7204 0ebf8283 2019-07-24 stsp goto done;
7206 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7207 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7209 0ebf8283 2019-07-24 stsp goto done;
7211 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
7212 0ebf8283 2019-07-24 stsp close(fd);
7214 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7216 0ebf8283 2019-07-24 stsp goto done;
7218 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7219 0ebf8283 2019-07-24 stsp if (err) {
7220 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7221 0ebf8283 2019-07-24 stsp goto done;
7222 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7225 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7226 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7227 0ebf8283 2019-07-24 stsp free(logmsg_path);
7228 0ebf8283 2019-07-24 stsp free(logmsg);
7229 5943eee2 2019-08-13 stsp free(orig_logmsg);
7230 0ebf8283 2019-07-24 stsp free(editor);
7231 0ebf8283 2019-07-24 stsp if (commit)
7232 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7233 0ebf8283 2019-07-24 stsp return err;
7236 0ebf8283 2019-07-24 stsp static const struct got_error *
7237 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7238 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7240 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7241 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7242 0ebf8283 2019-07-24 stsp size_t size;
7243 0ebf8283 2019-07-24 stsp ssize_t len;
7244 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7245 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7246 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7247 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7249 0ebf8283 2019-07-24 stsp for (;;) {
7250 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7251 0ebf8283 2019-07-24 stsp if (len == -1) {
7252 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7253 0ebf8283 2019-07-24 stsp if (feof(f))
7255 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7256 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7261 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7263 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7264 0ebf8283 2019-07-24 stsp free(line);
7265 0ebf8283 2019-07-24 stsp line = NULL;
7268 0ebf8283 2019-07-24 stsp cmd = NULL;
7269 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7270 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7271 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7272 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7273 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7276 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7281 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7282 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7285 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7287 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7288 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7289 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7292 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7293 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7297 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7298 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7299 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7303 0ebf8283 2019-07-24 stsp free(line);
7304 0ebf8283 2019-07-24 stsp line = NULL;
7308 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7310 0ebf8283 2019-07-24 stsp *end = '\0';
7312 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7313 0ebf8283 2019-07-24 stsp if (err) {
7314 0ebf8283 2019-07-24 stsp /* override error code */
7315 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7319 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7320 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7321 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7324 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7325 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7326 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7327 0ebf8283 2019-07-24 stsp commit_id = NULL;
7328 0ebf8283 2019-07-24 stsp free(line);
7329 0ebf8283 2019-07-24 stsp line = NULL;
7330 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7333 0ebf8283 2019-07-24 stsp free(line);
7334 0ebf8283 2019-07-24 stsp free(commit_id);
7335 0ebf8283 2019-07-24 stsp return err;
7338 0ebf8283 2019-07-24 stsp static const struct got_error *
7339 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7340 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7342 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7343 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7344 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7345 5b87815e 2020-03-05 stsp static char msg[92];
7346 bfce7f83 2019-07-27 stsp char *id_str;
7348 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7349 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7350 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7351 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7352 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7354 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7355 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7356 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7357 5b87815e 2020-03-05 stsp if (hle == hle2)
7359 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7360 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7362 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7364 5b87815e 2020-03-05 stsp return err;
7365 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7366 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7367 5b87815e 2020-03-05 stsp free(id_str);
7368 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7372 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7373 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7374 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7377 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7378 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7380 bfce7f83 2019-07-27 stsp return err;
7381 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7382 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7383 bfce7f83 2019-07-27 stsp free(id_str);
7384 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7388 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7389 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7390 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7391 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7393 bfce7f83 2019-07-27 stsp return NULL;
7396 bfce7f83 2019-07-27 stsp static const struct got_error *
7397 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7398 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7399 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7401 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7402 0ebf8283 2019-07-24 stsp char *editor;
7403 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7405 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7407 0ebf8283 2019-07-24 stsp return err;
7409 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7410 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7411 0ebf8283 2019-07-24 stsp goto done;
7414 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7415 0ebf8283 2019-07-24 stsp if (f == NULL) {
7416 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7417 0ebf8283 2019-07-24 stsp goto done;
7419 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7421 bfce7f83 2019-07-27 stsp goto done;
7423 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7425 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7426 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7427 0ebf8283 2019-07-24 stsp free(editor);
7428 0ebf8283 2019-07-24 stsp return err;
7431 0ebf8283 2019-07-24 stsp static const struct got_error *
7432 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7433 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7434 514f2ffe 2020-01-29 stsp struct got_repository *);
7436 0ebf8283 2019-07-24 stsp static const struct got_error *
7437 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7438 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7439 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7441 0ebf8283 2019-07-24 stsp const struct got_error *err;
7442 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7443 0ebf8283 2019-07-24 stsp char *path = NULL;
7445 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7447 0ebf8283 2019-07-24 stsp return err;
7449 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7451 0ebf8283 2019-07-24 stsp goto done;
7453 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7455 0ebf8283 2019-07-24 stsp goto done;
7457 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7458 083957f4 2020-02-24 stsp rewind(f);
7459 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7461 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7462 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7463 0ebf8283 2019-07-24 stsp goto done;
7466 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7467 083957f4 2020-02-24 stsp if (err) {
7468 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7469 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7470 083957f4 2020-02-24 stsp goto done;
7471 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7472 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7476 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7477 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7478 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7479 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7480 0ebf8283 2019-07-24 stsp free(path);
7481 0ebf8283 2019-07-24 stsp return err;
7484 0ebf8283 2019-07-24 stsp static const struct got_error *
7485 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7486 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7488 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7489 0ebf8283 2019-07-24 stsp char *path = NULL;
7490 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7491 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7492 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7494 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7496 0ebf8283 2019-07-24 stsp return err;
7498 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7499 0ebf8283 2019-07-24 stsp if (f == NULL) {
7500 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7501 0ebf8283 2019-07-24 stsp goto done;
7503 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7504 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7509 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7510 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7511 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7512 0ebf8283 2019-07-24 stsp if (n < 0) {
7513 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7519 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7520 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7521 0ebf8283 2019-07-24 stsp free(path);
7522 0ebf8283 2019-07-24 stsp if (commit)
7523 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7524 0ebf8283 2019-07-24 stsp return err;
7528 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7530 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7532 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7533 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7534 bfce7f83 2019-07-27 stsp free(hle);
7538 0ebf8283 2019-07-24 stsp static const struct got_error *
7539 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7540 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7542 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7543 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7545 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7546 0ebf8283 2019-07-24 stsp if (f == NULL) {
7547 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7548 0ebf8283 2019-07-24 stsp goto done;
7551 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7553 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7554 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7555 0ebf8283 2019-07-24 stsp return err;
7558 0ebf8283 2019-07-24 stsp static const struct got_error *
7559 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7560 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7561 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7563 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7564 0ebf8283 2019-07-24 stsp int resp = ' ';
7566 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7567 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7568 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7569 0ebf8283 2019-07-24 stsp resp = getchar();
7570 a61a4414 2019-08-07 stsp if (resp == '\n')
7571 a61a4414 2019-08-07 stsp resp = getchar();
7572 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7573 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7574 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7576 426ebf2e 2019-07-25 stsp if (err) {
7577 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7578 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7580 bfce7f83 2019-07-27 stsp prev_err = err;
7581 426ebf2e 2019-07-25 stsp resp = ' ';
7585 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7586 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7587 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7588 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7589 426ebf2e 2019-07-25 stsp if (err) {
7590 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7591 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7593 bfce7f83 2019-07-27 stsp prev_err = err;
7594 426ebf2e 2019-07-25 stsp resp = ' ';
7598 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7599 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7602 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7605 0ebf8283 2019-07-24 stsp return err;
7608 0ebf8283 2019-07-24 stsp static const struct got_error *
7609 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7610 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7611 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7613 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7614 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7615 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7616 3e3a69f1 2019-07-25 stsp branch, repo);
7619 0ebf8283 2019-07-24 stsp static const struct got_error *
7620 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7621 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7623 0ebf8283 2019-07-24 stsp const struct got_error *err;
7624 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7626 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7628 0ebf8283 2019-07-24 stsp goto done;
7630 0ebf8283 2019-07-24 stsp if (new_id) {
7631 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7633 0ebf8283 2019-07-24 stsp goto done;
7636 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7637 0ebf8283 2019-07-24 stsp if (new_id_str)
7638 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7640 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7641 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7642 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7643 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7644 0ebf8283 2019-07-24 stsp goto done;
7646 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7648 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7650 0ebf8283 2019-07-24 stsp goto done;
7653 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7654 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7655 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7656 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7657 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7659 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7660 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7661 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7668 0ebf8283 2019-07-24 stsp free(old_id_str);
7669 0ebf8283 2019-07-24 stsp free(new_id_str);
7670 0ebf8283 2019-07-24 stsp return err;
7673 0ebf8283 2019-07-24 stsp static const struct got_error *
7674 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7675 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7676 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7677 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7679 0ebf8283 2019-07-24 stsp const struct got_error *err;
7680 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7681 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7683 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7684 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7685 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7687 0ebf8283 2019-07-24 stsp return err;
7690 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7692 0ebf8283 2019-07-24 stsp return err;
7694 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7695 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7696 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7697 0ebf8283 2019-07-24 stsp if (err) {
7698 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7699 0ebf8283 2019-07-24 stsp goto done;
7700 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7702 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7703 0ebf8283 2019-07-24 stsp free(new_commit_id);
7706 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7707 0ebf8283 2019-07-24 stsp return err;
7710 0ebf8283 2019-07-24 stsp static const struct got_error *
7711 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7712 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7714 0ebf8283 2019-07-24 stsp const struct got_error *error;
7715 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7717 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7719 0ebf8283 2019-07-24 stsp if (error)
7720 0ebf8283 2019-07-24 stsp return error;
7722 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7723 0ebf8283 2019-07-24 stsp if (error)
7724 0ebf8283 2019-07-24 stsp return error;
7726 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7727 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7728 0ebf8283 2019-07-24 stsp return error;
7731 ab20a43a 2020-01-29 stsp static const struct got_error *
7732 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7733 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7734 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7735 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7737 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7739 ab20a43a 2020-01-29 stsp switch (status) {
7740 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7741 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7742 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7743 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7744 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7745 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7750 ab20a43a 2020-01-29 stsp switch (staged_status) {
7751 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7752 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7753 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7754 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7755 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7760 ab20a43a 2020-01-29 stsp return NULL;
7763 0ebf8283 2019-07-24 stsp static const struct got_error *
7764 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7766 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7767 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7768 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7769 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7770 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7771 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7772 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7773 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7774 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7775 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7776 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7777 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7778 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7779 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7780 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7781 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7782 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7783 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7784 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7785 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7786 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7787 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7789 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7790 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7791 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7793 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7794 0ebf8283 2019-07-24 stsp switch (ch) {
7796 0ebf8283 2019-07-24 stsp abort_edit = 1;
7799 0ebf8283 2019-07-24 stsp continue_edit = 1;
7802 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7805 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7808 0ebf8283 2019-07-24 stsp usage_histedit();
7809 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7813 0ebf8283 2019-07-24 stsp argc -= optind;
7814 0ebf8283 2019-07-24 stsp argv += optind;
7816 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7817 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7818 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7819 0ebf8283 2019-07-24 stsp err(1, "pledge");
7821 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7822 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7823 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7824 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7825 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7826 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7827 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7828 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7829 0ebf8283 2019-07-24 stsp if (argc != 0)
7830 0ebf8283 2019-07-24 stsp usage_histedit();
7833 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7834 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7835 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7836 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7837 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7838 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7841 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7842 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7843 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7844 0ebf8283 2019-07-24 stsp goto done;
7846 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7847 0ebf8283 2019-07-24 stsp if (error)
7848 0ebf8283 2019-07-24 stsp goto done;
7850 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7852 0ebf8283 2019-07-24 stsp if (error != NULL)
7853 0ebf8283 2019-07-24 stsp goto done;
7855 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7856 0ebf8283 2019-07-24 stsp if (error)
7857 0ebf8283 2019-07-24 stsp goto done;
7858 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7859 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7860 0ebf8283 2019-07-24 stsp goto done;
7863 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7864 0ebf8283 2019-07-24 stsp if (error)
7865 0ebf8283 2019-07-24 stsp goto done;
7867 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7868 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7869 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7870 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7871 083957f4 2020-02-24 stsp "before the -m option can be used");
7872 083957f4 2020-02-24 stsp goto done;
7875 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7876 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7877 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7878 3e3a69f1 2019-07-25 stsp worktree, repo);
7879 0ebf8283 2019-07-24 stsp if (error)
7880 0ebf8283 2019-07-24 stsp goto done;
7881 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7882 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7883 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7884 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7885 0ebf8283 2019-07-24 stsp if (error)
7886 0ebf8283 2019-07-24 stsp goto done;
7887 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7888 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7889 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7890 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7891 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7892 0ebf8283 2019-07-24 stsp goto done;
7895 0ebf8283 2019-07-24 stsp if (continue_edit) {
7896 0ebf8283 2019-07-24 stsp char *path;
7898 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7899 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7900 0ebf8283 2019-07-24 stsp goto done;
7903 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7904 0ebf8283 2019-07-24 stsp if (error)
7905 0ebf8283 2019-07-24 stsp goto done;
7907 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7908 0ebf8283 2019-07-24 stsp free(path);
7909 0ebf8283 2019-07-24 stsp if (error)
7910 0ebf8283 2019-07-24 stsp goto done;
7912 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7913 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7914 3e3a69f1 2019-07-25 stsp worktree, repo);
7915 0ebf8283 2019-07-24 stsp if (error)
7916 0ebf8283 2019-07-24 stsp goto done;
7918 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7919 0ebf8283 2019-07-24 stsp if (error)
7920 0ebf8283 2019-07-24 stsp goto done;
7922 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7923 0ebf8283 2019-07-24 stsp head_commit_id);
7924 0ebf8283 2019-07-24 stsp if (error)
7925 0ebf8283 2019-07-24 stsp goto done;
7926 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7927 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7928 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7929 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7930 3aac7cf7 2019-07-25 stsp goto done;
7932 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7933 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7934 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7935 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7936 0ebf8283 2019-07-24 stsp commit = NULL;
7937 0ebf8283 2019-07-24 stsp if (error)
7938 0ebf8283 2019-07-24 stsp goto done;
7940 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7941 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7942 0ebf8283 2019-07-24 stsp goto done;
7945 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7946 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7947 0ebf8283 2019-07-24 stsp if (error != NULL)
7948 0ebf8283 2019-07-24 stsp goto done;
7950 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7951 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7952 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7953 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7954 c7d20a3f 2019-07-30 stsp goto done;
7957 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7958 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7959 bfce7f83 2019-07-27 stsp branch = NULL;
7960 0ebf8283 2019-07-24 stsp if (error)
7961 0ebf8283 2019-07-24 stsp goto done;
7963 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7964 0ebf8283 2019-07-24 stsp head_commit_id);
7965 0ebf8283 2019-07-24 stsp if (error)
7966 0ebf8283 2019-07-24 stsp goto done;
7967 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7968 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7969 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7970 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7971 3aac7cf7 2019-07-25 stsp goto done;
7973 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7974 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7975 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7976 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7977 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7978 0ebf8283 2019-07-24 stsp commit = NULL;
7979 0ebf8283 2019-07-24 stsp if (error)
7980 0ebf8283 2019-07-24 stsp goto done;
7982 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7983 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7984 c5996fff 2020-01-29 stsp goto done;
7987 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7988 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7989 bfce7f83 2019-07-27 stsp if (error)
7990 bfce7f83 2019-07-27 stsp goto done;
7992 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7993 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7994 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7995 6ba2bea2 2019-07-27 stsp if (error) {
7996 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7997 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7998 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7999 0ebf8283 2019-07-24 stsp goto done;
8002 514f2ffe 2020-01-29 stsp const char *branch_name;
8003 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8004 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8005 514f2ffe 2020-01-29 stsp branch_name += 11;
8006 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8007 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8008 6ba2bea2 2019-07-27 stsp if (error) {
8009 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8010 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8011 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8012 0ebf8283 2019-07-24 stsp goto done;
8017 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8019 6ba2bea2 2019-07-27 stsp if (error) {
8020 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8021 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8022 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8023 0ebf8283 2019-07-24 stsp goto done;
8028 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8029 4ec14e60 2019-08-28 hiltjo if (error)
8030 0ebf8283 2019-07-24 stsp goto done;
8032 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8033 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8034 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8035 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8038 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8039 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8040 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8041 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8043 ab20a43a 2020-01-29 stsp if (error)
8044 ab20a43a 2020-01-29 stsp goto done;
8046 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8047 ab20a43a 2020-01-29 stsp int have_changes = 0;
8049 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8050 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8051 ab20a43a 2020-01-29 stsp if (error)
8052 ab20a43a 2020-01-29 stsp goto done;
8053 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8054 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8055 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8056 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8057 ab20a43a 2020-01-29 stsp if (error) {
8058 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8059 ab20a43a 2020-01-29 stsp goto done;
8060 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8061 ab20a43a 2020-01-29 stsp goto done;
8063 ab20a43a 2020-01-29 stsp if (have_changes) {
8064 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8065 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8066 ab20a43a 2020-01-29 stsp if (error)
8067 ab20a43a 2020-01-29 stsp goto done;
8069 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8070 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8071 ab20a43a 2020-01-29 stsp if (error)
8072 ab20a43a 2020-01-29 stsp goto done;
8073 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8074 ab20a43a 2020-01-29 stsp hle, NULL);
8075 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8076 ab20a43a 2020-01-29 stsp commit = NULL;
8077 ab20a43a 2020-01-29 stsp if (error)
8078 ab20a43a 2020-01-29 stsp goto done;
8084 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8085 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8086 0ebf8283 2019-07-24 stsp if (error)
8087 0ebf8283 2019-07-24 stsp goto done;
8091 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8092 0ebf8283 2019-07-24 stsp hle->commit_id);
8093 0ebf8283 2019-07-24 stsp if (error)
8094 0ebf8283 2019-07-24 stsp goto done;
8095 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8096 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8098 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8099 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8100 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
8101 0ebf8283 2019-07-24 stsp if (error)
8102 0ebf8283 2019-07-24 stsp goto done;
8103 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8104 0ebf8283 2019-07-24 stsp commit = NULL;
8106 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8107 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8109 a0ea4fc0 2020-02-28 stsp if (error)
8110 a0ea4fc0 2020-02-28 stsp goto done;
8111 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8115 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8116 0ebf8283 2019-07-24 stsp char *id_str;
8117 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8118 0ebf8283 2019-07-24 stsp if (error)
8119 0ebf8283 2019-07-24 stsp goto done;
8120 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8122 0ebf8283 2019-07-24 stsp free(id_str);
8123 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8124 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8125 3e3a69f1 2019-07-25 stsp fileindex);
8126 0ebf8283 2019-07-24 stsp goto done;
8129 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8130 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8131 0ebf8283 2019-07-24 stsp if (error)
8132 0ebf8283 2019-07-24 stsp goto done;
8136 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8137 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8138 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8139 0ebf8283 2019-07-24 stsp if (error)
8140 0ebf8283 2019-07-24 stsp goto done;
8143 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8144 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8145 0ebf8283 2019-07-24 stsp if (error)
8146 0ebf8283 2019-07-24 stsp goto done;
8147 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8148 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8150 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8151 3e3a69f1 2019-07-25 stsp branch, repo);
8153 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8154 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8155 0ebf8283 2019-07-24 stsp free(head_commit_id);
8156 0ebf8283 2019-07-24 stsp free(base_commit_id);
8157 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8158 0ebf8283 2019-07-24 stsp if (commit)
8159 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8160 0ebf8283 2019-07-24 stsp if (branch)
8161 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8162 0ebf8283 2019-07-24 stsp if (tmp_branch)
8163 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8164 0ebf8283 2019-07-24 stsp if (worktree)
8165 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8167 2822a352 2019-10-15 stsp got_repo_close(repo);
8168 2822a352 2019-10-15 stsp return error;
8171 2822a352 2019-10-15 stsp __dead static void
8172 2822a352 2019-10-15 stsp usage_integrate(void)
8174 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8178 2822a352 2019-10-15 stsp static const struct got_error *
8179 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8181 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8182 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8183 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8184 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8185 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8186 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8187 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8188 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8189 2822a352 2019-10-15 stsp int ch, did_something = 0;
8191 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8192 2822a352 2019-10-15 stsp switch (ch) {
8194 2822a352 2019-10-15 stsp usage_integrate();
8195 2822a352 2019-10-15 stsp /* NOTREACHED */
8199 2822a352 2019-10-15 stsp argc -= optind;
8200 2822a352 2019-10-15 stsp argv += optind;
8202 2822a352 2019-10-15 stsp if (argc != 1)
8203 2822a352 2019-10-15 stsp usage_integrate();
8204 2822a352 2019-10-15 stsp branch_arg = argv[0];
8206 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8207 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8208 2822a352 2019-10-15 stsp err(1, "pledge");
8210 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8211 2822a352 2019-10-15 stsp if (cwd == NULL) {
8212 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8213 2822a352 2019-10-15 stsp goto done;
8216 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8217 2822a352 2019-10-15 stsp if (error)
8218 2822a352 2019-10-15 stsp goto done;
8220 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8221 2822a352 2019-10-15 stsp if (error)
8222 2822a352 2019-10-15 stsp goto done;
8224 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8226 2822a352 2019-10-15 stsp if (error != NULL)
8227 2822a352 2019-10-15 stsp goto done;
8229 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8230 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8231 2822a352 2019-10-15 stsp if (error)
8232 2822a352 2019-10-15 stsp goto done;
8234 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8235 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8236 2822a352 2019-10-15 stsp goto done;
8239 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8240 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8241 2822a352 2019-10-15 stsp if (error)
8242 2822a352 2019-10-15 stsp goto done;
8244 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8245 2822a352 2019-10-15 stsp if (refname == NULL) {
8246 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8247 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8248 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8249 2822a352 2019-10-15 stsp goto done;
8251 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8252 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8253 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8254 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8255 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8256 2822a352 2019-10-15 stsp goto done;
8259 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8260 2822a352 2019-10-15 stsp if (error)
8261 2822a352 2019-10-15 stsp goto done;
8263 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8264 2822a352 2019-10-15 stsp if (error)
8265 2822a352 2019-10-15 stsp goto done;
8267 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8268 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8269 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8270 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8271 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8272 2822a352 2019-10-15 stsp goto done;
8275 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8276 2822a352 2019-10-15 stsp if (error) {
8277 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8278 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8279 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8280 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8281 2822a352 2019-10-15 stsp goto done;
8284 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8285 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
8286 2822a352 2019-10-15 stsp check_cancelled, NULL);
8287 2822a352 2019-10-15 stsp if (error)
8288 2822a352 2019-10-15 stsp goto done;
8290 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8293 715dc77e 2019-08-03 stsp got_repo_close(repo);
8294 2822a352 2019-10-15 stsp if (worktree)
8295 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8296 2822a352 2019-10-15 stsp free(cwd);
8297 2822a352 2019-10-15 stsp free(base_commit_id);
8298 2822a352 2019-10-15 stsp free(commit_id);
8299 2822a352 2019-10-15 stsp free(refname);
8300 2822a352 2019-10-15 stsp free(base_refname);
8301 715dc77e 2019-08-03 stsp return error;
8304 715dc77e 2019-08-03 stsp __dead static void
8305 715dc77e 2019-08-03 stsp usage_stage(void)
8307 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8308 f3055044 2019-08-07 stsp "[file-path ...]\n",
8309 715dc77e 2019-08-03 stsp getprogname());
8313 715dc77e 2019-08-03 stsp static const struct got_error *
8314 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8315 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8316 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8317 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8319 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8320 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8322 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8323 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8324 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8325 408b4ebc 2019-08-03 stsp return NULL;
8327 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8328 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8329 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8331 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8333 408b4ebc 2019-08-03 stsp return err;
8335 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8336 408b4ebc 2019-08-03 stsp free(id_str);
8337 dc424a06 2019-08-07 stsp return NULL;
8340 dc424a06 2019-08-07 stsp static const struct got_error *
8341 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8343 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8344 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8345 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8346 715dc77e 2019-08-03 stsp char *cwd = NULL;
8347 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8348 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8349 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8350 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8351 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8352 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8354 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8356 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8357 715dc77e 2019-08-03 stsp switch (ch) {
8359 408b4ebc 2019-08-03 stsp list_stage = 1;
8362 dc424a06 2019-08-07 stsp pflag = 1;
8365 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8368 715dc77e 2019-08-03 stsp usage_stage();
8369 715dc77e 2019-08-03 stsp /* NOTREACHED */
8373 715dc77e 2019-08-03 stsp argc -= optind;
8374 715dc77e 2019-08-03 stsp argv += optind;
8376 715dc77e 2019-08-03 stsp #ifndef PROFILE
8377 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8378 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8379 715dc77e 2019-08-03 stsp err(1, "pledge");
8381 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8382 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8383 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8384 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8386 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8387 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8388 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8389 715dc77e 2019-08-03 stsp goto done;
8392 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8393 715dc77e 2019-08-03 stsp if (error)
8394 715dc77e 2019-08-03 stsp goto done;
8396 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8398 715dc77e 2019-08-03 stsp if (error != NULL)
8399 715dc77e 2019-08-03 stsp goto done;
8401 dc424a06 2019-08-07 stsp if (patch_script_path) {
8402 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8403 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8404 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8405 dc424a06 2019-08-07 stsp patch_script_path);
8406 dc424a06 2019-08-07 stsp goto done;
8409 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8410 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8411 715dc77e 2019-08-03 stsp if (error)
8412 715dc77e 2019-08-03 stsp goto done;
8414 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8415 6d022e97 2019-08-04 stsp if (error)
8416 6d022e97 2019-08-04 stsp goto done;
8418 6d022e97 2019-08-04 stsp if (list_stage)
8419 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8420 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8422 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8423 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8424 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8425 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8426 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8429 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8430 2e1f37b0 2019-08-08 stsp error == NULL)
8431 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8433 ad493afc 2019-08-03 stsp got_repo_close(repo);
8434 ad493afc 2019-08-03 stsp if (worktree)
8435 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8436 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8437 ad493afc 2019-08-03 stsp free((char *)pe->path);
8438 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8439 ad493afc 2019-08-03 stsp free(cwd);
8440 ad493afc 2019-08-03 stsp return error;
8443 ad493afc 2019-08-03 stsp __dead static void
8444 ad493afc 2019-08-03 stsp usage_unstage(void)
8446 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8447 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8448 ad493afc 2019-08-03 stsp getprogname());
8453 ad493afc 2019-08-03 stsp static const struct got_error *
8454 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8456 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8457 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8458 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8459 ad493afc 2019-08-03 stsp char *cwd = NULL;
8460 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8461 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8462 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8463 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8464 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8465 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8467 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8469 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8470 ad493afc 2019-08-03 stsp switch (ch) {
8472 2e1f37b0 2019-08-08 stsp pflag = 1;
8475 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8478 ad493afc 2019-08-03 stsp usage_unstage();
8479 ad493afc 2019-08-03 stsp /* NOTREACHED */
8483 ad493afc 2019-08-03 stsp argc -= optind;
8484 ad493afc 2019-08-03 stsp argv += optind;
8486 ad493afc 2019-08-03 stsp #ifndef PROFILE
8487 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8488 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8489 ad493afc 2019-08-03 stsp err(1, "pledge");
8491 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8492 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8494 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8495 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8496 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8497 ad493afc 2019-08-03 stsp goto done;
8500 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8501 ad493afc 2019-08-03 stsp if (error)
8502 ad493afc 2019-08-03 stsp goto done;
8504 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8506 ad493afc 2019-08-03 stsp if (error != NULL)
8507 ad493afc 2019-08-03 stsp goto done;
8509 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8510 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8511 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8512 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8513 2e1f37b0 2019-08-08 stsp patch_script_path);
8514 2e1f37b0 2019-08-08 stsp goto done;
8518 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8519 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8520 ad493afc 2019-08-03 stsp if (error)
8521 ad493afc 2019-08-03 stsp goto done;
8523 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8524 ad493afc 2019-08-03 stsp if (error)
8525 ad493afc 2019-08-03 stsp goto done;
8527 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8528 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8529 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8530 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8532 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8533 2e1f37b0 2019-08-08 stsp error == NULL)
8534 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8536 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8537 715dc77e 2019-08-03 stsp if (worktree)
8538 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8539 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8540 715dc77e 2019-08-03 stsp free((char *)pe->path);
8541 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8542 715dc77e 2019-08-03 stsp free(cwd);
8543 01073a5d 2019-08-22 stsp return error;
8546 01073a5d 2019-08-22 stsp __dead static void
8547 01073a5d 2019-08-22 stsp usage_cat(void)
8549 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8550 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8554 01073a5d 2019-08-22 stsp static const struct got_error *
8555 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8557 01073a5d 2019-08-22 stsp const struct got_error *err;
8558 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8560 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8562 01073a5d 2019-08-22 stsp return err;
8564 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8565 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8566 01073a5d 2019-08-22 stsp return err;
8569 01073a5d 2019-08-22 stsp static const struct got_error *
8570 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8572 01073a5d 2019-08-22 stsp const struct got_error *err;
8573 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8574 56e0773d 2019-11-28 stsp int nentries, i;
8576 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8578 01073a5d 2019-08-22 stsp return err;
8580 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8581 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8582 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8583 01073a5d 2019-08-22 stsp char *id_str;
8584 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8586 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8587 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8590 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8591 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8592 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8593 01073a5d 2019-08-22 stsp free(id_str);
8596 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8597 01073a5d 2019-08-22 stsp return err;
8600 01073a5d 2019-08-22 stsp static const struct got_error *
8601 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8603 01073a5d 2019-08-22 stsp const struct got_error *err;
8604 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8605 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8606 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8607 01073a5d 2019-08-22 stsp char *id_str = NULL;
8608 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8610 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8612 01073a5d 2019-08-22 stsp return err;
8614 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8616 01073a5d 2019-08-22 stsp goto done;
8618 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8619 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8620 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8621 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8622 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8623 01073a5d 2019-08-22 stsp char *pid_str;
8624 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8626 01073a5d 2019-08-22 stsp goto done;
8627 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8628 01073a5d 2019-08-22 stsp free(pid_str);
8630 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8631 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8632 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8634 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8635 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8636 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8638 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8639 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8640 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8642 01073a5d 2019-08-22 stsp free(id_str);
8643 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8644 01073a5d 2019-08-22 stsp return err;
8647 01073a5d 2019-08-22 stsp static const struct got_error *
8648 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8650 01073a5d 2019-08-22 stsp const struct got_error *err;
8651 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8652 01073a5d 2019-08-22 stsp char *id_str = NULL;
8653 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8655 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8657 01073a5d 2019-08-22 stsp return err;
8659 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8661 01073a5d 2019-08-22 stsp goto done;
8663 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8665 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8666 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8667 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8668 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8670 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8671 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8672 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8674 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8675 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8676 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8678 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8679 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8680 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8686 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8687 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8689 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8690 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8691 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8693 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8694 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8695 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8697 01073a5d 2019-08-22 stsp free(id_str);
8698 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8699 01073a5d 2019-08-22 stsp return err;
8702 01073a5d 2019-08-22 stsp static const struct got_error *
8703 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8705 01073a5d 2019-08-22 stsp const struct got_error *error;
8706 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8707 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8708 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8709 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8710 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8711 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8713 01073a5d 2019-08-22 stsp #ifndef PROFILE
8714 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8715 01073a5d 2019-08-22 stsp NULL) == -1)
8716 01073a5d 2019-08-22 stsp err(1, "pledge");
8719 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8720 01073a5d 2019-08-22 stsp switch (ch) {
8722 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8725 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8726 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8727 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8729 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8732 896e9b6f 2019-08-26 stsp force_path = 1;
8735 01073a5d 2019-08-22 stsp usage_cat();
8736 01073a5d 2019-08-22 stsp /* NOTREACHED */
8740 01073a5d 2019-08-22 stsp argc -= optind;
8741 01073a5d 2019-08-22 stsp argv += optind;
8743 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8744 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8745 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8746 01073a5d 2019-08-22 stsp goto done;
8748 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8749 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8750 01073a5d 2019-08-22 stsp goto done;
8751 01073a5d 2019-08-22 stsp if (worktree) {
8752 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8753 01073a5d 2019-08-22 stsp repo_path = strdup(
8754 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8755 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8756 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8757 01073a5d 2019-08-22 stsp goto done;
8762 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8763 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8764 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8765 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8768 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8769 01073a5d 2019-08-22 stsp free(repo_path);
8770 01073a5d 2019-08-22 stsp if (error != NULL)
8771 01073a5d 2019-08-22 stsp goto done;
8773 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8774 896e9b6f 2019-08-26 stsp if (error)
8775 896e9b6f 2019-08-26 stsp goto done;
8777 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8778 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8779 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8780 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8781 01073a5d 2019-08-22 stsp if (error)
8782 01073a5d 2019-08-22 stsp goto done;
8784 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8785 896e9b6f 2019-08-26 stsp if (force_path) {
8786 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8788 896e9b6f 2019-08-26 stsp if (error)
8791 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8792 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8793 896e9b6f 2019-08-26 stsp if (error) {
8794 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8795 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8797 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8798 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8799 896e9b6f 2019-08-26 stsp if (error)
8804 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8805 01073a5d 2019-08-22 stsp if (error)
8808 01073a5d 2019-08-22 stsp switch (obj_type) {
8809 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8810 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8812 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8813 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8815 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8816 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8818 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8819 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8822 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8825 01073a5d 2019-08-22 stsp if (error)
8827 01073a5d 2019-08-22 stsp free(label);
8828 01073a5d 2019-08-22 stsp label = NULL;
8830 01073a5d 2019-08-22 stsp id = NULL;
8833 01073a5d 2019-08-22 stsp free(label);
8835 896e9b6f 2019-08-26 stsp free(commit_id);
8836 01073a5d 2019-08-22 stsp if (worktree)
8837 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8838 01073a5d 2019-08-22 stsp if (repo) {
8839 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8840 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8841 01073a5d 2019-08-22 stsp if (error == NULL)
8842 01073a5d 2019-08-22 stsp error = repo_error;
8844 0ebf8283 2019-07-24 stsp return error;