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"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
67 99437157 2018-11-11 stsp catch_sigint(int signo)
69 99437157 2018-11-11 stsp sigint_received = 1;
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
75 99437157 2018-11-11 stsp sigpipe_received = 1;
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 820059fa 2020-09-25 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
106 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
107 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
108 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
109 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
110 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
111 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
112 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
115 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
116 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
117 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
118 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
120 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
123 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
124 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
126 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
127 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
128 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
129 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
130 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
131 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
132 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
133 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
134 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
135 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
136 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
137 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
138 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
139 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
140 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
141 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
143 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
144 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
145 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
146 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
147 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
148 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
149 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
150 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
151 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
152 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
153 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
154 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
155 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
156 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
157 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
158 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
159 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
160 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
161 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
162 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
164 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
165 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
166 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
167 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
168 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
169 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
170 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
173 ce5b7c56 2019-07-09 stsp static void
174 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
178 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
179 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
180 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
181 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
183 6879ba42 2020-10-01 naddy fputc('\n', fp);
186 ff69268e 2020-12-13 stsp __dead static void
187 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
189 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
193 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
195 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
198 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
199 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
200 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
201 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
204 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
206 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
207 5c860e29 2018-03-12 stsp switch (ch) {
215 6879ba42 2020-10-01 naddy usage(hflag, 1);
216 5c860e29 2018-03-12 stsp /* NOTREACHED */
220 5c860e29 2018-03-12 stsp argc -= optind;
221 5c860e29 2018-03-12 stsp argv += optind;
222 9814e6a3 2020-09-27 naddy optind = 1;
223 9814e6a3 2020-09-27 naddy optreset = 1;
225 53ccebc2 2019-07-30 stsp if (Vflag) {
226 53ccebc2 2019-07-30 stsp got_version_print_str();
230 5c860e29 2018-03-12 stsp if (argc <= 0)
231 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
233 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
234 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
236 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
237 d7d4f210 2018-03-12 stsp const struct got_error *error;
239 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
241 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
242 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
246 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
248 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
249 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
250 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
251 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
252 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
253 70015d7a 2019-11-08 stsp !(sigint_received &&
254 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
255 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
262 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 6879ba42 2020-10-01 naddy list_commands(stderr);
267 4ed7e80c 2018-05-20 stsp __dead static void
268 6879ba42 2020-10-01 naddy usage(int hflag, int status)
270 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
272 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
273 53ccebc2 2019-07-30 stsp getprogname());
275 6879ba42 2020-10-01 naddy list_commands(fp);
276 6879ba42 2020-10-01 naddy exit(status);
279 0266afb7 2019-01-04 stsp static const struct got_error *
280 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
282 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
283 e2ba3d07 2019-05-13 stsp const char *editor;
285 8920fa04 2019-08-18 stsp *abspath = NULL;
287 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
288 e2ba3d07 2019-05-13 stsp if (editor == NULL)
289 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
291 0ee7065d 2019-05-13 stsp if (editor) {
292 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
294 0ee7065d 2019-05-13 stsp return err;
297 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
298 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
299 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
300 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
303 e2ba3d07 2019-05-13 stsp return NULL;
306 e2ba3d07 2019-05-13 stsp static const struct got_error *
307 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
308 c530dc23 2019-07-23 stsp const char *worktree_path)
310 163ce85a 2019-05-13 stsp const struct got_error *err;
312 37c06ea4 2019-07-15 stsp #ifdef PROFILE
313 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
314 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
316 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
319 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
320 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
322 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
323 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
325 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
326 163ce85a 2019-05-13 stsp if (err != NULL)
327 163ce85a 2019-05-13 stsp return err;
329 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
330 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
332 0266afb7 2019-01-04 stsp return NULL;
335 2c7829a4 2019-06-17 stsp __dead static void
336 2c7829a4 2019-06-17 stsp usage_init(void)
338 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
342 2c7829a4 2019-06-17 stsp static const struct got_error *
343 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
345 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
346 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
349 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
350 2c7829a4 2019-06-17 stsp switch (ch) {
352 2c7829a4 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp /* NOTREACHED */
357 2c7829a4 2019-06-17 stsp argc -= optind;
358 2c7829a4 2019-06-17 stsp argv += optind;
360 2c7829a4 2019-06-17 stsp #ifndef PROFILE
361 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
362 2c7829a4 2019-06-17 stsp err(1, "pledge");
364 2c7829a4 2019-06-17 stsp if (argc != 1)
365 bc20e173 2019-06-17 stsp usage_init();
367 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
368 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
369 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
371 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
373 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
374 2c7829a4 2019-06-17 stsp if (error &&
375 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
378 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
382 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
384 3ce1b845 2019-07-15 stsp free(repo_path);
385 3ce1b845 2019-07-15 stsp return error;
388 3ce1b845 2019-07-15 stsp __dead static void
389 3ce1b845 2019-07-15 stsp usage_import(void)
391 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
392 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
397 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
400 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
401 3ce1b845 2019-07-15 stsp int st = -1;
403 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
404 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
405 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
407 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
409 3ce1b845 2019-07-15 stsp goto doneediting;
411 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
412 3ce1b845 2019-07-15 stsp _exit(127);
415 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
416 3ce1b845 2019-07-15 stsp if (errno != EINTR)
419 3ce1b845 2019-07-15 stsp doneediting:
420 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
421 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
422 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
424 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
425 3ce1b845 2019-07-15 stsp errno = EINTR;
429 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
432 3ce1b845 2019-07-15 stsp static const struct got_error *
433 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
434 0d5bb276 2020-12-15 stsp const char *initial_content, size_t initial_content_len, int check_comments)
436 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
437 bfa12d5e 2020-09-26 stsp char *line = NULL;
438 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
439 bfa12d5e 2020-09-26 stsp ssize_t linelen;
440 3ce1b845 2019-07-15 stsp struct stat st, st2;
441 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
442 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
443 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
445 3ce1b845 2019-07-15 stsp *logmsg = NULL;
447 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
448 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
450 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
451 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
453 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
454 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
456 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
457 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
458 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
461 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
462 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
463 bfa12d5e 2020-09-26 stsp * has in fact been edited.
465 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
466 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
467 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
468 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
470 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
471 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
472 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
477 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
478 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
479 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
480 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
481 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
482 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
483 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
487 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
488 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
492 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
493 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
494 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
495 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
496 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
498 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
499 3ce1b845 2019-07-15 stsp if (fp == NULL) {
500 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
505 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
506 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
507 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
508 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
509 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
510 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
514 bfa12d5e 2020-09-26 stsp free(line);
515 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
516 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
519 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
520 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
524 bfa12d5e 2020-09-26 stsp if (len == 0) {
525 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
526 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
529 0d5bb276 2020-12-15 stsp if (check_comments && strcmp(*logmsg, initial_content_stripped) == 0)
530 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
531 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
533 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
535 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
536 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
538 3ce1b845 2019-07-15 stsp free(*logmsg);
539 3ce1b845 2019-07-15 stsp *logmsg = NULL;
541 3ce1b845 2019-07-15 stsp return err;
544 3ce1b845 2019-07-15 stsp static const struct got_error *
545 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
546 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
548 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
549 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
550 1601cb9f 2020-09-11 naddy int initial_content_len;
551 97972933 2020-09-11 stsp int fd = -1;
553 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
554 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
555 1601cb9f 2020-09-11 naddy branch_name);
556 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
557 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
559 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
560 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
564 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
565 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
569 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
570 0d5bb276 2020-12-15 stsp initial_content_len, 1);
572 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
573 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
574 3ce1b845 2019-07-15 stsp free(initial_content);
576 59f86c76 2020-09-11 stsp free(*logmsg_path);
577 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
579 3ce1b845 2019-07-15 stsp return err;
582 3ce1b845 2019-07-15 stsp static const struct got_error *
583 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
585 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
586 3ce1b845 2019-07-15 stsp return NULL;
589 3ce1b845 2019-07-15 stsp static const struct got_error *
590 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
591 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
593 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
594 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
595 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
597 84792843 2019-08-09 stsp *author = NULL;
599 50b0790e 2020-09-11 stsp if (worktree)
600 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
601 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
604 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
605 50b0790e 2020-09-11 stsp * significant to least significant:
606 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
607 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
608 50b0790e 2020-09-11 stsp * 3) repository's git config file
609 50b0790e 2020-09-11 stsp * 4) environment variables
610 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
613 50b0790e 2020-09-11 stsp if (worktree_conf)
614 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
615 50b0790e 2020-09-11 stsp if (got_author == NULL)
616 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
617 84792843 2019-08-09 stsp if (got_author == NULL) {
618 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
619 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
620 c9956ddf 2019-09-08 stsp if (name && email) {
621 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
622 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
623 c9956ddf 2019-09-08 stsp return NULL;
626 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
627 257add31 2020-09-09 stsp if (got_author == NULL) {
628 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
629 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
631 257add31 2020-09-09 stsp if (name && email) {
632 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
634 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
635 257add31 2020-09-09 stsp return NULL;
637 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
638 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
642 aba9c984 2019-09-08 stsp *author = strdup(got_author);
643 aba9c984 2019-09-08 stsp if (*author == NULL)
644 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
647 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
648 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
650 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
651 84792843 2019-08-09 stsp got_author++;
652 aba9c984 2019-09-08 stsp if (*got_author != '<') {
653 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
656 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
657 84792843 2019-08-09 stsp got_author++;
658 aba9c984 2019-09-08 stsp if (*got_author != '@') {
659 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
662 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
663 84792843 2019-08-09 stsp got_author++;
664 84792843 2019-08-09 stsp if (*got_author != '>')
665 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
668 aba9c984 2019-09-08 stsp free(*author);
669 aba9c984 2019-09-08 stsp *author = NULL;
671 aba9c984 2019-09-08 stsp return err;
674 c9956ddf 2019-09-08 stsp static const struct got_error *
675 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
677 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
679 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
680 c9956ddf 2019-09-08 stsp if (homedir) {
681 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
682 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
685 c9956ddf 2019-09-08 stsp return NULL;
688 84792843 2019-08-09 stsp static const struct got_error *
689 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
691 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
692 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
693 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
694 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
695 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
696 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
697 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
698 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
700 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
701 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
702 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
704 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
706 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
707 3ce1b845 2019-07-15 stsp switch (ch) {
709 3ce1b845 2019-07-15 stsp branch_name = optarg;
712 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
713 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
714 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
719 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
720 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
721 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
727 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
729 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
735 b2b65d18 2019-08-22 stsp usage_import();
736 3ce1b845 2019-07-15 stsp /* NOTREACHED */
740 3ce1b845 2019-07-15 stsp argc -= optind;
741 3ce1b845 2019-07-15 stsp argv += optind;
743 3ce1b845 2019-07-15 stsp #ifndef PROFILE
744 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
746 3ce1b845 2019-07-15 stsp NULL) == -1)
747 3ce1b845 2019-07-15 stsp err(1, "pledge");
749 3ce1b845 2019-07-15 stsp if (argc != 1)
750 3ce1b845 2019-07-15 stsp usage_import();
752 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
753 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
754 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
755 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
757 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
758 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
761 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
765 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
767 aba9c984 2019-09-08 stsp return error;
770 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
771 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
772 e560b7e0 2019-11-28 stsp * an unintended typo.
774 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
775 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
777 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
778 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
782 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
783 3ce1b845 2019-07-15 stsp if (error) {
784 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
787 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
788 3ce1b845 2019-07-15 stsp "import target branch already exists");
792 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
793 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
794 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
797 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
800 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
801 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
803 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
804 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
807 8e158b01 2019-09-22 stsp free(logmsg);
808 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
809 ef293bdd 2019-10-21 stsp path_dir, refname);
810 ef293bdd 2019-10-21 stsp if (error) {
811 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
812 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
813 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
818 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
819 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
820 ef293bdd 2019-10-21 stsp if (logmsg_path)
821 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
825 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
826 ef293bdd 2019-10-21 stsp if (error) {
827 ef293bdd 2019-10-21 stsp if (logmsg_path)
828 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
832 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
833 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
834 ef293bdd 2019-10-21 stsp if (error) {
835 ef293bdd 2019-10-21 stsp if (logmsg_path)
836 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
840 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
841 ef293bdd 2019-10-21 stsp if (error) {
842 ef293bdd 2019-10-21 stsp if (logmsg_path)
843 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
847 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
848 ef293bdd 2019-10-21 stsp if (error) {
849 ef293bdd 2019-10-21 stsp if (logmsg_path)
850 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
854 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
855 ef293bdd 2019-10-21 stsp if (error) {
856 ef293bdd 2019-10-21 stsp if (logmsg_path)
857 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
861 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
862 3ce1b845 2019-07-15 stsp if (error) {
863 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
864 ef293bdd 2019-10-21 stsp if (logmsg_path)
865 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
869 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
870 3ce1b845 2019-07-15 stsp branch_ref);
871 ef293bdd 2019-10-21 stsp if (error) {
872 ef293bdd 2019-10-21 stsp if (logmsg_path)
873 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
877 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
878 ef293bdd 2019-10-21 stsp if (error) {
879 ef293bdd 2019-10-21 stsp if (logmsg_path)
880 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
885 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
886 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
888 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
889 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
890 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
891 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
892 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
893 8e158b01 2019-09-22 stsp free(logmsg);
894 ef293bdd 2019-10-21 stsp free(logmsg_path);
895 2c7829a4 2019-06-17 stsp free(repo_path);
896 3ce1b845 2019-07-15 stsp free(editor);
897 3ce1b845 2019-07-15 stsp free(refname);
898 3ce1b845 2019-07-15 stsp free(new_commit_id);
899 3ce1b845 2019-07-15 stsp free(id_str);
900 aba9c984 2019-09-08 stsp free(author);
901 c9956ddf 2019-09-08 stsp free(gitconfig_path);
902 3ce1b845 2019-07-15 stsp if (branch_ref)
903 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
904 3ce1b845 2019-07-15 stsp if (head_ref)
905 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
906 2c7829a4 2019-06-17 stsp return error;
909 93658fb9 2020-03-18 stsp __dead static void
910 93658fb9 2020-03-18 stsp usage_clone(void)
912 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
913 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
917 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
918 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
919 892ac3b6 2020-03-18 stsp int last_p_indexed;
920 892ac3b6 2020-03-18 stsp int last_p_resolved;
921 68999b92 2020-03-18 stsp int verbosity;
923 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
925 04d9a9ec 2020-09-24 stsp int create_configs;
926 04d9a9ec 2020-09-24 stsp int configs_created;
928 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
929 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
930 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
931 04d9a9ec 2020-09-24 stsp const char *proto;
932 04d9a9ec 2020-09-24 stsp const char *host;
933 04d9a9ec 2020-09-24 stsp const char *port;
934 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
935 04d9a9ec 2020-09-24 stsp const char *git_url;
936 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
937 04d9a9ec 2020-09-24 stsp int mirror_references;
938 04d9a9ec 2020-09-24 stsp } config_info;
941 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
942 93658fb9 2020-03-18 stsp static const struct got_error *
943 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
944 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
945 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
946 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
947 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
949 04d9a9ec 2020-09-24 stsp static const struct got_error *
950 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
951 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
953 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
954 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
955 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
956 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
957 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
960 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
961 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
962 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
963 04d9a9ec 2020-09-24 stsp * we have all required information.
965 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
966 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
967 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
968 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
969 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
970 62d463ca 2020-10-20 naddy a->config_info.git_url,
971 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
972 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
973 62d463ca 2020-10-20 naddy a->config_info.symrefs,
974 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
975 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
977 04d9a9ec 2020-09-24 stsp return err;
978 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
981 68999b92 2020-03-18 stsp if (a->verbosity < 0)
982 68999b92 2020-03-18 stsp return NULL;
984 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
985 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
986 892ac3b6 2020-03-18 stsp fflush(stdout);
987 12d1281e 2020-03-19 stsp return NULL;
990 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
991 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
992 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
993 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
994 892ac3b6 2020-03-18 stsp print_size = 1;
995 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
996 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
997 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
999 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1000 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1001 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1002 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1003 892ac3b6 2020-03-18 stsp print_indexed = 1;
1004 892ac3b6 2020-03-18 stsp print_size = 1;
1007 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1008 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1009 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1010 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1011 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1012 892ac3b6 2020-03-18 stsp print_resolved = 1;
1013 892ac3b6 2020-03-18 stsp print_indexed = 1;
1014 892ac3b6 2020-03-18 stsp print_size = 1;
1019 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1020 892ac3b6 2020-03-18 stsp printf("\r");
1021 892ac3b6 2020-03-18 stsp if (print_size)
1022 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1023 d715f13e 2020-03-19 stsp if (print_indexed)
1024 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1025 d715f13e 2020-03-19 stsp if (print_resolved)
1026 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1027 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1028 892ac3b6 2020-03-18 stsp fflush(stdout);
1030 531c3985 2020-03-18 stsp return NULL;
1033 531c3985 2020-03-18 stsp static const struct got_error *
1034 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1035 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1037 4ba14133 2020-03-20 stsp const struct got_error *err;
1038 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1040 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1042 4ba14133 2020-03-20 stsp return err;
1044 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1045 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1046 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1047 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1049 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1050 4ba14133 2020-03-20 stsp return err;
1053 4ba14133 2020-03-20 stsp static const struct got_error *
1054 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1055 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1057 41b0de12 2020-03-21 stsp const struct got_error *err;
1058 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1060 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1061 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1062 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1064 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1067 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1068 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1069 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1070 41b0de12 2020-03-21 stsp char *id_str;
1072 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1074 41b0de12 2020-03-21 stsp return err;
1075 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1076 41b0de12 2020-03-21 stsp free(id_str);
1079 41b0de12 2020-03-21 stsp return NULL;
1082 6338a6a1 2020-03-21 stsp static const struct got_error *
1083 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1084 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1086 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1087 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1088 6338a6a1 2020-03-21 stsp char *id_str;
1090 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1092 6338a6a1 2020-03-21 stsp return err;
1094 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1096 6338a6a1 2020-03-21 stsp goto done;
1098 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1099 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1101 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1102 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1104 6338a6a1 2020-03-21 stsp free(id_str);
1105 6338a6a1 2020-03-21 stsp return err;
1108 0e4002ca 2020-03-21 stsp static int
1109 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1111 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1113 0e4002ca 2020-03-21 stsp refname += 5;
1116 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1117 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1119 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1121 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1124 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1125 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1127 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1128 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1131 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1132 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1135 0e4002ca 2020-03-21 stsp static int
1136 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1138 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1140 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1141 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1148 41b0de12 2020-03-21 stsp static const struct got_error *
1149 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1150 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1152 0e4002ca 2020-03-21 stsp const struct got_error *err;
1153 0e4002ca 2020-03-21 stsp char *remote_refname;
1155 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1156 0e4002ca 2020-03-21 stsp refname += 5;
1158 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1159 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1160 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1162 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1163 0e4002ca 2020-03-21 stsp free(remote_refname);
1164 7c0b7f42 2020-09-24 stsp return err;
1167 7c0b7f42 2020-09-24 stsp static const struct got_error *
1168 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1169 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1170 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1171 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1172 99495ddb 2021-01-10 stsp struct got_repository *repo)
1174 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1175 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1176 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1177 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1178 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1179 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1180 7c0b7f42 2020-09-24 stsp ssize_t n;
1182 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1183 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1184 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1186 132af4a5 2021-01-05 stsp branchname = pe->path;
1187 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1188 132af4a5 2021-01-05 stsp branchname += 11;
1189 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1190 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1191 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1192 132af4a5 2021-01-05 stsp goto done;
1194 132af4a5 2021-01-05 stsp free(branches);
1195 132af4a5 2021-01-05 stsp branches = s;
1197 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1198 15d3c221 2021-01-05 stsp branchname = default_branch;
1199 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1200 15d3c221 2021-01-05 stsp branchname += 11;
1201 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1202 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1203 132af4a5 2021-01-05 stsp goto done;
1206 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1207 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1208 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1210 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1211 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1212 99495ddb 2021-01-10 stsp branchname += 5;
1213 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1214 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1215 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1216 99495ddb 2021-01-10 stsp goto done;
1218 99495ddb 2021-01-10 stsp free(refs);
1223 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1224 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1225 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1226 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1227 7c0b7f42 2020-09-24 stsp goto done;
1229 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1230 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1231 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1232 7c0b7f42 2020-09-24 stsp goto done;
1234 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1235 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1236 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1237 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1239 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1245 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1246 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1247 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1248 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1249 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1250 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1251 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1252 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1253 7c0b7f42 2020-09-24 stsp goto done;
1255 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1256 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1257 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1258 7c0b7f42 2020-09-24 stsp goto done;
1262 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1263 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1264 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1265 132af4a5 2021-01-05 stsp free(branches);
1266 7c0b7f42 2020-09-24 stsp return err;
1269 7c0b7f42 2020-09-24 stsp static const struct got_error *
1270 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1271 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1272 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1273 99495ddb 2021-01-10 stsp struct got_repository *repo)
1275 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1276 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1277 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1278 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1279 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1280 132af4a5 2021-01-05 stsp const char *branchname, *mirror = NULL;
1281 7c0b7f42 2020-09-24 stsp ssize_t n;
1283 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1284 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1285 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1286 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1287 7c0b7f42 2020-09-24 stsp goto done;
1289 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1290 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1291 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1292 7c0b7f42 2020-09-24 stsp goto done;
1294 7c0b7f42 2020-09-24 stsp if (mirror_references) {
1295 132af4a5 2021-01-05 stsp mirror = "\tmirror = true\n";
1296 132af4a5 2021-01-05 stsp branches = strdup("\tfetch = +refs/*:refs/*\n");
1297 132af4a5 2021-01-05 stsp if (branches == NULL) {
1298 132af4a5 2021-01-05 stsp err = got_error_from_errno("strdup");
1299 7c0b7f42 2020-09-24 stsp goto done;
1301 7c0b7f42 2020-09-24 stsp } else if (fetch_all_branches) {
1302 132af4a5 2021-01-05 stsp if (asprintf(&branches,
1303 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1304 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1305 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1306 7c0b7f42 2020-09-24 stsp goto done;
1308 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1309 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1310 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1312 132af4a5 2021-01-05 stsp branchname = pe->path;
1313 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1314 132af4a5 2021-01-05 stsp branchname += 11;
1315 132af4a5 2021-01-05 stsp if (asprintf(&s,
1316 132af4a5 2021-01-05 stsp "%s\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1317 132af4a5 2021-01-05 stsp branches ? branches : "",
1318 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1319 132af4a5 2021-01-05 stsp branchname) == -1) {
1320 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1321 132af4a5 2021-01-05 stsp goto done;
1323 132af4a5 2021-01-05 stsp free(branches);
1324 132af4a5 2021-01-05 stsp branches = s;
1328 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1329 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1331 04d9a9ec 2020-09-24 stsp if (default_branch) {
1332 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1333 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1334 7c0b7f42 2020-09-24 stsp branchname += 11;
1336 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1337 132af4a5 2021-01-05 stsp if (asprintf(&branches,
1338 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1339 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1340 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1341 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1342 7c0b7f42 2020-09-24 stsp goto done;
1345 99495ddb 2021-01-10 stsp if (!mirror_references && !TAILQ_EMPTY(wanted_refs)) {
1346 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1347 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1349 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1350 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1351 99495ddb 2021-01-10 stsp refname += 5;
1352 99495ddb 2021-01-10 stsp if (asprintf(&s,
1353 99495ddb 2021-01-10 stsp "%s\tfetch = +refs/%s:refs/remotes/%s/%s\n",
1354 99495ddb 2021-01-10 stsp refs ? refs : "",
1355 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1356 99495ddb 2021-01-10 stsp refname) == -1) {
1357 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1358 99495ddb 2021-01-10 stsp goto done;
1360 99495ddb 2021-01-10 stsp free(refs);
1365 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1366 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1367 132af4a5 2021-01-05 stsp "\turl = %s\n"
1371 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1372 99495ddb 2021-01-10 stsp refs ? refs : "", mirror ? mirror : "") == -1) {
1373 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1374 132af4a5 2021-01-05 stsp goto done;
1376 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1377 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1378 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1379 7c0b7f42 2020-09-24 stsp goto done;
1382 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1383 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1384 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1385 132af4a5 2021-01-05 stsp free(branches);
1386 0e4002ca 2020-03-21 stsp return err;
1389 0e4002ca 2020-03-21 stsp static const struct got_error *
1390 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1391 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1392 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1393 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1394 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1396 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1397 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1398 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1401 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1402 04d9a9ec 2020-09-24 stsp * one of those.
1404 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1405 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1406 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1408 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1409 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1410 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1411 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1413 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1416 04d9a9ec 2020-09-24 stsp default_branch = target;
1421 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1422 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1423 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1424 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1426 04d9a9ec 2020-09-24 stsp return err;
1428 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1429 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1430 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1433 04d9a9ec 2020-09-24 stsp static const struct got_error *
1434 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1436 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1437 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1438 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1439 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1440 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1441 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1442 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1443 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1444 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1445 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1446 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1447 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1448 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1449 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1450 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1452 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1453 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1454 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1455 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1457 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1458 93658fb9 2020-03-18 stsp switch (ch) {
1460 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1463 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1464 4ba14133 2020-03-20 stsp optarg, NULL);
1465 4ba14133 2020-03-20 stsp if (error)
1466 4ba14133 2020-03-20 stsp return error;
1469 41b0de12 2020-03-21 stsp list_refs_only = 1;
1472 469dd726 2020-03-20 stsp mirror_references = 1;
1475 68999b92 2020-03-18 stsp if (verbosity < 0)
1476 68999b92 2020-03-18 stsp verbosity = 0;
1477 68999b92 2020-03-18 stsp else if (verbosity < 3)
1478 68999b92 2020-03-18 stsp verbosity++;
1481 68999b92 2020-03-18 stsp verbosity = -1;
1484 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1485 0e4002ca 2020-03-21 stsp optarg, NULL);
1486 0e4002ca 2020-03-21 stsp if (error)
1487 0e4002ca 2020-03-21 stsp return error;
1490 93658fb9 2020-03-18 stsp usage_clone();
1494 93658fb9 2020-03-18 stsp argc -= optind;
1495 93658fb9 2020-03-18 stsp argv += optind;
1497 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1498 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1499 41b0de12 2020-03-21 stsp if (list_refs_only) {
1500 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1501 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1502 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1503 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1504 41b0de12 2020-03-21 stsp if (mirror_references)
1505 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1506 41b0de12 2020-03-21 stsp if (verbosity == -1)
1507 ff69268e 2020-12-13 stsp option_conflict('l', 'q');
1508 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1509 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1512 93658fb9 2020-03-18 stsp uri = argv[0];
1514 9df6f38b 2020-03-18 stsp if (argc == 1)
1515 93658fb9 2020-03-18 stsp dirname = NULL;
1516 9df6f38b 2020-03-18 stsp else if (argc == 2)
1517 93658fb9 2020-03-18 stsp dirname = argv[1];
1519 93658fb9 2020-03-18 stsp usage_clone();
1521 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1522 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1523 39c64a6a 2020-03-18 stsp if (error)
1524 09f63084 2020-03-20 stsp goto done;
1526 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1527 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1528 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1529 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1530 09838ffc 2020-03-18 stsp goto done;
1533 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1534 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1535 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1536 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1537 39c64a6a 2020-03-18 stsp err(1, "pledge");
1539 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1540 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1541 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1542 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1543 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1544 39c64a6a 2020-03-18 stsp err(1, "pledge");
1546 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1547 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1548 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1549 39c64a6a 2020-03-18 stsp goto done;
1551 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1552 39c64a6a 2020-03-18 stsp goto done;
1554 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1555 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1556 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1557 bb64b798 2020-03-18 stsp goto done;
1559 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1561 bb64b798 2020-03-18 stsp repo_path = dirname;
1563 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1564 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1565 2751fe64 2020-09-24 stsp if (error &&
1566 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1567 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1568 41b0de12 2020-03-21 stsp goto done;
1569 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1570 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1571 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1572 41b0de12 2020-03-21 stsp goto done;
1576 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1577 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1578 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1579 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1580 ee448f5f 2020-03-18 stsp goto done;
1583 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1584 ee448f5f 2020-03-18 stsp if (error)
1585 ee448f5f 2020-03-18 stsp goto done;
1587 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1588 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1589 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1591 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1592 9c52365f 2020-03-21 stsp server_path, verbosity);
1593 39c64a6a 2020-03-18 stsp if (error)
1594 bb64b798 2020-03-18 stsp goto done;
1596 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1597 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1598 2751fe64 2020-09-24 stsp if (error)
1599 2751fe64 2020-09-24 stsp goto done;
1600 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1601 2751fe64 2020-09-24 stsp if (error)
1602 2751fe64 2020-09-24 stsp goto done;
1605 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1606 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1607 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1608 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1609 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1610 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1611 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1612 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1613 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1614 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1615 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1616 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1617 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1618 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1619 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1620 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1621 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1622 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1623 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1624 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1625 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1626 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1627 39c64a6a 2020-03-18 stsp if (error)
1628 d9b4d0c0 2020-03-18 stsp goto done;
1630 41b0de12 2020-03-21 stsp if (list_refs_only) {
1631 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1632 41b0de12 2020-03-21 stsp goto done;
1635 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1636 39c64a6a 2020-03-18 stsp if (error)
1637 d9b4d0c0 2020-03-18 stsp goto done;
1638 68999b92 2020-03-18 stsp if (verbosity >= 0)
1639 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1640 d9b4d0c0 2020-03-18 stsp free(id_str);
1642 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1643 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1644 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1645 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1646 7ebc0570 2020-03-18 stsp char *remote_refname;
1648 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1649 0e4002ca 2020-03-21 stsp !mirror_references) {
1650 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1651 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1652 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1653 0e4002ca 2020-03-21 stsp if (error)
1654 0e4002ca 2020-03-21 stsp goto done;
1658 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1659 39c64a6a 2020-03-18 stsp if (error)
1660 d9b4d0c0 2020-03-18 stsp goto done;
1662 469dd726 2020-03-20 stsp if (mirror_references)
1665 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1668 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1669 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1670 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1671 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1672 7ebc0570 2020-03-18 stsp goto done;
1674 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1675 f298ae0f 2020-03-25 stsp free(remote_refname);
1676 39c64a6a 2020-03-18 stsp if (error)
1677 d9b4d0c0 2020-03-18 stsp goto done;
1680 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1681 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1682 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1683 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1684 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1685 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1687 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1690 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1691 39c64a6a 2020-03-18 stsp if (error) {
1692 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1693 55330abe 2020-03-20 stsp error = NULL;
1696 d9b4d0c0 2020-03-18 stsp goto done;
1699 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1700 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1701 f298ae0f 2020-03-25 stsp if (error)
1702 f298ae0f 2020-03-25 stsp goto done;
1704 f298ae0f 2020-03-25 stsp if (mirror_references)
1707 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1710 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1711 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1712 f298ae0f 2020-03-25 stsp refname) == -1) {
1713 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1714 f298ae0f 2020-03-25 stsp goto done;
1716 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1717 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1718 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1719 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1720 f298ae0f 2020-03-25 stsp free(remote_refname);
1721 f298ae0f 2020-03-25 stsp goto done;
1723 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1724 f298ae0f 2020-03-25 stsp if (error) {
1725 f298ae0f 2020-03-25 stsp free(remote_refname);
1726 f298ae0f 2020-03-25 stsp free(remote_target);
1727 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1728 f298ae0f 2020-03-25 stsp error = NULL;
1731 f298ae0f 2020-03-25 stsp goto done;
1733 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1734 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1735 f298ae0f 2020-03-25 stsp free(remote_refname);
1736 f298ae0f 2020-03-25 stsp free(remote_target);
1737 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1738 39c64a6a 2020-03-18 stsp if (error)
1739 d9b4d0c0 2020-03-18 stsp goto done;
1741 4ba14133 2020-03-20 stsp if (pe == NULL) {
1743 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1744 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1745 4ba14133 2020-03-20 stsp * which could be fetched instead.
1747 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1748 4ba14133 2020-03-20 stsp const char *target = pe->path;
1749 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1751 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1752 4ba14133 2020-03-20 stsp if (error) {
1753 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1754 4ba14133 2020-03-20 stsp error = NULL;
1757 4ba14133 2020-03-20 stsp goto done;
1760 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1761 04d9a9ec 2020-09-24 stsp verbosity, repo);
1762 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1763 4ba14133 2020-03-20 stsp if (error)
1764 4ba14133 2020-03-20 stsp goto done;
1769 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1770 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1771 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1773 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1774 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1775 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1776 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1777 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1779 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1780 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1782 bb64b798 2020-03-18 stsp got_repo_close(repo);
1783 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1784 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1785 d9b4d0c0 2020-03-18 stsp free(pe->data);
1787 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1788 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1789 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1790 d9b4d0c0 2020-03-18 stsp free(pe->data);
1792 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1793 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1794 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1795 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1796 09838ffc 2020-03-18 stsp free(proto);
1797 09838ffc 2020-03-18 stsp free(host);
1798 09838ffc 2020-03-18 stsp free(port);
1799 09838ffc 2020-03-18 stsp free(server_path);
1800 09838ffc 2020-03-18 stsp free(repo_name);
1801 bb64b798 2020-03-18 stsp free(default_destdir);
1802 b46f3e71 2020-03-18 stsp free(git_url);
1803 39c64a6a 2020-03-18 stsp return error;
1806 7848a0e1 2020-03-19 stsp static const struct got_error *
1807 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1808 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1810 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1811 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1812 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1814 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1816 7848a0e1 2020-03-19 stsp goto done;
1818 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1819 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1820 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1822 88609724 2020-03-21 stsp goto done;
1823 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1824 88609724 2020-03-21 stsp goto done;
1825 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1826 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1827 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1829 db6d8ad8 2020-03-21 stsp goto done;
1832 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1833 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1834 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1835 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1836 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1838 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1840 7848a0e1 2020-03-19 stsp goto done;
1841 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1843 e8a967e0 2020-03-21 stsp goto done;
1845 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1847 7848a0e1 2020-03-19 stsp goto done;
1848 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1849 6338a6a1 2020-03-21 stsp goto done;
1851 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1853 6338a6a1 2020-03-21 stsp goto done;
1854 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1856 6338a6a1 2020-03-21 stsp goto done;
1859 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1860 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1861 6338a6a1 2020-03-21 stsp new_id_str);
1863 7848a0e1 2020-03-19 stsp free(old_id);
1864 7848a0e1 2020-03-19 stsp free(new_id_str);
1865 7848a0e1 2020-03-19 stsp return err;
1868 f1bcca34 2020-03-25 stsp static const struct got_error *
1869 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1870 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1872 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1873 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1874 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1876 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1877 bcf34b0e 2020-03-26 stsp if (err) {
1878 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1879 bcf34b0e 2020-03-26 stsp return err;
1880 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1882 bcf34b0e 2020-03-26 stsp goto done;
1884 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1886 bcf34b0e 2020-03-26 stsp goto done;
1888 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1889 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1890 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1891 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1893 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1895 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1896 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1897 bcf34b0e 2020-03-26 stsp goto done;
1899 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1900 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1902 bcf34b0e 2020-03-26 stsp goto done;
1904 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1906 bcf34b0e 2020-03-26 stsp goto done;
1908 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1909 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1910 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1914 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1915 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1916 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1917 bcf34b0e 2020-03-26 stsp err = unlock_err;
1919 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1920 f1bcca34 2020-03-25 stsp return err;
1923 2ab43947 2020-03-18 stsp __dead static void
1924 7848a0e1 2020-03-19 stsp usage_fetch(void)
1926 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1927 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1928 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1929 13f12b09 2020-03-21 stsp getprogname());
1933 7848a0e1 2020-03-19 stsp static const struct got_error *
1934 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1935 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1937 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1938 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1939 3789fd73 2020-03-26 stsp char *id_str = NULL;
1941 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1942 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1944 3789fd73 2020-03-26 stsp return err;
1945 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1946 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1947 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1948 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1951 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1953 3789fd73 2020-03-26 stsp return err;
1954 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1956 3789fd73 2020-03-26 stsp goto done;
1958 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1960 3789fd73 2020-03-26 stsp goto done;
1961 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1962 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1963 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1968 3789fd73 2020-03-26 stsp free(id_str);
1969 3789fd73 2020-03-26 stsp return NULL;
1972 3789fd73 2020-03-26 stsp static const struct got_error *
1973 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1974 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1975 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1976 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1978 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1979 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1980 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1981 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1982 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1983 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1985 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
1987 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1989 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1991 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1993 3789fd73 2020-03-26 stsp goto done;
1995 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
1996 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1998 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1999 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2000 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2001 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2002 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2004 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2005 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2006 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2007 3789fd73 2020-03-26 stsp goto done;
2009 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2013 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2014 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2017 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2020 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2021 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2024 3789fd73 2020-03-26 stsp if (pe != NULL)
2027 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2031 3789fd73 2020-03-26 stsp if (local_refname) {
2032 3789fd73 2020-03-26 stsp struct got_reference *ref;
2033 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2034 3789fd73 2020-03-26 stsp if (err) {
2035 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2037 3789fd73 2020-03-26 stsp free(local_refname);
2038 3789fd73 2020-03-26 stsp local_refname = NULL;
2041 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2044 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2045 3789fd73 2020-03-26 stsp got_ref_close(ref);
2046 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2047 3789fd73 2020-03-26 stsp err = unlock_err;
2051 3789fd73 2020-03-26 stsp free(local_refname);
2052 3789fd73 2020-03-26 stsp local_refname = NULL;
2056 3789fd73 2020-03-26 stsp free(remote_namespace);
2057 3789fd73 2020-03-26 stsp free(local_refname);
2058 0e4002ca 2020-03-21 stsp return err;
2061 0e4002ca 2020-03-21 stsp static const struct got_error *
2062 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2063 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2065 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2066 0e4002ca 2020-03-21 stsp char *remote_refname;
2067 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2069 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2070 0e4002ca 2020-03-21 stsp refname += 5;
2072 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2073 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2074 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2076 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2077 0e4002ca 2020-03-21 stsp if (err) {
2078 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2079 0e4002ca 2020-03-21 stsp goto done;
2080 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2082 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2083 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2084 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2085 9f142382 2020-03-21 stsp err = unlock_err;
2086 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2089 0e4002ca 2020-03-21 stsp free(remote_refname);
2090 f21ec2f0 2020-03-21 stsp return err;
2093 f21ec2f0 2020-03-21 stsp static const struct got_error *
2094 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2096 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2097 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2098 7848a0e1 2020-03-19 stsp const char *remote_name;
2099 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2100 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2101 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2102 7848a0e1 2020-03-19 stsp int nremotes;
2103 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2104 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2105 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2106 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2107 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2108 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2109 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2110 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2111 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2112 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2113 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2114 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
2116 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2117 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2118 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2119 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2121 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2122 7848a0e1 2020-03-19 stsp switch (ch) {
2124 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2127 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2128 4ba14133 2020-03-20 stsp optarg, NULL);
2129 4ba14133 2020-03-20 stsp if (error)
2130 4ba14133 2020-03-20 stsp return error;
2133 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2136 41b0de12 2020-03-21 stsp list_refs_only = 1;
2139 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2140 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2141 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2143 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2146 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2149 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2150 7848a0e1 2020-03-19 stsp verbosity = 0;
2151 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2152 7848a0e1 2020-03-19 stsp verbosity++;
2155 7848a0e1 2020-03-19 stsp verbosity = -1;
2158 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2159 0e4002ca 2020-03-21 stsp optarg, NULL);
2160 0e4002ca 2020-03-21 stsp if (error)
2161 0e4002ca 2020-03-21 stsp return error;
2164 7848a0e1 2020-03-19 stsp usage_fetch();
2168 7848a0e1 2020-03-19 stsp argc -= optind;
2169 7848a0e1 2020-03-19 stsp argv += optind;
2171 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2172 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2173 41b0de12 2020-03-21 stsp if (list_refs_only) {
2174 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2175 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2176 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2177 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2178 f21ec2f0 2020-03-21 stsp if (delete_refs)
2179 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2182 7848a0e1 2020-03-19 stsp if (argc == 0)
2183 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2184 7848a0e1 2020-03-19 stsp else if (argc == 1)
2185 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2187 7848a0e1 2020-03-19 stsp usage_fetch();
2189 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2190 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2191 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2192 7848a0e1 2020-03-19 stsp goto done;
2195 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2196 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2197 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2198 7848a0e1 2020-03-19 stsp goto done;
2200 7848a0e1 2020-03-19 stsp error = NULL;
2201 7848a0e1 2020-03-19 stsp if (worktree) {
2202 7848a0e1 2020-03-19 stsp repo_path =
2203 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2204 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2205 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2206 7848a0e1 2020-03-19 stsp if (error)
2207 7848a0e1 2020-03-19 stsp goto done;
2209 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2210 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2211 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2212 7848a0e1 2020-03-19 stsp goto done;
2217 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2218 7848a0e1 2020-03-19 stsp if (error)
2219 7848a0e1 2020-03-19 stsp goto done;
2221 50b0790e 2020-09-11 stsp if (worktree) {
2222 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2223 50b0790e 2020-09-11 stsp if (worktree_conf) {
2224 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2225 50b0790e 2020-09-11 stsp worktree_conf);
2226 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2227 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2228 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2234 50b0790e 2020-09-11 stsp if (remote == NULL) {
2235 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2236 50b0790e 2020-09-11 stsp if (repo_conf) {
2237 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2238 50b0790e 2020-09-11 stsp repo_conf);
2239 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2240 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2241 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2247 50b0790e 2020-09-11 stsp if (remote == NULL) {
2248 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2249 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2250 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2251 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2256 50b0790e 2020-09-11 stsp if (remote == NULL) {
2257 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2258 50b0790e 2020-09-11 stsp goto done;
2261 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2262 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2263 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2264 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2265 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2266 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2269 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2270 99495ddb 2021-01-10 stsp for (i = 0; i < remote->nrefs; i++) {
2271 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2272 99495ddb 2021-01-10 stsp remote->refs[i], NULL);
2276 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2277 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2278 7848a0e1 2020-03-19 stsp if (error)
2279 7848a0e1 2020-03-19 stsp goto done;
2281 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2282 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2283 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2284 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2285 7848a0e1 2020-03-19 stsp err(1, "pledge");
2287 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2288 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2289 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2290 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2291 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2292 7848a0e1 2020-03-19 stsp err(1, "pledge");
2294 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2295 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2296 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2297 7848a0e1 2020-03-19 stsp goto done;
2299 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2300 7848a0e1 2020-03-19 stsp goto done;
2303 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2304 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2305 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2306 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2307 7848a0e1 2020-03-19 stsp goto done;
2310 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2311 7848a0e1 2020-03-19 stsp if (error)
2312 7848a0e1 2020-03-19 stsp goto done;
2314 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2315 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2316 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2318 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2319 9c52365f 2020-03-21 stsp server_path, verbosity);
2320 7848a0e1 2020-03-19 stsp if (error)
2321 7848a0e1 2020-03-19 stsp goto done;
2323 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2324 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2325 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2326 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2327 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2328 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2329 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2330 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2331 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2332 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2333 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2334 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2335 7848a0e1 2020-03-19 stsp if (error)
2336 7848a0e1 2020-03-19 stsp goto done;
2338 41b0de12 2020-03-21 stsp if (list_refs_only) {
2339 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2340 41b0de12 2020-03-21 stsp goto done;
2343 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2344 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2345 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2346 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2347 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2348 984065c8 2020-03-19 stsp if (error)
2349 984065c8 2020-03-19 stsp goto done;
2350 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2351 984065c8 2020-03-19 stsp free(id_str);
2352 984065c8 2020-03-19 stsp id_str = NULL;
2355 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2356 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2357 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2358 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2359 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2360 7848a0e1 2020-03-19 stsp char *remote_refname;
2362 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2363 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2364 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2365 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2366 0e4002ca 2020-03-21 stsp if (error)
2367 0e4002ca 2020-03-21 stsp goto done;
2371 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2372 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2373 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2374 7848a0e1 2020-03-19 stsp if (error) {
2375 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2376 7848a0e1 2020-03-19 stsp goto done;
2377 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2379 7848a0e1 2020-03-19 stsp if (error)
2380 7848a0e1 2020-03-19 stsp goto done;
2382 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2383 db6d8ad8 2020-03-21 stsp verbosity, repo);
2384 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2385 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2386 9f142382 2020-03-21 stsp error = unlock_err;
2387 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2388 7848a0e1 2020-03-19 stsp if (error)
2389 7848a0e1 2020-03-19 stsp goto done;
2391 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2392 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2393 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2394 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2395 7848a0e1 2020-03-19 stsp goto done;
2398 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2399 7848a0e1 2020-03-19 stsp if (error) {
2400 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2401 7848a0e1 2020-03-19 stsp goto done;
2402 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2403 688f11b3 2020-03-21 stsp verbosity, repo);
2404 7848a0e1 2020-03-19 stsp if (error)
2405 7848a0e1 2020-03-19 stsp goto done;
2407 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2408 db6d8ad8 2020-03-21 stsp verbosity, repo);
2409 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2410 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2411 9f142382 2020-03-21 stsp error = unlock_err;
2412 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2413 7848a0e1 2020-03-19 stsp if (error)
2414 7848a0e1 2020-03-19 stsp goto done;
2417 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2418 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2419 2ec30c80 2020-03-20 stsp if (error) {
2420 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2421 2ec30c80 2020-03-20 stsp goto done;
2422 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2424 2ec30c80 2020-03-20 stsp if (error)
2425 2ec30c80 2020-03-20 stsp goto done;
2427 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2428 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2429 9f142382 2020-03-21 stsp error = unlock_err;
2430 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2434 f1bcca34 2020-03-25 stsp if (delete_refs) {
2435 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2436 3789fd73 2020-03-26 stsp verbosity, repo);
2437 f1bcca34 2020-03-25 stsp if (error)
2438 f1bcca34 2020-03-25 stsp goto done;
2441 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2442 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2443 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2444 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2445 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2446 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2447 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2449 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2452 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2455 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2456 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2457 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2458 f1bcca34 2020-03-25 stsp goto done;
2460 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2461 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2462 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2463 f1bcca34 2020-03-25 stsp free(remote_refname);
2464 f1bcca34 2020-03-25 stsp goto done;
2467 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2469 f1bcca34 2020-03-25 stsp if (error) {
2470 f1bcca34 2020-03-25 stsp free(remote_refname);
2471 f1bcca34 2020-03-25 stsp free(remote_target);
2472 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2473 f1bcca34 2020-03-25 stsp error = NULL;
2476 f1bcca34 2020-03-25 stsp goto done;
2478 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2479 f1bcca34 2020-03-25 stsp verbosity, repo);
2480 f1bcca34 2020-03-25 stsp free(remote_refname);
2481 f1bcca34 2020-03-25 stsp free(remote_target);
2482 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2483 f1bcca34 2020-03-25 stsp if (error)
2484 f1bcca34 2020-03-25 stsp goto done;
2488 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2489 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2490 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2491 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2492 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2494 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2495 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2497 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2498 7848a0e1 2020-03-19 stsp if (worktree)
2499 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2500 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2501 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2502 7848a0e1 2020-03-19 stsp free(pe->data);
2504 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2505 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2506 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2507 7848a0e1 2020-03-19 stsp free(pe->data);
2509 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2510 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2511 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2512 7848a0e1 2020-03-19 stsp free(id_str);
2513 7848a0e1 2020-03-19 stsp free(cwd);
2514 7848a0e1 2020-03-19 stsp free(repo_path);
2515 7848a0e1 2020-03-19 stsp free(pack_hash);
2516 7848a0e1 2020-03-19 stsp free(proto);
2517 7848a0e1 2020-03-19 stsp free(host);
2518 7848a0e1 2020-03-19 stsp free(port);
2519 7848a0e1 2020-03-19 stsp free(server_path);
2520 7848a0e1 2020-03-19 stsp free(repo_name);
2521 7848a0e1 2020-03-19 stsp return error;
2525 7848a0e1 2020-03-19 stsp __dead static void
2526 2ab43947 2020-03-18 stsp usage_checkout(void)
2528 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2529 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2533 2ab43947 2020-03-18 stsp static void
2534 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2536 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2537 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2538 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2539 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2540 2ab43947 2020-03-18 stsp getprogname());
2543 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2544 2ab43947 2020-03-18 stsp const char *worktree_path;
2545 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2548 2ab43947 2020-03-18 stsp static const struct got_error *
2549 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2551 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2553 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2554 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2555 2ab43947 2020-03-18 stsp return NULL;
2557 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2558 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2559 2ab43947 2020-03-18 stsp return NULL;
2562 2ab43947 2020-03-18 stsp while (path[0] == '/')
2565 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2566 2ab43947 2020-03-18 stsp return NULL;
2569 2ab43947 2020-03-18 stsp static const struct got_error *
2570 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2572 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2573 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2574 2ab43947 2020-03-18 stsp return NULL;
2577 2ab43947 2020-03-18 stsp static const struct got_error *
2578 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2579 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2580 2ab43947 2020-03-18 stsp struct got_repository *repo)
2582 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2583 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2585 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2586 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2588 2ab43947 2020-03-18 stsp return err;
2590 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2591 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2594 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2595 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2597 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2599 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2607 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2608 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2609 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2611 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2612 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2613 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2614 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2615 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2616 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2618 2ab43947 2020-03-18 stsp free(yca_id);
2619 2ab43947 2020-03-18 stsp return NULL;
2622 2ab43947 2020-03-18 stsp static const struct got_error *
2623 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2624 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2625 2ab43947 2020-03-18 stsp struct got_repository *repo)
2627 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2628 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2629 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2630 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2632 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2634 2ab43947 2020-03-18 stsp goto done;
2636 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2637 2ab43947 2020-03-18 stsp is_same_branch = 1;
2638 2ab43947 2020-03-18 stsp goto done;
2640 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2641 2ab43947 2020-03-18 stsp is_same_branch = 1;
2642 2ab43947 2020-03-18 stsp goto done;
2645 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2647 2ab43947 2020-03-18 stsp goto done;
2649 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2650 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2652 2ab43947 2020-03-18 stsp goto done;
2654 2ab43947 2020-03-18 stsp for (;;) {
2655 2ab43947 2020-03-18 stsp struct got_object_id *id;
2656 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2657 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2658 2ab43947 2020-03-18 stsp if (err) {
2659 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2660 2ab43947 2020-03-18 stsp err = NULL;
2665 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2667 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2668 2ab43947 2020-03-18 stsp is_same_branch = 1;
2674 2ab43947 2020-03-18 stsp if (graph)
2675 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2676 2ab43947 2020-03-18 stsp free(head_commit_id);
2677 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2678 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2679 2ab43947 2020-03-18 stsp return err;
2682 4ed7e80c 2018-05-20 stsp static const struct got_error *
2683 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2685 4b6c9460 2020-03-05 stsp static char msg[512];
2686 4b6c9460 2020-03-05 stsp const char *branch_name;
2688 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2689 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2691 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2693 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2694 4b6c9460 2020-03-05 stsp branch_name += 11;
2696 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2697 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2698 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2699 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2700 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2701 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2703 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2706 4b6c9460 2020-03-05 stsp static const struct got_error *
2707 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2709 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2710 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2711 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2712 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2713 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2714 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2715 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2716 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2717 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2718 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2719 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2720 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2721 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2723 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2725 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2726 0bb8a95e 2018-03-12 stsp switch (ch) {
2728 08573d5b 2019-05-14 stsp branch_name = optarg;
2731 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2732 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2733 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2736 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2739 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2742 2deda0b9 2019-03-07 stsp usage_checkout();
2743 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2747 0bb8a95e 2018-03-12 stsp argc -= optind;
2748 0bb8a95e 2018-03-12 stsp argv += optind;
2750 6715a751 2018-03-16 stsp #ifndef PROFILE
2751 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2752 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2753 c09a553d 2018-03-12 stsp err(1, "pledge");
2755 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2756 c47340da 2020-09-20 stsp char *base, *dotgit;
2757 f0207f6a 2020-10-19 stsp const char *path;
2758 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2759 76089277 2018-04-01 stsp if (repo_path == NULL)
2760 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2761 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2762 76089277 2018-04-01 stsp if (cwd == NULL) {
2763 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2764 76089277 2018-04-01 stsp goto done;
2766 c47340da 2020-09-20 stsp if (path_prefix[0])
2767 f0207f6a 2020-10-19 stsp path = path_prefix;
2769 f0207f6a 2020-10-19 stsp path = repo_path;
2770 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2771 f0207f6a 2020-10-19 stsp if (error)
2772 c47340da 2020-09-20 stsp goto done;
2773 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2774 c09a553d 2018-03-12 stsp if (dotgit)
2775 c09a553d 2018-03-12 stsp *dotgit = '\0';
2776 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2777 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2778 f0207f6a 2020-10-19 stsp free(base);
2779 76089277 2018-04-01 stsp goto done;
2781 f0207f6a 2020-10-19 stsp free(base);
2782 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2783 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2784 76089277 2018-04-01 stsp if (repo_path == NULL) {
2785 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2786 76089277 2018-04-01 stsp goto done;
2788 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2789 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2790 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2791 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2793 b4b3a7dd 2019-07-22 stsp goto done;
2795 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2796 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2797 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2798 b4b3a7dd 2019-07-22 stsp goto done;
2802 c09a553d 2018-03-12 stsp usage_checkout();
2804 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2805 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2807 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2808 c09a553d 2018-03-12 stsp if (error != NULL)
2809 c02c541e 2019-03-29 stsp goto done;
2811 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2812 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2813 c530dc23 2019-07-23 stsp if (error) {
2814 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2815 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2816 c530dc23 2019-07-23 stsp goto done;
2817 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2818 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2819 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2820 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2821 c530dc23 2019-07-23 stsp goto done;
2825 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2826 c02c541e 2019-03-29 stsp if (error)
2827 c09a553d 2018-03-12 stsp goto done;
2829 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2830 c09a553d 2018-03-12 stsp if (error != NULL)
2831 c09a553d 2018-03-12 stsp goto done;
2833 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2834 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2835 c09a553d 2018-03-12 stsp goto done;
2837 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2838 c09a553d 2018-03-12 stsp if (error != NULL)
2839 c09a553d 2018-03-12 stsp goto done;
2841 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2842 e5dc7198 2018-12-29 stsp path_prefix);
2843 e5dc7198 2018-12-29 stsp if (error != NULL)
2844 e5dc7198 2018-12-29 stsp goto done;
2845 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2846 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2847 49520a32 2018-12-29 stsp goto done;
2850 8069f636 2019-01-12 stsp if (commit_id_str) {
2851 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2852 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2853 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2854 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2856 84de9106 2020-12-26 stsp if (error)
2857 84de9106 2020-12-26 stsp goto done;
2858 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2859 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2860 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2861 30837e32 2019-07-25 stsp if (error)
2862 8069f636 2019-01-12 stsp goto done;
2863 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2864 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2865 8069f636 2019-01-12 stsp if (error != NULL) {
2866 8069f636 2019-01-12 stsp free(commit_id);
2867 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2868 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2869 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2871 8069f636 2019-01-12 stsp goto done;
2873 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2874 4b6c9460 2020-03-05 stsp if (error) {
2875 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2876 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2877 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2879 45d344f6 2019-05-14 stsp goto done;
2881 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2882 8069f636 2019-01-12 stsp commit_id);
2883 8069f636 2019-01-12 stsp free(commit_id);
2884 8069f636 2019-01-12 stsp if (error)
2885 8069f636 2019-01-12 stsp goto done;
2888 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2889 f2ea84fa 2019-07-27 stsp if (error)
2890 f2ea84fa 2019-07-27 stsp goto done;
2891 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2892 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2893 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2894 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2895 c09a553d 2018-03-12 stsp if (error != NULL)
2896 c09a553d 2018-03-12 stsp goto done;
2898 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2899 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2900 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2902 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2903 8069f636 2019-01-12 stsp free(commit_id_str);
2904 76089277 2018-04-01 stsp free(repo_path);
2905 507dc3bb 2018-12-29 stsp free(worktree_path);
2906 c47340da 2020-09-20 stsp free(cwd);
2907 507dc3bb 2018-12-29 stsp return error;
2910 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2911 9627c110 2020-04-18 stsp int did_something;
2912 9627c110 2020-04-18 stsp int conflicts;
2913 9627c110 2020-04-18 stsp int obstructed;
2914 9627c110 2020-04-18 stsp int not_updated;
2918 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2920 9627c110 2020-04-18 stsp if (!upa->did_something)
2923 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2924 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2925 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2926 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2927 9627c110 2020-04-18 stsp upa->obstructed);
2928 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2929 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2930 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2933 507dc3bb 2018-12-29 stsp __dead static void
2934 507dc3bb 2018-12-29 stsp usage_update(void)
2936 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2937 507dc3bb 2018-12-29 stsp getprogname());
2941 1ee397ad 2019-07-12 stsp static const struct got_error *
2942 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2944 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2946 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2947 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2948 1ee397ad 2019-07-12 stsp return NULL;
2950 9627c110 2020-04-18 stsp upa->did_something = 1;
2952 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2953 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2954 1ee397ad 2019-07-12 stsp return NULL;
2956 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2957 9627c110 2020-04-18 stsp upa->conflicts++;
2958 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2959 9627c110 2020-04-18 stsp upa->obstructed++;
2960 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2961 9627c110 2020-04-18 stsp upa->not_updated++;
2963 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2965 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2966 1ee397ad 2019-07-12 stsp return NULL;
2969 be7061eb 2018-12-30 stsp static const struct got_error *
2970 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2971 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2972 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2974 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2975 a1fb16d8 2019-05-24 stsp char *base_id_str;
2976 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2978 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2979 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2980 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2981 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2982 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2983 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2984 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2987 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2988 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2989 a1fb16d8 2019-05-24 stsp if (err) {
2990 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2991 a1fb16d8 2019-05-24 stsp return err;
2992 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2994 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2995 a1fb16d8 2019-05-24 stsp return NULL;
2997 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2998 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2999 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3001 a1fb16d8 2019-05-24 stsp return err;
3002 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3003 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3004 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3005 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3006 0ebf8283 2019-07-24 stsp return NULL;
3009 0ebf8283 2019-07-24 stsp static const struct got_error *
3010 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3012 0ebf8283 2019-07-24 stsp const struct got_error *err;
3013 0ebf8283 2019-07-24 stsp int in_progress;
3015 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3017 0ebf8283 2019-07-24 stsp return err;
3018 0ebf8283 2019-07-24 stsp if (in_progress)
3019 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3021 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3023 0ebf8283 2019-07-24 stsp return err;
3024 0ebf8283 2019-07-24 stsp if (in_progress)
3025 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3027 a1fb16d8 2019-05-24 stsp return NULL;
3030 a5edda0a 2019-07-27 stsp static const struct got_error *
3031 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3032 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3034 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3035 a5edda0a 2019-07-27 stsp char *path;
3038 a5edda0a 2019-07-27 stsp if (argc == 0) {
3039 a5edda0a 2019-07-27 stsp path = strdup("");
3040 a5edda0a 2019-07-27 stsp if (path == NULL)
3041 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3042 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3045 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3046 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3049 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3050 a5edda0a 2019-07-27 stsp if (err) {
3051 a5edda0a 2019-07-27 stsp free(path);
3056 a5edda0a 2019-07-27 stsp return err;
3059 a1fb16d8 2019-05-24 stsp static const struct got_error *
3060 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3061 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3063 fa51e947 2020-03-27 stsp const struct got_error *err;
3064 fa51e947 2020-03-27 stsp struct got_repository *repo;
3065 fa51e947 2020-03-27 stsp static char msg[512];
3067 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3069 fa51e947 2020-03-27 stsp return orig_err;
3071 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3072 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3073 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3074 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3075 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3076 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3077 fa51e947 2020-03-27 stsp got_repo_close(repo);
3078 fa51e947 2020-03-27 stsp return err;
3081 fa51e947 2020-03-27 stsp static const struct got_error *
3082 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3084 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3085 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3086 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3087 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3088 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3089 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3090 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3091 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3092 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3093 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3095 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3097 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3099 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3100 507dc3bb 2018-12-29 stsp switch (ch) {
3102 024e9686 2019-05-14 stsp branch_name = optarg;
3105 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3106 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3107 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3110 2deda0b9 2019-03-07 stsp usage_update();
3111 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3115 507dc3bb 2018-12-29 stsp argc -= optind;
3116 507dc3bb 2018-12-29 stsp argv += optind;
3118 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3119 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3120 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3121 507dc3bb 2018-12-29 stsp err(1, "pledge");
3123 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3124 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3125 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3126 c4cdcb68 2019-04-03 stsp goto done;
3128 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3129 fa51e947 2020-03-27 stsp if (error) {
3130 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3131 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3132 fa51e947 2020-03-27 stsp worktree_path);
3133 7d5807f4 2019-07-11 stsp goto done;
3136 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3137 f2ea84fa 2019-07-27 stsp if (error)
3138 f2ea84fa 2019-07-27 stsp goto done;
3140 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3142 507dc3bb 2018-12-29 stsp if (error != NULL)
3143 507dc3bb 2018-12-29 stsp goto done;
3145 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3146 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3147 087fb88c 2019-08-04 stsp if (error)
3148 087fb88c 2019-08-04 stsp goto done;
3150 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3151 0266afb7 2019-01-04 stsp if (error)
3152 0266afb7 2019-01-04 stsp goto done;
3154 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3155 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3156 024e9686 2019-05-14 stsp if (error != NULL)
3157 024e9686 2019-05-14 stsp goto done;
3158 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3159 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3160 9c4b8182 2019-01-02 stsp if (error != NULL)
3161 9c4b8182 2019-01-02 stsp goto done;
3162 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3163 507dc3bb 2018-12-29 stsp if (error != NULL)
3164 507dc3bb 2018-12-29 stsp goto done;
3166 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3167 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3168 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3170 84de9106 2020-12-26 stsp if (error)
3171 84de9106 2020-12-26 stsp goto done;
3172 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3173 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3174 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3175 04f57cb3 2019-07-25 stsp free(commit_id_str);
3176 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3177 30837e32 2019-07-25 stsp if (error)
3178 a297e751 2019-07-11 stsp goto done;
3179 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3180 a297e751 2019-07-11 stsp if (error)
3181 507dc3bb 2018-12-29 stsp goto done;
3184 a1fb16d8 2019-05-24 stsp if (branch_name) {
3185 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3186 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3187 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3189 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3190 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3191 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3192 024e9686 2019-05-14 stsp goto done;
3194 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3195 024e9686 2019-05-14 stsp if (error)
3196 024e9686 2019-05-14 stsp goto done;
3197 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3199 a367ff0f 2019-05-14 stsp free(head_commit_id);
3200 024e9686 2019-05-14 stsp if (error != NULL)
3201 024e9686 2019-05-14 stsp goto done;
3202 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3203 a367ff0f 2019-05-14 stsp if (error)
3204 a367ff0f 2019-05-14 stsp goto done;
3205 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3206 024e9686 2019-05-14 stsp if (error)
3207 024e9686 2019-05-14 stsp goto done;
3209 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3210 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3211 a367ff0f 2019-05-14 stsp if (error != NULL) {
3212 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3213 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3214 024e9686 2019-05-14 stsp goto done;
3216 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3217 a367ff0f 2019-05-14 stsp if (error)
3218 a367ff0f 2019-05-14 stsp goto done;
3221 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3222 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3223 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3224 507dc3bb 2018-12-29 stsp commit_id);
3225 507dc3bb 2018-12-29 stsp if (error)
3226 507dc3bb 2018-12-29 stsp goto done;
3229 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3230 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3231 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3232 507dc3bb 2018-12-29 stsp if (error != NULL)
3233 507dc3bb 2018-12-29 stsp goto done;
3235 9627c110 2020-04-18 stsp if (upa.did_something)
3236 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3238 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3239 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3241 c09a553d 2018-03-12 stsp free(worktree_path);
3242 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3243 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3244 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3245 507dc3bb 2018-12-29 stsp free(commit_id);
3246 9c4b8182 2019-01-02 stsp free(commit_id_str);
3247 c09a553d 2018-03-12 stsp return error;
3250 f42b1b34 2018-03-12 stsp static const struct got_error *
3251 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3252 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3253 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3255 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3256 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3258 44392932 2019-08-25 stsp if (blob_id1) {
3259 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3261 44392932 2019-08-25 stsp goto done;
3264 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3266 44392932 2019-08-25 stsp goto done;
3268 44392932 2019-08-25 stsp while (path[0] == '/')
3270 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3271 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3273 44392932 2019-08-25 stsp if (blob1)
3274 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3275 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3276 44392932 2019-08-25 stsp return err;
3279 44392932 2019-08-25 stsp static const struct got_error *
3280 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3281 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3282 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3284 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3285 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3286 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3288 44392932 2019-08-25 stsp if (tree_id1) {
3289 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3291 44392932 2019-08-25 stsp goto done;
3294 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3296 0f2b3dca 2018-12-22 stsp goto done;
3298 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3299 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3300 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3301 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3302 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3303 fe621944 2020-11-10 stsp arg.nlines = 0;
3304 44392932 2019-08-25 stsp while (path[0] == '/')
3306 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3307 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3309 79109fed 2018-03-27 stsp if (tree1)
3310 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3311 366e0a5f 2019-10-10 stsp if (tree2)
3312 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3313 44392932 2019-08-25 stsp return err;
3316 44392932 2019-08-25 stsp static const struct got_error *
3317 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3318 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3320 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3321 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3322 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3323 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3325 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3326 0208f208 2020-05-05 stsp if (qid != NULL) {
3327 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3328 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3331 0208f208 2020-05-05 stsp return err;
3333 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3334 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3338 0208f208 2020-05-05 stsp if (tree_id1) {
3339 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3341 0208f208 2020-05-05 stsp goto done;
3344 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3345 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3347 0208f208 2020-05-05 stsp goto done;
3349 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3350 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3352 0208f208 2020-05-05 stsp if (tree1)
3353 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3354 0208f208 2020-05-05 stsp if (tree2)
3355 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3356 0208f208 2020-05-05 stsp return err;
3359 0208f208 2020-05-05 stsp static const struct got_error *
3360 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3361 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3363 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3364 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3365 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3366 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3367 44392932 2019-08-25 stsp struct got_object_qid *qid;
3369 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3370 44392932 2019-08-25 stsp if (qid != NULL) {
3371 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3374 44392932 2019-08-25 stsp return err;
3377 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3378 44392932 2019-08-25 stsp int obj_type;
3379 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3381 44392932 2019-08-25 stsp goto done;
3382 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3383 44392932 2019-08-25 stsp if (err) {
3384 44392932 2019-08-25 stsp free(obj_id2);
3385 44392932 2019-08-25 stsp goto done;
3387 44392932 2019-08-25 stsp if (pcommit) {
3388 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3389 44392932 2019-08-25 stsp qid->id, path);
3390 44392932 2019-08-25 stsp if (err) {
3391 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3392 2e8c69d1 2020-05-04 stsp free(obj_id2);
3393 2e8c69d1 2020-05-04 stsp goto done;
3396 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3397 2e8c69d1 2020-05-04 stsp if (err) {
3398 2e8c69d1 2020-05-04 stsp free(obj_id2);
3399 2e8c69d1 2020-05-04 stsp goto done;
3403 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3404 44392932 2019-08-25 stsp if (err) {
3405 44392932 2019-08-25 stsp free(obj_id2);
3406 44392932 2019-08-25 stsp goto done;
3408 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3409 44392932 2019-08-25 stsp switch (obj_type) {
3410 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3411 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3412 64453f7e 2020-11-21 stsp 0, 0, repo);
3414 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3415 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3416 64453f7e 2020-11-21 stsp 0, 0, repo);
3419 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3422 44392932 2019-08-25 stsp free(obj_id1);
3423 44392932 2019-08-25 stsp free(obj_id2);
3425 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3426 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3428 44392932 2019-08-25 stsp goto done;
3429 579bd556 2020-10-24 stsp if (pcommit) {
3430 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3431 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3433 579bd556 2020-10-24 stsp goto done;
3435 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3437 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3441 0f2b3dca 2018-12-22 stsp free(id_str1);
3442 0f2b3dca 2018-12-22 stsp free(id_str2);
3443 44392932 2019-08-25 stsp if (pcommit)
3444 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3445 79109fed 2018-03-27 stsp return err;
3448 4bb494d5 2018-06-16 stsp static char *
3449 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3451 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3452 09867e48 2019-08-13 stsp char *p, *s;
3454 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3455 09867e48 2019-08-13 stsp if (tm == NULL)
3456 09867e48 2019-08-13 stsp return NULL;
3457 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3458 09867e48 2019-08-13 stsp if (s == NULL)
3459 09867e48 2019-08-13 stsp return NULL;
3460 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3462 6c281f94 2018-06-11 stsp *p = '\0';
3466 6841bf13 2019-11-29 kn static const struct got_error *
3467 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3468 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3470 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3471 6841bf13 2019-11-29 kn regmatch_t regmatch;
3472 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3474 6841bf13 2019-11-29 kn *have_match = 0;
3476 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3480 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3484 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3485 6841bf13 2019-11-29 kn *have_match = 1;
3487 6841bf13 2019-11-29 kn free(id_str);
3488 6841bf13 2019-11-29 kn free(logmsg);
3492 0208f208 2020-05-05 stsp static void
3493 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3494 0208f208 2020-05-05 stsp regex_t *regex)
3496 0208f208 2020-05-05 stsp regmatch_t regmatch;
3497 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3499 0208f208 2020-05-05 stsp *have_match = 0;
3501 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3502 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3503 0208f208 2020-05-05 stsp *have_match = 1;
3509 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3511 888b7d99 2020-12-26 stsp static const struct got_error*
3512 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3513 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3515 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3516 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3518 888b7d99 2020-12-26 stsp const char *name;
3520 888b7d99 2020-12-26 stsp *refs_str = NULL;
3522 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3523 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3524 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3527 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3528 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3530 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3531 199a4027 2019-02-02 stsp name += 5;
3532 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3534 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3535 e34f9ed6 2019-02-02 stsp name += 6;
3536 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3537 141c2bff 2019-02-04 stsp name += 8;
3538 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3539 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3542 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3545 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3546 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3547 5d844a1e 2019-08-13 stsp if (err) {
3548 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3549 48cae60d 2020-09-22 stsp free(ref_id);
3552 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3553 5d844a1e 2019-08-13 stsp err = NULL;
3554 5d844a1e 2019-08-13 stsp tag = NULL;
3557 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3558 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3559 48cae60d 2020-09-22 stsp free(ref_id);
3561 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3562 a436ad14 2019-08-13 stsp if (cmp != 0)
3564 888b7d99 2020-12-26 stsp s = *refs_str;
3565 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3566 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3567 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3569 888b7d99 2020-12-26 stsp *refs_str = NULL;
3575 888b7d99 2020-12-26 stsp return err;
3578 888b7d99 2020-12-26 stsp static const struct got_error *
3579 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3580 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3581 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3582 888b7d99 2020-12-26 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap)
3584 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3585 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3586 888b7d99 2020-12-26 stsp char datebuf[26];
3587 888b7d99 2020-12-26 stsp time_t committer_time;
3588 888b7d99 2020-12-26 stsp const char *author, *committer;
3589 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3590 888b7d99 2020-12-26 stsp struct got_reflist_head *refs;
3592 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3594 8bf5b3c9 2018-03-17 stsp return err;
3596 888b7d99 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3597 888b7d99 2020-12-26 stsp if (refs) {
3598 888b7d99 2020-12-26 stsp err = build_refs_str(&refs_str, refs, id, repo);
3600 888b7d99 2020-12-26 stsp goto done;
3603 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3604 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3605 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3606 832c249c 2018-06-10 stsp free(id_str);
3607 d3d493d7 2019-02-21 stsp id_str = NULL;
3608 d3d493d7 2019-02-21 stsp free(refs_str);
3609 d3d493d7 2019-02-21 stsp refs_str = NULL;
3610 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3611 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3612 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3613 09867e48 2019-08-13 stsp if (datestr)
3614 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3615 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3616 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3617 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3618 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3619 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3620 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3621 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3622 3fe1abad 2018-06-10 stsp int n = 1;
3623 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3624 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3625 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3627 888b7d99 2020-12-26 stsp goto done;
3628 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3629 a0603db2 2018-06-10 stsp free(id_str);
3630 888b7d99 2020-12-26 stsp id_str = NULL;
3634 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3636 888b7d99 2020-12-26 stsp goto done;
3638 621015ac 2018-07-23 stsp logmsg = logmsg0;
3640 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3642 832c249c 2018-06-10 stsp printf(" %s\n", line);
3643 832c249c 2018-06-10 stsp } while (line);
3644 621015ac 2018-07-23 stsp free(logmsg0);
3646 0208f208 2020-05-05 stsp if (changed_paths) {
3647 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3648 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3649 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3650 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3652 0208f208 2020-05-05 stsp printf("\n");
3654 971751ac 2018-03-27 stsp if (show_patch) {
3655 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3656 971751ac 2018-03-27 stsp if (err == 0)
3657 971751ac 2018-03-27 stsp printf("\n");
3660 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3661 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3663 888b7d99 2020-12-26 stsp free(id_str);
3664 888b7d99 2020-12-26 stsp free(refs_str);
3665 07862c20 2018-09-15 stsp return err;
3668 f42b1b34 2018-03-12 stsp static const struct got_error *
3669 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3670 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3671 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3672 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3673 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3675 f42b1b34 2018-03-12 stsp const struct got_error *err;
3676 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3677 6841bf13 2019-11-29 kn regex_t regex;
3678 6841bf13 2019-11-29 kn int have_match;
3679 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3680 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3681 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3682 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3683 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3685 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3686 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3688 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3689 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3690 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3692 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3694 f42b1b34 2018-03-12 stsp return err;
3695 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3696 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3698 fcc85cad 2018-07-22 stsp goto done;
3700 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3702 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3705 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3706 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3707 372ccdbb 2018-06-10 stsp if (err) {
3708 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3709 9ba79e04 2018-06-11 stsp err = NULL;
3712 b43fbaa0 2018-06-11 stsp if (id == NULL)
3715 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3719 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3720 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3725 6841bf13 2019-11-29 kn if (search_pattern) {
3726 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3728 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3731 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3732 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3733 0208f208 2020-05-05 stsp &changed_paths, ®ex);
3734 6841bf13 2019-11-29 kn if (have_match == 0) {
3735 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3736 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3737 0208f208 2020-05-05 stsp free((char *)pe->path);
3738 0208f208 2020-05-05 stsp free(pe->data);
3740 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3745 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3746 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3749 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3750 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3752 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3753 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3754 888b7d99 2020-12-26 stsp show_patch, diff_context, refs_idmap);
3755 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3759 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3760 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3763 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3764 0208f208 2020-05-05 stsp free((char *)pe->path);
3765 0208f208 2020-05-05 stsp free(pe->data);
3767 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3769 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3770 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3771 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3774 502b9684 2020-07-31 stsp if (show_changed_paths) {
3775 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3776 502b9684 2020-07-31 stsp commit, repo);
3780 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3781 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3782 888b7d99 2020-12-26 stsp show_patch, diff_context, refs_idmap);
3783 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3786 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3787 502b9684 2020-07-31 stsp free((char *)pe->path);
3788 502b9684 2020-07-31 stsp free(pe->data);
3790 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3794 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3795 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3796 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3797 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3799 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3800 0208f208 2020-05-05 stsp free((char *)pe->path);
3801 0208f208 2020-05-05 stsp free(pe->data);
3803 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3804 6841bf13 2019-11-29 kn if (search_pattern)
3805 6841bf13 2019-11-29 kn regfree(®ex);
3806 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3807 f42b1b34 2018-03-12 stsp return err;
3810 4ed7e80c 2018-05-20 stsp __dead static void
3811 6f3d1eb0 2018-03-12 stsp usage_log(void)
3813 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3814 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3815 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3819 b1ebc001 2019-08-13 stsp static int
3820 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3822 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3823 b1ebc001 2019-08-13 stsp long long n;
3824 b1ebc001 2019-08-13 stsp const char *errstr;
3826 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3827 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3829 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3830 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3835 4ed7e80c 2018-05-20 stsp static const struct got_error *
3836 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3838 f42b1b34 2018-03-12 stsp const struct got_error *error;
3839 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3840 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3841 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3842 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3843 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3844 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3845 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3846 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3847 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3848 64a96a6d 2018-04-01 stsp const char *errstr;
3849 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3850 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3852 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3854 6715a751 2018-03-16 stsp #ifndef PROFILE
3855 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3858 f42b1b34 2018-03-12 stsp err(1, "pledge");
3861 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3863 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3864 79109fed 2018-03-27 stsp switch (ch) {
3866 79109fed 2018-03-27 stsp show_patch = 1;
3869 0208f208 2020-05-05 stsp show_changed_paths = 1;
3872 d142fc45 2018-04-01 stsp start_commit = optarg;
3875 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3877 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3878 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3881 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3882 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3883 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3886 48c8c60d 2020-01-27 stsp log_branches = 1;
3889 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3890 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3891 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3893 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3896 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3899 6841bf13 2019-11-29 kn search_pattern = optarg;
3902 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3905 2deda0b9 2019-03-07 stsp usage_log();
3906 79109fed 2018-03-27 stsp /* NOTREACHED */
3910 79109fed 2018-03-27 stsp argc -= optind;
3911 79109fed 2018-03-27 stsp argv += optind;
3913 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3914 dc1edbfa 2019-11-29 kn diff_context = 3;
3915 dc1edbfa 2019-11-29 kn else if (!show_patch)
3916 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3918 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3919 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3920 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3921 04ca23f4 2018-07-16 stsp goto done;
3924 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3925 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3926 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3927 50f2fada 2020-04-24 stsp goto done;
3928 50f2fada 2020-04-24 stsp error = NULL;
3931 603cdeb0 2020-10-22 stsp if (argc == 1) {
3932 e7301579 2019-03-18 stsp if (worktree) {
3933 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3935 e7301579 2019-03-18 stsp if (error)
3936 e7301579 2019-03-18 stsp goto done;
3938 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3939 e7301579 2019-03-18 stsp if (path == NULL) {
3940 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3941 e7301579 2019-03-18 stsp goto done;
3944 603cdeb0 2020-10-22 stsp } else if (argc != 0)
3945 cbd1af7a 2019-03-18 stsp usage_log();
3947 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3948 5486daa2 2019-05-11 stsp repo_path = worktree ?
3949 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3951 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3952 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3953 cffc0aa4 2019-02-05 stsp goto done;
3956 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3957 d7d4f210 2018-03-12 stsp if (error != NULL)
3958 04ca23f4 2018-07-16 stsp goto done;
3960 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3961 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3962 c02c541e 2019-03-29 stsp if (error)
3963 c02c541e 2019-03-29 stsp goto done;
3965 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3966 84de9106 2020-12-26 stsp if (error)
3967 84de9106 2020-12-26 stsp goto done;
3969 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
3970 888b7d99 2020-12-26 stsp if (error)
3971 888b7d99 2020-12-26 stsp goto done;
3973 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3974 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3975 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3976 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3977 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3978 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3979 3235492e 2018-04-01 stsp if (error != NULL)
3980 d1fe46f9 2020-04-18 stsp goto done;
3981 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3982 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3983 3235492e 2018-04-01 stsp if (error != NULL)
3984 d1fe46f9 2020-04-18 stsp goto done;
3985 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3986 d1fe46f9 2020-04-18 stsp start_id);
3987 d1fe46f9 2020-04-18 stsp if (error != NULL)
3988 d1fe46f9 2020-04-18 stsp goto done;
3989 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3991 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
3992 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3993 d1fe46f9 2020-04-18 stsp if (error != NULL)
3994 d1fe46f9 2020-04-18 stsp goto done;
3996 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3997 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
3998 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3999 d1fe46f9 2020-04-18 stsp if (error != NULL)
4000 d1fe46f9 2020-04-18 stsp goto done;
4003 deeabeae 2019-08-27 stsp if (worktree) {
4005 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4006 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4007 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4009 603cdeb0 2020-10-22 stsp if (path) {
4010 603cdeb0 2020-10-22 stsp const char *prefix;
4011 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4012 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4013 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4014 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4015 603cdeb0 2020-10-22 stsp goto done;
4019 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4020 8fa913ec 2020-11-14 stsp path ? path : "");
4021 04ca23f4 2018-07-16 stsp if (error != NULL)
4022 04ca23f4 2018-07-16 stsp goto done;
4023 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4024 04ca23f4 2018-07-16 stsp free(path);
4025 04ca23f4 2018-07-16 stsp path = in_repo_path;
4028 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4029 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4030 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4032 04ca23f4 2018-07-16 stsp free(path);
4033 04ca23f4 2018-07-16 stsp free(repo_path);
4034 04ca23f4 2018-07-16 stsp free(cwd);
4035 cffc0aa4 2019-02-05 stsp if (worktree)
4036 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4037 ad242220 2018-09-08 stsp if (repo) {
4038 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4039 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4040 ad242220 2018-09-08 stsp if (error == NULL)
4041 ad242220 2018-09-08 stsp error = repo_error;
4043 888b7d99 2020-12-26 stsp if (refs_idmap)
4044 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4045 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4046 8bf5b3c9 2018-03-17 stsp return error;
4049 4ed7e80c 2018-05-20 stsp __dead static void
4050 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4052 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4053 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4057 b72f483a 2019-02-05 stsp struct print_diff_arg {
4058 b72f483a 2019-02-05 stsp struct got_repository *repo;
4059 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4060 b72f483a 2019-02-05 stsp int diff_context;
4061 3fc0c068 2019-02-10 stsp const char *id_str;
4062 3fc0c068 2019-02-10 stsp int header_shown;
4063 98eaaa12 2019-08-03 stsp int diff_staged;
4064 63035f9f 2019-10-06 stsp int ignore_whitespace;
4065 64453f7e 2020-11-21 stsp int force_text_diff;
4069 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4070 39449a05 2020-07-23 stsp * it as content to the diff engine.
4072 39449a05 2020-07-23 stsp static const struct got_error *
4073 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4074 39449a05 2020-07-23 stsp const char *abspath)
4076 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4077 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4078 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4082 39449a05 2020-07-23 stsp if (dirfd != -1) {
4083 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4084 39449a05 2020-07-23 stsp if (target_len == -1)
4085 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4087 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4088 39449a05 2020-07-23 stsp if (target_len == -1)
4089 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4092 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4093 39449a05 2020-07-23 stsp if (*fd == -1)
4094 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4096 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4097 39449a05 2020-07-23 stsp if (outlen == -1) {
4098 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4099 39449a05 2020-07-23 stsp goto done;
4102 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4103 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4104 39449a05 2020-07-23 stsp goto done;
4107 39449a05 2020-07-23 stsp if (err) {
4108 39449a05 2020-07-23 stsp close(*fd);
4111 39449a05 2020-07-23 stsp return err;
4114 4ed7e80c 2018-05-20 stsp static const struct got_error *
4115 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4116 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4117 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4118 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4120 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4121 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4122 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4123 12463d8b 2019-12-13 stsp int fd = -1;
4124 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4125 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4126 b72f483a 2019-02-05 stsp struct stat sb;
4128 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4129 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4130 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4131 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4132 98eaaa12 2019-08-03 stsp return NULL;
4134 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4135 98eaaa12 2019-08-03 stsp return NULL;
4136 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4137 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4138 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4139 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4140 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4141 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4142 98eaaa12 2019-08-03 stsp return NULL;
4145 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4146 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4147 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4148 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4149 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4152 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4153 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4154 98eaaa12 2019-08-03 stsp switch (staged_status) {
4155 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4156 98eaaa12 2019-08-03 stsp label1 = path;
4157 98eaaa12 2019-08-03 stsp label2 = path;
4159 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4160 98eaaa12 2019-08-03 stsp label2 = path;
4162 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4163 98eaaa12 2019-08-03 stsp label1 = path;
4166 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4168 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4169 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4170 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4173 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4174 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4175 4ce46740 2019-08-08 stsp char *id_str;
4176 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4179 4ce46740 2019-08-08 stsp goto done;
4180 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4182 4ce46740 2019-08-08 stsp goto done;
4183 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4184 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4185 4ce46740 2019-08-08 stsp free(id_str);
4186 4ce46740 2019-08-08 stsp goto done;
4188 4ce46740 2019-08-08 stsp free(id_str);
4189 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4190 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4192 4ce46740 2019-08-08 stsp goto done;
4195 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4196 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4197 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4198 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4199 2ec1f75b 2019-03-26 stsp goto done;
4202 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4203 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4204 12463d8b 2019-12-13 stsp if (fd == -1) {
4205 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4206 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4208 39449a05 2020-07-23 stsp goto done;
4210 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4211 39449a05 2020-07-23 stsp de_name, abspath);
4213 39449a05 2020-07-23 stsp goto done;
4216 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4217 12463d8b 2019-12-13 stsp if (fd == -1) {
4218 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4219 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4221 39449a05 2020-07-23 stsp goto done;
4223 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4224 39449a05 2020-07-23 stsp de_name, abspath);
4226 39449a05 2020-07-23 stsp goto done;
4229 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4230 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4231 2ec1f75b 2019-03-26 stsp goto done;
4233 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4234 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4235 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4236 2ec1f75b 2019-03-26 stsp goto done;
4240 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4242 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4243 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4245 b72f483a 2019-02-05 stsp if (blob1)
4246 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4247 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4248 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4249 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4250 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4251 b72f483a 2019-02-05 stsp free(abspath);
4252 927df6b7 2019-02-10 stsp return err;
4255 927df6b7 2019-02-10 stsp static const struct got_error *
4256 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4258 b00d56cd 2018-04-01 stsp const struct got_error *error;
4259 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4260 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4261 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4262 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4263 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4264 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4265 15a94983 2018-12-23 stsp int type1, type2;
4266 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4267 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4268 c0cc5c62 2018-10-18 stsp const char *errstr;
4269 927df6b7 2019-02-10 stsp char *path = NULL;
4270 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4272 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4274 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4275 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4276 25eccc22 2019-01-04 stsp NULL) == -1)
4277 b00d56cd 2018-04-01 stsp err(1, "pledge");
4280 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4281 b00d56cd 2018-04-01 stsp switch (ch) {
4283 64453f7e 2020-11-21 stsp force_text_diff = 1;
4286 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4288 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4289 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4292 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4293 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4294 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4296 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4299 98eaaa12 2019-08-03 stsp diff_staged = 1;
4302 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4305 2deda0b9 2019-03-07 stsp usage_diff();
4306 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4310 b00d56cd 2018-04-01 stsp argc -= optind;
4311 b00d56cd 2018-04-01 stsp argv += optind;
4313 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4314 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4315 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4316 b72f483a 2019-02-05 stsp goto done;
4318 927df6b7 2019-02-10 stsp if (argc <= 1) {
4319 b72f483a 2019-02-05 stsp if (repo_path)
4321 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4322 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4323 fa51e947 2020-03-27 stsp if (error) {
4324 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4325 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4327 1f03b8da 2020-03-20 stsp goto done;
4329 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4330 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4331 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4332 927df6b7 2019-02-10 stsp goto done;
4334 927df6b7 2019-02-10 stsp if (argc == 1) {
4335 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4337 927df6b7 2019-02-10 stsp if (error)
4338 927df6b7 2019-02-10 stsp goto done;
4340 927df6b7 2019-02-10 stsp path = strdup("");
4341 927df6b7 2019-02-10 stsp if (path == NULL) {
4342 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4343 927df6b7 2019-02-10 stsp goto done;
4346 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4347 98eaaa12 2019-08-03 stsp if (diff_staged)
4348 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4349 98eaaa12 2019-08-03 stsp "objects in repository");
4350 15a94983 2018-12-23 stsp id_str1 = argv[0];
4351 15a94983 2018-12-23 stsp id_str2 = argv[1];
4352 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4353 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4354 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4355 30db809c 2019-06-05 stsp goto done;
4356 1f03b8da 2020-03-20 stsp if (worktree) {
4357 1f03b8da 2020-03-20 stsp repo_path = strdup(
4358 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4359 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4360 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4361 1f03b8da 2020-03-20 stsp goto done;
4364 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4365 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4366 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4367 1f03b8da 2020-03-20 stsp goto done;
4372 b00d56cd 2018-04-01 stsp usage_diff();
4374 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4375 76089277 2018-04-01 stsp free(repo_path);
4376 b00d56cd 2018-04-01 stsp if (error != NULL)
4377 b00d56cd 2018-04-01 stsp goto done;
4379 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4380 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4381 c02c541e 2019-03-29 stsp if (error)
4382 c02c541e 2019-03-29 stsp goto done;
4384 30db809c 2019-06-05 stsp if (argc <= 1) {
4385 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4386 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4387 b72f483a 2019-02-05 stsp char *id_str;
4389 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4391 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4392 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4393 b72f483a 2019-02-05 stsp if (error)
4394 b72f483a 2019-02-05 stsp goto done;
4395 b72f483a 2019-02-05 stsp arg.repo = repo;
4396 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4397 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4398 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4399 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4400 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4401 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4402 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4404 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4405 72ea6654 2019-07-27 stsp if (error)
4406 72ea6654 2019-07-27 stsp goto done;
4408 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4409 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4410 b72f483a 2019-02-05 stsp free(id_str);
4411 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4412 b72f483a 2019-02-05 stsp goto done;
4415 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4416 84de9106 2020-12-26 stsp if (error)
4417 84de9106 2020-12-26 stsp return error;
4419 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4420 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4421 0adb7196 2019-08-11 stsp if (error)
4422 0adb7196 2019-08-11 stsp goto done;
4424 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4425 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4426 0adb7196 2019-08-11 stsp if (error)
4427 0adb7196 2019-08-11 stsp goto done;
4429 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4430 15a94983 2018-12-23 stsp if (error)
4431 15a94983 2018-12-23 stsp goto done;
4433 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4434 15a94983 2018-12-23 stsp if (error)
4435 15a94983 2018-12-23 stsp goto done;
4437 15a94983 2018-12-23 stsp if (type1 != type2) {
4438 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4439 b00d56cd 2018-04-01 stsp goto done;
4442 15a94983 2018-12-23 stsp switch (type1) {
4443 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4444 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4445 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4446 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4448 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4449 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4450 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4451 64453f7e 2020-11-21 stsp repo, stdout);
4453 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4454 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4455 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4456 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4460 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4463 5e70831e 2019-05-28 stsp free(label1);
4464 5e70831e 2019-05-28 stsp free(label2);
4465 15a94983 2018-12-23 stsp free(id1);
4466 15a94983 2018-12-23 stsp free(id2);
4467 927df6b7 2019-02-10 stsp free(path);
4468 b72f483a 2019-02-05 stsp if (worktree)
4469 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4470 ad242220 2018-09-08 stsp if (repo) {
4471 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4472 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4473 ad242220 2018-09-08 stsp if (error == NULL)
4474 ad242220 2018-09-08 stsp error = repo_error;
4476 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4477 b00d56cd 2018-04-01 stsp return error;
4480 404c43c4 2018-06-21 stsp __dead static void
4481 404c43c4 2018-06-21 stsp usage_blame(void)
4483 9270e621 2019-02-05 stsp fprintf(stderr,
4484 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4485 404c43c4 2018-06-21 stsp getprogname());
4489 28315671 2019-08-14 stsp struct blame_line {
4490 28315671 2019-08-14 stsp int annotated;
4491 28315671 2019-08-14 stsp char *id_str;
4492 82f6abb8 2019-08-14 stsp char *committer;
4493 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4496 28315671 2019-08-14 stsp struct blame_cb_args {
4497 28315671 2019-08-14 stsp struct blame_line *lines;
4498 28315671 2019-08-14 stsp int nlines;
4499 7ef28ff8 2019-08-14 stsp int nlines_prec;
4500 28315671 2019-08-14 stsp int lineno_cur;
4501 28315671 2019-08-14 stsp off_t *line_offsets;
4503 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4506 404c43c4 2018-06-21 stsp static const struct got_error *
4507 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4509 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4510 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4511 28315671 2019-08-14 stsp struct blame_line *bline;
4512 82f6abb8 2019-08-14 stsp char *line = NULL;
4513 28315671 2019-08-14 stsp size_t linesize = 0;
4514 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4515 28315671 2019-08-14 stsp off_t offset;
4516 bcb49d15 2019-08-14 stsp struct tm tm;
4517 bcb49d15 2019-08-14 stsp time_t committer_time;
4519 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4520 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4521 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4523 28315671 2019-08-14 stsp if (sigint_received)
4524 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4526 2839f8b9 2019-08-18 stsp if (lineno == -1)
4527 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4529 28315671 2019-08-14 stsp /* Annotate this line. */
4530 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4531 28315671 2019-08-14 stsp if (bline->annotated)
4532 28315671 2019-08-14 stsp return NULL;
4533 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4535 28315671 2019-08-14 stsp return err;
4537 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4539 82f6abb8 2019-08-14 stsp goto done;
4541 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4542 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4543 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4544 bcb49d15 2019-08-14 stsp goto done;
4547 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4548 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4549 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4550 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4551 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4552 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4553 82f6abb8 2019-08-14 stsp goto done;
4555 28315671 2019-08-14 stsp bline->annotated = 1;
4557 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4558 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4559 28315671 2019-08-14 stsp if (!bline->annotated)
4560 82f6abb8 2019-08-14 stsp goto done;
4562 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4563 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4564 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4565 82f6abb8 2019-08-14 stsp goto done;
4568 28315671 2019-08-14 stsp while (bline->annotated) {
4569 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4570 82f6abb8 2019-08-14 stsp size_t len;
4572 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4573 28315671 2019-08-14 stsp if (ferror(a->f))
4574 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4578 82f6abb8 2019-08-14 stsp committer = bline->committer;
4579 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4580 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4581 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4582 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4584 82f6abb8 2019-08-14 stsp *at = '\0';
4585 82f6abb8 2019-08-14 stsp len = strlen(committer);
4586 82f6abb8 2019-08-14 stsp if (len >= 9)
4587 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4589 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4591 28315671 2019-08-14 stsp *nl = '\0';
4592 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4593 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4595 28315671 2019-08-14 stsp a->lineno_cur++;
4596 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4599 82f6abb8 2019-08-14 stsp if (commit)
4600 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4601 28315671 2019-08-14 stsp free(line);
4602 28315671 2019-08-14 stsp return err;
4605 28315671 2019-08-14 stsp static const struct got_error *
4606 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4608 404c43c4 2018-06-21 stsp const struct got_error *error;
4609 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4610 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4611 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4612 0587e10c 2020-07-23 stsp char *link_target = NULL;
4613 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4614 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4615 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4616 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4617 28315671 2019-08-14 stsp struct blame_cb_args bca;
4618 28315671 2019-08-14 stsp int ch, obj_type, i;
4619 be659d10 2020-11-18 stsp off_t filesize;
4621 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4623 404c43c4 2018-06-21 stsp #ifndef PROFILE
4624 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4625 36e2fb66 2019-01-04 stsp NULL) == -1)
4626 404c43c4 2018-06-21 stsp err(1, "pledge");
4629 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4630 404c43c4 2018-06-21 stsp switch (ch) {
4632 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4635 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4636 66bea077 2018-08-02 stsp if (repo_path == NULL)
4637 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4639 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4642 2deda0b9 2019-03-07 stsp usage_blame();
4643 404c43c4 2018-06-21 stsp /* NOTREACHED */
4647 404c43c4 2018-06-21 stsp argc -= optind;
4648 404c43c4 2018-06-21 stsp argv += optind;
4650 a39318fd 2018-08-02 stsp if (argc == 1)
4651 404c43c4 2018-06-21 stsp path = argv[0];
4653 404c43c4 2018-06-21 stsp usage_blame();
4655 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4656 66bea077 2018-08-02 stsp if (cwd == NULL) {
4657 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4658 66bea077 2018-08-02 stsp goto done;
4660 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4661 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4662 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4663 66bea077 2018-08-02 stsp goto done;
4665 0c06baac 2019-02-05 stsp error = NULL;
4666 0c06baac 2019-02-05 stsp if (worktree) {
4667 0c06baac 2019-02-05 stsp repo_path =
4668 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4669 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4670 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4671 1f03b8da 2020-03-20 stsp if (error)
4672 1f03b8da 2020-03-20 stsp goto done;
4675 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4676 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4677 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4678 0c06baac 2019-02-05 stsp goto done;
4683 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4684 c02c541e 2019-03-29 stsp if (error != NULL)
4685 404c43c4 2018-06-21 stsp goto done;
4687 0c06baac 2019-02-05 stsp if (worktree) {
4688 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4691 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4692 01740607 2020-11-04 stsp if (error)
4693 01740607 2020-11-04 stsp goto done;
4694 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4695 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4696 01740607 2020-11-04 stsp p) == -1) {
4697 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4699 0c06baac 2019-02-05 stsp goto done;
4702 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4704 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4705 01740607 2020-11-04 stsp if (error)
4706 01740607 2020-11-04 stsp goto done;
4707 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4709 0c06baac 2019-02-05 stsp if (error)
4710 66bea077 2018-08-02 stsp goto done;
4712 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4713 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4714 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4715 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4716 404c43c4 2018-06-21 stsp if (error != NULL)
4717 66bea077 2018-08-02 stsp goto done;
4718 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4719 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4720 404c43c4 2018-06-21 stsp if (error != NULL)
4721 66bea077 2018-08-02 stsp goto done;
4723 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4724 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4725 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4727 84de9106 2020-12-26 stsp if (error)
4728 84de9106 2020-12-26 stsp goto done;
4729 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4730 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4731 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4732 30837e32 2019-07-25 stsp if (error)
4733 66bea077 2018-08-02 stsp goto done;
4736 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4737 0587e10c 2020-07-23 stsp commit_id, repo);
4738 0587e10c 2020-07-23 stsp if (error)
4739 0587e10c 2020-07-23 stsp goto done;
4741 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4742 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4743 28315671 2019-08-14 stsp if (error)
4744 28315671 2019-08-14 stsp goto done;
4746 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4747 28315671 2019-08-14 stsp if (error)
4748 28315671 2019-08-14 stsp goto done;
4750 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4751 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4752 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4753 28315671 2019-08-14 stsp goto done;
4756 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4757 28315671 2019-08-14 stsp if (error)
4758 28315671 2019-08-14 stsp goto done;
4759 28315671 2019-08-14 stsp bca.f = got_opentemp();
4760 28315671 2019-08-14 stsp if (bca.f == NULL) {
4761 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4762 28315671 2019-08-14 stsp goto done;
4764 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4765 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4766 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4767 28315671 2019-08-14 stsp goto done;
4769 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4770 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4771 b02560ec 2019-08-19 stsp bca.nlines--;
4773 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4774 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4775 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4776 28315671 2019-08-14 stsp goto done;
4778 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4779 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4780 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4781 7ef28ff8 2019-08-14 stsp while (i > 0) {
4783 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4785 82f6abb8 2019-08-14 stsp bca.repo = repo;
4787 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4788 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4790 66bea077 2018-08-02 stsp free(in_repo_path);
4791 0587e10c 2020-07-23 stsp free(link_target);
4792 66bea077 2018-08-02 stsp free(repo_path);
4793 66bea077 2018-08-02 stsp free(cwd);
4794 404c43c4 2018-06-21 stsp free(commit_id);
4795 28315671 2019-08-14 stsp free(obj_id);
4797 28315671 2019-08-14 stsp got_object_blob_close(blob);
4798 0c06baac 2019-02-05 stsp if (worktree)
4799 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4800 ad242220 2018-09-08 stsp if (repo) {
4801 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4802 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4803 ad242220 2018-09-08 stsp if (error == NULL)
4804 ad242220 2018-09-08 stsp error = repo_error;
4806 3affba96 2019-09-22 stsp if (bca.lines) {
4807 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4808 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4809 3affba96 2019-09-22 stsp free(bline->id_str);
4810 3affba96 2019-09-22 stsp free(bline->committer);
4812 3affba96 2019-09-22 stsp free(bca.lines);
4814 28315671 2019-08-14 stsp free(bca.line_offsets);
4815 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4816 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4817 404c43c4 2018-06-21 stsp return error;
4820 5de5890b 2018-10-18 stsp __dead static void
4821 5de5890b 2018-10-18 stsp usage_tree(void)
4823 c1669e2e 2019-01-09 stsp fprintf(stderr,
4824 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4825 5de5890b 2018-10-18 stsp getprogname());
4829 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4830 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4831 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4833 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4834 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4835 848d6979 2019-08-12 stsp const char *modestr = "";
4836 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4837 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4839 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4840 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4843 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4844 63c5ca5d 2019-08-24 stsp modestr = "$";
4845 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4848 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4850 0d6c6ee3 2020-05-20 stsp return err;
4851 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4852 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4853 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4856 848d6979 2019-08-12 stsp modestr = "@";
4858 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4859 848d6979 2019-08-12 stsp modestr = "/";
4860 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4861 848d6979 2019-08-12 stsp modestr = "*";
4863 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4864 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4865 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4867 0d6c6ee3 2020-05-20 stsp free(link_target);
4868 0d6c6ee3 2020-05-20 stsp return NULL;
4871 5de5890b 2018-10-18 stsp static const struct got_error *
4872 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4873 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4874 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4876 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4877 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4878 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4879 56e0773d 2019-11-28 stsp int nentries, i;
4881 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4883 5de5890b 2018-10-18 stsp goto done;
4885 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4887 5de5890b 2018-10-18 stsp goto done;
4888 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4889 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4890 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4891 5de5890b 2018-10-18 stsp char *id = NULL;
4893 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4896 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4897 5de5890b 2018-10-18 stsp if (show_ids) {
4898 5de5890b 2018-10-18 stsp char *id_str;
4899 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4900 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4902 5de5890b 2018-10-18 stsp goto done;
4903 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4904 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4905 5de5890b 2018-10-18 stsp free(id_str);
4906 5de5890b 2018-10-18 stsp goto done;
4908 5de5890b 2018-10-18 stsp free(id_str);
4910 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4913 0d6c6ee3 2020-05-20 stsp goto done;
4915 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4916 c1669e2e 2019-01-09 stsp char *child_path;
4917 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4918 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4919 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4920 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4921 c1669e2e 2019-01-09 stsp goto done;
4923 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4924 c1669e2e 2019-01-09 stsp root_path, repo);
4925 c1669e2e 2019-01-09 stsp free(child_path);
4927 c1669e2e 2019-01-09 stsp goto done;
4932 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4933 5de5890b 2018-10-18 stsp free(tree_id);
4934 5de5890b 2018-10-18 stsp return err;
4937 5de5890b 2018-10-18 stsp static const struct got_error *
4938 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4940 5de5890b 2018-10-18 stsp const struct got_error *error;
4941 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4942 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4943 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4944 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4945 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4946 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4947 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4950 5de5890b 2018-10-18 stsp #ifndef PROFILE
4951 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4952 0f8d269b 2019-01-04 stsp NULL) == -1)
4953 5de5890b 2018-10-18 stsp err(1, "pledge");
4956 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4957 5de5890b 2018-10-18 stsp switch (ch) {
4959 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4962 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4963 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4964 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4966 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4969 5de5890b 2018-10-18 stsp show_ids = 1;
4972 c1669e2e 2019-01-09 stsp recurse = 1;
4975 2deda0b9 2019-03-07 stsp usage_tree();
4976 5de5890b 2018-10-18 stsp /* NOTREACHED */
4980 5de5890b 2018-10-18 stsp argc -= optind;
4981 5de5890b 2018-10-18 stsp argv += optind;
4983 5de5890b 2018-10-18 stsp if (argc == 1)
4984 5de5890b 2018-10-18 stsp path = argv[0];
4985 5de5890b 2018-10-18 stsp else if (argc > 1)
4986 5de5890b 2018-10-18 stsp usage_tree();
4988 7a2c19d6 2019-02-05 stsp path = NULL;
4990 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4991 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4992 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4993 9bf7a39b 2019-02-05 stsp goto done;
4995 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4996 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4997 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4998 7a2c19d6 2019-02-05 stsp goto done;
5000 7a2c19d6 2019-02-05 stsp error = NULL;
5001 7a2c19d6 2019-02-05 stsp if (worktree) {
5002 7a2c19d6 2019-02-05 stsp repo_path =
5003 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5004 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5005 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5006 7a2c19d6 2019-02-05 stsp if (error)
5007 7a2c19d6 2019-02-05 stsp goto done;
5009 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5010 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5011 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5012 7a2c19d6 2019-02-05 stsp goto done;
5017 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5018 5de5890b 2018-10-18 stsp if (error != NULL)
5019 c02c541e 2019-03-29 stsp goto done;
5021 4fedbf4c 2020-11-07 stsp if (worktree) {
5022 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5025 4fedbf4c 2020-11-07 stsp if (path == NULL)
5026 4fedbf4c 2020-11-07 stsp path = "";
5027 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5028 4fedbf4c 2020-11-07 stsp if (error)
5029 4fedbf4c 2020-11-07 stsp goto done;
5030 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5031 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5032 4fedbf4c 2020-11-07 stsp p) == -1) {
5033 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5035 4fedbf4c 2020-11-07 stsp goto done;
5038 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5039 4fedbf4c 2020-11-07 stsp if (error)
5040 4fedbf4c 2020-11-07 stsp goto done;
5042 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5043 4fedbf4c 2020-11-07 stsp if (error)
5044 4fedbf4c 2020-11-07 stsp goto done;
5045 4fedbf4c 2020-11-07 stsp if (path == NULL)
5046 9bf7a39b 2019-02-05 stsp path = "/";
5047 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5048 9bf7a39b 2019-02-05 stsp if (error != NULL)
5049 9bf7a39b 2019-02-05 stsp goto done;
5052 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5053 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5054 4e0a20a4 2020-03-23 tracey if (worktree)
5055 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5057 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5058 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5059 5de5890b 2018-10-18 stsp if (error != NULL)
5060 5de5890b 2018-10-18 stsp goto done;
5061 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5062 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5063 5de5890b 2018-10-18 stsp if (error != NULL)
5064 5de5890b 2018-10-18 stsp goto done;
5066 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5067 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5068 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5070 84de9106 2020-12-26 stsp if (error)
5071 84de9106 2020-12-26 stsp goto done;
5072 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5073 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5074 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5075 30837e32 2019-07-25 stsp if (error)
5076 5de5890b 2018-10-18 stsp goto done;
5079 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5080 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5082 5de5890b 2018-10-18 stsp free(in_repo_path);
5083 5de5890b 2018-10-18 stsp free(repo_path);
5084 5de5890b 2018-10-18 stsp free(cwd);
5085 5de5890b 2018-10-18 stsp free(commit_id);
5086 7a2c19d6 2019-02-05 stsp if (worktree)
5087 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5088 5de5890b 2018-10-18 stsp if (repo) {
5089 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
5090 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
5091 5de5890b 2018-10-18 stsp if (error == NULL)
5092 5de5890b 2018-10-18 stsp error = repo_error;
5094 5de5890b 2018-10-18 stsp return error;
5097 6bad629b 2019-02-04 stsp __dead static void
5098 6bad629b 2019-02-04 stsp usage_status(void)
5100 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
5101 081470ac 2020-08-13 stsp getprogname());
5105 b72f483a 2019-02-05 stsp static const struct got_error *
5106 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5107 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5108 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5109 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5111 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5112 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5113 081470ac 2020-08-13 stsp if (arg) {
5114 081470ac 2020-08-13 stsp char *status_codes = arg;
5115 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5117 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5118 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5119 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5122 081470ac 2020-08-13 stsp if (i == ncodes)
5123 081470ac 2020-08-13 stsp return NULL;
5125 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5126 b72f483a 2019-02-05 stsp return NULL;
5129 6bad629b 2019-02-04 stsp static const struct got_error *
5130 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5132 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5133 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5134 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5135 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5136 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5137 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5138 081470ac 2020-08-13 stsp int ch, i;
5140 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5142 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
5143 6bad629b 2019-02-04 stsp switch (ch) {
5145 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5146 081470ac 2020-08-13 stsp switch (optarg[i]) {
5147 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5148 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5149 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5150 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5151 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5152 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5153 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5154 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5155 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5158 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5159 081470ac 2020-08-13 stsp optarg[i]);
5162 081470ac 2020-08-13 stsp status_codes = optarg;
5165 2deda0b9 2019-03-07 stsp usage_status();
5166 6bad629b 2019-02-04 stsp /* NOTREACHED */
5170 6bad629b 2019-02-04 stsp argc -= optind;
5171 6bad629b 2019-02-04 stsp argv += optind;
5173 6bad629b 2019-02-04 stsp #ifndef PROFILE
5174 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5175 6bad629b 2019-02-04 stsp NULL) == -1)
5176 6bad629b 2019-02-04 stsp err(1, "pledge");
5178 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5179 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5180 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5181 927df6b7 2019-02-10 stsp goto done;
5184 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5185 fa51e947 2020-03-27 stsp if (error) {
5186 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5187 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5188 a5edda0a 2019-07-27 stsp goto done;
5191 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5193 6bad629b 2019-02-04 stsp if (error != NULL)
5194 6bad629b 2019-02-04 stsp goto done;
5196 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5197 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5198 087fb88c 2019-08-04 stsp if (error)
5199 087fb88c 2019-08-04 stsp goto done;
5201 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5202 6bad629b 2019-02-04 stsp if (error)
5203 6bad629b 2019-02-04 stsp goto done;
5205 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
5206 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
5208 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5209 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5210 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5211 927df6b7 2019-02-10 stsp free(cwd);
5212 d0eebce4 2019-03-11 stsp return error;
5215 d0eebce4 2019-03-11 stsp __dead static void
5216 d0eebce4 2019-03-11 stsp usage_ref(void)
5218 d0eebce4 2019-03-11 stsp fprintf(stderr,
5219 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5220 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5221 d0eebce4 2019-03-11 stsp getprogname());
5225 d0eebce4 2019-03-11 stsp static const struct got_error *
5226 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5228 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5229 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5230 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5232 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5233 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5235 d0eebce4 2019-03-11 stsp return err;
5237 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5238 d0eebce4 2019-03-11 stsp char *refstr;
5239 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5240 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5241 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5242 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5243 d0eebce4 2019-03-11 stsp free(refstr);
5246 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5247 d0eebce4 2019-03-11 stsp return NULL;
5250 d0eebce4 2019-03-11 stsp static const struct got_error *
5251 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5253 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5254 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5256 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5258 d0eebce4 2019-03-11 stsp return err;
5260 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5261 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5262 d0eebce4 2019-03-11 stsp return err;
5265 d0eebce4 2019-03-11 stsp static const struct got_error *
5266 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5268 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5269 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5270 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5273 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5274 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5275 d1644381 2019-07-14 stsp * an unintended typo.
5277 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5278 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5280 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5282 d83d9d5c 2019-05-13 stsp if (err) {
5283 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5285 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5286 d83d9d5c 2019-05-13 stsp return err;
5287 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5289 d83d9d5c 2019-05-13 stsp return err;
5290 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5291 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5293 d83d9d5c 2019-05-13 stsp return err;
5296 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5298 d0eebce4 2019-03-11 stsp goto done;
5300 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5303 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5305 d1c1ae5f 2019-08-12 stsp return err;
5308 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5309 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5311 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5312 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5313 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5316 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5317 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5318 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5320 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5321 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5323 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5325 d1c1ae5f 2019-08-12 stsp return err;
5327 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5329 d1c1ae5f 2019-08-12 stsp goto done;
5331 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5333 d1c1ae5f 2019-08-12 stsp if (target_ref)
5334 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5336 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5337 d0eebce4 2019-03-11 stsp return err;
5340 d0eebce4 2019-03-11 stsp static const struct got_error *
5341 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5343 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5344 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5345 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5346 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5347 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5348 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5349 b2070a3f 2020-03-22 stsp char *refname = NULL;
5351 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5352 d0eebce4 2019-03-11 stsp switch (ch) {
5354 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5357 e31abbf2 2020-03-22 stsp do_delete = 1;
5360 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5361 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5362 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5364 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5367 d0eebce4 2019-03-11 stsp do_list = 1;
5370 e31abbf2 2020-03-22 stsp symref_target = optarg;
5373 d0eebce4 2019-03-11 stsp usage_ref();
5374 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5378 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5379 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5380 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5381 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5382 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5383 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5384 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5385 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5386 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5387 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5388 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5389 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5391 d0eebce4 2019-03-11 stsp argc -= optind;
5392 d0eebce4 2019-03-11 stsp argv += optind;
5394 e31abbf2 2020-03-22 stsp if (do_list) {
5395 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5396 d0eebce4 2019-03-11 stsp usage_ref();
5397 b2070a3f 2020-03-22 stsp if (argc == 1) {
5398 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5399 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5400 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5401 b2070a3f 2020-03-22 stsp goto done;
5405 e31abbf2 2020-03-22 stsp if (argc != 1)
5406 e31abbf2 2020-03-22 stsp usage_ref();
5407 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5408 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5409 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5410 b2070a3f 2020-03-22 stsp goto done;
5414 b2070a3f 2020-03-22 stsp if (refname)
5415 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5417 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5418 e0b57350 2019-03-12 stsp if (do_list) {
5419 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5420 e0b57350 2019-03-12 stsp NULL) == -1)
5421 e0b57350 2019-03-12 stsp err(1, "pledge");
5423 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5424 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5425 e0b57350 2019-03-12 stsp err(1, "pledge");
5428 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5429 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5430 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5431 d0eebce4 2019-03-11 stsp goto done;
5434 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5435 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5436 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5437 d0eebce4 2019-03-11 stsp goto done;
5439 d0eebce4 2019-03-11 stsp error = NULL;
5440 d0eebce4 2019-03-11 stsp if (worktree) {
5441 d0eebce4 2019-03-11 stsp repo_path =
5442 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5443 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5444 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5445 d0eebce4 2019-03-11 stsp if (error)
5446 d0eebce4 2019-03-11 stsp goto done;
5448 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5449 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5450 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5451 d0eebce4 2019-03-11 stsp goto done;
5456 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5457 d0eebce4 2019-03-11 stsp if (error != NULL)
5458 d0eebce4 2019-03-11 stsp goto done;
5460 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5461 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5462 c02c541e 2019-03-29 stsp if (error)
5463 c02c541e 2019-03-29 stsp goto done;
5465 d0eebce4 2019-03-11 stsp if (do_list)
5466 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5467 e31abbf2 2020-03-22 stsp else if (do_delete)
5468 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5469 e31abbf2 2020-03-22 stsp else if (symref_target)
5470 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5472 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5473 e31abbf2 2020-03-22 stsp usage_ref();
5474 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5477 b2070a3f 2020-03-22 stsp free(refname);
5479 4e759de4 2019-06-26 stsp got_repo_close(repo);
5480 4e759de4 2019-06-26 stsp if (worktree)
5481 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5482 4e759de4 2019-06-26 stsp free(cwd);
5483 4e759de4 2019-06-26 stsp free(repo_path);
5484 4e759de4 2019-06-26 stsp return error;
5487 4e759de4 2019-06-26 stsp __dead static void
5488 4e759de4 2019-06-26 stsp usage_branch(void)
5490 4e759de4 2019-06-26 stsp fprintf(stderr,
5491 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5492 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5496 4e759de4 2019-06-26 stsp static const struct got_error *
5497 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5498 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5500 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5501 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5502 4e99b47e 2019-10-04 stsp char *refstr;
5504 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5505 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5506 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5507 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5509 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5511 4e99b47e 2019-10-04 stsp return err;
5512 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5513 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5514 4e99b47e 2019-10-04 stsp marker = "* ";
5516 4e99b47e 2019-10-04 stsp marker = "~ ";
5520 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5521 4e99b47e 2019-10-04 stsp refname += 11;
5522 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5523 4e99b47e 2019-10-04 stsp refname += 18;
5525 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5526 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5527 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5529 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5530 4e99b47e 2019-10-04 stsp free(refstr);
5531 4e99b47e 2019-10-04 stsp return NULL;
5534 4e99b47e 2019-10-04 stsp static const struct got_error *
5535 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5537 ad89fa31 2019-10-04 stsp const char *refname;
5539 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5540 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5542 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5544 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5545 ad89fa31 2019-10-04 stsp refname += 11;
5546 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5547 ad89fa31 2019-10-04 stsp refname += 18;
5549 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5551 ad89fa31 2019-10-04 stsp return NULL;
5554 ad89fa31 2019-10-04 stsp static const struct got_error *
5555 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5557 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5558 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5559 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5560 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5561 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5563 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5565 4e99b47e 2019-10-04 stsp if (worktree) {
5566 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5567 4e99b47e 2019-10-04 stsp worktree);
5569 4e99b47e 2019-10-04 stsp return err;
5571 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5572 4e99b47e 2019-10-04 stsp worktree);
5574 4e99b47e 2019-10-04 stsp return err;
5576 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5577 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5578 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5580 4e99b47e 2019-10-04 stsp return err;
5581 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5582 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5586 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5587 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5589 4e759de4 2019-06-26 stsp return err;
5591 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5592 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5594 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5595 4e759de4 2019-06-26 stsp return NULL;
5598 4e759de4 2019-06-26 stsp static const struct got_error *
5599 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5600 45cd4e47 2019-08-25 stsp const char *branch_name)
5602 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5603 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5604 4e759de4 2019-06-26 stsp char *refname;
5606 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5607 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5609 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5611 4e759de4 2019-06-26 stsp goto done;
5613 45cd4e47 2019-08-25 stsp if (worktree &&
5614 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5615 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5616 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5617 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5618 45cd4e47 2019-08-25 stsp goto done;
5621 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5624 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5625 4e759de4 2019-06-26 stsp free(refname);
5626 4e759de4 2019-06-26 stsp return err;
5629 4e759de4 2019-06-26 stsp static const struct got_error *
5630 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5631 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5633 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5634 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5635 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5638 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5639 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5640 d3f84d51 2019-07-11 stsp * an unintended typo.
5642 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5643 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5645 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5646 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5647 62d463ca 2020-10-20 naddy goto done;
5650 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5651 4e759de4 2019-06-26 stsp if (err == NULL) {
5652 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5653 4e759de4 2019-06-26 stsp goto done;
5654 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5655 4e759de4 2019-06-26 stsp goto done;
5657 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5659 4e759de4 2019-06-26 stsp goto done;
5661 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5664 4e759de4 2019-06-26 stsp got_ref_close(ref);
5665 4e759de4 2019-06-26 stsp free(base_refname);
5666 4e759de4 2019-06-26 stsp free(refname);
5667 4e759de4 2019-06-26 stsp return err;
5670 4e759de4 2019-06-26 stsp static const struct got_error *
5671 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5673 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5674 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5675 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5676 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5677 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5678 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5679 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5680 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5681 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5682 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5683 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5685 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5687 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5688 4e759de4 2019-06-26 stsp switch (ch) {
5690 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5693 4e759de4 2019-06-26 stsp delref = optarg;
5696 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5697 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5698 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5700 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5703 4e759de4 2019-06-26 stsp do_list = 1;
5706 da76fce2 2020-02-24 stsp do_update = 0;
5709 4e759de4 2019-06-26 stsp usage_branch();
5710 4e759de4 2019-06-26 stsp /* NOTREACHED */
5714 4e759de4 2019-06-26 stsp if (do_list && delref)
5715 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5717 4e759de4 2019-06-26 stsp argc -= optind;
5718 4e759de4 2019-06-26 stsp argv += optind;
5720 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5721 ad89fa31 2019-10-04 stsp do_show = 1;
5723 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5724 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5726 4e759de4 2019-06-26 stsp if (do_list || delref) {
5727 4e759de4 2019-06-26 stsp if (argc > 0)
5728 4e759de4 2019-06-26 stsp usage_branch();
5729 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5730 4e759de4 2019-06-26 stsp usage_branch();
5732 4e759de4 2019-06-26 stsp #ifndef PROFILE
5733 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5734 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5735 4e759de4 2019-06-26 stsp NULL) == -1)
5736 4e759de4 2019-06-26 stsp err(1, "pledge");
5738 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5739 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5740 4e759de4 2019-06-26 stsp err(1, "pledge");
5743 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5744 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5745 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5746 4e759de4 2019-06-26 stsp goto done;
5749 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5750 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5751 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5752 4e759de4 2019-06-26 stsp goto done;
5754 4e759de4 2019-06-26 stsp error = NULL;
5755 4e759de4 2019-06-26 stsp if (worktree) {
5756 4e759de4 2019-06-26 stsp repo_path =
5757 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5758 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5759 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5760 4e759de4 2019-06-26 stsp if (error)
5761 4e759de4 2019-06-26 stsp goto done;
5763 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5764 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5765 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5766 4e759de4 2019-06-26 stsp goto done;
5771 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5772 4e759de4 2019-06-26 stsp if (error != NULL)
5773 4e759de4 2019-06-26 stsp goto done;
5775 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5776 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5777 4e759de4 2019-06-26 stsp if (error)
5778 4e759de4 2019-06-26 stsp goto done;
5780 ad89fa31 2019-10-04 stsp if (do_show)
5781 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5782 ad89fa31 2019-10-04 stsp else if (do_list)
5783 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5784 4e759de4 2019-06-26 stsp else if (delref)
5785 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5787 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5788 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5789 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5791 84de9106 2020-12-26 stsp if (error)
5792 84de9106 2020-12-26 stsp goto done;
5793 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5794 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5795 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5796 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5797 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5798 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5799 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5800 a74f7e83 2019-11-10 stsp if (error)
5801 a74f7e83 2019-11-10 stsp goto done;
5802 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5803 da76fce2 2020-02-24 stsp if (error)
5804 da76fce2 2020-02-24 stsp goto done;
5805 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5806 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5807 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5809 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5810 da76fce2 2020-02-24 stsp if (error)
5811 da76fce2 2020-02-24 stsp goto done;
5812 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5813 da76fce2 2020-02-24 stsp worktree);
5814 da76fce2 2020-02-24 stsp if (error)
5815 da76fce2 2020-02-24 stsp goto done;
5816 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5818 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5819 da76fce2 2020-02-24 stsp goto done;
5821 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5822 da76fce2 2020-02-24 stsp free(branch_refname);
5823 da76fce2 2020-02-24 stsp if (error)
5824 da76fce2 2020-02-24 stsp goto done;
5825 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5827 da76fce2 2020-02-24 stsp if (error)
5828 da76fce2 2020-02-24 stsp goto done;
5829 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5830 da76fce2 2020-02-24 stsp commit_id);
5831 da76fce2 2020-02-24 stsp if (error)
5832 da76fce2 2020-02-24 stsp goto done;
5833 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5834 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5835 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5837 da76fce2 2020-02-24 stsp if (error)
5838 da76fce2 2020-02-24 stsp goto done;
5839 9627c110 2020-04-18 stsp if (upa.did_something)
5840 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5841 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5846 da76fce2 2020-02-24 stsp got_ref_close(ref);
5848 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5849 d0eebce4 2019-03-11 stsp if (worktree)
5850 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5851 d0eebce4 2019-03-11 stsp free(cwd);
5852 d0eebce4 2019-03-11 stsp free(repo_path);
5853 da76fce2 2020-02-24 stsp free(commit_id);
5854 da76fce2 2020-02-24 stsp free(commit_id_str);
5855 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5856 da76fce2 2020-02-24 stsp free((char *)pe->path);
5857 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5858 d00136be 2019-03-26 stsp return error;
5862 d00136be 2019-03-26 stsp __dead static void
5863 8e7bd50a 2019-08-22 stsp usage_tag(void)
5865 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5866 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5867 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5872 b8bad2ba 2019-08-23 stsp static const struct got_error *
5873 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5875 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5876 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5877 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5878 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5879 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5881 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5882 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5883 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5884 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5886 b8bad2ba 2019-08-23 stsp return err;
5887 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5890 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5893 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5894 b8bad2ba 2019-08-23 stsp free(re_id);
5897 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5898 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5901 b8bad2ba 2019-08-23 stsp while (se) {
5902 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5905 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5906 b8bad2ba 2019-08-23 stsp free(se_id);
5909 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5910 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5912 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5913 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5915 b8bad2ba 2019-08-23 stsp return err;
5916 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5919 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5924 b8bad2ba 2019-08-23 stsp return err;
5928 b8bad2ba 2019-08-23 stsp static const struct got_error *
5929 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5931 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5932 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5933 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5935 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5937 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5939 8e7bd50a 2019-08-22 stsp return err;
5941 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5942 8e7bd50a 2019-08-22 stsp const char *refname;
5943 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5944 8e7bd50a 2019-08-22 stsp char datebuf[26];
5945 d4efa91b 2020-01-14 stsp const char *tagger;
5946 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5947 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5948 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5949 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5951 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5952 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5954 8e7bd50a 2019-08-22 stsp refname += 10;
5955 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5956 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5957 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5960 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5961 8e7bd50a 2019-08-22 stsp free(refstr);
5963 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5966 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5967 d4efa91b 2020-01-14 stsp if (err) {
5968 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5972 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5973 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5974 d4efa91b 2020-01-14 stsp if (err) {
5978 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5979 d4efa91b 2020-01-14 stsp tagger_time =
5980 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5981 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5987 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5988 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5989 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5990 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5994 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5995 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5996 2417344c 2019-08-23 stsp if (datestr)
5997 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5998 d4efa91b 2020-01-14 stsp if (commit)
5999 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6001 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6002 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6003 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6006 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6007 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6010 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6011 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6014 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6015 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6022 8e7bd50a 2019-08-22 stsp free(id_str);
6023 d4efa91b 2020-01-14 stsp if (commit) {
6024 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6027 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6029 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6030 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6031 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6032 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6037 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6039 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6041 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6042 8e7bd50a 2019-08-22 stsp } while (line);
6043 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6046 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6047 8e7bd50a 2019-08-22 stsp return NULL;
6050 8e7bd50a 2019-08-22 stsp static const struct got_error *
6051 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6052 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6054 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6055 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6056 f372d5cd 2019-10-21 stsp char *editor = NULL;
6057 1601cb9f 2020-09-11 naddy int initial_content_len;
6058 8e7bd50a 2019-08-22 stsp int fd = -1;
6060 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6061 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6062 8e7bd50a 2019-08-22 stsp goto done;
6065 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6066 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6067 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6068 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6069 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6070 8e7bd50a 2019-08-22 stsp goto done;
6073 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6075 8e7bd50a 2019-08-22 stsp goto done;
6077 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6078 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6079 97972933 2020-09-11 stsp goto done;
6082 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6084 8e7bd50a 2019-08-22 stsp goto done;
6085 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6086 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6088 8e7bd50a 2019-08-22 stsp free(initial_content);
6089 8e7bd50a 2019-08-22 stsp free(template);
6090 8e7bd50a 2019-08-22 stsp free(editor);
6092 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6093 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6095 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6096 59f86c76 2020-09-11 stsp if (err == NULL)
6097 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6098 59f86c76 2020-09-11 stsp if (err) {
6099 59f86c76 2020-09-11 stsp free(*tagmsg);
6100 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6102 8e7bd50a 2019-08-22 stsp return err;
6105 8e7bd50a 2019-08-22 stsp static const struct got_error *
6106 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6107 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6109 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6110 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6111 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6112 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6113 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6114 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6115 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6116 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6118 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6121 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6122 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6123 8e7bd50a 2019-08-22 stsp * an unintended typo.
6125 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6126 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6128 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6130 8e7bd50a 2019-08-22 stsp return err;
6132 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6134 84de9106 2020-12-26 stsp goto done;
6136 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6137 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6139 8e7bd50a 2019-08-22 stsp goto done;
6141 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6143 8e7bd50a 2019-08-22 stsp goto done;
6145 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6146 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6147 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6148 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6149 62d463ca 2020-10-20 naddy goto done;
6151 8e7bd50a 2019-08-22 stsp tag_name += 10;
6152 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6153 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6154 62d463ca 2020-10-20 naddy goto done;
6157 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6158 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6159 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6160 8e7bd50a 2019-08-22 stsp goto done;
6161 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6162 8e7bd50a 2019-08-22 stsp goto done;
6164 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6165 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6166 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6167 f372d5cd 2019-10-21 stsp if (err) {
6168 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6169 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6170 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6171 8e7bd50a 2019-08-22 stsp goto done;
6175 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6176 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6177 f372d5cd 2019-10-21 stsp if (err) {
6178 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6179 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6180 8e7bd50a 2019-08-22 stsp goto done;
6183 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6184 f372d5cd 2019-10-21 stsp if (err) {
6185 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6186 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6187 8e7bd50a 2019-08-22 stsp goto done;
6190 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6191 f372d5cd 2019-10-21 stsp if (err) {
6192 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6193 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6194 f372d5cd 2019-10-21 stsp goto done;
6197 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6198 f372d5cd 2019-10-21 stsp if (err) {
6199 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6200 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6201 f372d5cd 2019-10-21 stsp goto done;
6203 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6205 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6206 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6207 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6208 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6209 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6210 f372d5cd 2019-10-21 stsp free(tag_id_str);
6212 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6213 8e7bd50a 2019-08-22 stsp free(commit_id);
6214 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6215 8e7bd50a 2019-08-22 stsp free(refname);
6216 8e7bd50a 2019-08-22 stsp free(tagmsg);
6217 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6218 aba9c984 2019-09-08 stsp free(tagger);
6219 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6220 8e7bd50a 2019-08-22 stsp return err;
6223 8e7bd50a 2019-08-22 stsp static const struct got_error *
6224 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6226 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6227 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6228 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6229 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6230 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6231 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6232 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6234 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6235 8e7bd50a 2019-08-22 stsp switch (ch) {
6237 80106605 2020-02-24 stsp commit_id_arg = optarg;
6240 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6243 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6244 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6245 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6247 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6250 8e7bd50a 2019-08-22 stsp do_list = 1;
6253 8e7bd50a 2019-08-22 stsp usage_tag();
6254 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6258 8e7bd50a 2019-08-22 stsp argc -= optind;
6259 8e7bd50a 2019-08-22 stsp argv += optind;
6261 8e7bd50a 2019-08-22 stsp if (do_list) {
6262 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6264 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6265 8e7bd50a 2019-08-22 stsp if (tagmsg)
6266 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6267 8e7bd50a 2019-08-22 stsp if (argc > 0)
6268 8e7bd50a 2019-08-22 stsp usage_tag();
6269 80106605 2020-02-24 stsp } else if (argc != 1)
6270 8e7bd50a 2019-08-22 stsp usage_tag();
6272 a2887370 2019-08-23 stsp tag_name = argv[0];
6274 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6275 8e7bd50a 2019-08-22 stsp if (do_list) {
6276 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6277 8e7bd50a 2019-08-22 stsp NULL) == -1)
6278 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6280 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6281 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6282 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6285 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6286 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6287 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6288 8e7bd50a 2019-08-22 stsp goto done;
6291 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6292 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6293 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6294 8e7bd50a 2019-08-22 stsp goto done;
6296 8e7bd50a 2019-08-22 stsp error = NULL;
6297 8e7bd50a 2019-08-22 stsp if (worktree) {
6298 8e7bd50a 2019-08-22 stsp repo_path =
6299 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6300 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6301 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6302 8e7bd50a 2019-08-22 stsp if (error)
6303 8e7bd50a 2019-08-22 stsp goto done;
6305 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6306 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6307 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6308 8e7bd50a 2019-08-22 stsp goto done;
6313 8e7bd50a 2019-08-22 stsp if (do_list) {
6314 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6315 c9956ddf 2019-09-08 stsp if (error != NULL)
6316 c9956ddf 2019-09-08 stsp goto done;
6317 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6318 8e7bd50a 2019-08-22 stsp if (error)
6319 8e7bd50a 2019-08-22 stsp goto done;
6320 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6322 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6323 c9956ddf 2019-09-08 stsp if (error)
6324 c9956ddf 2019-09-08 stsp goto done;
6325 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6326 c9956ddf 2019-09-08 stsp if (error != NULL)
6327 c9956ddf 2019-09-08 stsp goto done;
6329 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6330 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6331 8e7bd50a 2019-08-22 stsp if (error)
6332 8e7bd50a 2019-08-22 stsp goto done;
6335 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6336 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6337 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6338 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6339 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6340 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6341 8e7bd50a 2019-08-22 stsp if (error)
6342 8e7bd50a 2019-08-22 stsp goto done;
6343 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6344 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6345 8e7bd50a 2019-08-22 stsp if (error)
6346 8e7bd50a 2019-08-22 stsp goto done;
6347 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6348 8e7bd50a 2019-08-22 stsp free(commit_id);
6349 8e7bd50a 2019-08-22 stsp if (error)
6350 8e7bd50a 2019-08-22 stsp goto done;
6353 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6354 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6358 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6359 8e7bd50a 2019-08-22 stsp if (worktree)
6360 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6361 8e7bd50a 2019-08-22 stsp free(cwd);
6362 8e7bd50a 2019-08-22 stsp free(repo_path);
6363 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6364 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6365 8e7bd50a 2019-08-22 stsp return error;
6368 8e7bd50a 2019-08-22 stsp __dead static void
6369 d00136be 2019-03-26 stsp usage_add(void)
6371 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6372 022fae89 2019-12-06 tracey getprogname());
6376 d00136be 2019-03-26 stsp static const struct got_error *
6377 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6379 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6381 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6382 4e68cba3 2019-11-23 stsp return NULL;
6385 4e68cba3 2019-11-23 stsp static const struct got_error *
6386 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6388 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6389 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6390 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6391 1dd54920 2019-05-11 stsp char *cwd = NULL;
6392 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6393 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6394 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6396 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6398 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6399 d00136be 2019-03-26 stsp switch (ch) {
6400 022fae89 2019-12-06 tracey case 'I':
6401 022fae89 2019-12-06 tracey no_ignores = 1;
6404 4e68cba3 2019-11-23 stsp can_recurse = 1;
6407 d00136be 2019-03-26 stsp usage_add();
6408 d00136be 2019-03-26 stsp /* NOTREACHED */
6412 d00136be 2019-03-26 stsp argc -= optind;
6413 d00136be 2019-03-26 stsp argv += optind;
6415 43012d58 2019-07-14 stsp #ifndef PROFILE
6416 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6417 43012d58 2019-07-14 stsp NULL) == -1)
6418 43012d58 2019-07-14 stsp err(1, "pledge");
6420 723c305c 2019-05-11 jcs if (argc < 1)
6421 d00136be 2019-03-26 stsp usage_add();
6423 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6424 d00136be 2019-03-26 stsp if (cwd == NULL) {
6425 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6426 d00136be 2019-03-26 stsp goto done;
6429 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6430 fa51e947 2020-03-27 stsp if (error) {
6431 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6432 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6433 d00136be 2019-03-26 stsp goto done;
6436 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6438 031a5338 2019-03-26 stsp if (error != NULL)
6439 031a5338 2019-03-26 stsp goto done;
6441 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6442 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6443 d00136be 2019-03-26 stsp if (error)
6444 d00136be 2019-03-26 stsp goto done;
6446 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6447 6d022e97 2019-08-04 stsp if (error)
6448 022fae89 2019-12-06 tracey goto done;
6450 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6451 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6452 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6453 6d022e97 2019-08-04 stsp goto done;
6457 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6458 4e68cba3 2019-11-23 stsp char *ondisk_path;
6459 4e68cba3 2019-11-23 stsp struct stat sb;
6460 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6461 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6462 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6463 62d463ca 2020-10-20 naddy pe->path) == -1) {
6464 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6465 4e68cba3 2019-11-23 stsp goto done;
6467 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6468 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6469 4e68cba3 2019-11-23 stsp free(ondisk_path);
6472 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6473 4e68cba3 2019-11-23 stsp ondisk_path);
6474 4e68cba3 2019-11-23 stsp free(ondisk_path);
6475 4e68cba3 2019-11-23 stsp goto done;
6477 4e68cba3 2019-11-23 stsp free(ondisk_path);
6478 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6479 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6480 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6481 4e68cba3 2019-11-23 stsp goto done;
6486 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6487 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6490 031a5338 2019-03-26 stsp got_repo_close(repo);
6491 d00136be 2019-03-26 stsp if (worktree)
6492 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6493 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6494 1dd54920 2019-05-11 stsp free((char *)pe->path);
6495 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6496 2ec1f75b 2019-03-26 stsp free(cwd);
6497 2ec1f75b 2019-03-26 stsp return error;
6500 2ec1f75b 2019-03-26 stsp __dead static void
6501 648e4ef7 2019-07-09 stsp usage_remove(void)
6503 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6504 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6508 2a06fe5f 2019-08-24 stsp static const struct got_error *
6509 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6510 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6512 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6514 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6515 2a06fe5f 2019-08-24 stsp return NULL;
6516 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6517 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6518 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6519 2a06fe5f 2019-08-24 stsp return NULL;
6522 2ec1f75b 2019-03-26 stsp static const struct got_error *
6523 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6525 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6526 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6527 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6528 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6529 17ed4618 2019-06-02 stsp char *cwd = NULL;
6530 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6531 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6532 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6534 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6536 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6537 2ec1f75b 2019-03-26 stsp switch (ch) {
6539 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6541 70e3e7f5 2019-12-13 tracey case 'k':
6542 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6544 f2a9dc41 2019-12-13 tracey case 'R':
6545 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6548 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6549 766841c2 2020-08-13 stsp switch (optarg[i]) {
6550 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6551 766841c2 2020-08-13 stsp delete_local_mods = 1;
6553 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6556 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6557 766841c2 2020-08-13 stsp optarg[i]);
6560 766841c2 2020-08-13 stsp status_codes = optarg;
6563 f2a9dc41 2019-12-13 tracey usage_remove();
6564 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6568 2ec1f75b 2019-03-26 stsp argc -= optind;
6569 2ec1f75b 2019-03-26 stsp argv += optind;
6571 43012d58 2019-07-14 stsp #ifndef PROFILE
6572 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6573 43012d58 2019-07-14 stsp NULL) == -1)
6574 43012d58 2019-07-14 stsp err(1, "pledge");
6576 17ed4618 2019-06-02 stsp if (argc < 1)
6577 648e4ef7 2019-07-09 stsp usage_remove();
6579 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6580 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6581 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6582 2ec1f75b 2019-03-26 stsp goto done;
6584 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6585 fa51e947 2020-03-27 stsp if (error) {
6586 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6587 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6588 2ec1f75b 2019-03-26 stsp goto done;
6591 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6594 2ec1f75b 2019-03-26 stsp goto done;
6596 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6597 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6598 2ec1f75b 2019-03-26 stsp if (error)
6599 2ec1f75b 2019-03-26 stsp goto done;
6601 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6602 6d022e97 2019-08-04 stsp if (error)
6603 6d022e97 2019-08-04 stsp goto done;
6605 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6606 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6607 f2a9dc41 2019-12-13 tracey struct stat sb;
6608 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6609 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6610 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6611 62d463ca 2020-10-20 naddy pe->path) == -1) {
6612 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6613 f2a9dc41 2019-12-13 tracey goto done;
6615 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6616 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6617 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6618 f2a9dc41 2019-12-13 tracey continue;
6620 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6621 f2a9dc41 2019-12-13 tracey ondisk_path);
6622 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6623 f2a9dc41 2019-12-13 tracey goto done;
6625 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6626 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6627 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6628 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6629 f2a9dc41 2019-12-13 tracey goto done;
6634 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6635 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6636 766841c2 2020-08-13 stsp repo, keep_on_disk);
6639 a129376b 2019-03-28 stsp got_repo_close(repo);
6640 a129376b 2019-03-28 stsp if (worktree)
6641 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6642 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6643 17ed4618 2019-06-02 stsp free((char *)pe->path);
6644 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6645 a129376b 2019-03-28 stsp free(cwd);
6646 a129376b 2019-03-28 stsp return error;
6649 a129376b 2019-03-28 stsp __dead static void
6650 a129376b 2019-03-28 stsp usage_revert(void)
6652 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6653 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6657 1ee397ad 2019-07-12 stsp static const struct got_error *
6658 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6660 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6661 fb9704af 2020-01-27 stsp return NULL;
6663 a129376b 2019-03-28 stsp while (path[0] == '/')
6665 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6666 33aa809d 2019-08-08 stsp return NULL;
6669 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6670 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6671 33aa809d 2019-08-08 stsp const char *action;
6674 33aa809d 2019-08-08 stsp static const struct got_error *
6675 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6676 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6678 33aa809d 2019-08-08 stsp char *line = NULL;
6679 33aa809d 2019-08-08 stsp size_t linesize = 0;
6680 33aa809d 2019-08-08 stsp ssize_t linelen;
6682 33aa809d 2019-08-08 stsp switch (status) {
6683 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6684 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6686 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6687 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6689 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6690 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6691 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6692 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6693 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6694 33aa809d 2019-08-08 stsp printf("%s", line);
6695 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6696 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6697 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6698 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6699 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6702 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6705 33aa809d 2019-08-08 stsp return NULL;
6708 33aa809d 2019-08-08 stsp static const struct got_error *
6709 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6710 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6712 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6713 33aa809d 2019-08-08 stsp char *line = NULL;
6714 33aa809d 2019-08-08 stsp size_t linesize = 0;
6715 33aa809d 2019-08-08 stsp ssize_t linelen;
6716 33aa809d 2019-08-08 stsp int resp = ' ';
6717 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6719 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6721 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6723 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6724 33aa809d 2019-08-08 stsp a->action);
6726 33aa809d 2019-08-08 stsp return err;
6727 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6728 33aa809d 2019-08-08 stsp if (linelen == -1) {
6729 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6730 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6731 33aa809d 2019-08-08 stsp return NULL;
6733 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6735 33aa809d 2019-08-08 stsp *nl = '\0';
6736 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6737 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6738 33aa809d 2019-08-08 stsp printf("y\n");
6739 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6740 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6741 33aa809d 2019-08-08 stsp printf("n\n");
6742 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6743 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6744 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6745 33aa809d 2019-08-08 stsp printf("q\n");
6747 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6748 33aa809d 2019-08-08 stsp free(line);
6749 33aa809d 2019-08-08 stsp return NULL;
6752 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6753 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6754 33aa809d 2019-08-08 stsp a->action);
6756 33aa809d 2019-08-08 stsp return err;
6757 33aa809d 2019-08-08 stsp resp = getchar();
6758 33aa809d 2019-08-08 stsp if (resp == '\n')
6759 33aa809d 2019-08-08 stsp resp = getchar();
6760 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6761 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6762 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6763 33aa809d 2019-08-08 stsp resp = ' ';
6765 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6766 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6767 33aa809d 2019-08-08 stsp resp = ' ';
6771 33aa809d 2019-08-08 stsp if (resp == 'y')
6772 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6773 33aa809d 2019-08-08 stsp else if (resp == 'n')
6774 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6775 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6776 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6778 1ee397ad 2019-07-12 stsp return NULL;
6782 a129376b 2019-03-28 stsp static const struct got_error *
6783 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6785 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6786 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6787 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6788 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6789 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6790 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6791 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6792 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6793 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6794 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6796 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6798 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6799 a129376b 2019-03-28 stsp switch (ch) {
6801 33aa809d 2019-08-08 stsp pflag = 1;
6804 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6807 0f6d7415 2019-08-08 stsp can_recurse = 1;
6810 a129376b 2019-03-28 stsp usage_revert();
6811 a129376b 2019-03-28 stsp /* NOTREACHED */
6815 a129376b 2019-03-28 stsp argc -= optind;
6816 a129376b 2019-03-28 stsp argv += optind;
6818 43012d58 2019-07-14 stsp #ifndef PROFILE
6819 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6820 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6821 43012d58 2019-07-14 stsp err(1, "pledge");
6823 e20a8b6f 2019-06-04 stsp if (argc < 1)
6824 a129376b 2019-03-28 stsp usage_revert();
6825 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6826 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6828 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6829 a129376b 2019-03-28 stsp if (cwd == NULL) {
6830 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6831 a129376b 2019-03-28 stsp goto done;
6833 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6834 fa51e947 2020-03-27 stsp if (error) {
6835 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6836 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6837 2ec1f75b 2019-03-26 stsp goto done;
6840 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6842 a129376b 2019-03-28 stsp if (error != NULL)
6843 a129376b 2019-03-28 stsp goto done;
6845 33aa809d 2019-08-08 stsp if (patch_script_path) {
6846 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6847 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6848 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6849 33aa809d 2019-08-08 stsp patch_script_path);
6850 33aa809d 2019-08-08 stsp goto done;
6853 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6854 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6855 a129376b 2019-03-28 stsp if (error)
6856 a129376b 2019-03-28 stsp goto done;
6858 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6859 6d022e97 2019-08-04 stsp if (error)
6860 6d022e97 2019-08-04 stsp goto done;
6862 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6863 0f6d7415 2019-08-08 stsp char *ondisk_path;
6864 0f6d7415 2019-08-08 stsp struct stat sb;
6865 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6866 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6867 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6868 62d463ca 2020-10-20 naddy pe->path) == -1) {
6869 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6870 0f6d7415 2019-08-08 stsp goto done;
6872 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6873 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6874 0f6d7415 2019-08-08 stsp free(ondisk_path);
6877 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6878 0f6d7415 2019-08-08 stsp ondisk_path);
6879 0f6d7415 2019-08-08 stsp free(ondisk_path);
6880 0f6d7415 2019-08-08 stsp goto done;
6882 0f6d7415 2019-08-08 stsp free(ondisk_path);
6883 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6884 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6885 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6886 0f6d7415 2019-08-08 stsp goto done;
6891 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6892 33aa809d 2019-08-08 stsp cpa.action = "revert";
6893 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6894 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6896 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6897 33aa809d 2019-08-08 stsp error == NULL)
6898 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6900 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6901 2ec1f75b 2019-03-26 stsp if (worktree)
6902 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6903 2ec1f75b 2019-03-26 stsp free(path);
6904 d00136be 2019-03-26 stsp free(cwd);
6905 6bad629b 2019-02-04 stsp return error;
6908 c4296144 2019-05-09 stsp __dead static void
6909 c4296144 2019-05-09 stsp usage_commit(void)
6911 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6912 5c1e53bc 2019-07-28 stsp getprogname());
6916 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6917 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6918 e2ba3d07 2019-05-13 stsp const char *editor;
6919 e0870e44 2019-05-13 stsp const char *worktree_path;
6920 76d98825 2019-06-03 stsp const char *branch_name;
6921 314a6357 2019-05-13 stsp const char *repo_path;
6922 e0870e44 2019-05-13 stsp char *logmsg_path;
6926 33ad4cbe 2019-05-12 jcs static const struct got_error *
6927 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6930 76d98825 2019-06-03 stsp char *initial_content = NULL;
6931 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6932 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6933 e0870e44 2019-05-13 stsp char *template = NULL;
6934 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6935 1601cb9f 2020-09-11 naddy int initial_content_len;
6936 97972933 2020-09-11 stsp int fd = -1;
6937 33ad4cbe 2019-05-12 jcs size_t len;
6939 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6940 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6941 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6942 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6943 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6944 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6945 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6946 33ad4cbe 2019-05-12 jcs return NULL;
6949 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6950 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6952 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6953 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6954 1601cb9f 2020-09-11 naddy a->branch_name);
6955 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6956 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6958 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6960 e0870e44 2019-05-13 stsp goto done;
6962 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6963 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6964 97972933 2020-09-11 stsp goto done;
6967 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6968 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6969 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6970 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6971 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6974 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6975 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6977 76d98825 2019-06-03 stsp free(initial_content);
6978 e0870e44 2019-05-13 stsp free(template);
6980 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6981 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6983 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6984 59f86c76 2020-09-11 stsp if (err == NULL)
6985 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6986 59f86c76 2020-09-11 stsp if (err) {
6987 59f86c76 2020-09-11 stsp free(*logmsg);
6988 59f86c76 2020-09-11 stsp *logmsg = NULL;
6990 33ad4cbe 2019-05-12 jcs return err;
6993 c4296144 2019-05-09 stsp static const struct got_error *
6994 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6996 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6997 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6998 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6999 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7000 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7001 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7002 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7003 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7004 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7005 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
7006 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7008 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7009 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7011 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
7012 c4296144 2019-05-09 stsp switch (ch) {
7014 c4296144 2019-05-09 stsp logmsg = optarg;
7017 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7020 c4296144 2019-05-09 stsp usage_commit();
7021 c4296144 2019-05-09 stsp /* NOTREACHED */
7025 c4296144 2019-05-09 stsp argc -= optind;
7026 c4296144 2019-05-09 stsp argv += optind;
7028 43012d58 2019-07-14 stsp #ifndef PROFILE
7029 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7030 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7031 43012d58 2019-07-14 stsp err(1, "pledge");
7033 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7034 c4296144 2019-05-09 stsp if (cwd == NULL) {
7035 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7036 c4296144 2019-05-09 stsp goto done;
7038 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7039 fa51e947 2020-03-27 stsp if (error) {
7040 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7041 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7042 7d5807f4 2019-07-11 stsp goto done;
7045 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7046 c4296144 2019-05-09 stsp if (error)
7047 a698f62e 2019-07-25 stsp goto done;
7048 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7049 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7050 c4296144 2019-05-09 stsp goto done;
7053 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7054 916f288c 2019-07-30 stsp worktree);
7055 5c1e53bc 2019-07-28 stsp if (error)
7056 5c1e53bc 2019-07-28 stsp goto done;
7058 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7059 c9956ddf 2019-09-08 stsp if (error)
7060 c9956ddf 2019-09-08 stsp goto done;
7061 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7062 c9956ddf 2019-09-08 stsp gitconfig_path);
7063 c4296144 2019-05-09 stsp if (error != NULL)
7064 c4296144 2019-05-09 stsp goto done;
7066 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7067 aba9c984 2019-09-08 stsp if (error)
7068 aba9c984 2019-09-08 stsp return error;
7071 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7072 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7074 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7075 314a6357 2019-05-13 stsp error = get_editor(&editor);
7077 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7078 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7079 c4296144 2019-05-09 stsp if (error)
7080 c4296144 2019-05-09 stsp goto done;
7082 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7083 087fb88c 2019-08-04 stsp if (error)
7084 087fb88c 2019-08-04 stsp goto done;
7086 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7087 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7088 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7089 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7090 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7091 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7092 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7093 916f288c 2019-07-30 stsp goto done;
7095 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7097 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7098 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7099 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7100 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7101 e0870e44 2019-05-13 stsp if (error) {
7102 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7103 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7104 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7105 c4296144 2019-05-09 stsp goto done;
7108 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7109 c4296144 2019-05-09 stsp if (error)
7110 c4296144 2019-05-09 stsp goto done;
7111 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7113 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7114 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7115 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7116 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7117 7266f21f 2019-10-21 stsp error == NULL)
7118 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7119 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7121 c4296144 2019-05-09 stsp got_repo_close(repo);
7122 c4296144 2019-05-09 stsp if (worktree)
7123 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7124 c4296144 2019-05-09 stsp free(cwd);
7125 c4296144 2019-05-09 stsp free(id_str);
7126 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7127 e2ba3d07 2019-05-13 stsp free(editor);
7128 aba9c984 2019-09-08 stsp free(author);
7129 234035bc 2019-06-01 stsp return error;
7132 234035bc 2019-06-01 stsp __dead static void
7133 234035bc 2019-06-01 stsp usage_cherrypick(void)
7135 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7139 234035bc 2019-06-01 stsp static const struct got_error *
7140 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7142 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7143 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7144 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7145 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7146 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7147 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7148 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7149 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
7151 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7153 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7154 234035bc 2019-06-01 stsp switch (ch) {
7156 234035bc 2019-06-01 stsp usage_cherrypick();
7157 234035bc 2019-06-01 stsp /* NOTREACHED */
7161 234035bc 2019-06-01 stsp argc -= optind;
7162 234035bc 2019-06-01 stsp argv += optind;
7164 43012d58 2019-07-14 stsp #ifndef PROFILE
7165 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7166 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7167 43012d58 2019-07-14 stsp err(1, "pledge");
7169 234035bc 2019-06-01 stsp if (argc != 1)
7170 234035bc 2019-06-01 stsp usage_cherrypick();
7172 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7173 234035bc 2019-06-01 stsp if (cwd == NULL) {
7174 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7175 234035bc 2019-06-01 stsp goto done;
7177 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7178 fa51e947 2020-03-27 stsp if (error) {
7179 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7180 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7182 234035bc 2019-06-01 stsp goto done;
7185 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7187 234035bc 2019-06-01 stsp if (error != NULL)
7188 234035bc 2019-06-01 stsp goto done;
7190 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7191 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7192 234035bc 2019-06-01 stsp if (error)
7193 234035bc 2019-06-01 stsp goto done;
7195 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7196 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7197 234035bc 2019-06-01 stsp if (error != NULL) {
7198 234035bc 2019-06-01 stsp struct got_reference *ref;
7199 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7200 234035bc 2019-06-01 stsp goto done;
7201 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7202 234035bc 2019-06-01 stsp if (error != NULL)
7203 234035bc 2019-06-01 stsp goto done;
7204 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7205 234035bc 2019-06-01 stsp got_ref_close(ref);
7206 234035bc 2019-06-01 stsp if (error != NULL)
7207 234035bc 2019-06-01 stsp goto done;
7209 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7210 234035bc 2019-06-01 stsp if (error)
7211 234035bc 2019-06-01 stsp goto done;
7213 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7214 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7215 234035bc 2019-06-01 stsp if (error != NULL)
7216 234035bc 2019-06-01 stsp goto done;
7218 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7219 234035bc 2019-06-01 stsp if (error) {
7220 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7221 234035bc 2019-06-01 stsp goto done;
7222 234035bc 2019-06-01 stsp error = NULL;
7224 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7225 234035bc 2019-06-01 stsp goto done;
7228 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7229 234035bc 2019-06-01 stsp if (error)
7230 234035bc 2019-06-01 stsp goto done;
7231 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7232 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7233 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7234 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7236 234035bc 2019-06-01 stsp if (error != NULL)
7237 234035bc 2019-06-01 stsp goto done;
7239 9627c110 2020-04-18 stsp if (upa.did_something)
7240 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7241 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7243 234035bc 2019-06-01 stsp if (commit)
7244 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7245 234035bc 2019-06-01 stsp free(commit_id_str);
7246 234035bc 2019-06-01 stsp if (head_ref)
7247 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7248 234035bc 2019-06-01 stsp if (worktree)
7249 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7251 234035bc 2019-06-01 stsp got_repo_close(repo);
7252 c4296144 2019-05-09 stsp return error;
7255 5ef14e63 2019-06-02 stsp __dead static void
7256 5ef14e63 2019-06-02 stsp usage_backout(void)
7258 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7262 5ef14e63 2019-06-02 stsp static const struct got_error *
7263 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7265 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7266 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7267 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7268 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7269 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7270 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7271 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7272 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7274 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7276 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7277 5ef14e63 2019-06-02 stsp switch (ch) {
7279 5ef14e63 2019-06-02 stsp usage_backout();
7280 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7284 5ef14e63 2019-06-02 stsp argc -= optind;
7285 5ef14e63 2019-06-02 stsp argv += optind;
7287 43012d58 2019-07-14 stsp #ifndef PROFILE
7288 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7289 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7290 43012d58 2019-07-14 stsp err(1, "pledge");
7292 5ef14e63 2019-06-02 stsp if (argc != 1)
7293 5ef14e63 2019-06-02 stsp usage_backout();
7295 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7296 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7297 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7298 5ef14e63 2019-06-02 stsp goto done;
7300 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7301 fa51e947 2020-03-27 stsp if (error) {
7302 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7303 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7304 5ef14e63 2019-06-02 stsp goto done;
7307 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7309 5ef14e63 2019-06-02 stsp if (error != NULL)
7310 5ef14e63 2019-06-02 stsp goto done;
7312 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7313 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7314 5ef14e63 2019-06-02 stsp if (error)
7315 5ef14e63 2019-06-02 stsp goto done;
7317 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7318 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7319 5ef14e63 2019-06-02 stsp if (error != NULL) {
7320 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7321 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7322 5ef14e63 2019-06-02 stsp goto done;
7323 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7324 5ef14e63 2019-06-02 stsp if (error != NULL)
7325 5ef14e63 2019-06-02 stsp goto done;
7326 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7327 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7328 5ef14e63 2019-06-02 stsp if (error != NULL)
7329 5ef14e63 2019-06-02 stsp goto done;
7331 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7332 5ef14e63 2019-06-02 stsp if (error)
7333 5ef14e63 2019-06-02 stsp goto done;
7335 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7336 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7337 5ef14e63 2019-06-02 stsp if (error != NULL)
7338 5ef14e63 2019-06-02 stsp goto done;
7340 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7341 5ef14e63 2019-06-02 stsp if (error)
7342 5ef14e63 2019-06-02 stsp goto done;
7344 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7345 5ef14e63 2019-06-02 stsp if (error)
7346 5ef14e63 2019-06-02 stsp goto done;
7347 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7348 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7349 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7350 5ef14e63 2019-06-02 stsp goto done;
7353 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7354 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7355 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7356 5ef14e63 2019-06-02 stsp if (error != NULL)
7357 5ef14e63 2019-06-02 stsp goto done;
7359 9627c110 2020-04-18 stsp if (upa.did_something)
7360 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7361 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7363 5ef14e63 2019-06-02 stsp if (commit)
7364 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7365 5ef14e63 2019-06-02 stsp free(commit_id_str);
7366 5ef14e63 2019-06-02 stsp if (head_ref)
7367 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7368 818c7501 2019-07-11 stsp if (worktree)
7369 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7371 818c7501 2019-07-11 stsp got_repo_close(repo);
7372 818c7501 2019-07-11 stsp return error;
7375 818c7501 2019-07-11 stsp __dead static void
7376 818c7501 2019-07-11 stsp usage_rebase(void)
7378 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7379 af54c8f8 2019-07-11 stsp getprogname());
7384 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7387 0ebf8283 2019-07-24 stsp size_t len;
7389 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7390 0ebf8283 2019-07-24 stsp if (len > limit)
7391 0ebf8283 2019-07-24 stsp len = limit;
7392 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7393 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7395 0ebf8283 2019-07-24 stsp *nl = '\0';
7398 0ebf8283 2019-07-24 stsp static const struct got_error *
7399 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7401 5943eee2 2019-08-13 stsp const struct got_error *err;
7402 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7403 5943eee2 2019-08-13 stsp const char *s;
7405 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7407 5943eee2 2019-08-13 stsp return err;
7409 5943eee2 2019-08-13 stsp s = logmsg0;
7410 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7413 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7414 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7415 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7416 5943eee2 2019-08-13 stsp goto done;
7419 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7421 5943eee2 2019-08-13 stsp free(logmsg0);
7422 a0ea4fc0 2020-02-28 stsp return err;
7425 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7426 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7428 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7429 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7430 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7432 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7434 a0ea4fc0 2020-02-28 stsp return err;
7436 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7438 a0ea4fc0 2020-02-28 stsp goto done;
7440 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7442 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7444 a0ea4fc0 2020-02-28 stsp goto done;
7446 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7448 a0ea4fc0 2020-02-28 stsp free(id_str);
7449 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7450 a0ea4fc0 2020-02-28 stsp free(logmsg);
7451 5943eee2 2019-08-13 stsp return err;
7454 818c7501 2019-07-11 stsp static const struct got_error *
7455 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7456 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7458 818c7501 2019-07-11 stsp const struct got_error *err;
7459 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7461 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7463 818c7501 2019-07-11 stsp goto done;
7465 ff0d2220 2019-07-11 stsp if (new_id) {
7466 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7468 ff0d2220 2019-07-11 stsp goto done;
7471 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7472 ff0d2220 2019-07-11 stsp if (new_id_str)
7473 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7475 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7477 0ebf8283 2019-07-24 stsp goto done;
7479 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7480 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7482 818c7501 2019-07-11 stsp free(old_id_str);
7483 818c7501 2019-07-11 stsp free(new_id_str);
7484 272a1371 2020-02-28 stsp free(logmsg);
7485 818c7501 2019-07-11 stsp return err;
7488 1ee397ad 2019-07-12 stsp static const struct got_error *
7489 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7490 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7491 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7493 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7494 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7495 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7498 818c7501 2019-07-11 stsp static const struct got_error *
7499 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7500 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7501 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7502 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7504 ff0d2220 2019-07-11 stsp const struct got_error *error;
7505 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7506 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7508 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7509 ff0d2220 2019-07-11 stsp if (error)
7510 ff0d2220 2019-07-11 stsp return error;
7512 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7513 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7514 ff0d2220 2019-07-11 stsp if (error) {
7515 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7516 ff0d2220 2019-07-11 stsp goto done;
7517 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7519 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7520 ff0d2220 2019-07-11 stsp free(new_commit_id);
7523 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7524 ff0d2220 2019-07-11 stsp return error;
7527 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7528 64c6d990 2019-07-11 stsp const char *path_prefix;
7529 64c6d990 2019-07-11 stsp size_t len;
7530 8ca9bd68 2019-07-25 stsp int errcode;
7533 64c6d990 2019-07-11 stsp static const struct got_error *
7534 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7535 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7536 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7537 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7539 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7541 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7542 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7543 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7545 64c6d990 2019-07-11 stsp return NULL;
7548 64c6d990 2019-07-11 stsp static const struct got_error *
7549 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7550 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7551 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7553 64c6d990 2019-07-11 stsp const struct got_error *err;
7554 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7555 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7556 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7558 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7559 64c6d990 2019-07-11 stsp return NULL;
7561 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7563 64c6d990 2019-07-11 stsp goto done;
7565 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7567 64c6d990 2019-07-11 stsp goto done;
7569 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7570 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7572 64c6d990 2019-07-11 stsp goto done;
7574 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7575 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7577 64c6d990 2019-07-11 stsp goto done;
7579 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7580 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7581 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7582 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7583 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7584 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7585 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7587 64c6d990 2019-07-11 stsp if (tree1)
7588 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7589 64c6d990 2019-07-11 stsp if (tree2)
7590 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7591 64c6d990 2019-07-11 stsp if (commit)
7592 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7593 64c6d990 2019-07-11 stsp if (parent_commit)
7594 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7595 64c6d990 2019-07-11 stsp return err;
7598 ff0d2220 2019-07-11 stsp static const struct got_error *
7599 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7600 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7601 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7602 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7603 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7605 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7606 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7607 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7608 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7609 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7611 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7613 af61c510 2019-07-19 stsp return err;
7615 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7616 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7618 af61c510 2019-07-19 stsp goto done;
7619 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7620 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7621 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7622 af61c510 2019-07-19 stsp if (err) {
7623 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7624 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7625 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7626 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7627 af61c510 2019-07-19 stsp "been reached?!?");
7629 ee780d5c 2020-01-04 stsp goto done;
7631 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7632 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7634 af61c510 2019-07-19 stsp goto done;
7636 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7638 af61c510 2019-07-19 stsp goto done;
7639 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7640 af61c510 2019-07-19 stsp commit_id = parent_id;
7644 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7645 af61c510 2019-07-19 stsp return err;
7648 af61c510 2019-07-19 stsp static const struct got_error *
7649 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7651 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7652 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7653 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7654 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7655 818c7501 2019-07-11 stsp char *cwd = NULL;
7656 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7657 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7658 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7659 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7660 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7661 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7662 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7663 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7664 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7665 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7666 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7667 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7668 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7670 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7671 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7673 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7674 818c7501 2019-07-11 stsp switch (ch) {
7676 818c7501 2019-07-11 stsp abort_rebase = 1;
7679 818c7501 2019-07-11 stsp continue_rebase = 1;
7682 818c7501 2019-07-11 stsp usage_rebase();
7683 818c7501 2019-07-11 stsp /* NOTREACHED */
7687 818c7501 2019-07-11 stsp argc -= optind;
7688 818c7501 2019-07-11 stsp argv += optind;
7690 43012d58 2019-07-14 stsp #ifndef PROFILE
7691 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7692 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7693 43012d58 2019-07-14 stsp err(1, "pledge");
7695 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7696 818c7501 2019-07-11 stsp usage_rebase();
7697 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7698 818c7501 2019-07-11 stsp if (argc != 0)
7699 818c7501 2019-07-11 stsp usage_rebase();
7700 818c7501 2019-07-11 stsp } else if (argc != 1)
7701 818c7501 2019-07-11 stsp usage_rebase();
7703 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7704 818c7501 2019-07-11 stsp if (cwd == NULL) {
7705 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7706 818c7501 2019-07-11 stsp goto done;
7708 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7709 fa51e947 2020-03-27 stsp if (error) {
7710 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7711 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7712 818c7501 2019-07-11 stsp goto done;
7715 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7717 818c7501 2019-07-11 stsp if (error != NULL)
7718 818c7501 2019-07-11 stsp goto done;
7720 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7721 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7722 7ef62c4e 2020-02-24 stsp if (error)
7723 7ef62c4e 2020-02-24 stsp goto done;
7725 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7726 7ef62c4e 2020-02-24 stsp worktree);
7727 818c7501 2019-07-11 stsp if (error)
7728 7ef62c4e 2020-02-24 stsp goto done;
7729 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7730 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7731 818c7501 2019-07-11 stsp goto done;
7734 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7735 818c7501 2019-07-11 stsp if (error)
7736 818c7501 2019-07-11 stsp goto done;
7738 f6794adc 2019-07-23 stsp if (abort_rebase) {
7739 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7740 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7741 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7742 f6794adc 2019-07-23 stsp goto done;
7744 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7745 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7746 3e3a69f1 2019-07-25 stsp worktree, repo);
7747 818c7501 2019-07-11 stsp if (error)
7748 818c7501 2019-07-11 stsp goto done;
7749 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7750 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7751 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7752 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7753 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7754 818c7501 2019-07-11 stsp if (error)
7755 818c7501 2019-07-11 stsp goto done;
7756 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7757 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7758 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7761 818c7501 2019-07-11 stsp if (continue_rebase) {
7762 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7763 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7764 f6794adc 2019-07-23 stsp goto done;
7766 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7767 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7768 3e3a69f1 2019-07-25 stsp worktree, repo);
7769 818c7501 2019-07-11 stsp if (error)
7770 818c7501 2019-07-11 stsp goto done;
7772 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7773 01757395 2019-07-12 stsp resume_commit_id, repo);
7774 818c7501 2019-07-11 stsp if (error)
7775 818c7501 2019-07-11 stsp goto done;
7777 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7778 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7779 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7780 818c7501 2019-07-11 stsp goto done;
7783 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7784 818c7501 2019-07-11 stsp if (error != NULL)
7785 818c7501 2019-07-11 stsp goto done;
7788 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7789 ff0d2220 2019-07-11 stsp if (error)
7790 ff0d2220 2019-07-11 stsp goto done;
7792 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7793 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7795 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7796 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7797 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7798 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7799 818c7501 2019-07-11 stsp if (error)
7800 818c7501 2019-07-11 stsp goto done;
7801 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7802 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7803 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7804 818c7501 2019-07-11 stsp "with work tree's branch");
7805 818c7501 2019-07-11 stsp goto done;
7808 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7809 a51a74b3 2019-07-27 stsp if (error) {
7810 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7811 a51a74b3 2019-07-27 stsp goto done;
7812 a51a74b3 2019-07-27 stsp error = NULL;
7814 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7815 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7816 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7817 a51a74b3 2019-07-27 stsp goto done;
7819 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7820 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7821 818c7501 2019-07-11 stsp if (error)
7822 818c7501 2019-07-11 stsp goto done;
7825 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7826 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7827 818c7501 2019-07-11 stsp if (error)
7828 818c7501 2019-07-11 stsp goto done;
7830 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7831 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7832 fc66b545 2019-08-12 stsp if (pid == NULL) {
7833 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7834 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7835 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7836 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7837 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7838 fc66b545 2019-08-12 stsp if (error)
7839 fc66b545 2019-08-12 stsp goto done;
7840 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7841 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7842 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7845 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7846 fc66b545 2019-08-12 stsp goto done;
7848 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7849 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7850 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7851 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7852 818c7501 2019-07-11 stsp commit = NULL;
7853 818c7501 2019-07-11 stsp if (error)
7854 818c7501 2019-07-11 stsp goto done;
7856 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7857 38b0338b 2019-11-29 stsp if (continue_rebase) {
7858 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7859 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7860 38b0338b 2019-11-29 stsp goto done;
7862 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7863 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7864 38b0338b 2019-11-29 stsp char *id_str;
7865 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7866 38b0338b 2019-11-29 stsp new_base_branch);
7867 38b0338b 2019-11-29 stsp if (error)
7868 38b0338b 2019-11-29 stsp goto done;
7869 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7870 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7871 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7872 38b0338b 2019-11-29 stsp free(id_str);
7873 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7874 38b0338b 2019-11-29 stsp new_head_commit_id);
7875 38b0338b 2019-11-29 stsp if (error)
7876 38b0338b 2019-11-29 stsp goto done;
7880 818c7501 2019-07-11 stsp pid = NULL;
7881 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7882 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7884 818c7501 2019-07-11 stsp commit_id = qid->id;
7885 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7886 818c7501 2019-07-11 stsp pid = qid;
7888 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7889 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7890 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7891 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7892 818c7501 2019-07-11 stsp if (error)
7893 818c7501 2019-07-11 stsp goto done;
7895 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7896 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7897 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7899 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7900 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7901 a0ea4fc0 2020-02-28 stsp if (error)
7902 a0ea4fc0 2020-02-28 stsp goto done;
7903 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7907 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7908 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7909 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7910 818c7501 2019-07-11 stsp if (error)
7911 818c7501 2019-07-11 stsp goto done;
7914 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7915 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7916 818c7501 2019-07-11 stsp if (error)
7917 818c7501 2019-07-11 stsp goto done;
7918 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7919 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7921 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7922 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7924 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7925 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7926 818c7501 2019-07-11 stsp free(resume_commit_id);
7927 818c7501 2019-07-11 stsp free(yca_id);
7928 818c7501 2019-07-11 stsp if (commit)
7929 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7930 818c7501 2019-07-11 stsp if (branch)
7931 818c7501 2019-07-11 stsp got_ref_close(branch);
7932 818c7501 2019-07-11 stsp if (new_base_branch)
7933 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7934 818c7501 2019-07-11 stsp if (tmp_branch)
7935 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7936 5ef14e63 2019-06-02 stsp if (worktree)
7937 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7939 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7940 5ef14e63 2019-06-02 stsp return error;
7943 0ebf8283 2019-07-24 stsp __dead static void
7944 0ebf8283 2019-07-24 stsp usage_histedit(void)
7946 466785b9 2020-12-10 jrick fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] [-F histedit-script] [-m]\n",
7947 0ebf8283 2019-07-24 stsp getprogname());
7951 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7952 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7953 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7954 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7955 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7957 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7958 0ebf8283 2019-07-24 stsp unsigned char code;
7959 0ebf8283 2019-07-24 stsp const char *name;
7960 0ebf8283 2019-07-24 stsp const char *desc;
7961 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7962 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7963 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7964 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7965 82997472 2020-01-29 stsp "be used" },
7966 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7967 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7968 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7971 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7972 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7973 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7974 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7975 0ebf8283 2019-07-24 stsp char *logmsg;
7977 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7979 0ebf8283 2019-07-24 stsp static const struct got_error *
7980 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7981 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7983 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7984 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7985 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7988 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7990 0ebf8283 2019-07-24 stsp goto done;
7992 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7994 0ebf8283 2019-07-24 stsp goto done;
7996 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7998 0ebf8283 2019-07-24 stsp goto done;
8000 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8001 0ebf8283 2019-07-24 stsp if (n < 0)
8002 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8004 0ebf8283 2019-07-24 stsp if (commit)
8005 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8006 0ebf8283 2019-07-24 stsp free(id_str);
8007 0ebf8283 2019-07-24 stsp free(logmsg);
8008 0ebf8283 2019-07-24 stsp return err;
8011 0ebf8283 2019-07-24 stsp static const struct got_error *
8012 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
8013 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8015 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8016 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
8017 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
8019 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
8020 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8022 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8023 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
8024 466785b9 2020-12-10 jrick if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
8025 466785b9 2020-12-10 jrick histedit_cmd = "fold";
8026 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8029 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8030 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8031 083957f4 2020-02-24 stsp if (n < 0) {
8032 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
8038 0ebf8283 2019-07-24 stsp return err;
8041 0ebf8283 2019-07-24 stsp static const struct got_error *
8042 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
8043 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
8045 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8048 514f2ffe 2020-01-29 stsp char *id_str;
8049 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
8051 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
8052 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
8054 514f2ffe 2020-01-29 stsp return err;
8056 514f2ffe 2020-01-29 stsp n = fprintf(f,
8057 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
8058 514f2ffe 2020-01-29 stsp "# commit %s\n"
8059 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
8060 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
8061 514f2ffe 2020-01-29 stsp if (n < 0) {
8062 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8063 514f2ffe 2020-01-29 stsp goto done;
8066 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
8067 514f2ffe 2020-01-29 stsp if (n < 0) {
8068 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8069 514f2ffe 2020-01-29 stsp goto done;
8072 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8073 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8074 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8075 0ebf8283 2019-07-24 stsp cmd->desc);
8076 0ebf8283 2019-07-24 stsp if (n < 0) {
8077 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8082 514f2ffe 2020-01-29 stsp free(id_str);
8083 0ebf8283 2019-07-24 stsp return err;
8086 0ebf8283 2019-07-24 stsp static const struct got_error *
8087 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
8089 0ebf8283 2019-07-24 stsp static char msg[42];
8092 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8094 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
8095 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8097 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8100 0ebf8283 2019-07-24 stsp static const struct got_error *
8101 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8102 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
8104 0ebf8283 2019-07-24 stsp const struct got_error *err;
8105 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
8106 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
8108 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8110 0ebf8283 2019-07-24 stsp return err;
8112 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8114 0ebf8283 2019-07-24 stsp goto done;
8116 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8118 5943eee2 2019-08-13 stsp goto done;
8119 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8120 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8121 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
8122 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8125 0ebf8283 2019-07-24 stsp if (folded_commit)
8126 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
8127 0ebf8283 2019-07-24 stsp free(id_str);
8128 5943eee2 2019-08-13 stsp free(folded_logmsg);
8129 0ebf8283 2019-07-24 stsp return err;
8132 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
8133 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
8135 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
8137 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
8138 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8139 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
8140 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8141 3f9de99f 2019-07-24 stsp folded = prev;
8142 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
8145 0ebf8283 2019-07-24 stsp return folded;
8148 0ebf8283 2019-07-24 stsp static const struct got_error *
8149 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8150 0ebf8283 2019-07-24 stsp struct got_repository *repo)
8152 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8153 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8154 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8155 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8156 1601cb9f 2020-09-11 naddy int logmsg_len;
8158 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
8160 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8162 0ebf8283 2019-07-24 stsp return err;
8164 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
8165 0ebf8283 2019-07-24 stsp if (folded) {
8166 0ebf8283 2019-07-24 stsp while (folded != hle) {
8167 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8168 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8171 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
8172 0ebf8283 2019-07-24 stsp logmsg, repo);
8174 0ebf8283 2019-07-24 stsp goto done;
8175 0ebf8283 2019-07-24 stsp free(logmsg);
8176 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8177 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8181 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8183 0ebf8283 2019-07-24 stsp goto done;
8184 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8186 5943eee2 2019-08-13 stsp goto done;
8187 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
8188 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
8189 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
8190 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
8191 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8192 0ebf8283 2019-07-24 stsp goto done;
8194 0ebf8283 2019-07-24 stsp free(logmsg);
8195 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8197 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8199 0ebf8283 2019-07-24 stsp goto done;
8201 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
8202 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
8204 0ebf8283 2019-07-24 stsp goto done;
8206 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
8207 0ebf8283 2019-07-24 stsp close(fd);
8209 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8211 0ebf8283 2019-07-24 stsp goto done;
8213 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8214 0d5bb276 2020-12-15 stsp logmsg_len, 0);
8215 0ebf8283 2019-07-24 stsp if (err) {
8216 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8217 0ebf8283 2019-07-24 stsp goto done;
8218 71392a05 2020-12-13 stsp err = NULL;
8219 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
8220 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
8221 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
8224 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8225 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
8226 0ebf8283 2019-07-24 stsp free(logmsg_path);
8227 0ebf8283 2019-07-24 stsp free(logmsg);
8228 5943eee2 2019-08-13 stsp free(orig_logmsg);
8229 0ebf8283 2019-07-24 stsp free(editor);
8230 0ebf8283 2019-07-24 stsp if (commit)
8231 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8232 0ebf8283 2019-07-24 stsp return err;
8235 0ebf8283 2019-07-24 stsp static const struct got_error *
8236 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8237 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8239 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8240 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8241 6059809a 2020-12-17 stsp size_t i, size;
8242 0ebf8283 2019-07-24 stsp ssize_t len;
8243 6059809a 2020-12-17 stsp int lineno = 0;
8244 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8245 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8246 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8248 0ebf8283 2019-07-24 stsp for (;;) {
8249 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8250 0ebf8283 2019-07-24 stsp if (len == -1) {
8251 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8252 0ebf8283 2019-07-24 stsp if (feof(f))
8254 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8255 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8260 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8262 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8263 0ebf8283 2019-07-24 stsp free(line);
8264 0ebf8283 2019-07-24 stsp line = NULL;
8267 0ebf8283 2019-07-24 stsp cmd = NULL;
8268 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8269 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8270 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8271 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8272 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8275 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8280 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8281 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8284 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8286 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8287 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8288 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8291 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8292 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8296 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8297 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8298 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8302 0ebf8283 2019-07-24 stsp free(line);
8303 0ebf8283 2019-07-24 stsp line = NULL;
8307 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8309 0ebf8283 2019-07-24 stsp *end = '\0';
8311 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8312 0ebf8283 2019-07-24 stsp if (err) {
8313 0ebf8283 2019-07-24 stsp /* override error code */
8314 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8318 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8319 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8320 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8323 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8324 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8325 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8326 0ebf8283 2019-07-24 stsp commit_id = NULL;
8327 0ebf8283 2019-07-24 stsp free(line);
8328 0ebf8283 2019-07-24 stsp line = NULL;
8329 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8332 0ebf8283 2019-07-24 stsp free(line);
8333 0ebf8283 2019-07-24 stsp free(commit_id);
8334 0ebf8283 2019-07-24 stsp return err;
8337 0ebf8283 2019-07-24 stsp static const struct got_error *
8338 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8339 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8341 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8342 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8343 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8344 5b87815e 2020-03-05 stsp static char msg[92];
8345 bfce7f83 2019-07-27 stsp char *id_str;
8347 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8348 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8349 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8350 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8351 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8353 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8354 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8355 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8356 5b87815e 2020-03-05 stsp if (hle == hle2)
8358 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8359 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8361 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8363 5b87815e 2020-03-05 stsp return err;
8364 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8365 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8366 5b87815e 2020-03-05 stsp free(id_str);
8367 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8371 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8372 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8373 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8376 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8377 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8379 bfce7f83 2019-07-27 stsp return err;
8380 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8381 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8382 bfce7f83 2019-07-27 stsp free(id_str);
8383 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8387 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8388 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8389 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8390 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8392 bfce7f83 2019-07-27 stsp return NULL;
8395 bfce7f83 2019-07-27 stsp static const struct got_error *
8396 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8397 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8398 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8400 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8401 0ebf8283 2019-07-24 stsp char *editor;
8402 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8404 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8406 0ebf8283 2019-07-24 stsp return err;
8408 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8409 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8410 0ebf8283 2019-07-24 stsp goto done;
8413 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8414 0ebf8283 2019-07-24 stsp if (f == NULL) {
8415 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8416 0ebf8283 2019-07-24 stsp goto done;
8418 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8420 bfce7f83 2019-07-27 stsp goto done;
8422 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8424 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8425 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8426 0ebf8283 2019-07-24 stsp free(editor);
8427 0ebf8283 2019-07-24 stsp return err;
8430 0ebf8283 2019-07-24 stsp static const struct got_error *
8431 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8432 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8433 514f2ffe 2020-01-29 stsp struct got_repository *);
8435 0ebf8283 2019-07-24 stsp static const struct got_error *
8436 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8437 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8438 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
8440 0ebf8283 2019-07-24 stsp const struct got_error *err;
8441 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8442 0ebf8283 2019-07-24 stsp char *path = NULL;
8444 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8446 0ebf8283 2019-07-24 stsp return err;
8448 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8450 0ebf8283 2019-07-24 stsp goto done;
8452 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8453 466785b9 2020-12-10 jrick fold_only, repo);
8455 0ebf8283 2019-07-24 stsp goto done;
8457 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
8458 083957f4 2020-02-24 stsp rewind(f);
8459 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8461 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8462 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8463 0ebf8283 2019-07-24 stsp goto done;
8466 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8467 083957f4 2020-02-24 stsp if (err) {
8468 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8469 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8470 083957f4 2020-02-24 stsp goto done;
8471 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8472 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8476 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8477 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8478 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8479 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8480 0ebf8283 2019-07-24 stsp free(path);
8481 0ebf8283 2019-07-24 stsp return err;
8484 0ebf8283 2019-07-24 stsp static const struct got_error *
8485 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8486 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8488 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8489 0ebf8283 2019-07-24 stsp char *path = NULL;
8490 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8491 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8492 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8494 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8496 0ebf8283 2019-07-24 stsp return err;
8498 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8499 0ebf8283 2019-07-24 stsp if (f == NULL) {
8500 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8501 0ebf8283 2019-07-24 stsp goto done;
8503 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8504 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8509 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8510 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8511 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8512 0ebf8283 2019-07-24 stsp if (n < 0) {
8513 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8519 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8520 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8521 0ebf8283 2019-07-24 stsp free(path);
8522 0ebf8283 2019-07-24 stsp if (commit)
8523 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8524 0ebf8283 2019-07-24 stsp return err;
8528 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8530 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8532 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8533 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8534 bfce7f83 2019-07-27 stsp free(hle);
8538 0ebf8283 2019-07-24 stsp static const struct got_error *
8539 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8540 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8542 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8543 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8545 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8546 0ebf8283 2019-07-24 stsp if (f == NULL) {
8547 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8548 0ebf8283 2019-07-24 stsp goto done;
8551 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8553 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8554 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8555 0ebf8283 2019-07-24 stsp return err;
8558 0ebf8283 2019-07-24 stsp static const struct got_error *
8559 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8560 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8561 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8563 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8564 0ebf8283 2019-07-24 stsp int resp = ' ';
8566 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8567 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8568 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8569 0ebf8283 2019-07-24 stsp resp = getchar();
8570 a61a4414 2019-08-07 stsp if (resp == '\n')
8571 a61a4414 2019-08-07 stsp resp = getchar();
8572 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8573 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8574 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8576 426ebf2e 2019-07-25 stsp if (err) {
8577 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8578 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8580 bfce7f83 2019-07-27 stsp prev_err = err;
8581 426ebf2e 2019-07-25 stsp resp = ' ';
8585 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8586 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8587 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8588 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
8589 426ebf2e 2019-07-25 stsp if (err) {
8590 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8591 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8593 bfce7f83 2019-07-27 stsp prev_err = err;
8594 426ebf2e 2019-07-25 stsp resp = ' ';
8598 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8599 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8602 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8605 0ebf8283 2019-07-24 stsp return err;
8608 0ebf8283 2019-07-24 stsp static const struct got_error *
8609 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8610 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8611 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8613 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8614 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8615 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8616 3e3a69f1 2019-07-25 stsp branch, repo);
8619 0ebf8283 2019-07-24 stsp static const struct got_error *
8620 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8621 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8623 0ebf8283 2019-07-24 stsp const struct got_error *err;
8624 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8626 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8628 0ebf8283 2019-07-24 stsp goto done;
8630 0ebf8283 2019-07-24 stsp if (new_id) {
8631 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8633 0ebf8283 2019-07-24 stsp goto done;
8636 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8637 0ebf8283 2019-07-24 stsp if (new_id_str)
8638 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8640 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8641 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8642 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8643 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8644 0ebf8283 2019-07-24 stsp goto done;
8646 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8648 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8650 0ebf8283 2019-07-24 stsp goto done;
8653 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8654 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8655 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8656 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8657 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8659 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8660 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8661 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8668 0ebf8283 2019-07-24 stsp free(old_id_str);
8669 0ebf8283 2019-07-24 stsp free(new_id_str);
8670 0ebf8283 2019-07-24 stsp return err;
8673 0ebf8283 2019-07-24 stsp static const struct got_error *
8674 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8675 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8676 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8677 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8679 0ebf8283 2019-07-24 stsp const struct got_error *err;
8680 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8681 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8683 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8684 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8685 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8687 0ebf8283 2019-07-24 stsp return err;
8690 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8692 0ebf8283 2019-07-24 stsp return err;
8694 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8695 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8696 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8697 0ebf8283 2019-07-24 stsp if (err) {
8698 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8699 0ebf8283 2019-07-24 stsp goto done;
8700 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8702 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8703 0ebf8283 2019-07-24 stsp free(new_commit_id);
8706 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8707 0ebf8283 2019-07-24 stsp return err;
8710 0ebf8283 2019-07-24 stsp static const struct got_error *
8711 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8712 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8714 0ebf8283 2019-07-24 stsp const struct got_error *error;
8715 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8717 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8719 0ebf8283 2019-07-24 stsp if (error)
8720 0ebf8283 2019-07-24 stsp return error;
8722 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8723 0ebf8283 2019-07-24 stsp if (error)
8724 0ebf8283 2019-07-24 stsp return error;
8726 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8727 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8728 0ebf8283 2019-07-24 stsp return error;
8731 ab20a43a 2020-01-29 stsp static const struct got_error *
8732 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8733 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8734 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8735 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8737 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8739 ab20a43a 2020-01-29 stsp switch (status) {
8740 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8741 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8742 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8743 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8744 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8745 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8750 ab20a43a 2020-01-29 stsp switch (staged_status) {
8751 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8752 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8753 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8754 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8755 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8760 ab20a43a 2020-01-29 stsp return NULL;
8763 0ebf8283 2019-07-24 stsp static const struct got_error *
8764 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8766 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8767 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8768 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8769 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8770 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8771 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8772 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8773 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8774 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8775 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8776 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8777 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8778 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8779 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8780 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
8781 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8782 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8783 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8784 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8785 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8786 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8787 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8788 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8790 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8791 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8792 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8793 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8795 466785b9 2020-12-10 jrick while ((ch = getopt(argc, argv, "acfF:m")) != -1) {
8796 0ebf8283 2019-07-24 stsp switch (ch) {
8798 0ebf8283 2019-07-24 stsp abort_edit = 1;
8801 0ebf8283 2019-07-24 stsp continue_edit = 1;
8803 466785b9 2020-12-10 jrick case 'f':
8804 466785b9 2020-12-10 jrick fold_only = 1;
8807 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8810 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8813 0ebf8283 2019-07-24 stsp usage_histedit();
8814 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8818 0ebf8283 2019-07-24 stsp argc -= optind;
8819 0ebf8283 2019-07-24 stsp argv += optind;
8821 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8822 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8823 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8824 0ebf8283 2019-07-24 stsp err(1, "pledge");
8826 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8827 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
8828 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8829 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
8830 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8831 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
8832 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8833 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
8834 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
8835 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
8836 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
8837 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
8838 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
8839 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
8840 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
8841 b3805337 2020-12-13 stsp option_conflict('F', 'f');
8842 0ebf8283 2019-07-24 stsp if (argc != 0)
8843 0ebf8283 2019-07-24 stsp usage_histedit();
8846 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8847 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8848 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8849 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8850 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8851 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8854 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8855 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8856 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8857 0ebf8283 2019-07-24 stsp goto done;
8859 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8860 fa51e947 2020-03-27 stsp if (error) {
8861 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8862 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8863 0ebf8283 2019-07-24 stsp goto done;
8866 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8868 0ebf8283 2019-07-24 stsp if (error != NULL)
8869 0ebf8283 2019-07-24 stsp goto done;
8871 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8872 0ebf8283 2019-07-24 stsp if (error)
8873 0ebf8283 2019-07-24 stsp goto done;
8874 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8875 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8876 0ebf8283 2019-07-24 stsp goto done;
8879 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8880 0ebf8283 2019-07-24 stsp if (error)
8881 0ebf8283 2019-07-24 stsp goto done;
8883 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8884 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8885 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8886 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8887 083957f4 2020-02-24 stsp "before the -m option can be used");
8888 083957f4 2020-02-24 stsp goto done;
8890 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
8891 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8892 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
8893 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
8894 466785b9 2020-12-10 jrick "before the -f option can be used");
8895 466785b9 2020-12-10 jrick goto done;
8898 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8899 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8900 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8901 3e3a69f1 2019-07-25 stsp worktree, repo);
8902 0ebf8283 2019-07-24 stsp if (error)
8903 0ebf8283 2019-07-24 stsp goto done;
8904 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8905 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8906 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8907 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8908 0ebf8283 2019-07-24 stsp if (error)
8909 0ebf8283 2019-07-24 stsp goto done;
8910 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8911 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8912 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8913 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8914 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8915 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8916 0ebf8283 2019-07-24 stsp goto done;
8919 0ebf8283 2019-07-24 stsp if (continue_edit) {
8920 0ebf8283 2019-07-24 stsp char *path;
8922 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8923 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8924 0ebf8283 2019-07-24 stsp goto done;
8927 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8928 0ebf8283 2019-07-24 stsp if (error)
8929 0ebf8283 2019-07-24 stsp goto done;
8931 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8932 0ebf8283 2019-07-24 stsp free(path);
8933 0ebf8283 2019-07-24 stsp if (error)
8934 0ebf8283 2019-07-24 stsp goto done;
8936 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8937 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8938 3e3a69f1 2019-07-25 stsp worktree, repo);
8939 0ebf8283 2019-07-24 stsp if (error)
8940 0ebf8283 2019-07-24 stsp goto done;
8942 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8943 0ebf8283 2019-07-24 stsp if (error)
8944 0ebf8283 2019-07-24 stsp goto done;
8946 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8947 0ebf8283 2019-07-24 stsp head_commit_id);
8948 0ebf8283 2019-07-24 stsp if (error)
8949 0ebf8283 2019-07-24 stsp goto done;
8950 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8951 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8952 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8953 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8954 3aac7cf7 2019-07-25 stsp goto done;
8956 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8957 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8958 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8959 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8960 0ebf8283 2019-07-24 stsp commit = NULL;
8961 0ebf8283 2019-07-24 stsp if (error)
8962 0ebf8283 2019-07-24 stsp goto done;
8964 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8965 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8966 0ebf8283 2019-07-24 stsp goto done;
8969 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8970 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8971 0ebf8283 2019-07-24 stsp if (error != NULL)
8972 0ebf8283 2019-07-24 stsp goto done;
8974 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8975 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8976 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8977 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8978 c7d20a3f 2019-07-30 stsp goto done;
8981 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8982 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8983 bfce7f83 2019-07-27 stsp branch = NULL;
8984 0ebf8283 2019-07-24 stsp if (error)
8985 0ebf8283 2019-07-24 stsp goto done;
8987 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8988 0ebf8283 2019-07-24 stsp head_commit_id);
8989 0ebf8283 2019-07-24 stsp if (error)
8990 0ebf8283 2019-07-24 stsp goto done;
8991 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8992 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8993 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8994 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8995 3aac7cf7 2019-07-25 stsp goto done;
8997 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8998 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8999 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
9000 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
9001 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9002 0ebf8283 2019-07-24 stsp commit = NULL;
9003 0ebf8283 2019-07-24 stsp if (error)
9004 0ebf8283 2019-07-24 stsp goto done;
9006 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
9007 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9008 c5996fff 2020-01-29 stsp goto done;
9011 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9012 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
9013 bfce7f83 2019-07-27 stsp if (error)
9014 bfce7f83 2019-07-27 stsp goto done;
9016 0ebf8283 2019-07-24 stsp if (edit_script_path) {
9017 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
9018 0ebf8283 2019-07-24 stsp edit_script_path, repo);
9019 6ba2bea2 2019-07-27 stsp if (error) {
9020 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9021 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9022 9627c110 2020-04-18 stsp update_progress, &upa);
9023 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9024 0ebf8283 2019-07-24 stsp goto done;
9027 514f2ffe 2020-01-29 stsp const char *branch_name;
9028 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
9029 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
9030 514f2ffe 2020-01-29 stsp branch_name += 11;
9031 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
9032 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
9033 6ba2bea2 2019-07-27 stsp if (error) {
9034 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9035 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9036 9627c110 2020-04-18 stsp update_progress, &upa);
9037 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9038 0ebf8283 2019-07-24 stsp goto done;
9043 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
9045 6ba2bea2 2019-07-27 stsp if (error) {
9046 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9047 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9048 9627c110 2020-04-18 stsp update_progress, &upa);
9049 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9050 0ebf8283 2019-07-24 stsp goto done;
9055 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
9056 4ec14e60 2019-08-28 hiltjo if (error)
9057 0ebf8283 2019-07-24 stsp goto done;
9059 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9060 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
9061 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
9062 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
9065 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
9066 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9067 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
9068 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
9070 ab20a43a 2020-01-29 stsp if (error)
9071 ab20a43a 2020-01-29 stsp goto done;
9073 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
9074 ab20a43a 2020-01-29 stsp int have_changes = 0;
9076 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
9077 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
9078 ab20a43a 2020-01-29 stsp if (error)
9079 ab20a43a 2020-01-29 stsp goto done;
9080 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
9081 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
9082 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
9083 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
9084 ab20a43a 2020-01-29 stsp if (error) {
9085 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
9086 ab20a43a 2020-01-29 stsp goto done;
9087 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
9088 ab20a43a 2020-01-29 stsp goto done;
9090 ab20a43a 2020-01-29 stsp if (have_changes) {
9091 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
9092 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
9093 ab20a43a 2020-01-29 stsp if (error)
9094 ab20a43a 2020-01-29 stsp goto done;
9096 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
9097 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
9098 ab20a43a 2020-01-29 stsp if (error)
9099 ab20a43a 2020-01-29 stsp goto done;
9100 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
9101 ab20a43a 2020-01-29 stsp hle, NULL);
9102 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
9103 ab20a43a 2020-01-29 stsp commit = NULL;
9104 ab20a43a 2020-01-29 stsp if (error)
9105 ab20a43a 2020-01-29 stsp goto done;
9111 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9112 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9113 0ebf8283 2019-07-24 stsp if (error)
9114 0ebf8283 2019-07-24 stsp goto done;
9118 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9119 0ebf8283 2019-07-24 stsp hle->commit_id);
9120 0ebf8283 2019-07-24 stsp if (error)
9121 0ebf8283 2019-07-24 stsp goto done;
9122 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9123 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
9125 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
9126 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
9127 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9128 0ebf8283 2019-07-24 stsp if (error)
9129 0ebf8283 2019-07-24 stsp goto done;
9130 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9131 0ebf8283 2019-07-24 stsp commit = NULL;
9133 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9134 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
9135 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
9137 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9138 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
9140 a0ea4fc0 2020-02-28 stsp if (error)
9141 a0ea4fc0 2020-02-28 stsp goto done;
9142 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9146 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9147 0ebf8283 2019-07-24 stsp char *id_str;
9148 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
9149 0ebf8283 2019-07-24 stsp if (error)
9150 0ebf8283 2019-07-24 stsp goto done;
9151 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
9153 0ebf8283 2019-07-24 stsp free(id_str);
9154 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9155 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
9156 3e3a69f1 2019-07-25 stsp fileindex);
9157 0ebf8283 2019-07-24 stsp goto done;
9160 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9161 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9162 0ebf8283 2019-07-24 stsp if (error)
9163 0ebf8283 2019-07-24 stsp goto done;
9167 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
9168 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
9169 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9170 0ebf8283 2019-07-24 stsp if (error)
9171 0ebf8283 2019-07-24 stsp goto done;
9174 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9175 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
9176 0ebf8283 2019-07-24 stsp if (error)
9177 0ebf8283 2019-07-24 stsp goto done;
9178 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9179 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
9181 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
9182 3e3a69f1 2019-07-25 stsp branch, repo);
9184 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
9185 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
9186 0ebf8283 2019-07-24 stsp free(head_commit_id);
9187 0ebf8283 2019-07-24 stsp free(base_commit_id);
9188 0ebf8283 2019-07-24 stsp free(resume_commit_id);
9189 0ebf8283 2019-07-24 stsp if (commit)
9190 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9191 0ebf8283 2019-07-24 stsp if (branch)
9192 0ebf8283 2019-07-24 stsp got_ref_close(branch);
9193 0ebf8283 2019-07-24 stsp if (tmp_branch)
9194 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
9195 0ebf8283 2019-07-24 stsp if (worktree)
9196 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
9198 2822a352 2019-10-15 stsp got_repo_close(repo);
9199 2822a352 2019-10-15 stsp return error;
9202 2822a352 2019-10-15 stsp __dead static void
9203 2822a352 2019-10-15 stsp usage_integrate(void)
9205 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9209 2822a352 2019-10-15 stsp static const struct got_error *
9210 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
9212 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
9213 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
9214 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
9215 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9216 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
9217 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9218 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
9219 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9221 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9223 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9224 2822a352 2019-10-15 stsp switch (ch) {
9226 2822a352 2019-10-15 stsp usage_integrate();
9227 2822a352 2019-10-15 stsp /* NOTREACHED */
9231 2822a352 2019-10-15 stsp argc -= optind;
9232 2822a352 2019-10-15 stsp argv += optind;
9234 2822a352 2019-10-15 stsp if (argc != 1)
9235 2822a352 2019-10-15 stsp usage_integrate();
9236 2822a352 2019-10-15 stsp branch_arg = argv[0];
9237 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
9238 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9239 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
9240 2822a352 2019-10-15 stsp err(1, "pledge");
9242 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
9243 2822a352 2019-10-15 stsp if (cwd == NULL) {
9244 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
9245 2822a352 2019-10-15 stsp goto done;
9248 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
9249 fa51e947 2020-03-27 stsp if (error) {
9250 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9251 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
9253 2822a352 2019-10-15 stsp goto done;
9256 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9257 2822a352 2019-10-15 stsp if (error)
9258 2822a352 2019-10-15 stsp goto done;
9260 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9262 2822a352 2019-10-15 stsp if (error != NULL)
9263 2822a352 2019-10-15 stsp goto done;
9265 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9266 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9267 2822a352 2019-10-15 stsp if (error)
9268 2822a352 2019-10-15 stsp goto done;
9270 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9271 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9272 2822a352 2019-10-15 stsp goto done;
9275 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9276 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9277 2822a352 2019-10-15 stsp if (error)
9278 2822a352 2019-10-15 stsp goto done;
9280 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9281 2822a352 2019-10-15 stsp if (refname == NULL) {
9282 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9283 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9284 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9285 2822a352 2019-10-15 stsp goto done;
9287 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9288 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9289 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9290 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9291 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9292 2822a352 2019-10-15 stsp goto done;
9295 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9296 2822a352 2019-10-15 stsp if (error)
9297 2822a352 2019-10-15 stsp goto done;
9299 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9300 2822a352 2019-10-15 stsp if (error)
9301 2822a352 2019-10-15 stsp goto done;
9303 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9304 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9305 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9306 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9307 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9308 2822a352 2019-10-15 stsp goto done;
9311 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9312 2822a352 2019-10-15 stsp if (error) {
9313 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9314 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9315 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9316 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9317 2822a352 2019-10-15 stsp goto done;
9320 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9321 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9322 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9323 2822a352 2019-10-15 stsp check_cancelled, NULL);
9324 2822a352 2019-10-15 stsp if (error)
9325 2822a352 2019-10-15 stsp goto done;
9327 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9328 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9331 715dc77e 2019-08-03 stsp got_repo_close(repo);
9332 2822a352 2019-10-15 stsp if (worktree)
9333 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9334 2822a352 2019-10-15 stsp free(cwd);
9335 2822a352 2019-10-15 stsp free(base_commit_id);
9336 2822a352 2019-10-15 stsp free(commit_id);
9337 2822a352 2019-10-15 stsp free(refname);
9338 2822a352 2019-10-15 stsp free(base_refname);
9339 715dc77e 2019-08-03 stsp return error;
9342 715dc77e 2019-08-03 stsp __dead static void
9343 715dc77e 2019-08-03 stsp usage_stage(void)
9345 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9346 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9347 715dc77e 2019-08-03 stsp getprogname());
9351 715dc77e 2019-08-03 stsp static const struct got_error *
9352 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9353 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9354 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9355 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9357 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9358 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9360 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9361 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9362 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9363 408b4ebc 2019-08-03 stsp return NULL;
9365 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9366 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9367 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9369 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9371 408b4ebc 2019-08-03 stsp return err;
9373 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9374 408b4ebc 2019-08-03 stsp free(id_str);
9375 dc424a06 2019-08-07 stsp return NULL;
9378 dc424a06 2019-08-07 stsp static const struct got_error *
9379 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9381 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9382 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9383 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9384 715dc77e 2019-08-03 stsp char *cwd = NULL;
9385 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9386 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9387 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9388 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9389 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9390 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9392 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9394 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9395 715dc77e 2019-08-03 stsp switch (ch) {
9397 408b4ebc 2019-08-03 stsp list_stage = 1;
9400 dc424a06 2019-08-07 stsp pflag = 1;
9403 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9406 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9409 715dc77e 2019-08-03 stsp usage_stage();
9410 715dc77e 2019-08-03 stsp /* NOTREACHED */
9414 715dc77e 2019-08-03 stsp argc -= optind;
9415 715dc77e 2019-08-03 stsp argv += optind;
9417 715dc77e 2019-08-03 stsp #ifndef PROFILE
9418 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9419 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9420 715dc77e 2019-08-03 stsp err(1, "pledge");
9422 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9423 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9424 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9425 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9427 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9428 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9429 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9430 715dc77e 2019-08-03 stsp goto done;
9433 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9434 fa51e947 2020-03-27 stsp if (error) {
9435 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9436 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9437 715dc77e 2019-08-03 stsp goto done;
9440 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9442 715dc77e 2019-08-03 stsp if (error != NULL)
9443 715dc77e 2019-08-03 stsp goto done;
9445 dc424a06 2019-08-07 stsp if (patch_script_path) {
9446 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9447 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9448 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9449 dc424a06 2019-08-07 stsp patch_script_path);
9450 dc424a06 2019-08-07 stsp goto done;
9453 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9454 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9455 715dc77e 2019-08-03 stsp if (error)
9456 715dc77e 2019-08-03 stsp goto done;
9458 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9459 6d022e97 2019-08-04 stsp if (error)
9460 6d022e97 2019-08-04 stsp goto done;
9462 6d022e97 2019-08-04 stsp if (list_stage)
9463 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9464 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9466 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9467 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9468 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9469 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9470 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9471 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9474 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9475 2e1f37b0 2019-08-08 stsp error == NULL)
9476 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9478 ad493afc 2019-08-03 stsp got_repo_close(repo);
9479 ad493afc 2019-08-03 stsp if (worktree)
9480 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9481 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9482 ad493afc 2019-08-03 stsp free((char *)pe->path);
9483 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9484 ad493afc 2019-08-03 stsp free(cwd);
9485 ad493afc 2019-08-03 stsp return error;
9488 ad493afc 2019-08-03 stsp __dead static void
9489 ad493afc 2019-08-03 stsp usage_unstage(void)
9491 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9492 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9493 ad493afc 2019-08-03 stsp getprogname());
9498 ad493afc 2019-08-03 stsp static const struct got_error *
9499 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9501 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9502 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9503 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9504 ad493afc 2019-08-03 stsp char *cwd = NULL;
9505 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9506 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9507 9627c110 2020-04-18 stsp int ch, pflag = 0;
9508 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9509 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9510 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9511 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9513 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9515 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9516 ad493afc 2019-08-03 stsp switch (ch) {
9518 2e1f37b0 2019-08-08 stsp pflag = 1;
9521 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9524 ad493afc 2019-08-03 stsp usage_unstage();
9525 ad493afc 2019-08-03 stsp /* NOTREACHED */
9529 ad493afc 2019-08-03 stsp argc -= optind;
9530 ad493afc 2019-08-03 stsp argv += optind;
9532 ad493afc 2019-08-03 stsp #ifndef PROFILE
9533 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9534 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9535 ad493afc 2019-08-03 stsp err(1, "pledge");
9537 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9538 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9540 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9541 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9542 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9543 ad493afc 2019-08-03 stsp goto done;
9546 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9547 fa51e947 2020-03-27 stsp if (error) {
9548 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9549 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9550 ad493afc 2019-08-03 stsp goto done;
9553 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9555 ad493afc 2019-08-03 stsp if (error != NULL)
9556 ad493afc 2019-08-03 stsp goto done;
9558 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9559 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9560 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9561 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9562 2e1f37b0 2019-08-08 stsp patch_script_path);
9563 2e1f37b0 2019-08-08 stsp goto done;
9567 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9568 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9569 ad493afc 2019-08-03 stsp if (error)
9570 ad493afc 2019-08-03 stsp goto done;
9572 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9573 ad493afc 2019-08-03 stsp if (error)
9574 ad493afc 2019-08-03 stsp goto done;
9576 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9577 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9578 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9579 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9580 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9581 9627c110 2020-04-18 stsp if (!error)
9582 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9584 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9585 2e1f37b0 2019-08-08 stsp error == NULL)
9586 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9588 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9589 715dc77e 2019-08-03 stsp if (worktree)
9590 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9591 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9592 715dc77e 2019-08-03 stsp free((char *)pe->path);
9593 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9594 715dc77e 2019-08-03 stsp free(cwd);
9595 01073a5d 2019-08-22 stsp return error;
9598 01073a5d 2019-08-22 stsp __dead static void
9599 01073a5d 2019-08-22 stsp usage_cat(void)
9601 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9602 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9606 01073a5d 2019-08-22 stsp static const struct got_error *
9607 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9609 01073a5d 2019-08-22 stsp const struct got_error *err;
9610 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9612 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9614 01073a5d 2019-08-22 stsp return err;
9616 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9617 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9618 01073a5d 2019-08-22 stsp return err;
9621 01073a5d 2019-08-22 stsp static const struct got_error *
9622 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9624 01073a5d 2019-08-22 stsp const struct got_error *err;
9625 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9626 56e0773d 2019-11-28 stsp int nentries, i;
9628 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9630 01073a5d 2019-08-22 stsp return err;
9632 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9633 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9634 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9635 01073a5d 2019-08-22 stsp char *id_str;
9636 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9638 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9639 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9642 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9643 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9644 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9645 01073a5d 2019-08-22 stsp free(id_str);
9648 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9649 01073a5d 2019-08-22 stsp return err;
9652 01073a5d 2019-08-22 stsp static const struct got_error *
9653 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9655 01073a5d 2019-08-22 stsp const struct got_error *err;
9656 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9657 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9658 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9659 01073a5d 2019-08-22 stsp char *id_str = NULL;
9660 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9662 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9664 01073a5d 2019-08-22 stsp return err;
9666 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9668 01073a5d 2019-08-22 stsp goto done;
9670 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9671 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9672 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9673 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9674 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9675 01073a5d 2019-08-22 stsp char *pid_str;
9676 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9678 01073a5d 2019-08-22 stsp goto done;
9679 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9680 01073a5d 2019-08-22 stsp free(pid_str);
9682 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9683 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9684 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
9686 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9687 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9688 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
9690 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9691 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9692 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9694 01073a5d 2019-08-22 stsp free(id_str);
9695 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9696 01073a5d 2019-08-22 stsp return err;
9699 01073a5d 2019-08-22 stsp static const struct got_error *
9700 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9702 01073a5d 2019-08-22 stsp const struct got_error *err;
9703 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9704 01073a5d 2019-08-22 stsp char *id_str = NULL;
9705 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9707 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9709 01073a5d 2019-08-22 stsp return err;
9711 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9713 01073a5d 2019-08-22 stsp goto done;
9715 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9717 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9718 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9719 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9720 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9722 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9723 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9724 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9726 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9727 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9728 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9730 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9731 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9732 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9738 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9739 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9741 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9742 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9743 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
9745 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9746 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9747 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9749 01073a5d 2019-08-22 stsp free(id_str);
9750 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9751 01073a5d 2019-08-22 stsp return err;
9754 01073a5d 2019-08-22 stsp static const struct got_error *
9755 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9757 01073a5d 2019-08-22 stsp const struct got_error *error;
9758 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9759 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9760 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9761 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9762 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9763 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9764 84de9106 2020-12-26 stsp struct got_reflist_head refs;
9766 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
9768 01073a5d 2019-08-22 stsp #ifndef PROFILE
9769 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9770 01073a5d 2019-08-22 stsp NULL) == -1)
9771 01073a5d 2019-08-22 stsp err(1, "pledge");
9774 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9775 01073a5d 2019-08-22 stsp switch (ch) {
9777 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9780 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9781 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9782 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9784 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9787 896e9b6f 2019-08-26 stsp force_path = 1;
9790 01073a5d 2019-08-22 stsp usage_cat();
9791 01073a5d 2019-08-22 stsp /* NOTREACHED */
9795 01073a5d 2019-08-22 stsp argc -= optind;
9796 01073a5d 2019-08-22 stsp argv += optind;
9798 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9799 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9800 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9801 01073a5d 2019-08-22 stsp goto done;
9803 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9804 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9805 01073a5d 2019-08-22 stsp goto done;
9806 01073a5d 2019-08-22 stsp if (worktree) {
9807 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9808 01073a5d 2019-08-22 stsp repo_path = strdup(
9809 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9810 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9811 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9812 01073a5d 2019-08-22 stsp goto done;
9817 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9818 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9819 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9820 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9823 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9824 01073a5d 2019-08-22 stsp free(repo_path);
9825 01073a5d 2019-08-22 stsp if (error != NULL)
9826 01073a5d 2019-08-22 stsp goto done;
9828 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9829 896e9b6f 2019-08-26 stsp if (error)
9830 896e9b6f 2019-08-26 stsp goto done;
9832 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9833 84de9106 2020-12-26 stsp if (error)
9834 84de9106 2020-12-26 stsp goto done;
9836 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9837 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9838 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9839 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
9840 01073a5d 2019-08-22 stsp if (error)
9841 01073a5d 2019-08-22 stsp goto done;
9843 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9844 896e9b6f 2019-08-26 stsp if (force_path) {
9845 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9847 896e9b6f 2019-08-26 stsp if (error)
9850 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9851 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
9853 896e9b6f 2019-08-26 stsp if (error) {
9854 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9855 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9857 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9858 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9859 896e9b6f 2019-08-26 stsp if (error)
9864 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9865 01073a5d 2019-08-22 stsp if (error)
9868 01073a5d 2019-08-22 stsp switch (obj_type) {
9869 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9870 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9872 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9873 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9875 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9876 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9878 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9879 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9882 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9885 01073a5d 2019-08-22 stsp if (error)
9887 01073a5d 2019-08-22 stsp free(label);
9888 01073a5d 2019-08-22 stsp label = NULL;
9890 01073a5d 2019-08-22 stsp id = NULL;
9893 01073a5d 2019-08-22 stsp free(label);
9895 896e9b6f 2019-08-26 stsp free(commit_id);
9896 01073a5d 2019-08-22 stsp if (worktree)
9897 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9898 01073a5d 2019-08-22 stsp if (repo) {
9899 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9900 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9901 01073a5d 2019-08-22 stsp if (error == NULL)
9902 01073a5d 2019-08-22 stsp error = repo_error;
9904 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
9905 b2118c49 2020-07-28 stsp return error;
9908 b2118c49 2020-07-28 stsp __dead static void
9909 b2118c49 2020-07-28 stsp usage_info(void)
9911 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9912 b2118c49 2020-07-28 stsp getprogname());
9916 b2118c49 2020-07-28 stsp static const struct got_error *
9917 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9918 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9919 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9921 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9922 b2118c49 2020-07-28 stsp char *id_str = NULL;
9923 b2118c49 2020-07-28 stsp char datebuf[128];
9924 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9925 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9926 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9929 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9930 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9932 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9933 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9934 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9935 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9936 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9939 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9940 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9941 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9942 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9943 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9944 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9945 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9946 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9948 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9950 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9951 b2118c49 2020-07-28 stsp if (tm == NULL)
9952 b2118c49 2020-07-28 stsp return NULL;
9953 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9954 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9955 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9957 b2118c49 2020-07-28 stsp if (blob_id) {
9958 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9960 b2118c49 2020-07-28 stsp return err;
9961 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9962 b2118c49 2020-07-28 stsp free(id_str);
9965 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9966 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9968 b2118c49 2020-07-28 stsp return err;
9969 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9970 b2118c49 2020-07-28 stsp free(id_str);
9973 b2118c49 2020-07-28 stsp if (commit_id) {
9974 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9976 b2118c49 2020-07-28 stsp return err;
9977 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9978 b2118c49 2020-07-28 stsp free(id_str);
9981 b2118c49 2020-07-28 stsp return NULL;
9984 b2118c49 2020-07-28 stsp static const struct got_error *
9985 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9987 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9988 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9989 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9990 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9991 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9992 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9993 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9995 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9997 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9998 b2118c49 2020-07-28 stsp switch (ch) {
10000 b2118c49 2020-07-28 stsp usage_info();
10001 b2118c49 2020-07-28 stsp /* NOTREACHED */
10005 b2118c49 2020-07-28 stsp argc -= optind;
10006 b2118c49 2020-07-28 stsp argv += optind;
10008 b2118c49 2020-07-28 stsp #ifndef PROFILE
10009 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10010 b2118c49 2020-07-28 stsp NULL) == -1)
10011 b2118c49 2020-07-28 stsp err(1, "pledge");
10013 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
10014 b2118c49 2020-07-28 stsp if (cwd == NULL) {
10015 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
10016 b2118c49 2020-07-28 stsp goto done;
10019 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
10020 b2118c49 2020-07-28 stsp if (error) {
10021 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10022 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
10023 b2118c49 2020-07-28 stsp goto done;
10026 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10027 b2118c49 2020-07-28 stsp if (error)
10028 b2118c49 2020-07-28 stsp goto done;
10030 b2118c49 2020-07-28 stsp if (argc >= 1) {
10031 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
10032 b2118c49 2020-07-28 stsp worktree);
10033 b2118c49 2020-07-28 stsp if (error)
10034 b2118c49 2020-07-28 stsp goto done;
10035 b2118c49 2020-07-28 stsp show_files = 1;
10038 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
10039 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
10040 b2118c49 2020-07-28 stsp if (error)
10041 b2118c49 2020-07-28 stsp goto done;
10043 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
10044 b2118c49 2020-07-28 stsp if (error)
10045 b2118c49 2020-07-28 stsp goto done;
10047 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10048 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
10049 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
10050 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
10051 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
10052 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
10053 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
10054 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10056 b2118c49 2020-07-28 stsp if (show_files) {
10057 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10058 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10059 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
10060 b2118c49 2020-07-28 stsp continue;
10062 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
10063 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
10065 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
10066 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
10068 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
10069 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
10070 b2118c49 2020-07-28 stsp if (error)
10071 b2118c49 2020-07-28 stsp goto done;
10072 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10073 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
10074 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
10080 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
10081 b2118c49 2020-07-28 stsp free((char *)pe->path);
10082 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
10083 b2118c49 2020-07-28 stsp free(cwd);
10084 b2118c49 2020-07-28 stsp free(id_str);
10085 b2118c49 2020-07-28 stsp free(uuidstr);
10086 0ebf8283 2019-07-24 stsp return error;