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 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12463d8b 2019-12-13 stsp #include <fcntl.h>
27 12ce7a6c 2019-08-12 stsp #include <limits.h>
28 5c860e29 2018-03-12 stsp #include <locale.h>
29 818c7501 2019-07-11 stsp #include <ctype.h>
30 99437157 2018-11-11 stsp #include <signal.h>
31 5c860e29 2018-03-12 stsp #include <stdio.h>
32 5c860e29 2018-03-12 stsp #include <stdlib.h>
33 5c860e29 2018-03-12 stsp #include <string.h>
34 5c860e29 2018-03-12 stsp #include <unistd.h>
35 c09a553d 2018-03-12 stsp #include <libgen.h>
36 c0768b0f 2018-06-10 stsp #include <time.h>
37 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 6841bf13 2019-11-29 kn #include <regex.h>
39 83cd27f8 2020-01-13 stsp #include <getopt.h>
40 d2cdc636 2020-03-18 stsp #include <util.h>
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 f8a36e22 2021-08-26 stsp #include "got_send.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"
58 d65a88a2 2021-09-05 stsp #include "got_dial.h"
60 5c860e29 2018-03-12 stsp #ifndef nitems
61 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
65 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
68 99437157 2018-11-11 stsp catch_sigint(int signo)
70 99437157 2018-11-11 stsp sigint_received = 1;
74 99437157 2018-11-11 stsp catch_sigpipe(int signo)
76 99437157 2018-11-11 stsp sigpipe_received = 1;
80 8cfb4057 2019-07-09 stsp struct got_cmd {
81 820059fa 2020-09-25 stsp const char *cmd_name;
82 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
84 97b3a7be 2019-07-09 stsp const char *cmd_alias;
87 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
88 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
89 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
90 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
91 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
92 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
93 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
95 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
96 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
97 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
98 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
99 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
100 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
101 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
102 d00136be 2019-03-26 stsp __dead static void usage_add(void);
103 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
104 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
105 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
106 f8a36e22 2021-08-26 stsp __dead static void usage_send(void);
107 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
108 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
109 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
110 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
111 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
112 f259c4c1 2021-09-24 stsp __dead static void usage_merge(void);
113 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
114 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
115 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
116 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
118 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
119 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
120 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
121 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
123 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
124 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
126 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
127 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
128 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
129 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
130 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
131 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
132 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
133 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
134 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
135 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
136 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
137 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
138 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
139 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
140 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
141 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
142 f259c4c1 2021-09-24 stsp static const struct got_error* cmd_merge(int, char *[]);
143 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
144 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
145 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
146 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
148 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
149 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
150 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
151 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
152 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
153 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
154 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
155 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
156 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
157 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
158 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
159 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
160 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
161 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
162 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
163 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
164 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
165 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
166 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
167 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
168 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
169 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
170 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
171 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
172 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
173 f259c4c1 2021-09-24 stsp { "merge", cmd_merge, usage_merge, "mg" },
174 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
175 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
176 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
177 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
180 ce5b7c56 2019-07-09 stsp static void
181 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
185 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
186 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
187 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
188 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
190 6879ba42 2020-10-01 naddy fputc('\n', fp);
193 ff69268e 2020-12-13 stsp __dead static void
194 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
196 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
200 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
202 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
205 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
206 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
207 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
208 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
211 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
213 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
214 5c860e29 2018-03-12 stsp switch (ch) {
222 6879ba42 2020-10-01 naddy usage(hflag, 1);
223 5c860e29 2018-03-12 stsp /* NOTREACHED */
227 5c860e29 2018-03-12 stsp argc -= optind;
228 5c860e29 2018-03-12 stsp argv += optind;
229 9814e6a3 2020-09-27 naddy optind = 1;
230 9814e6a3 2020-09-27 naddy optreset = 1;
232 53ccebc2 2019-07-30 stsp if (Vflag) {
233 53ccebc2 2019-07-30 stsp got_version_print_str();
237 5c860e29 2018-03-12 stsp if (argc <= 0)
238 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
240 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
241 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
243 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
244 d7d4f210 2018-03-12 stsp const struct got_error *error;
246 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
248 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
249 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
253 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
255 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
256 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
257 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
258 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
259 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
260 70015d7a 2019-11-08 stsp !(sigint_received &&
261 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
262 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
269 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
270 6879ba42 2020-10-01 naddy list_commands(stderr);
274 4ed7e80c 2018-05-20 stsp __dead static void
275 6879ba42 2020-10-01 naddy usage(int hflag, int status)
277 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
279 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
280 53ccebc2 2019-07-30 stsp getprogname());
282 6879ba42 2020-10-01 naddy list_commands(fp);
283 6879ba42 2020-10-01 naddy exit(status);
286 0266afb7 2019-01-04 stsp static const struct got_error *
287 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
289 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
290 e2ba3d07 2019-05-13 stsp const char *editor;
292 8920fa04 2019-08-18 stsp *abspath = NULL;
294 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
295 e2ba3d07 2019-05-13 stsp if (editor == NULL)
296 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
298 0ee7065d 2019-05-13 stsp if (editor) {
299 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
301 0ee7065d 2019-05-13 stsp return err;
304 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
305 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
306 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
307 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
310 e2ba3d07 2019-05-13 stsp return NULL;
313 e2ba3d07 2019-05-13 stsp static const struct got_error *
314 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
315 c530dc23 2019-07-23 stsp const char *worktree_path)
317 163ce85a 2019-05-13 stsp const struct got_error *err;
319 37c06ea4 2019-07-15 stsp #ifdef PROFILE
320 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
321 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
323 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
324 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
326 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
327 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
329 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
330 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
332 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
333 163ce85a 2019-05-13 stsp if (err != NULL)
334 163ce85a 2019-05-13 stsp return err;
336 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
337 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
339 0266afb7 2019-01-04 stsp return NULL;
342 2c7829a4 2019-06-17 stsp __dead static void
343 2c7829a4 2019-06-17 stsp usage_init(void)
345 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
349 2c7829a4 2019-06-17 stsp static const struct got_error *
350 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
352 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
353 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
356 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
357 2c7829a4 2019-06-17 stsp switch (ch) {
359 2c7829a4 2019-06-17 stsp usage_init();
360 2c7829a4 2019-06-17 stsp /* NOTREACHED */
364 2c7829a4 2019-06-17 stsp argc -= optind;
365 2c7829a4 2019-06-17 stsp argv += optind;
367 2c7829a4 2019-06-17 stsp #ifndef PROFILE
368 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
369 2c7829a4 2019-06-17 stsp err(1, "pledge");
371 2c7829a4 2019-06-17 stsp if (argc != 1)
372 bc20e173 2019-06-17 stsp usage_init();
374 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
375 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
376 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
378 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
380 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
381 2c7829a4 2019-06-17 stsp if (error &&
382 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
385 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
389 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
391 3ce1b845 2019-07-15 stsp free(repo_path);
392 3ce1b845 2019-07-15 stsp return error;
395 3ce1b845 2019-07-15 stsp __dead static void
396 3ce1b845 2019-07-15 stsp usage_import(void)
398 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
399 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
404 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
407 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
408 3ce1b845 2019-07-15 stsp int st = -1;
410 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
411 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
412 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
414 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
416 3ce1b845 2019-07-15 stsp goto doneediting;
418 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
419 3ce1b845 2019-07-15 stsp _exit(127);
422 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
423 3ce1b845 2019-07-15 stsp if (errno != EINTR)
426 3ce1b845 2019-07-15 stsp doneediting:
427 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
428 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
429 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
431 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
432 3ce1b845 2019-07-15 stsp errno = EINTR;
436 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
439 3ce1b845 2019-07-15 stsp static const struct got_error *
440 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
441 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
442 28cf319f 2021-01-28 stsp int require_modification)
444 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
445 bfa12d5e 2020-09-26 stsp char *line = NULL;
446 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
447 bfa12d5e 2020-09-26 stsp ssize_t linelen;
448 3ce1b845 2019-07-15 stsp struct stat st, st2;
449 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
450 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
451 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
453 3ce1b845 2019-07-15 stsp *logmsg = NULL;
455 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
456 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
458 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
459 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
461 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
462 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
464 28cf319f 2021-01-28 stsp if (require_modification &&
465 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
466 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
467 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
470 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
471 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
472 bfa12d5e 2020-09-26 stsp * has in fact been edited.
474 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
475 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
476 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
477 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
479 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
480 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
481 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
486 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
487 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
488 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
489 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
490 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
491 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
492 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
496 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
497 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
501 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
502 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
503 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
504 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
505 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
507 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
508 3ce1b845 2019-07-15 stsp if (fp == NULL) {
509 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
514 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
515 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
516 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
517 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
518 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
519 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
523 bfa12d5e 2020-09-26 stsp free(line);
524 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
525 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
528 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
529 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
533 bfa12d5e 2020-09-26 stsp if (len == 0) {
534 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
535 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
538 28cf319f 2021-01-28 stsp if (require_modification &&
539 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
540 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
541 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
543 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
545 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
546 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
548 3ce1b845 2019-07-15 stsp free(*logmsg);
549 3ce1b845 2019-07-15 stsp *logmsg = NULL;
551 3ce1b845 2019-07-15 stsp return err;
554 3ce1b845 2019-07-15 stsp static const struct got_error *
555 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
556 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
558 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
559 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
560 1601cb9f 2020-09-11 naddy int initial_content_len;
561 97972933 2020-09-11 stsp int fd = -1;
563 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
564 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
565 1601cb9f 2020-09-11 naddy branch_name);
566 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
567 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
569 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
570 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
574 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
575 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
579 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
580 0d5bb276 2020-12-15 stsp initial_content_len, 1);
582 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
583 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
584 3ce1b845 2019-07-15 stsp free(initial_content);
586 59f86c76 2020-09-11 stsp free(*logmsg_path);
587 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
589 3ce1b845 2019-07-15 stsp return err;
592 3ce1b845 2019-07-15 stsp static const struct got_error *
593 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
595 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
596 3ce1b845 2019-07-15 stsp return NULL;
599 3ce1b845 2019-07-15 stsp static const struct got_error *
600 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
601 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
603 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
604 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
605 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
607 84792843 2019-08-09 stsp *author = NULL;
609 50b0790e 2020-09-11 stsp if (worktree)
610 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
611 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
614 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
615 50b0790e 2020-09-11 stsp * significant to least significant:
616 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
617 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
618 50b0790e 2020-09-11 stsp * 3) repository's git config file
619 50b0790e 2020-09-11 stsp * 4) environment variables
620 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
623 50b0790e 2020-09-11 stsp if (worktree_conf)
624 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
625 50b0790e 2020-09-11 stsp if (got_author == NULL)
626 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
627 84792843 2019-08-09 stsp if (got_author == NULL) {
628 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
629 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
630 c9956ddf 2019-09-08 stsp if (name && email) {
631 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
632 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
633 c9956ddf 2019-09-08 stsp return NULL;
636 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
637 257add31 2020-09-09 stsp if (got_author == NULL) {
638 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
639 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
641 257add31 2020-09-09 stsp if (name && email) {
642 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
644 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
645 257add31 2020-09-09 stsp return NULL;
647 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
648 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
652 aba9c984 2019-09-08 stsp *author = strdup(got_author);
653 aba9c984 2019-09-08 stsp if (*author == NULL)
654 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
657 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
658 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
660 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
661 84792843 2019-08-09 stsp got_author++;
662 aba9c984 2019-09-08 stsp if (*got_author != '<') {
663 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
666 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
667 84792843 2019-08-09 stsp got_author++;
668 aba9c984 2019-09-08 stsp if (*got_author != '@') {
669 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
672 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
673 84792843 2019-08-09 stsp got_author++;
674 84792843 2019-08-09 stsp if (*got_author != '>')
675 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
678 aba9c984 2019-09-08 stsp free(*author);
679 aba9c984 2019-09-08 stsp *author = NULL;
681 aba9c984 2019-09-08 stsp return err;
684 c9956ddf 2019-09-08 stsp static const struct got_error *
685 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
687 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
689 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
690 c9956ddf 2019-09-08 stsp if (homedir) {
691 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
692 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
695 c9956ddf 2019-09-08 stsp return NULL;
698 84792843 2019-08-09 stsp static const struct got_error *
699 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
701 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
702 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
703 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
704 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
705 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
706 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
707 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
708 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
710 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
711 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
712 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
714 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
716 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
717 3ce1b845 2019-07-15 stsp switch (ch) {
719 3ce1b845 2019-07-15 stsp branch_name = optarg;
722 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
723 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
724 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
729 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
730 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
731 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
737 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
739 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
745 b2b65d18 2019-08-22 stsp usage_import();
746 3ce1b845 2019-07-15 stsp /* NOTREACHED */
750 3ce1b845 2019-07-15 stsp argc -= optind;
751 3ce1b845 2019-07-15 stsp argv += optind;
753 3ce1b845 2019-07-15 stsp #ifndef PROFILE
754 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
756 3ce1b845 2019-07-15 stsp NULL) == -1)
757 3ce1b845 2019-07-15 stsp err(1, "pledge");
759 3ce1b845 2019-07-15 stsp if (argc != 1)
760 3ce1b845 2019-07-15 stsp usage_import();
762 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
763 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
764 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
765 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
767 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
768 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
771 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
775 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
777 aba9c984 2019-09-08 stsp return error;
780 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
781 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
782 e560b7e0 2019-11-28 stsp * an unintended typo.
784 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
785 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
787 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
788 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
792 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
793 3ce1b845 2019-07-15 stsp if (error) {
794 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
797 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
798 3ce1b845 2019-07-15 stsp "import target branch already exists");
802 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
803 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
804 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
807 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
810 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
811 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
813 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
814 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
817 8e158b01 2019-09-22 stsp free(logmsg);
818 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
819 ef293bdd 2019-10-21 stsp path_dir, refname);
820 ef293bdd 2019-10-21 stsp if (error) {
821 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
822 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
823 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
828 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
829 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
830 ef293bdd 2019-10-21 stsp if (logmsg_path)
831 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
835 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
836 ef293bdd 2019-10-21 stsp if (error) {
837 ef293bdd 2019-10-21 stsp if (logmsg_path)
838 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
842 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
843 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
844 ef293bdd 2019-10-21 stsp if (error) {
845 ef293bdd 2019-10-21 stsp if (logmsg_path)
846 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
850 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
851 ef293bdd 2019-10-21 stsp if (error) {
852 ef293bdd 2019-10-21 stsp if (logmsg_path)
853 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
857 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
858 ef293bdd 2019-10-21 stsp if (error) {
859 ef293bdd 2019-10-21 stsp if (logmsg_path)
860 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
864 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
865 ef293bdd 2019-10-21 stsp if (error) {
866 ef293bdd 2019-10-21 stsp if (logmsg_path)
867 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
871 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
872 3ce1b845 2019-07-15 stsp if (error) {
873 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
874 ef293bdd 2019-10-21 stsp if (logmsg_path)
875 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
879 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
880 3ce1b845 2019-07-15 stsp branch_ref);
881 ef293bdd 2019-10-21 stsp if (error) {
882 ef293bdd 2019-10-21 stsp if (logmsg_path)
883 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
887 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
888 ef293bdd 2019-10-21 stsp if (error) {
889 ef293bdd 2019-10-21 stsp if (logmsg_path)
890 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
895 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
896 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
898 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
899 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
900 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
901 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
902 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
903 8e158b01 2019-09-22 stsp free(logmsg);
904 ef293bdd 2019-10-21 stsp free(logmsg_path);
905 2c7829a4 2019-06-17 stsp free(repo_path);
906 3ce1b845 2019-07-15 stsp free(editor);
907 3ce1b845 2019-07-15 stsp free(refname);
908 3ce1b845 2019-07-15 stsp free(new_commit_id);
909 3ce1b845 2019-07-15 stsp free(id_str);
910 aba9c984 2019-09-08 stsp free(author);
911 c9956ddf 2019-09-08 stsp free(gitconfig_path);
912 3ce1b845 2019-07-15 stsp if (branch_ref)
913 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
914 3ce1b845 2019-07-15 stsp if (head_ref)
915 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
916 2c7829a4 2019-06-17 stsp return error;
919 93658fb9 2020-03-18 stsp __dead static void
920 93658fb9 2020-03-18 stsp usage_clone(void)
922 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
923 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
927 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
928 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
929 892ac3b6 2020-03-18 stsp int last_p_indexed;
930 892ac3b6 2020-03-18 stsp int last_p_resolved;
931 68999b92 2020-03-18 stsp int verbosity;
933 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
935 04d9a9ec 2020-09-24 stsp int create_configs;
936 04d9a9ec 2020-09-24 stsp int configs_created;
938 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
939 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
940 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
941 04d9a9ec 2020-09-24 stsp const char *proto;
942 04d9a9ec 2020-09-24 stsp const char *host;
943 04d9a9ec 2020-09-24 stsp const char *port;
944 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
945 04d9a9ec 2020-09-24 stsp const char *git_url;
946 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
947 04d9a9ec 2020-09-24 stsp int mirror_references;
948 04d9a9ec 2020-09-24 stsp } config_info;
951 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
952 93658fb9 2020-03-18 stsp static const struct got_error *
953 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
954 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
955 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
956 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
957 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
959 04d9a9ec 2020-09-24 stsp static const struct got_error *
960 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
961 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
963 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
964 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
965 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
966 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
967 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
970 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
971 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
972 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
973 04d9a9ec 2020-09-24 stsp * we have all required information.
975 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
976 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
977 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
978 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
979 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
980 62d463ca 2020-10-20 naddy a->config_info.git_url,
981 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
982 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
983 62d463ca 2020-10-20 naddy a->config_info.symrefs,
984 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
985 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
987 04d9a9ec 2020-09-24 stsp return err;
988 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
991 68999b92 2020-03-18 stsp if (a->verbosity < 0)
992 68999b92 2020-03-18 stsp return NULL;
994 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
995 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
996 892ac3b6 2020-03-18 stsp fflush(stdout);
997 12d1281e 2020-03-19 stsp return NULL;
1000 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
1001 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1002 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
1003 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1004 892ac3b6 2020-03-18 stsp print_size = 1;
1005 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1006 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1007 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1009 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1010 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1011 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1012 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1013 892ac3b6 2020-03-18 stsp print_indexed = 1;
1014 892ac3b6 2020-03-18 stsp print_size = 1;
1017 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1018 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1019 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1020 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1021 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1022 892ac3b6 2020-03-18 stsp print_resolved = 1;
1023 892ac3b6 2020-03-18 stsp print_indexed = 1;
1024 892ac3b6 2020-03-18 stsp print_size = 1;
1029 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1030 892ac3b6 2020-03-18 stsp printf("\r");
1031 892ac3b6 2020-03-18 stsp if (print_size)
1032 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1033 d715f13e 2020-03-19 stsp if (print_indexed)
1034 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1035 d715f13e 2020-03-19 stsp if (print_resolved)
1036 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1037 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1038 892ac3b6 2020-03-18 stsp fflush(stdout);
1040 531c3985 2020-03-18 stsp return NULL;
1043 531c3985 2020-03-18 stsp static const struct got_error *
1044 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1045 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1047 4ba14133 2020-03-20 stsp const struct got_error *err;
1048 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1050 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1052 4ba14133 2020-03-20 stsp return err;
1054 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1055 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1056 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1057 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1059 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1060 4ba14133 2020-03-20 stsp return err;
1063 4ba14133 2020-03-20 stsp static const struct got_error *
1064 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1065 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1067 41b0de12 2020-03-21 stsp const struct got_error *err;
1068 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1070 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1071 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1072 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1074 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1077 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1078 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1079 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1080 41b0de12 2020-03-21 stsp char *id_str;
1082 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1084 41b0de12 2020-03-21 stsp return err;
1085 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1086 41b0de12 2020-03-21 stsp free(id_str);
1089 41b0de12 2020-03-21 stsp return NULL;
1092 6338a6a1 2020-03-21 stsp static const struct got_error *
1093 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1094 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1096 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1097 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1098 6338a6a1 2020-03-21 stsp char *id_str;
1100 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1102 6338a6a1 2020-03-21 stsp return err;
1104 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1106 6338a6a1 2020-03-21 stsp goto done;
1108 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1109 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1111 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1112 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1114 6338a6a1 2020-03-21 stsp free(id_str);
1115 6338a6a1 2020-03-21 stsp return err;
1118 0e4002ca 2020-03-21 stsp static int
1119 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1121 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1123 0e4002ca 2020-03-21 stsp refname += 5;
1126 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1127 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1129 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1131 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1134 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1135 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1137 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1138 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1141 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1142 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1145 0e4002ca 2020-03-21 stsp static int
1146 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1148 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1150 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1151 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1158 41b0de12 2020-03-21 stsp static const struct got_error *
1159 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1160 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1162 0e4002ca 2020-03-21 stsp const struct got_error *err;
1163 0e4002ca 2020-03-21 stsp char *remote_refname;
1165 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1166 0e4002ca 2020-03-21 stsp refname += 5;
1168 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1169 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1170 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1172 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1173 0e4002ca 2020-03-21 stsp free(remote_refname);
1174 7c0b7f42 2020-09-24 stsp return err;
1177 7c0b7f42 2020-09-24 stsp static const struct got_error *
1178 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1179 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1180 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1181 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1182 99495ddb 2021-01-10 stsp struct got_repository *repo)
1184 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1185 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1186 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1187 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1188 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1189 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1190 7c0b7f42 2020-09-24 stsp ssize_t n;
1192 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1193 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1194 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1196 132af4a5 2021-01-05 stsp branchname = pe->path;
1197 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1198 132af4a5 2021-01-05 stsp branchname += 11;
1199 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1200 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1201 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1202 132af4a5 2021-01-05 stsp goto done;
1204 132af4a5 2021-01-05 stsp free(branches);
1205 132af4a5 2021-01-05 stsp branches = s;
1207 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1208 15d3c221 2021-01-05 stsp branchname = default_branch;
1209 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1210 15d3c221 2021-01-05 stsp branchname += 11;
1211 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1212 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1213 132af4a5 2021-01-05 stsp goto done;
1216 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1217 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1218 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1220 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1221 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1222 99495ddb 2021-01-10 stsp branchname += 5;
1223 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1224 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1225 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1226 99495ddb 2021-01-10 stsp goto done;
1228 99495ddb 2021-01-10 stsp free(refs);
1233 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1234 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1235 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1236 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1237 7c0b7f42 2020-09-24 stsp goto done;
1239 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1240 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1241 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1242 7c0b7f42 2020-09-24 stsp goto done;
1244 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1245 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1246 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1247 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1249 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1255 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1256 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1257 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1258 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1259 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1260 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1261 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1262 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1263 7c0b7f42 2020-09-24 stsp goto done;
1265 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1266 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1267 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1268 7c0b7f42 2020-09-24 stsp goto done;
1272 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1273 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1274 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1275 132af4a5 2021-01-05 stsp free(branches);
1276 7c0b7f42 2020-09-24 stsp return err;
1279 7c0b7f42 2020-09-24 stsp static const struct got_error *
1280 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1281 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1282 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1283 99495ddb 2021-01-10 stsp struct got_repository *repo)
1285 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1286 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1287 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1288 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1289 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1290 56d0a753 2021-01-20 stsp const char *branchname;
1291 7c0b7f42 2020-09-24 stsp ssize_t n;
1293 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1294 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1295 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1296 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1297 7c0b7f42 2020-09-24 stsp goto done;
1299 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1300 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1301 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1302 7c0b7f42 2020-09-24 stsp goto done;
1304 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1305 56d0a753 2021-01-20 stsp if (mirror_references) {
1306 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1307 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1308 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1309 56d0a753 2021-01-20 stsp goto done;
1311 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1312 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1313 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1314 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1315 7c0b7f42 2020-09-24 stsp goto done;
1317 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1318 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1319 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1321 132af4a5 2021-01-05 stsp branchname = pe->path;
1322 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1323 132af4a5 2021-01-05 stsp branchname += 11;
1324 56d0a753 2021-01-20 stsp if (mirror_references) {
1325 56d0a753 2021-01-20 stsp if (asprintf(&s,
1326 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1327 56d0a753 2021-01-20 stsp branches ? branches : "",
1328 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1329 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1330 56d0a753 2021-01-20 stsp goto done;
1332 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1333 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1334 132af4a5 2021-01-05 stsp branches ? branches : "",
1335 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1336 132af4a5 2021-01-05 stsp branchname) == -1) {
1337 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1338 132af4a5 2021-01-05 stsp goto done;
1340 132af4a5 2021-01-05 stsp free(branches);
1341 132af4a5 2021-01-05 stsp branches = s;
1345 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1346 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1348 04d9a9ec 2020-09-24 stsp if (default_branch) {
1349 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1350 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1351 7c0b7f42 2020-09-24 stsp branchname += 11;
1353 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1354 56d0a753 2021-01-20 stsp if (mirror_references) {
1355 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1356 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1357 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1358 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1359 56d0a753 2021-01-20 stsp goto done;
1361 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1362 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1363 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1364 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1365 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1366 7c0b7f42 2020-09-24 stsp goto done;
1369 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1370 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1371 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1373 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1374 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1375 99495ddb 2021-01-10 stsp refname += 5;
1376 56d0a753 2021-01-20 stsp if (mirror_references) {
1377 56d0a753 2021-01-20 stsp if (asprintf(&s,
1378 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1379 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1380 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1381 56d0a753 2021-01-20 stsp goto done;
1383 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1384 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1385 99495ddb 2021-01-10 stsp refs ? refs : "",
1386 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1387 99495ddb 2021-01-10 stsp refname) == -1) {
1388 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1389 99495ddb 2021-01-10 stsp goto done;
1391 99495ddb 2021-01-10 stsp free(refs);
1396 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1397 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1398 132af4a5 2021-01-05 stsp "\turl = %s\n"
1401 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1402 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1403 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1404 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1405 132af4a5 2021-01-05 stsp goto done;
1407 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1408 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1409 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1410 7c0b7f42 2020-09-24 stsp goto done;
1413 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1414 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1415 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1416 132af4a5 2021-01-05 stsp free(branches);
1417 0e4002ca 2020-03-21 stsp return err;
1420 0e4002ca 2020-03-21 stsp static const struct got_error *
1421 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1422 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1423 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1424 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1425 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1427 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1428 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1429 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1432 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1433 04d9a9ec 2020-09-24 stsp * one of those.
1435 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1436 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1437 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1439 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1440 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1441 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1442 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1444 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1447 04d9a9ec 2020-09-24 stsp default_branch = target;
1452 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1453 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1454 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1455 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1457 04d9a9ec 2020-09-24 stsp return err;
1459 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1460 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1461 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1464 04d9a9ec 2020-09-24 stsp static const struct got_error *
1465 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1467 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1468 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1469 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1470 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1471 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1472 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1473 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1474 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1475 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1476 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1477 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1478 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1479 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1480 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1481 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1483 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1484 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1485 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1486 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1488 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1489 93658fb9 2020-03-18 stsp switch (ch) {
1491 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1494 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1495 4ba14133 2020-03-20 stsp optarg, NULL);
1496 4ba14133 2020-03-20 stsp if (error)
1497 4ba14133 2020-03-20 stsp return error;
1500 41b0de12 2020-03-21 stsp list_refs_only = 1;
1503 469dd726 2020-03-20 stsp mirror_references = 1;
1506 68999b92 2020-03-18 stsp if (verbosity < 0)
1507 68999b92 2020-03-18 stsp verbosity = 0;
1508 68999b92 2020-03-18 stsp else if (verbosity < 3)
1509 68999b92 2020-03-18 stsp verbosity++;
1512 68999b92 2020-03-18 stsp verbosity = -1;
1515 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1516 0e4002ca 2020-03-21 stsp optarg, NULL);
1517 0e4002ca 2020-03-21 stsp if (error)
1518 0e4002ca 2020-03-21 stsp return error;
1521 93658fb9 2020-03-18 stsp usage_clone();
1525 93658fb9 2020-03-18 stsp argc -= optind;
1526 93658fb9 2020-03-18 stsp argv += optind;
1528 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1529 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1530 41b0de12 2020-03-21 stsp if (list_refs_only) {
1531 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1532 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1533 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1534 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1535 41b0de12 2020-03-21 stsp if (mirror_references)
1536 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1537 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1538 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1541 93658fb9 2020-03-18 stsp uri = argv[0];
1543 9df6f38b 2020-03-18 stsp if (argc == 1)
1544 93658fb9 2020-03-18 stsp dirname = NULL;
1545 9df6f38b 2020-03-18 stsp else if (argc == 2)
1546 93658fb9 2020-03-18 stsp dirname = argv[1];
1548 93658fb9 2020-03-18 stsp usage_clone();
1550 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1551 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1552 39c64a6a 2020-03-18 stsp if (error)
1553 09f63084 2020-03-20 stsp goto done;
1555 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1556 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1557 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1558 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1559 09838ffc 2020-03-18 stsp goto done;
1562 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1563 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1564 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1565 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1566 39c64a6a 2020-03-18 stsp err(1, "pledge");
1568 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1569 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1570 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1571 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1572 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1573 39c64a6a 2020-03-18 stsp err(1, "pledge");
1575 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1576 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1577 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1578 39c64a6a 2020-03-18 stsp goto done;
1580 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1581 39c64a6a 2020-03-18 stsp goto done;
1583 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1584 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1585 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1586 bb64b798 2020-03-18 stsp goto done;
1588 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1590 bb64b798 2020-03-18 stsp repo_path = dirname;
1592 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1593 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1594 2751fe64 2020-09-24 stsp if (error &&
1595 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1596 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1597 41b0de12 2020-03-21 stsp goto done;
1598 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1599 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1600 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1601 41b0de12 2020-03-21 stsp goto done;
1605 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
1606 d65a88a2 2021-09-05 stsp if (error)
1607 d65a88a2 2021-09-05 stsp goto done;
1609 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1610 ee448f5f 2020-03-18 stsp if (error)
1611 ee448f5f 2020-03-18 stsp goto done;
1613 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1614 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1615 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1617 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1618 9c52365f 2020-03-21 stsp server_path, verbosity);
1619 39c64a6a 2020-03-18 stsp if (error)
1620 bb64b798 2020-03-18 stsp goto done;
1622 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1623 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1624 2751fe64 2020-09-24 stsp if (error)
1625 2751fe64 2020-09-24 stsp goto done;
1626 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1627 2751fe64 2020-09-24 stsp if (error)
1628 2751fe64 2020-09-24 stsp goto done;
1631 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1632 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1633 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1634 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1635 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1636 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1637 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1640 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1644 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1645 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1646 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1647 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1648 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1649 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1650 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1651 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1652 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1653 39c64a6a 2020-03-18 stsp if (error)
1654 d9b4d0c0 2020-03-18 stsp goto done;
1656 41b0de12 2020-03-21 stsp if (list_refs_only) {
1657 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1658 41b0de12 2020-03-21 stsp goto done;
1661 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1662 39c64a6a 2020-03-18 stsp if (error)
1663 d9b4d0c0 2020-03-18 stsp goto done;
1664 68999b92 2020-03-18 stsp if (verbosity >= 0)
1665 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1666 d9b4d0c0 2020-03-18 stsp free(id_str);
1668 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1669 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1670 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1671 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1672 7ebc0570 2020-03-18 stsp char *remote_refname;
1674 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1675 0e4002ca 2020-03-21 stsp !mirror_references) {
1676 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1677 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1678 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1679 0e4002ca 2020-03-21 stsp if (error)
1680 0e4002ca 2020-03-21 stsp goto done;
1684 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1685 39c64a6a 2020-03-18 stsp if (error)
1686 d9b4d0c0 2020-03-18 stsp goto done;
1688 469dd726 2020-03-20 stsp if (mirror_references)
1691 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1694 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1695 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1696 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1697 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1698 7ebc0570 2020-03-18 stsp goto done;
1700 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1701 f298ae0f 2020-03-25 stsp free(remote_refname);
1702 39c64a6a 2020-03-18 stsp if (error)
1703 d9b4d0c0 2020-03-18 stsp goto done;
1706 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1707 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1708 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1709 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1710 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1711 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1713 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1716 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1717 39c64a6a 2020-03-18 stsp if (error) {
1718 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1719 55330abe 2020-03-20 stsp error = NULL;
1722 d9b4d0c0 2020-03-18 stsp goto done;
1725 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1726 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1727 f298ae0f 2020-03-25 stsp if (error)
1728 f298ae0f 2020-03-25 stsp goto done;
1730 f298ae0f 2020-03-25 stsp if (mirror_references)
1733 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1736 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1737 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1738 f298ae0f 2020-03-25 stsp refname) == -1) {
1739 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1740 f298ae0f 2020-03-25 stsp goto done;
1742 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1743 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1744 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1745 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1746 f298ae0f 2020-03-25 stsp free(remote_refname);
1747 f298ae0f 2020-03-25 stsp goto done;
1749 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1750 f298ae0f 2020-03-25 stsp if (error) {
1751 f298ae0f 2020-03-25 stsp free(remote_refname);
1752 f298ae0f 2020-03-25 stsp free(remote_target);
1753 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1754 f298ae0f 2020-03-25 stsp error = NULL;
1757 f298ae0f 2020-03-25 stsp goto done;
1759 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1760 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1761 f298ae0f 2020-03-25 stsp free(remote_refname);
1762 f298ae0f 2020-03-25 stsp free(remote_target);
1763 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1764 39c64a6a 2020-03-18 stsp if (error)
1765 d9b4d0c0 2020-03-18 stsp goto done;
1767 4ba14133 2020-03-20 stsp if (pe == NULL) {
1769 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1770 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1771 4ba14133 2020-03-20 stsp * which could be fetched instead.
1773 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1774 4ba14133 2020-03-20 stsp const char *target = pe->path;
1775 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1777 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1778 4ba14133 2020-03-20 stsp if (error) {
1779 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1780 4ba14133 2020-03-20 stsp error = NULL;
1783 4ba14133 2020-03-20 stsp goto done;
1786 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1787 04d9a9ec 2020-09-24 stsp verbosity, repo);
1788 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1789 4ba14133 2020-03-20 stsp if (error)
1790 4ba14133 2020-03-20 stsp goto done;
1795 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1796 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1797 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1799 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1800 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1801 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1802 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1803 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1805 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1806 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1807 1d0f4054 2021-06-17 stsp if (repo) {
1808 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1809 1d0f4054 2021-06-17 stsp if (error == NULL)
1810 1d0f4054 2021-06-17 stsp error = close_err;
1812 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1813 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1814 d9b4d0c0 2020-03-18 stsp free(pe->data);
1816 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1817 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1818 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1819 d9b4d0c0 2020-03-18 stsp free(pe->data);
1821 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1822 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1823 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1824 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1825 09838ffc 2020-03-18 stsp free(proto);
1826 09838ffc 2020-03-18 stsp free(host);
1827 09838ffc 2020-03-18 stsp free(port);
1828 09838ffc 2020-03-18 stsp free(server_path);
1829 09838ffc 2020-03-18 stsp free(repo_name);
1830 bb64b798 2020-03-18 stsp free(default_destdir);
1831 b46f3e71 2020-03-18 stsp free(git_url);
1832 39c64a6a 2020-03-18 stsp return error;
1835 7848a0e1 2020-03-19 stsp static const struct got_error *
1836 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1837 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1839 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1840 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1841 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1843 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1845 7848a0e1 2020-03-19 stsp goto done;
1847 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1848 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1849 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1851 88609724 2020-03-21 stsp goto done;
1852 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1853 88609724 2020-03-21 stsp goto done;
1854 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1855 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1856 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1858 db6d8ad8 2020-03-21 stsp goto done;
1861 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1862 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1863 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1864 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1865 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1867 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1869 7848a0e1 2020-03-19 stsp goto done;
1870 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1872 e8a967e0 2020-03-21 stsp goto done;
1874 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1876 7848a0e1 2020-03-19 stsp goto done;
1877 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1878 6338a6a1 2020-03-21 stsp goto done;
1880 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1882 6338a6a1 2020-03-21 stsp goto done;
1883 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1885 6338a6a1 2020-03-21 stsp goto done;
1888 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1889 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1890 6338a6a1 2020-03-21 stsp new_id_str);
1892 7848a0e1 2020-03-19 stsp free(old_id);
1893 7848a0e1 2020-03-19 stsp free(new_id_str);
1894 7848a0e1 2020-03-19 stsp return err;
1897 f1bcca34 2020-03-25 stsp static const struct got_error *
1898 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1899 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1901 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1902 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1903 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1905 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1906 bcf34b0e 2020-03-26 stsp if (err) {
1907 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1908 bcf34b0e 2020-03-26 stsp return err;
1909 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1911 bcf34b0e 2020-03-26 stsp goto done;
1913 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1915 bcf34b0e 2020-03-26 stsp goto done;
1917 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1918 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1919 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1920 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1922 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1924 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1925 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1926 bcf34b0e 2020-03-26 stsp goto done;
1928 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1929 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1931 bcf34b0e 2020-03-26 stsp goto done;
1933 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1935 bcf34b0e 2020-03-26 stsp goto done;
1937 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1938 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1939 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1943 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1944 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1945 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1946 bcf34b0e 2020-03-26 stsp err = unlock_err;
1948 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1949 f1bcca34 2020-03-25 stsp return err;
1952 2ab43947 2020-03-18 stsp __dead static void
1953 7848a0e1 2020-03-19 stsp usage_fetch(void)
1955 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1956 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1957 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1958 13f12b09 2020-03-21 stsp getprogname());
1962 7848a0e1 2020-03-19 stsp static const struct got_error *
1963 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1964 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1966 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1967 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1968 3789fd73 2020-03-26 stsp char *id_str = NULL;
1970 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1971 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1973 3789fd73 2020-03-26 stsp return err;
1974 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1975 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1976 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1977 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1980 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1982 3789fd73 2020-03-26 stsp return err;
1983 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1985 3789fd73 2020-03-26 stsp goto done;
1987 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1989 3789fd73 2020-03-26 stsp goto done;
1990 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1991 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1992 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1997 3789fd73 2020-03-26 stsp free(id_str);
1998 3789fd73 2020-03-26 stsp return NULL;
2001 3789fd73 2020-03-26 stsp static const struct got_error *
2002 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2003 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2004 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2005 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2007 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2008 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2009 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2010 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2011 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2012 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2014 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2016 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2018 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2020 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2022 3789fd73 2020-03-26 stsp goto done;
2024 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2025 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2026 1b796c3f 2021-09-11 stsp const char *their_refname;
2028 1b796c3f 2021-09-11 stsp if (remote->mirror_references) {
2029 1b796c3f 2021-09-11 stsp their_refname = refname;
2031 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2032 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2033 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2034 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2036 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2037 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2038 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2039 3789fd73 2020-03-26 stsp goto done;
2041 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2044 1b796c3f 2021-09-11 stsp their_refname = local_refname;
2047 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2048 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2051 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2054 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2055 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2058 3789fd73 2020-03-26 stsp if (pe != NULL)
2061 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2065 3789fd73 2020-03-26 stsp if (local_refname) {
2066 3789fd73 2020-03-26 stsp struct got_reference *ref;
2067 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2068 3789fd73 2020-03-26 stsp if (err) {
2069 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2071 3789fd73 2020-03-26 stsp free(local_refname);
2072 3789fd73 2020-03-26 stsp local_refname = NULL;
2075 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2078 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2079 3789fd73 2020-03-26 stsp got_ref_close(ref);
2080 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2081 3789fd73 2020-03-26 stsp err = unlock_err;
2085 3789fd73 2020-03-26 stsp free(local_refname);
2086 3789fd73 2020-03-26 stsp local_refname = NULL;
2090 3789fd73 2020-03-26 stsp free(remote_namespace);
2091 3789fd73 2020-03-26 stsp free(local_refname);
2092 0e4002ca 2020-03-21 stsp return err;
2095 0e4002ca 2020-03-21 stsp static const struct got_error *
2096 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2097 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2099 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2100 0e4002ca 2020-03-21 stsp char *remote_refname;
2101 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2103 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2104 0e4002ca 2020-03-21 stsp refname += 5;
2106 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2107 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2108 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2110 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2111 0e4002ca 2020-03-21 stsp if (err) {
2112 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2113 0e4002ca 2020-03-21 stsp goto done;
2114 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2116 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2117 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2118 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2119 9f142382 2020-03-21 stsp err = unlock_err;
2120 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2123 0e4002ca 2020-03-21 stsp free(remote_refname);
2124 161728eb 2021-07-24 stsp return err;
2127 161728eb 2021-07-24 stsp static const struct got_error *
2128 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2130 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2131 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2132 161728eb 2021-07-24 stsp char *id_str = NULL;
2133 161728eb 2021-07-24 stsp const char *target;
2135 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2136 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2138 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2140 161728eb 2021-07-24 stsp goto done;
2141 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2143 161728eb 2021-07-24 stsp goto done;
2144 161728eb 2021-07-24 stsp target = id_str;
2147 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2149 161728eb 2021-07-24 stsp goto done;
2151 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2154 161728eb 2021-07-24 stsp free(id_str);
2155 f21ec2f0 2020-03-21 stsp return err;
2158 f21ec2f0 2020-03-21 stsp static const struct got_error *
2159 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2161 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2162 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2163 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2164 161728eb 2021-07-24 stsp char *prefix;
2166 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2168 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2169 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2170 161728eb 2021-07-24 stsp goto done;
2172 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2174 161728eb 2021-07-24 stsp goto done;
2176 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2177 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2179 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2180 161728eb 2021-07-24 stsp return err;
2183 161728eb 2021-07-24 stsp static const struct got_error *
2184 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2186 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2187 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2188 7848a0e1 2020-03-19 stsp const char *remote_name;
2189 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2190 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2191 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2192 7848a0e1 2020-03-19 stsp int nremotes;
2193 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2194 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2195 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2196 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2197 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2198 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2199 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2200 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2201 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2202 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2203 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2204 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2206 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2207 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2208 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2209 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2211 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2212 7848a0e1 2020-03-19 stsp switch (ch) {
2214 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2217 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2218 4ba14133 2020-03-20 stsp optarg, NULL);
2219 4ba14133 2020-03-20 stsp if (error)
2220 4ba14133 2020-03-20 stsp return error;
2223 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2226 41b0de12 2020-03-21 stsp list_refs_only = 1;
2229 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2230 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2231 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2233 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2236 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2239 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2240 7848a0e1 2020-03-19 stsp verbosity = 0;
2241 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2242 7848a0e1 2020-03-19 stsp verbosity++;
2245 7848a0e1 2020-03-19 stsp verbosity = -1;
2248 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2249 0e4002ca 2020-03-21 stsp optarg, NULL);
2250 0e4002ca 2020-03-21 stsp if (error)
2251 0e4002ca 2020-03-21 stsp return error;
2254 161728eb 2021-07-24 stsp delete_remote = 1;
2257 7848a0e1 2020-03-19 stsp usage_fetch();
2261 7848a0e1 2020-03-19 stsp argc -= optind;
2262 7848a0e1 2020-03-19 stsp argv += optind;
2264 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2265 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2266 41b0de12 2020-03-21 stsp if (list_refs_only) {
2267 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2268 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2269 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2270 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2271 f21ec2f0 2020-03-21 stsp if (delete_refs)
2272 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2273 161728eb 2021-07-24 stsp if (delete_remote)
2274 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2276 161728eb 2021-07-24 stsp if (delete_remote) {
2277 161728eb 2021-07-24 stsp if (fetch_all_branches)
2278 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2279 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2280 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2281 161728eb 2021-07-24 stsp if (delete_refs)
2282 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2283 161728eb 2021-07-24 stsp if (replace_tags)
2284 161728eb 2021-07-24 stsp option_conflict('X', 't');
2285 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2286 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2289 161728eb 2021-07-24 stsp if (argc == 0) {
2290 161728eb 2021-07-24 stsp if (delete_remote)
2291 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2292 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2293 161728eb 2021-07-24 stsp } else if (argc == 1)
2294 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2296 7848a0e1 2020-03-19 stsp usage_fetch();
2298 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2299 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2300 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2301 7848a0e1 2020-03-19 stsp goto done;
2304 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2305 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2306 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2307 7848a0e1 2020-03-19 stsp goto done;
2309 7848a0e1 2020-03-19 stsp error = NULL;
2310 7848a0e1 2020-03-19 stsp if (worktree) {
2311 7848a0e1 2020-03-19 stsp repo_path =
2312 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2313 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2314 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2315 7848a0e1 2020-03-19 stsp if (error)
2316 7848a0e1 2020-03-19 stsp goto done;
2318 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2319 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2320 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2321 7848a0e1 2020-03-19 stsp goto done;
2326 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2327 7848a0e1 2020-03-19 stsp if (error)
2328 7848a0e1 2020-03-19 stsp goto done;
2330 161728eb 2021-07-24 stsp if (delete_remote) {
2331 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2332 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2335 50b0790e 2020-09-11 stsp if (worktree) {
2336 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2337 50b0790e 2020-09-11 stsp if (worktree_conf) {
2338 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2339 50b0790e 2020-09-11 stsp worktree_conf);
2340 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2341 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2342 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2348 50b0790e 2020-09-11 stsp if (remote == NULL) {
2349 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2350 50b0790e 2020-09-11 stsp if (repo_conf) {
2351 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2352 50b0790e 2020-09-11 stsp repo_conf);
2353 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2354 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2355 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2361 50b0790e 2020-09-11 stsp if (remote == NULL) {
2362 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2363 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2364 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2365 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2370 50b0790e 2020-09-11 stsp if (remote == NULL) {
2371 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2372 50b0790e 2020-09-11 stsp goto done;
2375 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2376 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2377 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2378 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++) {
2379 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2380 6480c871 2021-08-30 stsp remote->fetch_branches[i], NULL);
2383 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2384 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++) {
2385 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2386 6480c871 2021-08-30 stsp remote->fetch_refs[i], NULL);
2390 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2391 6480c871 2021-08-30 stsp &repo_name, remote->fetch_url);
2392 7848a0e1 2020-03-19 stsp if (error)
2393 7848a0e1 2020-03-19 stsp goto done;
2395 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2396 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2397 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2398 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2399 7848a0e1 2020-03-19 stsp err(1, "pledge");
2401 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2402 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2403 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2404 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2405 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2406 7848a0e1 2020-03-19 stsp err(1, "pledge");
2408 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2409 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2410 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2411 7848a0e1 2020-03-19 stsp goto done;
2413 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2414 7848a0e1 2020-03-19 stsp goto done;
2417 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
2418 d65a88a2 2021-09-05 stsp if (error)
2419 d65a88a2 2021-09-05 stsp goto done;
2421 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2422 7848a0e1 2020-03-19 stsp if (error)
2423 7848a0e1 2020-03-19 stsp goto done;
2425 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2426 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2427 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2429 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2430 9c52365f 2020-03-21 stsp server_path, verbosity);
2431 7848a0e1 2020-03-19 stsp if (error)
2432 7848a0e1 2020-03-19 stsp goto done;
2434 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2435 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2436 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2437 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2438 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2439 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2440 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2441 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2442 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2443 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2444 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2445 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2446 7848a0e1 2020-03-19 stsp if (error)
2447 7848a0e1 2020-03-19 stsp goto done;
2449 41b0de12 2020-03-21 stsp if (list_refs_only) {
2450 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2451 41b0de12 2020-03-21 stsp goto done;
2454 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2455 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2456 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2457 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2458 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2459 984065c8 2020-03-19 stsp if (error)
2460 984065c8 2020-03-19 stsp goto done;
2461 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2462 984065c8 2020-03-19 stsp free(id_str);
2463 984065c8 2020-03-19 stsp id_str = NULL;
2466 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2467 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2468 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2469 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2470 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2471 7848a0e1 2020-03-19 stsp char *remote_refname;
2473 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2474 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2475 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2476 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2477 0e4002ca 2020-03-21 stsp if (error)
2478 0e4002ca 2020-03-21 stsp goto done;
2482 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2483 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2484 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2485 7848a0e1 2020-03-19 stsp if (error) {
2486 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2487 7848a0e1 2020-03-19 stsp goto done;
2488 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2490 7848a0e1 2020-03-19 stsp if (error)
2491 7848a0e1 2020-03-19 stsp goto done;
2493 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2494 db6d8ad8 2020-03-21 stsp verbosity, repo);
2495 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2496 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2497 9f142382 2020-03-21 stsp error = unlock_err;
2498 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2499 7848a0e1 2020-03-19 stsp if (error)
2500 7848a0e1 2020-03-19 stsp goto done;
2502 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2503 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2504 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2505 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2506 7848a0e1 2020-03-19 stsp goto done;
2509 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2510 7848a0e1 2020-03-19 stsp if (error) {
2511 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2512 7848a0e1 2020-03-19 stsp goto done;
2513 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2514 688f11b3 2020-03-21 stsp verbosity, repo);
2515 7848a0e1 2020-03-19 stsp if (error)
2516 7848a0e1 2020-03-19 stsp goto done;
2518 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2519 db6d8ad8 2020-03-21 stsp verbosity, repo);
2520 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2521 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2522 9f142382 2020-03-21 stsp error = unlock_err;
2523 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2524 7848a0e1 2020-03-19 stsp if (error)
2525 7848a0e1 2020-03-19 stsp goto done;
2528 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2529 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2530 2ec30c80 2020-03-20 stsp if (error) {
2531 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2532 2ec30c80 2020-03-20 stsp goto done;
2533 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2535 2ec30c80 2020-03-20 stsp if (error)
2536 2ec30c80 2020-03-20 stsp goto done;
2538 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2539 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2540 9f142382 2020-03-21 stsp error = unlock_err;
2541 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2545 f1bcca34 2020-03-25 stsp if (delete_refs) {
2546 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2547 3789fd73 2020-03-26 stsp verbosity, repo);
2548 f1bcca34 2020-03-25 stsp if (error)
2549 f1bcca34 2020-03-25 stsp goto done;
2552 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2553 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2554 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2555 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2556 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2557 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2558 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2560 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2563 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2566 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2567 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2568 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2569 f1bcca34 2020-03-25 stsp goto done;
2571 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2572 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2573 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2574 f1bcca34 2020-03-25 stsp free(remote_refname);
2575 f1bcca34 2020-03-25 stsp goto done;
2578 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2580 f1bcca34 2020-03-25 stsp if (error) {
2581 f1bcca34 2020-03-25 stsp free(remote_refname);
2582 f1bcca34 2020-03-25 stsp free(remote_target);
2583 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2584 f1bcca34 2020-03-25 stsp error = NULL;
2587 f1bcca34 2020-03-25 stsp goto done;
2589 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2590 f1bcca34 2020-03-25 stsp verbosity, repo);
2591 f1bcca34 2020-03-25 stsp free(remote_refname);
2592 f1bcca34 2020-03-25 stsp free(remote_target);
2593 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2594 f1bcca34 2020-03-25 stsp if (error)
2595 f1bcca34 2020-03-25 stsp goto done;
2599 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2600 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2601 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2602 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2603 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2605 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2606 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2607 1d0f4054 2021-06-17 stsp if (repo) {
2608 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2609 1d0f4054 2021-06-17 stsp if (error == NULL)
2610 1d0f4054 2021-06-17 stsp error = close_err;
2612 7848a0e1 2020-03-19 stsp if (worktree)
2613 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2614 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2615 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2616 7848a0e1 2020-03-19 stsp free(pe->data);
2618 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2619 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2620 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2621 7848a0e1 2020-03-19 stsp free(pe->data);
2623 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2624 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2625 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2626 7848a0e1 2020-03-19 stsp free(id_str);
2627 7848a0e1 2020-03-19 stsp free(cwd);
2628 7848a0e1 2020-03-19 stsp free(repo_path);
2629 7848a0e1 2020-03-19 stsp free(pack_hash);
2630 7848a0e1 2020-03-19 stsp free(proto);
2631 7848a0e1 2020-03-19 stsp free(host);
2632 7848a0e1 2020-03-19 stsp free(port);
2633 7848a0e1 2020-03-19 stsp free(server_path);
2634 7848a0e1 2020-03-19 stsp free(repo_name);
2635 7848a0e1 2020-03-19 stsp return error;
2639 7848a0e1 2020-03-19 stsp __dead static void
2640 2ab43947 2020-03-18 stsp usage_checkout(void)
2642 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2643 4ad4a1ec 2021-09-13 tracey "[-p prefix] [-q] repository-path [worktree-path]\n",
2644 4ad4a1ec 2021-09-13 tracey getprogname());
2648 2ab43947 2020-03-18 stsp static void
2649 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2651 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2652 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2653 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2654 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2655 2ab43947 2020-03-18 stsp getprogname());
2658 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2659 2ab43947 2020-03-18 stsp const char *worktree_path;
2660 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2661 4ad4a1ec 2021-09-13 tracey int verbosity;
2664 2ab43947 2020-03-18 stsp static const struct got_error *
2665 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2667 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2669 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2670 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2671 2ab43947 2020-03-18 stsp return NULL;
2673 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2674 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2675 2ab43947 2020-03-18 stsp return NULL;
2678 2ab43947 2020-03-18 stsp while (path[0] == '/')
2681 4ad4a1ec 2021-09-13 tracey if (a->verbosity >= 0)
2682 4ad4a1ec 2021-09-13 tracey printf("%c %s/%s\n", status, a->worktree_path, path);
2684 2ab43947 2020-03-18 stsp return NULL;
2687 2ab43947 2020-03-18 stsp static const struct got_error *
2688 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2690 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2691 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2692 2ab43947 2020-03-18 stsp return NULL;
2695 2ab43947 2020-03-18 stsp static const struct got_error *
2696 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2697 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2698 2ab43947 2020-03-18 stsp struct got_repository *repo)
2700 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2701 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2703 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2704 4e91ef15 2021-09-26 stsp commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2706 2ab43947 2020-03-18 stsp return err;
2708 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2709 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2712 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2713 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2715 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2717 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2725 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2726 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2727 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2729 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2730 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2731 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2732 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2733 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2734 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2736 2ab43947 2020-03-18 stsp free(yca_id);
2737 2ab43947 2020-03-18 stsp return NULL;
2740 2ab43947 2020-03-18 stsp static const struct got_error *
2741 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2742 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2743 2ab43947 2020-03-18 stsp struct got_repository *repo)
2745 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2746 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2747 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2748 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2750 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2752 2ab43947 2020-03-18 stsp goto done;
2754 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2755 2ab43947 2020-03-18 stsp is_same_branch = 1;
2756 2ab43947 2020-03-18 stsp goto done;
2758 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2759 2ab43947 2020-03-18 stsp is_same_branch = 1;
2760 2ab43947 2020-03-18 stsp goto done;
2763 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2765 2ab43947 2020-03-18 stsp goto done;
2767 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2768 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2770 2ab43947 2020-03-18 stsp goto done;
2772 2ab43947 2020-03-18 stsp for (;;) {
2773 2ab43947 2020-03-18 stsp struct got_object_id *id;
2774 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2775 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2776 2ab43947 2020-03-18 stsp if (err) {
2777 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2778 2ab43947 2020-03-18 stsp err = NULL;
2783 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2785 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2786 2ab43947 2020-03-18 stsp is_same_branch = 1;
2792 2ab43947 2020-03-18 stsp if (graph)
2793 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2794 2ab43947 2020-03-18 stsp free(head_commit_id);
2795 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2796 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2797 2ab43947 2020-03-18 stsp return err;
2800 4ed7e80c 2018-05-20 stsp static const struct got_error *
2801 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2803 4b6c9460 2020-03-05 stsp static char msg[512];
2804 4b6c9460 2020-03-05 stsp const char *branch_name;
2806 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2807 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2809 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2811 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2812 4b6c9460 2020-03-05 stsp branch_name += 11;
2814 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2815 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2816 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2817 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2818 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2819 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2821 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2824 4b6c9460 2020-03-05 stsp static const struct got_error *
2825 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2827 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2828 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2829 08e5873e 2021-09-14 stsp struct got_reference *head_ref = NULL, *ref = NULL;
2830 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2831 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2832 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2833 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2834 08e5873e 2021-09-14 stsp const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2835 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2836 08e5873e 2021-09-14 stsp struct got_object_id *commit_id = NULL;
2837 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2838 4ad4a1ec 2021-09-13 tracey int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2839 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2840 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2842 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2844 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2845 0bb8a95e 2018-03-12 stsp switch (ch) {
2847 08573d5b 2019-05-14 stsp branch_name = optarg;
2850 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2851 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2852 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2855 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2858 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2860 4ad4a1ec 2021-09-13 tracey case 'q':
2861 4ad4a1ec 2021-09-13 tracey verbosity = -1;
2864 2deda0b9 2019-03-07 stsp usage_checkout();
2865 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2869 0bb8a95e 2018-03-12 stsp argc -= optind;
2870 0bb8a95e 2018-03-12 stsp argv += optind;
2872 6715a751 2018-03-16 stsp #ifndef PROFILE
2873 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2874 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2875 c09a553d 2018-03-12 stsp err(1, "pledge");
2877 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2878 c47340da 2020-09-20 stsp char *base, *dotgit;
2879 f0207f6a 2020-10-19 stsp const char *path;
2880 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2881 76089277 2018-04-01 stsp if (repo_path == NULL)
2882 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2883 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2884 76089277 2018-04-01 stsp if (cwd == NULL) {
2885 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2886 76089277 2018-04-01 stsp goto done;
2888 c47340da 2020-09-20 stsp if (path_prefix[0])
2889 f0207f6a 2020-10-19 stsp path = path_prefix;
2891 f0207f6a 2020-10-19 stsp path = repo_path;
2892 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2893 f0207f6a 2020-10-19 stsp if (error)
2894 c47340da 2020-09-20 stsp goto done;
2895 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2896 c09a553d 2018-03-12 stsp if (dotgit)
2897 c09a553d 2018-03-12 stsp *dotgit = '\0';
2898 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2899 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2900 f0207f6a 2020-10-19 stsp free(base);
2901 76089277 2018-04-01 stsp goto done;
2903 f0207f6a 2020-10-19 stsp free(base);
2904 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2905 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2906 76089277 2018-04-01 stsp if (repo_path == NULL) {
2907 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2908 76089277 2018-04-01 stsp goto done;
2910 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2911 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2912 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2913 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2915 b4b3a7dd 2019-07-22 stsp goto done;
2917 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2918 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2919 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2920 b4b3a7dd 2019-07-22 stsp goto done;
2924 c09a553d 2018-03-12 stsp usage_checkout();
2926 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2927 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2929 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2930 c09a553d 2018-03-12 stsp if (error != NULL)
2931 c02c541e 2019-03-29 stsp goto done;
2933 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2934 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2935 c530dc23 2019-07-23 stsp if (error) {
2936 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2937 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2938 c530dc23 2019-07-23 stsp goto done;
2939 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2940 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2941 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2942 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2943 c530dc23 2019-07-23 stsp goto done;
2947 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2948 c02c541e 2019-03-29 stsp if (error)
2949 c09a553d 2018-03-12 stsp goto done;
2951 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2952 c09a553d 2018-03-12 stsp if (error != NULL)
2953 c09a553d 2018-03-12 stsp goto done;
2955 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2956 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2957 c09a553d 2018-03-12 stsp goto done;
2959 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2960 c09a553d 2018-03-12 stsp if (error != NULL)
2961 c09a553d 2018-03-12 stsp goto done;
2963 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2964 e5dc7198 2018-12-29 stsp path_prefix);
2965 e5dc7198 2018-12-29 stsp if (error != NULL)
2966 e5dc7198 2018-12-29 stsp goto done;
2967 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2968 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2969 49520a32 2018-12-29 stsp goto done;
2972 8069f636 2019-01-12 stsp if (commit_id_str) {
2973 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2974 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2975 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2977 84de9106 2020-12-26 stsp if (error)
2978 84de9106 2020-12-26 stsp goto done;
2979 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2980 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2981 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2982 30837e32 2019-07-25 stsp if (error)
2983 8069f636 2019-01-12 stsp goto done;
2984 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2985 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2986 8069f636 2019-01-12 stsp if (error != NULL) {
2987 8069f636 2019-01-12 stsp free(commit_id);
2988 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2989 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2990 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2992 8069f636 2019-01-12 stsp goto done;
2994 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2995 4b6c9460 2020-03-05 stsp if (error) {
2996 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2997 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2998 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
3000 45d344f6 2019-05-14 stsp goto done;
3002 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3003 8069f636 2019-01-12 stsp commit_id);
3004 8069f636 2019-01-12 stsp if (error)
3005 8069f636 2019-01-12 stsp goto done;
3006 08e5873e 2021-09-14 stsp /* Expand potentially abbreviated commit ID string. */
3007 08e5873e 2021-09-14 stsp free(commit_id_str);
3008 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3009 08e5873e 2021-09-14 stsp if (error)
3010 08e5873e 2021-09-14 stsp goto done;
3012 08e5873e 2021-09-14 stsp commit_id = got_object_id_dup(
3013 08e5873e 2021-09-14 stsp got_worktree_get_base_commit_id(worktree));
3014 08e5873e 2021-09-14 stsp if (commit_id == NULL) {
3015 08e5873e 2021-09-14 stsp error = got_error_from_errno("got_object_id_dup");
3016 08e5873e 2021-09-14 stsp goto done;
3018 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3019 08e5873e 2021-09-14 stsp if (error)
3020 08e5873e 2021-09-14 stsp goto done;
3023 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
3024 f2ea84fa 2019-07-27 stsp if (error)
3025 f2ea84fa 2019-07-27 stsp goto done;
3026 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3027 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3028 4ad4a1ec 2021-09-13 tracey cpa.verbosity = verbosity;
3029 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3030 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3031 c09a553d 2018-03-12 stsp if (error != NULL)
3032 c09a553d 2018-03-12 stsp goto done;
3034 08e5873e 2021-09-14 stsp if (got_ref_is_symbolic(head_ref)) {
3035 08e5873e 2021-09-14 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3036 08e5873e 2021-09-14 stsp if (error)
3037 08e5873e 2021-09-14 stsp goto done;
3038 08e5873e 2021-09-14 stsp refname = got_ref_get_name(ref);
3040 08e5873e 2021-09-14 stsp refname = got_ref_get_name(head_ref);
3041 08e5873e 2021-09-14 stsp printf("Checked out %s: %s\n", refname, commit_id_str);
3042 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3043 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3044 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3046 08e5873e 2021-09-14 stsp if (head_ref)
3047 08e5873e 2021-09-14 stsp got_ref_close(head_ref);
3049 08e5873e 2021-09-14 stsp got_ref_close(ref);
3050 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3051 8069f636 2019-01-12 stsp free(commit_id_str);
3052 08e5873e 2021-09-14 stsp free(commit_id);
3053 76089277 2018-04-01 stsp free(repo_path);
3054 507dc3bb 2018-12-29 stsp free(worktree_path);
3055 c47340da 2020-09-20 stsp free(cwd);
3056 507dc3bb 2018-12-29 stsp return error;
3059 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3060 9627c110 2020-04-18 stsp int did_something;
3061 9627c110 2020-04-18 stsp int conflicts;
3062 9627c110 2020-04-18 stsp int obstructed;
3063 9627c110 2020-04-18 stsp int not_updated;
3064 f259c4c1 2021-09-24 stsp int missing;
3065 35ca1db7 2021-09-28 stsp int not_deleted;
3066 35ca1db7 2021-09-28 stsp int unversioned;
3067 4ad4a1ec 2021-09-13 tracey int verbosity;
3071 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3073 9627c110 2020-04-18 stsp if (!upa->did_something)
3076 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3077 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3078 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3079 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3080 9627c110 2020-04-18 stsp upa->obstructed);
3081 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3082 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3083 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3087 35ca1db7 2021-09-28 stsp * The meaning of some status codes differs between merge-style operations and
3088 35ca1db7 2021-09-28 stsp * update operations. For example, the ! status code means "file was missing"
3089 35ca1db7 2021-09-28 stsp * if changes were merged into the work tree, and "missing file was restored"
3090 35ca1db7 2021-09-28 stsp * if the work tree was updated. This function should be used by any operation
3091 35ca1db7 2021-09-28 stsp * which merges changes into the work tree without updating the work tree.
3094 35ca1db7 2021-09-28 stsp print_merge_progress_stats(struct got_update_progress_arg *upa)
3096 35ca1db7 2021-09-28 stsp if (!upa->did_something)
3099 35ca1db7 2021-09-28 stsp if (upa->conflicts > 0)
3100 35ca1db7 2021-09-28 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3101 35ca1db7 2021-09-28 stsp if (upa->obstructed > 0)
3102 35ca1db7 2021-09-28 stsp printf("File paths obstructed by a non-regular file: %d\n",
3103 35ca1db7 2021-09-28 stsp upa->obstructed);
3104 35ca1db7 2021-09-28 stsp if (upa->missing > 0)
3105 35ca1db7 2021-09-28 stsp printf("Files which had incoming changes but could not be "
3106 35ca1db7 2021-09-28 stsp "found in the work tree: %d\n", upa->missing);
3107 35ca1db7 2021-09-28 stsp if (upa->not_deleted > 0)
3108 35ca1db7 2021-09-28 stsp printf("Files not deleted due to differences in deleted "
3109 35ca1db7 2021-09-28 stsp "content: %d\n", upa->not_deleted);
3110 35ca1db7 2021-09-28 stsp if (upa->unversioned > 0)
3111 35ca1db7 2021-09-28 stsp printf("Files not merged because an unversioned file was "
3112 35ca1db7 2021-09-28 stsp "found in the work tree: %d\n", upa->unversioned);
3115 507dc3bb 2018-12-29 stsp __dead static void
3116 507dc3bb 2018-12-29 stsp usage_update(void)
3118 4ad4a1ec 2021-09-13 tracey fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3119 4ad4a1ec 2021-09-13 tracey "[path ...]\n",
3120 507dc3bb 2018-12-29 stsp getprogname());
3124 1ee397ad 2019-07-12 stsp static const struct got_error *
3125 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3127 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3129 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3130 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3131 1ee397ad 2019-07-12 stsp return NULL;
3133 9627c110 2020-04-18 stsp upa->did_something = 1;
3135 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3136 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3137 1ee397ad 2019-07-12 stsp return NULL;
3139 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3140 9627c110 2020-04-18 stsp upa->conflicts++;
3141 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3142 9627c110 2020-04-18 stsp upa->obstructed++;
3143 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3144 9627c110 2020-04-18 stsp upa->not_updated++;
3145 f259c4c1 2021-09-24 stsp if (status == GOT_STATUS_MISSING)
3146 f259c4c1 2021-09-24 stsp upa->missing++;
3147 35ca1db7 2021-09-28 stsp if (status == GOT_STATUS_CANNOT_DELETE)
3148 35ca1db7 2021-09-28 stsp upa->not_deleted++;
3149 35ca1db7 2021-09-28 stsp if (status == GOT_STATUS_UNVERSIONED)
3150 35ca1db7 2021-09-28 stsp upa->unversioned++;
3152 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3154 4ad4a1ec 2021-09-13 tracey if (upa->verbosity >= 0)
3155 4ad4a1ec 2021-09-13 tracey printf("%c %s\n", status, path);
3157 1ee397ad 2019-07-12 stsp return NULL;
3160 be7061eb 2018-12-30 stsp static const struct got_error *
3161 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3162 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3163 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3165 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3166 a1fb16d8 2019-05-24 stsp char *base_id_str;
3167 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3169 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3170 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3171 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3172 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3173 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3174 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3175 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3178 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3179 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3180 a1fb16d8 2019-05-24 stsp if (err) {
3181 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3182 a1fb16d8 2019-05-24 stsp return err;
3183 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3185 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3186 a1fb16d8 2019-05-24 stsp return NULL;
3188 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3189 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3190 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3192 a1fb16d8 2019-05-24 stsp return err;
3193 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3194 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3195 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3196 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3197 0ebf8283 2019-07-24 stsp return NULL;
3200 0ebf8283 2019-07-24 stsp static const struct got_error *
3201 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3203 0ebf8283 2019-07-24 stsp const struct got_error *err;
3204 0ebf8283 2019-07-24 stsp int in_progress;
3206 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3208 0ebf8283 2019-07-24 stsp return err;
3209 0ebf8283 2019-07-24 stsp if (in_progress)
3210 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3212 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3214 0ebf8283 2019-07-24 stsp return err;
3215 0ebf8283 2019-07-24 stsp if (in_progress)
3216 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3218 f259c4c1 2021-09-24 stsp return NULL;
3221 f259c4c1 2021-09-24 stsp static const struct got_error *
3222 f259c4c1 2021-09-24 stsp check_merge_in_progress(struct got_worktree *worktree,
3223 f259c4c1 2021-09-24 stsp struct got_repository *repo)
3225 f259c4c1 2021-09-24 stsp const struct got_error *err;
3226 f259c4c1 2021-09-24 stsp int in_progress;
3228 f259c4c1 2021-09-24 stsp err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3230 f259c4c1 2021-09-24 stsp return err;
3231 f259c4c1 2021-09-24 stsp if (in_progress)
3232 f259c4c1 2021-09-24 stsp return got_error(GOT_ERR_MERGE_BUSY);
3234 a1fb16d8 2019-05-24 stsp return NULL;
3237 a5edda0a 2019-07-27 stsp static const struct got_error *
3238 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3239 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3241 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3242 a5edda0a 2019-07-27 stsp char *path;
3245 a5edda0a 2019-07-27 stsp if (argc == 0) {
3246 a5edda0a 2019-07-27 stsp path = strdup("");
3247 a5edda0a 2019-07-27 stsp if (path == NULL)
3248 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3249 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3252 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3253 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3256 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3257 a5edda0a 2019-07-27 stsp if (err) {
3258 a5edda0a 2019-07-27 stsp free(path);
3263 a5edda0a 2019-07-27 stsp return err;
3266 a1fb16d8 2019-05-24 stsp static const struct got_error *
3267 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3268 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3270 fa51e947 2020-03-27 stsp const struct got_error *err;
3271 fa51e947 2020-03-27 stsp struct got_repository *repo;
3272 fa51e947 2020-03-27 stsp static char msg[512];
3274 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3276 fa51e947 2020-03-27 stsp return orig_err;
3278 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3279 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3280 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3281 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3282 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3283 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3284 fa51e947 2020-03-27 stsp got_repo_close(repo);
3285 fa51e947 2020-03-27 stsp return err;
3288 fa51e947 2020-03-27 stsp static const struct got_error *
3289 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3291 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3292 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3293 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3294 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3295 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3296 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3297 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3298 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3299 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3300 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3301 4ad4a1ec 2021-09-13 tracey int ch, verbosity = 0;
3302 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3304 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3306 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3307 507dc3bb 2018-12-29 stsp switch (ch) {
3309 024e9686 2019-05-14 stsp branch_name = optarg;
3312 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3313 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3314 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3316 4ad4a1ec 2021-09-13 tracey case 'q':
3317 4ad4a1ec 2021-09-13 tracey verbosity = -1;
3320 2deda0b9 2019-03-07 stsp usage_update();
3321 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3325 507dc3bb 2018-12-29 stsp argc -= optind;
3326 507dc3bb 2018-12-29 stsp argv += optind;
3328 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3329 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3330 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3331 507dc3bb 2018-12-29 stsp err(1, "pledge");
3333 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3334 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3335 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3336 c4cdcb68 2019-04-03 stsp goto done;
3338 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3339 fa51e947 2020-03-27 stsp if (error) {
3340 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3341 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3342 fa51e947 2020-03-27 stsp worktree_path);
3343 7d5807f4 2019-07-11 stsp goto done;
3346 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3347 f2ea84fa 2019-07-27 stsp if (error)
3348 f2ea84fa 2019-07-27 stsp goto done;
3350 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3352 507dc3bb 2018-12-29 stsp if (error != NULL)
3353 507dc3bb 2018-12-29 stsp goto done;
3355 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3356 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3357 087fb88c 2019-08-04 stsp if (error)
3358 087fb88c 2019-08-04 stsp goto done;
3360 f259c4c1 2021-09-24 stsp error = check_merge_in_progress(worktree, repo);
3361 f259c4c1 2021-09-24 stsp if (error)
3362 f259c4c1 2021-09-24 stsp goto done;
3364 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3365 0266afb7 2019-01-04 stsp if (error)
3366 0266afb7 2019-01-04 stsp goto done;
3368 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3369 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3370 024e9686 2019-05-14 stsp if (error != NULL)
3371 024e9686 2019-05-14 stsp goto done;
3372 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3373 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3374 9c4b8182 2019-01-02 stsp if (error != NULL)
3375 9c4b8182 2019-01-02 stsp goto done;
3376 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3377 507dc3bb 2018-12-29 stsp if (error != NULL)
3378 507dc3bb 2018-12-29 stsp goto done;
3380 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3381 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3382 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3384 84de9106 2020-12-26 stsp if (error)
3385 84de9106 2020-12-26 stsp goto done;
3386 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3387 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3388 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3389 04f57cb3 2019-07-25 stsp free(commit_id_str);
3390 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3391 30837e32 2019-07-25 stsp if (error)
3392 a297e751 2019-07-11 stsp goto done;
3393 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3394 a297e751 2019-07-11 stsp if (error)
3395 507dc3bb 2018-12-29 stsp goto done;
3398 a1fb16d8 2019-05-24 stsp if (branch_name) {
3399 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3400 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3401 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3403 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3404 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3405 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3406 024e9686 2019-05-14 stsp goto done;
3408 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3409 024e9686 2019-05-14 stsp if (error)
3410 024e9686 2019-05-14 stsp goto done;
3411 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3413 a367ff0f 2019-05-14 stsp free(head_commit_id);
3414 024e9686 2019-05-14 stsp if (error != NULL)
3415 024e9686 2019-05-14 stsp goto done;
3416 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3417 a367ff0f 2019-05-14 stsp if (error)
3418 a367ff0f 2019-05-14 stsp goto done;
3419 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3420 024e9686 2019-05-14 stsp if (error)
3421 024e9686 2019-05-14 stsp goto done;
3423 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3424 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3425 a367ff0f 2019-05-14 stsp if (error != NULL) {
3426 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3427 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3428 024e9686 2019-05-14 stsp goto done;
3430 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3431 a367ff0f 2019-05-14 stsp if (error)
3432 a367ff0f 2019-05-14 stsp goto done;
3435 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3436 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3437 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3438 507dc3bb 2018-12-29 stsp commit_id);
3439 507dc3bb 2018-12-29 stsp if (error)
3440 507dc3bb 2018-12-29 stsp goto done;
3443 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3444 4ad4a1ec 2021-09-13 tracey upa.verbosity = verbosity;
3445 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3446 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3447 507dc3bb 2018-12-29 stsp if (error != NULL)
3448 507dc3bb 2018-12-29 stsp goto done;
3450 4f3c844b 2021-09-14 stsp if (upa.did_something) {
3451 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
3452 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree), commit_id_str);
3454 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3455 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3457 c09a553d 2018-03-12 stsp free(worktree_path);
3458 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3459 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3460 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3461 507dc3bb 2018-12-29 stsp free(commit_id);
3462 9c4b8182 2019-01-02 stsp free(commit_id_str);
3463 c09a553d 2018-03-12 stsp return error;
3466 f42b1b34 2018-03-12 stsp static const struct got_error *
3467 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3468 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3469 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3471 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3472 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3474 44392932 2019-08-25 stsp if (blob_id1) {
3475 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3477 44392932 2019-08-25 stsp goto done;
3480 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3482 44392932 2019-08-25 stsp goto done;
3484 44392932 2019-08-25 stsp while (path[0] == '/')
3486 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3487 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3489 44392932 2019-08-25 stsp if (blob1)
3490 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3491 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3492 44392932 2019-08-25 stsp return err;
3495 44392932 2019-08-25 stsp static const struct got_error *
3496 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3497 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3498 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3500 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3501 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3502 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3504 44392932 2019-08-25 stsp if (tree_id1) {
3505 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3507 44392932 2019-08-25 stsp goto done;
3510 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3512 0f2b3dca 2018-12-22 stsp goto done;
3514 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3515 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3516 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3517 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3518 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3519 fe621944 2020-11-10 stsp arg.nlines = 0;
3520 44392932 2019-08-25 stsp while (path[0] == '/')
3522 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3523 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3525 79109fed 2018-03-27 stsp if (tree1)
3526 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3527 366e0a5f 2019-10-10 stsp if (tree2)
3528 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3529 44392932 2019-08-25 stsp return err;
3532 44392932 2019-08-25 stsp static const struct got_error *
3533 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3534 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3536 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3537 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3538 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3539 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3541 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3542 0208f208 2020-05-05 stsp if (qid != NULL) {
3543 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3544 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3547 0208f208 2020-05-05 stsp return err;
3549 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3550 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3551 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3552 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3553 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3555 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3559 0208f208 2020-05-05 stsp if (tree_id1) {
3560 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3562 0208f208 2020-05-05 stsp goto done;
3565 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3566 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3568 0208f208 2020-05-05 stsp goto done;
3570 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3571 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3573 0208f208 2020-05-05 stsp if (tree1)
3574 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3575 0208f208 2020-05-05 stsp if (tree2)
3576 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3577 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3578 0208f208 2020-05-05 stsp return err;
3581 0208f208 2020-05-05 stsp static const struct got_error *
3582 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3583 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3585 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3586 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3587 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3588 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3589 44392932 2019-08-25 stsp struct got_object_qid *qid;
3591 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3592 44392932 2019-08-25 stsp if (qid != NULL) {
3593 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3596 44392932 2019-08-25 stsp return err;
3599 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3600 44392932 2019-08-25 stsp int obj_type;
3601 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3603 44392932 2019-08-25 stsp goto done;
3604 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3605 44392932 2019-08-25 stsp if (err) {
3606 44392932 2019-08-25 stsp free(obj_id2);
3607 44392932 2019-08-25 stsp goto done;
3609 44392932 2019-08-25 stsp if (pcommit) {
3610 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3611 44392932 2019-08-25 stsp qid->id, path);
3612 44392932 2019-08-25 stsp if (err) {
3613 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3614 2e8c69d1 2020-05-04 stsp free(obj_id2);
3615 2e8c69d1 2020-05-04 stsp goto done;
3618 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3619 2e8c69d1 2020-05-04 stsp if (err) {
3620 2e8c69d1 2020-05-04 stsp free(obj_id2);
3621 2e8c69d1 2020-05-04 stsp goto done;
3625 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3626 44392932 2019-08-25 stsp if (err) {
3627 44392932 2019-08-25 stsp free(obj_id2);
3628 44392932 2019-08-25 stsp goto done;
3630 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3631 44392932 2019-08-25 stsp switch (obj_type) {
3632 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3633 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3634 64453f7e 2020-11-21 stsp 0, 0, repo);
3636 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3637 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3638 64453f7e 2020-11-21 stsp 0, 0, repo);
3641 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3644 44392932 2019-08-25 stsp free(obj_id1);
3645 44392932 2019-08-25 stsp free(obj_id2);
3647 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3648 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3650 44392932 2019-08-25 stsp goto done;
3651 579bd556 2020-10-24 stsp if (pcommit) {
3652 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3653 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3655 579bd556 2020-10-24 stsp goto done;
3657 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3659 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3663 0f2b3dca 2018-12-22 stsp free(id_str1);
3664 0f2b3dca 2018-12-22 stsp free(id_str2);
3665 44392932 2019-08-25 stsp if (pcommit)
3666 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3667 79109fed 2018-03-27 stsp return err;
3670 4bb494d5 2018-06-16 stsp static char *
3671 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3673 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3674 09867e48 2019-08-13 stsp char *p, *s;
3676 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3677 09867e48 2019-08-13 stsp if (tm == NULL)
3678 09867e48 2019-08-13 stsp return NULL;
3679 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3680 09867e48 2019-08-13 stsp if (s == NULL)
3681 09867e48 2019-08-13 stsp return NULL;
3682 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3684 6c281f94 2018-06-11 stsp *p = '\0';
3688 6841bf13 2019-11-29 kn static const struct got_error *
3689 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3690 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3692 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3693 6841bf13 2019-11-29 kn regmatch_t regmatch;
3694 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3696 6841bf13 2019-11-29 kn *have_match = 0;
3698 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3702 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3706 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3707 6841bf13 2019-11-29 kn *have_match = 1;
3709 6841bf13 2019-11-29 kn free(id_str);
3710 6841bf13 2019-11-29 kn free(logmsg);
3714 0208f208 2020-05-05 stsp static void
3715 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3716 0208f208 2020-05-05 stsp regex_t *regex)
3718 0208f208 2020-05-05 stsp regmatch_t regmatch;
3719 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3721 0208f208 2020-05-05 stsp *have_match = 0;
3723 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3724 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3725 0208f208 2020-05-05 stsp *have_match = 1;
3731 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3733 888b7d99 2020-12-26 stsp static const struct got_error*
3734 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3735 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3737 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3738 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3740 888b7d99 2020-12-26 stsp const char *name;
3742 888b7d99 2020-12-26 stsp *refs_str = NULL;
3744 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3745 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3746 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3749 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3750 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3752 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3753 199a4027 2019-02-02 stsp name += 5;
3754 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3756 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3757 e34f9ed6 2019-02-02 stsp name += 6;
3758 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3759 141c2bff 2019-02-04 stsp name += 8;
3760 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3761 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3764 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3767 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3768 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3769 5d844a1e 2019-08-13 stsp if (err) {
3770 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3771 48cae60d 2020-09-22 stsp free(ref_id);
3774 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3775 5d844a1e 2019-08-13 stsp err = NULL;
3776 5d844a1e 2019-08-13 stsp tag = NULL;
3779 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3780 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3781 48cae60d 2020-09-22 stsp free(ref_id);
3783 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3784 a436ad14 2019-08-13 stsp if (cmp != 0)
3786 888b7d99 2020-12-26 stsp s = *refs_str;
3787 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3788 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3789 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3791 888b7d99 2020-12-26 stsp *refs_str = NULL;
3797 888b7d99 2020-12-26 stsp return err;
3800 888b7d99 2020-12-26 stsp static const struct got_error *
3801 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3802 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3803 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3804 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3805 e600f124 2021-03-21 stsp const char *custom_refs_str)
3807 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3808 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3809 888b7d99 2020-12-26 stsp char datebuf[26];
3810 888b7d99 2020-12-26 stsp time_t committer_time;
3811 888b7d99 2020-12-26 stsp const char *author, *committer;
3812 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3814 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3816 8bf5b3c9 2018-03-17 stsp return err;
3818 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3819 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3820 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3821 e600f124 2021-03-21 stsp if (refs) {
3822 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3824 e600f124 2021-03-21 stsp goto done;
3828 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3829 e600f124 2021-03-21 stsp if (custom_refs_str)
3830 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3832 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3833 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3834 832c249c 2018-06-10 stsp free(id_str);
3835 d3d493d7 2019-02-21 stsp id_str = NULL;
3836 d3d493d7 2019-02-21 stsp free(refs_str);
3837 d3d493d7 2019-02-21 stsp refs_str = NULL;
3838 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3839 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3840 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3841 09867e48 2019-08-13 stsp if (datestr)
3842 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3843 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3844 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3845 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3846 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3847 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3848 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3849 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3850 3fe1abad 2018-06-10 stsp int n = 1;
3851 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3852 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3853 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3855 888b7d99 2020-12-26 stsp goto done;
3856 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3857 a0603db2 2018-06-10 stsp free(id_str);
3858 888b7d99 2020-12-26 stsp id_str = NULL;
3862 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3864 888b7d99 2020-12-26 stsp goto done;
3866 621015ac 2018-07-23 stsp logmsg = logmsg0;
3868 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3870 832c249c 2018-06-10 stsp printf(" %s\n", line);
3871 832c249c 2018-06-10 stsp } while (line);
3872 621015ac 2018-07-23 stsp free(logmsg0);
3874 0208f208 2020-05-05 stsp if (changed_paths) {
3875 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3876 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3877 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3878 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3880 0208f208 2020-05-05 stsp printf("\n");
3882 971751ac 2018-03-27 stsp if (show_patch) {
3883 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3884 971751ac 2018-03-27 stsp if (err == 0)
3885 971751ac 2018-03-27 stsp printf("\n");
3888 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3889 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3891 888b7d99 2020-12-26 stsp free(id_str);
3892 888b7d99 2020-12-26 stsp free(refs_str);
3893 07862c20 2018-09-15 stsp return err;
3896 f42b1b34 2018-03-12 stsp static const struct got_error *
3897 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3898 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3899 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3900 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3901 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3903 f42b1b34 2018-03-12 stsp const struct got_error *err;
3904 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3905 6841bf13 2019-11-29 kn regex_t regex;
3906 6841bf13 2019-11-29 kn int have_match;
3907 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3908 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3909 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3910 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3911 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3913 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3914 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3916 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3917 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3918 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3920 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3922 f42b1b34 2018-03-12 stsp return err;
3923 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3924 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3926 fcc85cad 2018-07-22 stsp goto done;
3928 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3930 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3933 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3934 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3935 372ccdbb 2018-06-10 stsp if (err) {
3936 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3937 9ba79e04 2018-06-11 stsp err = NULL;
3940 b43fbaa0 2018-06-11 stsp if (id == NULL)
3943 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3947 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3948 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3953 6841bf13 2019-11-29 kn if (search_pattern) {
3954 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3956 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3959 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3960 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3961 0208f208 2020-05-05 stsp &changed_paths, ®ex);
3962 6841bf13 2019-11-29 kn if (have_match == 0) {
3963 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3964 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3965 0208f208 2020-05-05 stsp free((char *)pe->path);
3966 0208f208 2020-05-05 stsp free(pe->data);
3968 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3973 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3974 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3977 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3978 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3980 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3981 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3982 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3983 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3987 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3988 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3991 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3992 0208f208 2020-05-05 stsp free((char *)pe->path);
3993 0208f208 2020-05-05 stsp free(pe->data);
3995 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3997 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3998 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
3999 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
4002 502b9684 2020-07-31 stsp if (show_changed_paths) {
4003 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
4004 502b9684 2020-07-31 stsp commit, repo);
4008 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
4009 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
4010 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
4011 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
4014 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4015 502b9684 2020-07-31 stsp free((char *)pe->path);
4016 502b9684 2020-07-31 stsp free(pe->data);
4018 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
4022 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
4023 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
4024 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4025 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
4027 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4028 0208f208 2020-05-05 stsp free((char *)pe->path);
4029 0208f208 2020-05-05 stsp free(pe->data);
4031 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4032 6841bf13 2019-11-29 kn if (search_pattern)
4033 6841bf13 2019-11-29 kn regfree(®ex);
4034 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
4035 f42b1b34 2018-03-12 stsp return err;
4038 4ed7e80c 2018-05-20 stsp __dead static void
4039 6f3d1eb0 2018-03-12 stsp usage_log(void)
4041 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4042 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4043 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
4047 b1ebc001 2019-08-13 stsp static int
4048 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
4050 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
4051 b1ebc001 2019-08-13 stsp long long n;
4052 b1ebc001 2019-08-13 stsp const char *errstr;
4054 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4055 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
4057 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4058 b1ebc001 2019-08-13 stsp if (errstr != NULL)
4063 4ed7e80c 2018-05-20 stsp static const struct got_error *
4064 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
4066 f42b1b34 2018-03-12 stsp const struct got_error *error;
4067 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
4068 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
4069 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
4070 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4071 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
4072 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
4073 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
4074 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4075 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
4076 64a96a6d 2018-04-01 stsp const char *errstr;
4077 199a4027 2019-02-02 stsp struct got_reflist_head refs;
4078 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
4080 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4082 6715a751 2018-03-16 stsp #ifndef PROFILE
4083 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4086 f42b1b34 2018-03-12 stsp err(1, "pledge");
4089 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
4091 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4092 79109fed 2018-03-27 stsp switch (ch) {
4094 79109fed 2018-03-27 stsp show_patch = 1;
4097 0208f208 2020-05-05 stsp show_changed_paths = 1;
4100 d142fc45 2018-04-01 stsp start_commit = optarg;
4103 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4105 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4106 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4109 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4110 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4111 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
4114 48c8c60d 2020-01-27 stsp log_branches = 1;
4117 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4118 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4119 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4121 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4124 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4127 6841bf13 2019-11-29 kn search_pattern = optarg;
4130 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4133 2deda0b9 2019-03-07 stsp usage_log();
4134 79109fed 2018-03-27 stsp /* NOTREACHED */
4138 79109fed 2018-03-27 stsp argc -= optind;
4139 79109fed 2018-03-27 stsp argv += optind;
4141 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4142 dc1edbfa 2019-11-29 kn diff_context = 3;
4143 dc1edbfa 2019-11-29 kn else if (!show_patch)
4144 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4146 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4147 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4148 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4149 04ca23f4 2018-07-16 stsp goto done;
4152 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4153 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4154 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4155 50f2fada 2020-04-24 stsp goto done;
4156 50f2fada 2020-04-24 stsp error = NULL;
4159 603cdeb0 2020-10-22 stsp if (argc == 1) {
4160 e7301579 2019-03-18 stsp if (worktree) {
4161 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4163 e7301579 2019-03-18 stsp if (error)
4164 e7301579 2019-03-18 stsp goto done;
4166 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4167 e7301579 2019-03-18 stsp if (path == NULL) {
4168 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4169 e7301579 2019-03-18 stsp goto done;
4172 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4173 cbd1af7a 2019-03-18 stsp usage_log();
4175 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4176 5486daa2 2019-05-11 stsp repo_path = worktree ?
4177 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4179 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4180 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4181 cffc0aa4 2019-02-05 stsp goto done;
4184 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4185 d7d4f210 2018-03-12 stsp if (error != NULL)
4186 04ca23f4 2018-07-16 stsp goto done;
4188 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4189 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4190 c02c541e 2019-03-29 stsp if (error)
4191 c02c541e 2019-03-29 stsp goto done;
4193 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4194 84de9106 2020-12-26 stsp if (error)
4195 84de9106 2020-12-26 stsp goto done;
4197 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4198 888b7d99 2020-12-26 stsp if (error)
4199 888b7d99 2020-12-26 stsp goto done;
4201 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4202 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4203 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4204 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4205 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4206 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4207 3235492e 2018-04-01 stsp if (error != NULL)
4208 d1fe46f9 2020-04-18 stsp goto done;
4209 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4210 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4211 3235492e 2018-04-01 stsp if (error != NULL)
4212 d1fe46f9 2020-04-18 stsp goto done;
4213 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4214 d1fe46f9 2020-04-18 stsp start_id);
4215 d1fe46f9 2020-04-18 stsp if (error != NULL)
4216 d1fe46f9 2020-04-18 stsp goto done;
4217 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4219 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4220 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4221 d1fe46f9 2020-04-18 stsp if (error != NULL)
4222 d1fe46f9 2020-04-18 stsp goto done;
4224 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4225 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4226 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4227 d1fe46f9 2020-04-18 stsp if (error != NULL)
4228 d1fe46f9 2020-04-18 stsp goto done;
4231 deeabeae 2019-08-27 stsp if (worktree) {
4233 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4234 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4235 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4237 603cdeb0 2020-10-22 stsp if (path) {
4238 603cdeb0 2020-10-22 stsp const char *prefix;
4239 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4240 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4241 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4242 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4243 603cdeb0 2020-10-22 stsp goto done;
4247 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4248 8fa913ec 2020-11-14 stsp path ? path : "");
4249 04ca23f4 2018-07-16 stsp if (error != NULL)
4250 04ca23f4 2018-07-16 stsp goto done;
4251 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4252 04ca23f4 2018-07-16 stsp free(path);
4253 04ca23f4 2018-07-16 stsp path = in_repo_path;
4256 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4257 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4258 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4260 04ca23f4 2018-07-16 stsp free(path);
4261 04ca23f4 2018-07-16 stsp free(repo_path);
4262 04ca23f4 2018-07-16 stsp free(cwd);
4263 cffc0aa4 2019-02-05 stsp if (worktree)
4264 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4265 ad242220 2018-09-08 stsp if (repo) {
4266 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4267 ad242220 2018-09-08 stsp if (error == NULL)
4268 1d0f4054 2021-06-17 stsp error = close_err;
4270 888b7d99 2020-12-26 stsp if (refs_idmap)
4271 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4272 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4273 8bf5b3c9 2018-03-17 stsp return error;
4276 4ed7e80c 2018-05-20 stsp __dead static void
4277 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4279 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4280 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4284 b72f483a 2019-02-05 stsp struct print_diff_arg {
4285 b72f483a 2019-02-05 stsp struct got_repository *repo;
4286 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4287 b72f483a 2019-02-05 stsp int diff_context;
4288 3fc0c068 2019-02-10 stsp const char *id_str;
4289 3fc0c068 2019-02-10 stsp int header_shown;
4290 98eaaa12 2019-08-03 stsp int diff_staged;
4291 63035f9f 2019-10-06 stsp int ignore_whitespace;
4292 64453f7e 2020-11-21 stsp int force_text_diff;
4296 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4297 39449a05 2020-07-23 stsp * it as content to the diff engine.
4299 39449a05 2020-07-23 stsp static const struct got_error *
4300 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4301 39449a05 2020-07-23 stsp const char *abspath)
4303 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4304 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4305 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4309 39449a05 2020-07-23 stsp if (dirfd != -1) {
4310 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4311 39449a05 2020-07-23 stsp if (target_len == -1)
4312 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4314 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4315 39449a05 2020-07-23 stsp if (target_len == -1)
4316 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4319 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4320 39449a05 2020-07-23 stsp if (*fd == -1)
4321 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4323 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4324 39449a05 2020-07-23 stsp if (outlen == -1) {
4325 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4326 39449a05 2020-07-23 stsp goto done;
4329 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4330 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4331 39449a05 2020-07-23 stsp goto done;
4334 39449a05 2020-07-23 stsp if (err) {
4335 39449a05 2020-07-23 stsp close(*fd);
4338 39449a05 2020-07-23 stsp return err;
4341 4ed7e80c 2018-05-20 stsp static const struct got_error *
4342 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4343 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4344 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4345 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4347 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4348 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4349 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4350 12463d8b 2019-12-13 stsp int fd = -1;
4351 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4352 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4353 b72f483a 2019-02-05 stsp struct stat sb;
4355 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4356 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4357 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4358 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4359 98eaaa12 2019-08-03 stsp return NULL;
4361 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4362 98eaaa12 2019-08-03 stsp return NULL;
4363 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4364 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4365 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4366 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4367 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4368 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4369 98eaaa12 2019-08-03 stsp return NULL;
4372 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4373 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4374 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4375 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4376 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4379 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4380 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4381 98eaaa12 2019-08-03 stsp switch (staged_status) {
4382 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4383 98eaaa12 2019-08-03 stsp label1 = path;
4384 98eaaa12 2019-08-03 stsp label2 = path;
4386 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4387 98eaaa12 2019-08-03 stsp label2 = path;
4389 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4390 98eaaa12 2019-08-03 stsp label1 = path;
4393 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4395 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4396 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4397 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4400 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4401 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4402 4ce46740 2019-08-08 stsp char *id_str;
4403 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4406 4ce46740 2019-08-08 stsp goto done;
4407 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4409 4ce46740 2019-08-08 stsp goto done;
4410 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4411 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4412 4ce46740 2019-08-08 stsp free(id_str);
4413 4ce46740 2019-08-08 stsp goto done;
4415 4ce46740 2019-08-08 stsp free(id_str);
4416 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4417 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4419 4ce46740 2019-08-08 stsp goto done;
4422 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4423 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4424 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4425 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4426 2ec1f75b 2019-03-26 stsp goto done;
4429 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4430 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4431 12463d8b 2019-12-13 stsp if (fd == -1) {
4432 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4433 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4435 39449a05 2020-07-23 stsp goto done;
4437 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4438 39449a05 2020-07-23 stsp de_name, abspath);
4440 39449a05 2020-07-23 stsp goto done;
4443 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4444 12463d8b 2019-12-13 stsp if (fd == -1) {
4445 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4446 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4448 39449a05 2020-07-23 stsp goto done;
4450 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4451 39449a05 2020-07-23 stsp de_name, abspath);
4453 39449a05 2020-07-23 stsp goto done;
4456 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4457 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4458 2ec1f75b 2019-03-26 stsp goto done;
4460 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4461 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4462 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4463 2ec1f75b 2019-03-26 stsp goto done;
4467 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4469 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4470 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4472 b72f483a 2019-02-05 stsp if (blob1)
4473 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4474 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4475 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4476 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4477 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4478 b72f483a 2019-02-05 stsp free(abspath);
4479 927df6b7 2019-02-10 stsp return err;
4482 927df6b7 2019-02-10 stsp static const struct got_error *
4483 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4485 b00d56cd 2018-04-01 stsp const struct got_error *error;
4486 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4487 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4488 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4489 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4490 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4491 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4492 15a94983 2018-12-23 stsp int type1, type2;
4493 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4494 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4495 c0cc5c62 2018-10-18 stsp const char *errstr;
4496 927df6b7 2019-02-10 stsp char *path = NULL;
4497 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4499 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4501 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4502 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4503 25eccc22 2019-01-04 stsp NULL) == -1)
4504 b00d56cd 2018-04-01 stsp err(1, "pledge");
4507 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4508 b00d56cd 2018-04-01 stsp switch (ch) {
4510 64453f7e 2020-11-21 stsp force_text_diff = 1;
4513 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4515 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4516 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4519 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4520 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4521 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4523 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4526 98eaaa12 2019-08-03 stsp diff_staged = 1;
4529 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4532 2deda0b9 2019-03-07 stsp usage_diff();
4533 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4537 b00d56cd 2018-04-01 stsp argc -= optind;
4538 b00d56cd 2018-04-01 stsp argv += optind;
4540 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4541 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4542 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4543 b72f483a 2019-02-05 stsp goto done;
4545 927df6b7 2019-02-10 stsp if (argc <= 1) {
4546 b72f483a 2019-02-05 stsp if (repo_path)
4548 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4549 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4550 fa51e947 2020-03-27 stsp if (error) {
4551 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4552 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4554 1f03b8da 2020-03-20 stsp goto done;
4556 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4557 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4558 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4559 927df6b7 2019-02-10 stsp goto done;
4561 927df6b7 2019-02-10 stsp if (argc == 1) {
4562 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4564 927df6b7 2019-02-10 stsp if (error)
4565 927df6b7 2019-02-10 stsp goto done;
4567 927df6b7 2019-02-10 stsp path = strdup("");
4568 927df6b7 2019-02-10 stsp if (path == NULL) {
4569 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4570 927df6b7 2019-02-10 stsp goto done;
4573 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4574 98eaaa12 2019-08-03 stsp if (diff_staged)
4575 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4576 98eaaa12 2019-08-03 stsp "objects in repository");
4577 15a94983 2018-12-23 stsp id_str1 = argv[0];
4578 15a94983 2018-12-23 stsp id_str2 = argv[1];
4579 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4580 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4581 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4582 30db809c 2019-06-05 stsp goto done;
4583 1a1242a9 2021-04-01 kn repo_path = strdup(worktree ?
4584 1a1242a9 2021-04-01 kn got_worktree_get_repo_path(worktree) : cwd);
4585 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4586 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4591 b00d56cd 2018-04-01 stsp usage_diff();
4593 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4594 76089277 2018-04-01 stsp free(repo_path);
4595 b00d56cd 2018-04-01 stsp if (error != NULL)
4596 b00d56cd 2018-04-01 stsp goto done;
4598 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4599 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4600 c02c541e 2019-03-29 stsp if (error)
4601 c02c541e 2019-03-29 stsp goto done;
4603 30db809c 2019-06-05 stsp if (argc <= 1) {
4604 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4605 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4606 b72f483a 2019-02-05 stsp char *id_str;
4608 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4610 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4611 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4612 b72f483a 2019-02-05 stsp if (error)
4613 b72f483a 2019-02-05 stsp goto done;
4614 b72f483a 2019-02-05 stsp arg.repo = repo;
4615 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4616 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4617 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4618 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4619 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4620 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4621 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4623 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4624 72ea6654 2019-07-27 stsp if (error)
4625 72ea6654 2019-07-27 stsp goto done;
4627 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4628 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4629 b72f483a 2019-02-05 stsp free(id_str);
4630 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4631 b72f483a 2019-02-05 stsp goto done;
4634 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4635 84de9106 2020-12-26 stsp if (error)
4636 84de9106 2020-12-26 stsp return error;
4638 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4639 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4640 0adb7196 2019-08-11 stsp if (error)
4641 0adb7196 2019-08-11 stsp goto done;
4643 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4644 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4645 0adb7196 2019-08-11 stsp if (error)
4646 0adb7196 2019-08-11 stsp goto done;
4648 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4649 15a94983 2018-12-23 stsp if (error)
4650 15a94983 2018-12-23 stsp goto done;
4652 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4653 15a94983 2018-12-23 stsp if (error)
4654 15a94983 2018-12-23 stsp goto done;
4656 15a94983 2018-12-23 stsp if (type1 != type2) {
4657 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4658 b00d56cd 2018-04-01 stsp goto done;
4661 15a94983 2018-12-23 stsp switch (type1) {
4662 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4663 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4664 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4665 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4667 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4668 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4669 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4670 64453f7e 2020-11-21 stsp repo, stdout);
4672 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4673 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4674 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4675 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4679 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4682 5e70831e 2019-05-28 stsp free(label1);
4683 5e70831e 2019-05-28 stsp free(label2);
4684 15a94983 2018-12-23 stsp free(id1);
4685 15a94983 2018-12-23 stsp free(id2);
4686 927df6b7 2019-02-10 stsp free(path);
4687 b72f483a 2019-02-05 stsp if (worktree)
4688 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4689 ad242220 2018-09-08 stsp if (repo) {
4690 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4691 ad242220 2018-09-08 stsp if (error == NULL)
4692 1d0f4054 2021-06-17 stsp error = close_err;
4694 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4695 b00d56cd 2018-04-01 stsp return error;
4698 404c43c4 2018-06-21 stsp __dead static void
4699 404c43c4 2018-06-21 stsp usage_blame(void)
4701 9270e621 2019-02-05 stsp fprintf(stderr,
4702 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4703 404c43c4 2018-06-21 stsp getprogname());
4707 28315671 2019-08-14 stsp struct blame_line {
4708 28315671 2019-08-14 stsp int annotated;
4709 28315671 2019-08-14 stsp char *id_str;
4710 82f6abb8 2019-08-14 stsp char *committer;
4711 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4714 28315671 2019-08-14 stsp struct blame_cb_args {
4715 28315671 2019-08-14 stsp struct blame_line *lines;
4716 28315671 2019-08-14 stsp int nlines;
4717 7ef28ff8 2019-08-14 stsp int nlines_prec;
4718 28315671 2019-08-14 stsp int lineno_cur;
4719 28315671 2019-08-14 stsp off_t *line_offsets;
4721 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4724 404c43c4 2018-06-21 stsp static const struct got_error *
4725 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4727 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4728 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4729 28315671 2019-08-14 stsp struct blame_line *bline;
4730 82f6abb8 2019-08-14 stsp char *line = NULL;
4731 28315671 2019-08-14 stsp size_t linesize = 0;
4732 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4733 28315671 2019-08-14 stsp off_t offset;
4734 bcb49d15 2019-08-14 stsp struct tm tm;
4735 bcb49d15 2019-08-14 stsp time_t committer_time;
4737 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4738 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4739 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4741 28315671 2019-08-14 stsp if (sigint_received)
4742 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4744 2839f8b9 2019-08-18 stsp if (lineno == -1)
4745 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4747 28315671 2019-08-14 stsp /* Annotate this line. */
4748 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4749 28315671 2019-08-14 stsp if (bline->annotated)
4750 28315671 2019-08-14 stsp return NULL;
4751 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4753 28315671 2019-08-14 stsp return err;
4755 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4757 82f6abb8 2019-08-14 stsp goto done;
4759 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4760 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4761 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4762 bcb49d15 2019-08-14 stsp goto done;
4765 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4766 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
4767 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
4768 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4769 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4770 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4771 82f6abb8 2019-08-14 stsp goto done;
4773 28315671 2019-08-14 stsp bline->annotated = 1;
4775 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4776 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4777 28315671 2019-08-14 stsp if (!bline->annotated)
4778 82f6abb8 2019-08-14 stsp goto done;
4780 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4781 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4782 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4783 82f6abb8 2019-08-14 stsp goto done;
4786 28315671 2019-08-14 stsp while (bline->annotated) {
4787 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4788 82f6abb8 2019-08-14 stsp size_t len;
4790 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4791 28315671 2019-08-14 stsp if (ferror(a->f))
4792 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4796 82f6abb8 2019-08-14 stsp committer = bline->committer;
4797 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4798 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4799 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4800 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4802 82f6abb8 2019-08-14 stsp *at = '\0';
4803 82f6abb8 2019-08-14 stsp len = strlen(committer);
4804 82f6abb8 2019-08-14 stsp if (len >= 9)
4805 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4807 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4809 28315671 2019-08-14 stsp *nl = '\0';
4810 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4811 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4813 28315671 2019-08-14 stsp a->lineno_cur++;
4814 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4817 82f6abb8 2019-08-14 stsp if (commit)
4818 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4819 28315671 2019-08-14 stsp free(line);
4820 28315671 2019-08-14 stsp return err;
4823 28315671 2019-08-14 stsp static const struct got_error *
4824 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4826 404c43c4 2018-06-21 stsp const struct got_error *error;
4827 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4828 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4829 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4830 0587e10c 2020-07-23 stsp char *link_target = NULL;
4831 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4832 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4833 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4834 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4835 28315671 2019-08-14 stsp struct blame_cb_args bca;
4836 28315671 2019-08-14 stsp int ch, obj_type, i;
4837 be659d10 2020-11-18 stsp off_t filesize;
4839 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4841 404c43c4 2018-06-21 stsp #ifndef PROFILE
4842 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4843 36e2fb66 2019-01-04 stsp NULL) == -1)
4844 404c43c4 2018-06-21 stsp err(1, "pledge");
4847 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4848 404c43c4 2018-06-21 stsp switch (ch) {
4850 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4853 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4854 66bea077 2018-08-02 stsp if (repo_path == NULL)
4855 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4857 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4860 2deda0b9 2019-03-07 stsp usage_blame();
4861 404c43c4 2018-06-21 stsp /* NOTREACHED */
4865 404c43c4 2018-06-21 stsp argc -= optind;
4866 404c43c4 2018-06-21 stsp argv += optind;
4868 a39318fd 2018-08-02 stsp if (argc == 1)
4869 404c43c4 2018-06-21 stsp path = argv[0];
4871 404c43c4 2018-06-21 stsp usage_blame();
4873 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4874 66bea077 2018-08-02 stsp if (cwd == NULL) {
4875 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4876 66bea077 2018-08-02 stsp goto done;
4878 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4879 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4880 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4881 66bea077 2018-08-02 stsp goto done;
4883 0c06baac 2019-02-05 stsp error = NULL;
4884 0c06baac 2019-02-05 stsp if (worktree) {
4885 0c06baac 2019-02-05 stsp repo_path =
4886 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4887 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4888 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4889 1f03b8da 2020-03-20 stsp if (error)
4890 1f03b8da 2020-03-20 stsp goto done;
4893 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4894 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4895 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4896 0c06baac 2019-02-05 stsp goto done;
4901 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4902 c02c541e 2019-03-29 stsp if (error != NULL)
4903 404c43c4 2018-06-21 stsp goto done;
4905 0c06baac 2019-02-05 stsp if (worktree) {
4906 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4909 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4910 01740607 2020-11-04 stsp if (error)
4911 01740607 2020-11-04 stsp goto done;
4912 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4913 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4914 01740607 2020-11-04 stsp p) == -1) {
4915 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4917 0c06baac 2019-02-05 stsp goto done;
4920 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4922 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4923 01740607 2020-11-04 stsp if (error)
4924 01740607 2020-11-04 stsp goto done;
4925 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4927 0c06baac 2019-02-05 stsp if (error)
4928 66bea077 2018-08-02 stsp goto done;
4930 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4931 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4932 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4933 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4934 404c43c4 2018-06-21 stsp if (error != NULL)
4935 66bea077 2018-08-02 stsp goto done;
4936 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4937 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4938 404c43c4 2018-06-21 stsp if (error != NULL)
4939 66bea077 2018-08-02 stsp goto done;
4941 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4942 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4943 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4945 84de9106 2020-12-26 stsp if (error)
4946 84de9106 2020-12-26 stsp goto done;
4947 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4948 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4949 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4950 30837e32 2019-07-25 stsp if (error)
4951 66bea077 2018-08-02 stsp goto done;
4954 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4955 0587e10c 2020-07-23 stsp commit_id, repo);
4956 0587e10c 2020-07-23 stsp if (error)
4957 0587e10c 2020-07-23 stsp goto done;
4959 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4960 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4961 28315671 2019-08-14 stsp if (error)
4962 28315671 2019-08-14 stsp goto done;
4964 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4965 28315671 2019-08-14 stsp if (error)
4966 28315671 2019-08-14 stsp goto done;
4968 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4969 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4970 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4971 28315671 2019-08-14 stsp goto done;
4974 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4975 28315671 2019-08-14 stsp if (error)
4976 28315671 2019-08-14 stsp goto done;
4977 28315671 2019-08-14 stsp bca.f = got_opentemp();
4978 28315671 2019-08-14 stsp if (bca.f == NULL) {
4979 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4980 28315671 2019-08-14 stsp goto done;
4982 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4983 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4984 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4985 28315671 2019-08-14 stsp goto done;
4987 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4988 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4989 b02560ec 2019-08-19 stsp bca.nlines--;
4991 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4992 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4993 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4994 28315671 2019-08-14 stsp goto done;
4996 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4997 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4998 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4999 7ef28ff8 2019-08-14 stsp while (i > 0) {
5001 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
5003 82f6abb8 2019-08-14 stsp bca.repo = repo;
5005 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5006 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
5008 66bea077 2018-08-02 stsp free(in_repo_path);
5009 0587e10c 2020-07-23 stsp free(link_target);
5010 66bea077 2018-08-02 stsp free(repo_path);
5011 66bea077 2018-08-02 stsp free(cwd);
5012 404c43c4 2018-06-21 stsp free(commit_id);
5013 28315671 2019-08-14 stsp free(obj_id);
5015 28315671 2019-08-14 stsp got_object_blob_close(blob);
5016 0c06baac 2019-02-05 stsp if (worktree)
5017 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
5018 ad242220 2018-09-08 stsp if (repo) {
5019 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5020 ad242220 2018-09-08 stsp if (error == NULL)
5021 1d0f4054 2021-06-17 stsp error = close_err;
5023 3affba96 2019-09-22 stsp if (bca.lines) {
5024 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
5025 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
5026 3affba96 2019-09-22 stsp free(bline->id_str);
5027 3affba96 2019-09-22 stsp free(bline->committer);
5029 3affba96 2019-09-22 stsp free(bca.lines);
5031 28315671 2019-08-14 stsp free(bca.line_offsets);
5032 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
5033 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
5034 404c43c4 2018-06-21 stsp return error;
5037 5de5890b 2018-10-18 stsp __dead static void
5038 5de5890b 2018-10-18 stsp usage_tree(void)
5040 c1669e2e 2019-01-09 stsp fprintf(stderr,
5041 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5042 5de5890b 2018-10-18 stsp getprogname());
5046 0d6c6ee3 2020-05-20 stsp static const struct got_error *
5047 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
5048 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
5050 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
5051 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
5052 848d6979 2019-08-12 stsp const char *modestr = "";
5053 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
5054 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
5056 c1669e2e 2019-01-09 stsp path += strlen(root_path);
5057 c1669e2e 2019-01-09 stsp while (path[0] == '/')
5060 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5061 63c5ca5d 2019-08-24 stsp modestr = "$";
5062 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5065 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5067 0d6c6ee3 2020-05-20 stsp return err;
5068 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5069 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5070 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5073 848d6979 2019-08-12 stsp modestr = "@";
5075 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5076 848d6979 2019-08-12 stsp modestr = "/";
5077 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5078 848d6979 2019-08-12 stsp modestr = "*";
5080 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5081 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5082 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
5084 0d6c6ee3 2020-05-20 stsp free(link_target);
5085 0d6c6ee3 2020-05-20 stsp return NULL;
5088 5de5890b 2018-10-18 stsp static const struct got_error *
5089 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
5090 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
5091 c1669e2e 2019-01-09 stsp struct got_repository *repo)
5093 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
5094 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
5095 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
5096 56e0773d 2019-11-28 stsp int nentries, i;
5098 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5100 5de5890b 2018-10-18 stsp goto done;
5102 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5104 5de5890b 2018-10-18 stsp goto done;
5105 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
5106 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
5107 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5108 5de5890b 2018-10-18 stsp char *id = NULL;
5110 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5113 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5114 5de5890b 2018-10-18 stsp if (show_ids) {
5115 5de5890b 2018-10-18 stsp char *id_str;
5116 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5117 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5119 5de5890b 2018-10-18 stsp goto done;
5120 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5121 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5122 5de5890b 2018-10-18 stsp free(id_str);
5123 5de5890b 2018-10-18 stsp goto done;
5125 5de5890b 2018-10-18 stsp free(id_str);
5127 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5130 0d6c6ee3 2020-05-20 stsp goto done;
5132 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5133 c1669e2e 2019-01-09 stsp char *child_path;
5134 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5135 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5136 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5137 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5138 c1669e2e 2019-01-09 stsp goto done;
5140 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5141 c1669e2e 2019-01-09 stsp root_path, repo);
5142 c1669e2e 2019-01-09 stsp free(child_path);
5144 c1669e2e 2019-01-09 stsp goto done;
5149 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5150 5de5890b 2018-10-18 stsp free(tree_id);
5151 5de5890b 2018-10-18 stsp return err;
5154 5de5890b 2018-10-18 stsp static const struct got_error *
5155 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5157 5de5890b 2018-10-18 stsp const struct got_error *error;
5158 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5159 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5160 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5161 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5162 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5163 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5164 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5167 5de5890b 2018-10-18 stsp #ifndef PROFILE
5168 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5169 0f8d269b 2019-01-04 stsp NULL) == -1)
5170 5de5890b 2018-10-18 stsp err(1, "pledge");
5173 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5174 5de5890b 2018-10-18 stsp switch (ch) {
5176 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5179 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5180 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5181 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5183 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5186 5de5890b 2018-10-18 stsp show_ids = 1;
5189 c1669e2e 2019-01-09 stsp recurse = 1;
5192 2deda0b9 2019-03-07 stsp usage_tree();
5193 5de5890b 2018-10-18 stsp /* NOTREACHED */
5197 5de5890b 2018-10-18 stsp argc -= optind;
5198 5de5890b 2018-10-18 stsp argv += optind;
5200 5de5890b 2018-10-18 stsp if (argc == 1)
5201 5de5890b 2018-10-18 stsp path = argv[0];
5202 5de5890b 2018-10-18 stsp else if (argc > 1)
5203 5de5890b 2018-10-18 stsp usage_tree();
5205 7a2c19d6 2019-02-05 stsp path = NULL;
5207 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5208 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5209 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5210 9bf7a39b 2019-02-05 stsp goto done;
5212 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5213 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5214 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5215 7a2c19d6 2019-02-05 stsp goto done;
5217 7a2c19d6 2019-02-05 stsp error = NULL;
5218 7a2c19d6 2019-02-05 stsp if (worktree) {
5219 7a2c19d6 2019-02-05 stsp repo_path =
5220 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5221 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5222 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5223 7a2c19d6 2019-02-05 stsp if (error)
5224 7a2c19d6 2019-02-05 stsp goto done;
5226 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5227 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5228 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5229 7a2c19d6 2019-02-05 stsp goto done;
5234 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5235 5de5890b 2018-10-18 stsp if (error != NULL)
5236 c02c541e 2019-03-29 stsp goto done;
5238 4fedbf4c 2020-11-07 stsp if (worktree) {
5239 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5242 4fedbf4c 2020-11-07 stsp if (path == NULL)
5243 4fedbf4c 2020-11-07 stsp path = "";
5244 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5245 4fedbf4c 2020-11-07 stsp if (error)
5246 4fedbf4c 2020-11-07 stsp goto done;
5247 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5248 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5249 4fedbf4c 2020-11-07 stsp p) == -1) {
5250 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5252 4fedbf4c 2020-11-07 stsp goto done;
5255 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5256 4fedbf4c 2020-11-07 stsp if (error)
5257 4fedbf4c 2020-11-07 stsp goto done;
5259 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5260 4fedbf4c 2020-11-07 stsp if (error)
5261 4fedbf4c 2020-11-07 stsp goto done;
5262 4fedbf4c 2020-11-07 stsp if (path == NULL)
5263 9bf7a39b 2019-02-05 stsp path = "/";
5264 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5265 9bf7a39b 2019-02-05 stsp if (error != NULL)
5266 9bf7a39b 2019-02-05 stsp goto done;
5269 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5270 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5271 4e0a20a4 2020-03-23 tracey if (worktree)
5272 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5274 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5275 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5276 5de5890b 2018-10-18 stsp if (error != NULL)
5277 5de5890b 2018-10-18 stsp goto done;
5278 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5279 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5280 5de5890b 2018-10-18 stsp if (error != NULL)
5281 5de5890b 2018-10-18 stsp goto done;
5283 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5284 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5285 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5287 84de9106 2020-12-26 stsp if (error)
5288 84de9106 2020-12-26 stsp goto done;
5289 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5290 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5291 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5292 30837e32 2019-07-25 stsp if (error)
5293 5de5890b 2018-10-18 stsp goto done;
5296 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5297 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5299 5de5890b 2018-10-18 stsp free(in_repo_path);
5300 5de5890b 2018-10-18 stsp free(repo_path);
5301 5de5890b 2018-10-18 stsp free(cwd);
5302 5de5890b 2018-10-18 stsp free(commit_id);
5303 7a2c19d6 2019-02-05 stsp if (worktree)
5304 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5305 5de5890b 2018-10-18 stsp if (repo) {
5306 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5307 5de5890b 2018-10-18 stsp if (error == NULL)
5308 1d0f4054 2021-06-17 stsp error = close_err;
5310 5de5890b 2018-10-18 stsp return error;
5313 6bad629b 2019-02-04 stsp __dead static void
5314 6bad629b 2019-02-04 stsp usage_status(void)
5316 00357e4d 2021-09-14 tracey fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5317 00357e4d 2021-09-14 tracey "[-S status-codes] [path ...]\n", getprogname());
5321 00357e4d 2021-09-14 tracey struct got_status_arg {
5322 00357e4d 2021-09-14 tracey char *status_codes;
5323 00357e4d 2021-09-14 tracey int suppress;
5326 b72f483a 2019-02-05 stsp static const struct got_error *
5327 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5328 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5329 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5330 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5332 00357e4d 2021-09-14 tracey struct got_status_arg *st = arg;
5334 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5335 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5336 00357e4d 2021-09-14 tracey if (st != NULL && st->status_codes) {
5337 00357e4d 2021-09-14 tracey size_t ncodes = strlen(st->status_codes);
5338 00357e4d 2021-09-14 tracey int i, j = 0;
5340 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5341 00357e4d 2021-09-14 tracey if (st->suppress) {
5342 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5343 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i]) {
5345 00357e4d 2021-09-14 tracey continue;
5347 00357e4d 2021-09-14 tracey } else {
5348 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5349 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i])
5354 00357e4d 2021-09-14 tracey if (st->suppress && j == 0)
5355 00357e4d 2021-09-14 tracey goto print;
5357 081470ac 2020-08-13 stsp if (i == ncodes)
5358 081470ac 2020-08-13 stsp return NULL;
5361 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5362 b72f483a 2019-02-05 stsp return NULL;
5365 6bad629b 2019-02-04 stsp static const struct got_error *
5366 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5368 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5369 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5370 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5371 00357e4d 2021-09-14 tracey struct got_status_arg st;
5372 00357e4d 2021-09-14 tracey char *cwd = NULL;
5373 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5374 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5375 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5377 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5379 00357e4d 2021-09-14 tracey memset(&st, 0, sizeof(st));
5380 00357e4d 2021-09-14 tracey st.status_codes = NULL;
5381 00357e4d 2021-09-14 tracey st.suppress = 0;
5383 00357e4d 2021-09-14 tracey while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5384 6bad629b 2019-02-04 stsp switch (ch) {
5386 f6343036 2021-06-22 stsp no_ignores = 1;
5389 788d4a19 2021-09-14 stsp if (st.status_codes != NULL && st.suppress == 0)
5390 788d4a19 2021-09-14 stsp option_conflict('S', 's');
5391 788d4a19 2021-09-14 stsp st.suppress = 1;
5392 788d4a19 2021-09-14 stsp /* fallthrough */
5394 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5395 081470ac 2020-08-13 stsp switch (optarg[i]) {
5396 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5397 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5398 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5399 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5400 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5401 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5402 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5403 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5404 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5407 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5408 081470ac 2020-08-13 stsp optarg[i]);
5411 788d4a19 2021-09-14 stsp if (ch == 's' && st.suppress)
5412 b043307b 2021-09-14 stsp option_conflict('s', 'S');
5413 00357e4d 2021-09-14 tracey st.status_codes = optarg;
5416 2deda0b9 2019-03-07 stsp usage_status();
5417 6bad629b 2019-02-04 stsp /* NOTREACHED */
5421 6bad629b 2019-02-04 stsp argc -= optind;
5422 6bad629b 2019-02-04 stsp argv += optind;
5424 6bad629b 2019-02-04 stsp #ifndef PROFILE
5425 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5426 6bad629b 2019-02-04 stsp NULL) == -1)
5427 6bad629b 2019-02-04 stsp err(1, "pledge");
5429 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5430 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5431 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5432 927df6b7 2019-02-10 stsp goto done;
5435 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5436 fa51e947 2020-03-27 stsp if (error) {
5437 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5438 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5439 a5edda0a 2019-07-27 stsp goto done;
5442 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5444 6bad629b 2019-02-04 stsp if (error != NULL)
5445 6bad629b 2019-02-04 stsp goto done;
5447 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5448 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5449 087fb88c 2019-08-04 stsp if (error)
5450 087fb88c 2019-08-04 stsp goto done;
5452 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5453 6bad629b 2019-02-04 stsp if (error)
5454 6bad629b 2019-02-04 stsp goto done;
5456 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5457 00357e4d 2021-09-14 tracey print_status, &st, check_cancelled, NULL);
5459 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5460 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5461 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5462 927df6b7 2019-02-10 stsp free(cwd);
5463 d0eebce4 2019-03-11 stsp return error;
5466 d0eebce4 2019-03-11 stsp __dead static void
5467 d0eebce4 2019-03-11 stsp usage_ref(void)
5469 d0eebce4 2019-03-11 stsp fprintf(stderr,
5470 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5471 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5472 d0eebce4 2019-03-11 stsp getprogname());
5476 d0eebce4 2019-03-11 stsp static const struct got_error *
5477 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5479 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5480 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5481 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5483 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5484 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5486 d0eebce4 2019-03-11 stsp return err;
5488 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5489 d0eebce4 2019-03-11 stsp char *refstr;
5490 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5491 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5492 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5493 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5494 d0eebce4 2019-03-11 stsp free(refstr);
5497 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5498 d0eebce4 2019-03-11 stsp return NULL;
5501 d0eebce4 2019-03-11 stsp static const struct got_error *
5502 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5504 161728eb 2021-07-24 stsp const struct got_error *err;
5505 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5507 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5509 d0eebce4 2019-03-11 stsp return err;
5511 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5512 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5513 d0eebce4 2019-03-11 stsp return err;
5516 d0eebce4 2019-03-11 stsp static const struct got_error *
5517 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5519 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5520 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5521 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5524 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5525 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5526 d1644381 2019-07-14 stsp * an unintended typo.
5528 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5529 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5531 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5533 d83d9d5c 2019-05-13 stsp if (err) {
5534 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5536 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5537 d83d9d5c 2019-05-13 stsp return err;
5538 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5540 d83d9d5c 2019-05-13 stsp return err;
5541 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5542 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5544 d83d9d5c 2019-05-13 stsp return err;
5547 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5549 d0eebce4 2019-03-11 stsp goto done;
5551 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5554 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5556 d1c1ae5f 2019-08-12 stsp return err;
5559 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5560 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5562 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5563 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5564 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5567 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5568 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5569 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5571 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5572 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5574 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5576 d1c1ae5f 2019-08-12 stsp return err;
5578 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5580 d1c1ae5f 2019-08-12 stsp goto done;
5582 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5584 d1c1ae5f 2019-08-12 stsp if (target_ref)
5585 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5587 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5588 d0eebce4 2019-03-11 stsp return err;
5591 d0eebce4 2019-03-11 stsp static const struct got_error *
5592 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5594 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5595 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5596 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5597 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5598 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5599 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5600 b2070a3f 2020-03-22 stsp char *refname = NULL;
5602 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5603 d0eebce4 2019-03-11 stsp switch (ch) {
5605 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5608 e31abbf2 2020-03-22 stsp do_delete = 1;
5611 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5612 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5613 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5615 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5618 d0eebce4 2019-03-11 stsp do_list = 1;
5621 e31abbf2 2020-03-22 stsp symref_target = optarg;
5624 d0eebce4 2019-03-11 stsp usage_ref();
5625 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5629 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5630 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5631 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5632 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5633 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5634 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5635 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5636 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5637 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5638 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5639 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5640 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5642 d0eebce4 2019-03-11 stsp argc -= optind;
5643 d0eebce4 2019-03-11 stsp argv += optind;
5645 e31abbf2 2020-03-22 stsp if (do_list) {
5646 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5647 d0eebce4 2019-03-11 stsp usage_ref();
5648 b2070a3f 2020-03-22 stsp if (argc == 1) {
5649 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5650 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5651 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5652 b2070a3f 2020-03-22 stsp goto done;
5656 e31abbf2 2020-03-22 stsp if (argc != 1)
5657 e31abbf2 2020-03-22 stsp usage_ref();
5658 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5659 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5660 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5661 b2070a3f 2020-03-22 stsp goto done;
5665 b2070a3f 2020-03-22 stsp if (refname)
5666 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5668 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5669 e0b57350 2019-03-12 stsp if (do_list) {
5670 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5671 e0b57350 2019-03-12 stsp NULL) == -1)
5672 e0b57350 2019-03-12 stsp err(1, "pledge");
5674 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5675 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5676 e0b57350 2019-03-12 stsp err(1, "pledge");
5679 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5680 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5681 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5682 d0eebce4 2019-03-11 stsp goto done;
5685 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5686 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5687 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5688 d0eebce4 2019-03-11 stsp goto done;
5690 d0eebce4 2019-03-11 stsp error = NULL;
5691 d0eebce4 2019-03-11 stsp if (worktree) {
5692 d0eebce4 2019-03-11 stsp repo_path =
5693 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5694 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5695 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5696 d0eebce4 2019-03-11 stsp if (error)
5697 d0eebce4 2019-03-11 stsp goto done;
5699 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5700 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5701 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5702 d0eebce4 2019-03-11 stsp goto done;
5707 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5708 d0eebce4 2019-03-11 stsp if (error != NULL)
5709 d0eebce4 2019-03-11 stsp goto done;
5711 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5712 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5713 c02c541e 2019-03-29 stsp if (error)
5714 c02c541e 2019-03-29 stsp goto done;
5716 d0eebce4 2019-03-11 stsp if (do_list)
5717 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5718 e31abbf2 2020-03-22 stsp else if (do_delete)
5719 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5720 e31abbf2 2020-03-22 stsp else if (symref_target)
5721 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5723 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5724 e31abbf2 2020-03-22 stsp usage_ref();
5725 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5728 b2070a3f 2020-03-22 stsp free(refname);
5729 1d0f4054 2021-06-17 stsp if (repo) {
5730 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5731 1d0f4054 2021-06-17 stsp if (error == NULL)
5732 1d0f4054 2021-06-17 stsp error = close_err;
5734 4e759de4 2019-06-26 stsp if (worktree)
5735 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5736 4e759de4 2019-06-26 stsp free(cwd);
5737 4e759de4 2019-06-26 stsp free(repo_path);
5738 4e759de4 2019-06-26 stsp return error;
5741 4e759de4 2019-06-26 stsp __dead static void
5742 4e759de4 2019-06-26 stsp usage_branch(void)
5744 4e759de4 2019-06-26 stsp fprintf(stderr,
5745 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5746 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5750 4e759de4 2019-06-26 stsp static const struct got_error *
5751 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5752 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5754 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5755 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5756 4e99b47e 2019-10-04 stsp char *refstr;
5758 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5759 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5760 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5761 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5763 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5765 4e99b47e 2019-10-04 stsp return err;
5766 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5767 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5768 4e99b47e 2019-10-04 stsp marker = "* ";
5770 4e99b47e 2019-10-04 stsp marker = "~ ";
5774 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5775 4e99b47e 2019-10-04 stsp refname += 11;
5776 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5777 4e99b47e 2019-10-04 stsp refname += 18;
5778 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5779 34d4e04c 2021-02-08 stsp refname += 13;
5781 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5782 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5783 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5785 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5786 4e99b47e 2019-10-04 stsp free(refstr);
5787 4e99b47e 2019-10-04 stsp return NULL;
5790 4e99b47e 2019-10-04 stsp static const struct got_error *
5791 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5793 ad89fa31 2019-10-04 stsp const char *refname;
5795 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5796 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5798 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5800 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5801 ad89fa31 2019-10-04 stsp refname += 11;
5802 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5803 ad89fa31 2019-10-04 stsp refname += 18;
5805 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5807 ad89fa31 2019-10-04 stsp return NULL;
5810 ad89fa31 2019-10-04 stsp static const struct got_error *
5811 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5813 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5814 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5815 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5816 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5817 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5819 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5821 4e99b47e 2019-10-04 stsp if (worktree) {
5822 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5823 4e99b47e 2019-10-04 stsp worktree);
5825 4e99b47e 2019-10-04 stsp return err;
5827 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5828 4e99b47e 2019-10-04 stsp worktree);
5830 4e99b47e 2019-10-04 stsp return err;
5832 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5833 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5834 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5836 4e99b47e 2019-10-04 stsp return err;
5837 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5838 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5842 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5843 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5845 4e759de4 2019-06-26 stsp return err;
5847 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5848 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5850 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5852 34d4e04c 2021-02-08 stsp err = got_ref_list(&refs, repo, "refs/remotes",
5853 34d4e04c 2021-02-08 stsp got_ref_cmp_by_name, NULL);
5855 34d4e04c 2021-02-08 stsp return err;
5857 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
5858 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
5860 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
5862 4e759de4 2019-06-26 stsp return NULL;
5865 4e759de4 2019-06-26 stsp static const struct got_error *
5866 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5867 45cd4e47 2019-08-25 stsp const char *branch_name)
5869 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5870 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5871 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
5873 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
5874 2f1457c6 2021-08-27 stsp branch_name += 5;
5875 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
5876 2f1457c6 2021-08-27 stsp branch_name += 6;
5877 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
5878 2f1457c6 2021-08-27 stsp branch_name += 8;
5880 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5881 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5883 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
5884 2f1457c6 2021-08-27 stsp branch_name) == -1) {
5885 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
5886 4e759de4 2019-06-26 stsp goto done;
5889 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
5890 2f1457c6 2021-08-27 stsp if (err) {
5891 2f1457c6 2021-08-27 stsp const struct got_error *err2;
5892 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
5893 2f1457c6 2021-08-27 stsp goto done;
5895 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
5896 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
5897 2f1457c6 2021-08-27 stsp * our error message.
5899 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
5901 2f1457c6 2021-08-27 stsp goto done;
5902 2f1457c6 2021-08-27 stsp err = NULL;
5905 45cd4e47 2019-08-25 stsp if (worktree &&
5906 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5907 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5908 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5909 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5910 45cd4e47 2019-08-25 stsp goto done;
5913 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
5916 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5917 4e759de4 2019-06-26 stsp free(refname);
5918 2f1457c6 2021-08-27 stsp free(remote_refname);
5919 4e759de4 2019-06-26 stsp return err;
5922 4e759de4 2019-06-26 stsp static const struct got_error *
5923 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5924 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5926 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5927 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5928 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5931 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5932 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5933 d3f84d51 2019-07-11 stsp * an unintended typo.
5935 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5936 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5938 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
5939 2f1457c6 2021-08-27 stsp branch_name += 11;
5941 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5942 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5943 62d463ca 2020-10-20 naddy goto done;
5946 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5947 4e759de4 2019-06-26 stsp if (err == NULL) {
5948 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5949 4e759de4 2019-06-26 stsp goto done;
5950 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5951 4e759de4 2019-06-26 stsp goto done;
5953 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5955 4e759de4 2019-06-26 stsp goto done;
5957 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5960 4e759de4 2019-06-26 stsp got_ref_close(ref);
5961 4e759de4 2019-06-26 stsp free(base_refname);
5962 4e759de4 2019-06-26 stsp free(refname);
5963 4e759de4 2019-06-26 stsp return err;
5966 4e759de4 2019-06-26 stsp static const struct got_error *
5967 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5969 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5970 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5971 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5972 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5973 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5974 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5975 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5976 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5977 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5978 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5979 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5981 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5983 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5984 4e759de4 2019-06-26 stsp switch (ch) {
5986 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5989 4e759de4 2019-06-26 stsp delref = optarg;
5992 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5993 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5994 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5996 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5999 4e759de4 2019-06-26 stsp do_list = 1;
6002 da76fce2 2020-02-24 stsp do_update = 0;
6005 4e759de4 2019-06-26 stsp usage_branch();
6006 4e759de4 2019-06-26 stsp /* NOTREACHED */
6010 4e759de4 2019-06-26 stsp if (do_list && delref)
6011 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
6013 4e759de4 2019-06-26 stsp argc -= optind;
6014 4e759de4 2019-06-26 stsp argv += optind;
6016 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
6017 ad89fa31 2019-10-04 stsp do_show = 1;
6019 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
6020 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
6022 4e759de4 2019-06-26 stsp if (do_list || delref) {
6023 4e759de4 2019-06-26 stsp if (argc > 0)
6024 4e759de4 2019-06-26 stsp usage_branch();
6025 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
6026 4e759de4 2019-06-26 stsp usage_branch();
6028 4e759de4 2019-06-26 stsp #ifndef PROFILE
6029 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
6030 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6031 4e759de4 2019-06-26 stsp NULL) == -1)
6032 4e759de4 2019-06-26 stsp err(1, "pledge");
6034 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6035 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
6036 4e759de4 2019-06-26 stsp err(1, "pledge");
6039 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
6040 4e759de4 2019-06-26 stsp if (cwd == NULL) {
6041 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
6042 4e759de4 2019-06-26 stsp goto done;
6045 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6046 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
6047 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6048 4e759de4 2019-06-26 stsp goto done;
6050 4e759de4 2019-06-26 stsp error = NULL;
6051 4e759de4 2019-06-26 stsp if (worktree) {
6052 4e759de4 2019-06-26 stsp repo_path =
6053 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
6054 4e759de4 2019-06-26 stsp if (repo_path == NULL)
6055 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6056 4e759de4 2019-06-26 stsp if (error)
6057 4e759de4 2019-06-26 stsp goto done;
6059 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
6060 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6061 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6062 4e759de4 2019-06-26 stsp goto done;
6067 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6068 4e759de4 2019-06-26 stsp if (error != NULL)
6069 4e759de4 2019-06-26 stsp goto done;
6071 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
6072 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
6073 4e759de4 2019-06-26 stsp if (error)
6074 4e759de4 2019-06-26 stsp goto done;
6076 ad89fa31 2019-10-04 stsp if (do_show)
6077 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
6078 ad89fa31 2019-10-04 stsp else if (do_list)
6079 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
6080 4e759de4 2019-06-26 stsp else if (delref)
6081 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
6083 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6084 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6085 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6087 84de9106 2020-12-26 stsp if (error)
6088 84de9106 2020-12-26 stsp goto done;
6089 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
6090 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
6091 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
6092 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
6093 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6094 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6095 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6096 a74f7e83 2019-11-10 stsp if (error)
6097 a74f7e83 2019-11-10 stsp goto done;
6098 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
6099 da76fce2 2020-02-24 stsp if (error)
6100 da76fce2 2020-02-24 stsp goto done;
6101 da76fce2 2020-02-24 stsp if (worktree && do_update) {
6102 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6103 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
6105 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
6106 da76fce2 2020-02-24 stsp if (error)
6107 da76fce2 2020-02-24 stsp goto done;
6108 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
6109 da76fce2 2020-02-24 stsp worktree);
6110 da76fce2 2020-02-24 stsp if (error)
6111 da76fce2 2020-02-24 stsp goto done;
6112 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6114 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
6115 da76fce2 2020-02-24 stsp goto done;
6117 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
6118 da76fce2 2020-02-24 stsp free(branch_refname);
6119 da76fce2 2020-02-24 stsp if (error)
6120 da76fce2 2020-02-24 stsp goto done;
6121 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
6123 da76fce2 2020-02-24 stsp if (error)
6124 da76fce2 2020-02-24 stsp goto done;
6125 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
6126 da76fce2 2020-02-24 stsp commit_id);
6127 da76fce2 2020-02-24 stsp if (error)
6128 da76fce2 2020-02-24 stsp goto done;
6129 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6130 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
6131 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
6133 da76fce2 2020-02-24 stsp if (error)
6134 da76fce2 2020-02-24 stsp goto done;
6135 4f3c844b 2021-09-14 stsp if (upa.did_something) {
6136 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
6137 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree),
6138 4f3c844b 2021-09-14 stsp commit_id_str);
6140 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6145 da76fce2 2020-02-24 stsp got_ref_close(ref);
6146 1d0f4054 2021-06-17 stsp if (repo) {
6147 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6148 1d0f4054 2021-06-17 stsp if (error == NULL)
6149 1d0f4054 2021-06-17 stsp error = close_err;
6151 d0eebce4 2019-03-11 stsp if (worktree)
6152 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6153 d0eebce4 2019-03-11 stsp free(cwd);
6154 d0eebce4 2019-03-11 stsp free(repo_path);
6155 da76fce2 2020-02-24 stsp free(commit_id);
6156 da76fce2 2020-02-24 stsp free(commit_id_str);
6157 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6158 da76fce2 2020-02-24 stsp free((char *)pe->path);
6159 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6160 d00136be 2019-03-26 stsp return error;
6164 d00136be 2019-03-26 stsp __dead static void
6165 8e7bd50a 2019-08-22 stsp usage_tag(void)
6167 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6168 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6169 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6174 b8bad2ba 2019-08-23 stsp static const struct got_error *
6175 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6177 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6178 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6179 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6180 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6181 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6183 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6184 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6185 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6186 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6188 b8bad2ba 2019-08-23 stsp return err;
6189 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6192 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6195 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6196 b8bad2ba 2019-08-23 stsp free(re_id);
6199 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6200 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6203 b8bad2ba 2019-08-23 stsp while (se) {
6204 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6207 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6208 b8bad2ba 2019-08-23 stsp free(se_id);
6211 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6212 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6214 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6215 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6217 b8bad2ba 2019-08-23 stsp return err;
6218 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6221 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6226 b8bad2ba 2019-08-23 stsp return err;
6230 b8bad2ba 2019-08-23 stsp static const struct got_error *
6231 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6233 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6234 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6235 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6237 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6239 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6241 8e7bd50a 2019-08-22 stsp return err;
6243 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6244 8e7bd50a 2019-08-22 stsp const char *refname;
6245 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6246 8e7bd50a 2019-08-22 stsp char datebuf[26];
6247 d4efa91b 2020-01-14 stsp const char *tagger;
6248 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6249 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6250 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6251 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6253 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6254 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6256 8e7bd50a 2019-08-22 stsp refname += 10;
6257 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6258 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6259 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6262 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6263 8e7bd50a 2019-08-22 stsp free(refstr);
6265 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6268 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6269 d4efa91b 2020-01-14 stsp if (err) {
6270 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6274 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6275 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6276 d4efa91b 2020-01-14 stsp if (err) {
6280 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6281 d4efa91b 2020-01-14 stsp tagger_time =
6282 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6283 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6289 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6290 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6291 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6292 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6296 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6297 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6298 2417344c 2019-08-23 stsp if (datestr)
6299 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6300 d4efa91b 2020-01-14 stsp if (commit)
6301 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6303 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6304 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6305 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6308 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6309 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6312 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6313 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6316 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6317 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6324 8e7bd50a 2019-08-22 stsp free(id_str);
6325 d4efa91b 2020-01-14 stsp if (commit) {
6326 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6329 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6331 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6332 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6333 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6334 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6339 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6341 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6343 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6344 8e7bd50a 2019-08-22 stsp } while (line);
6345 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6348 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6349 8e7bd50a 2019-08-22 stsp return NULL;
6352 8e7bd50a 2019-08-22 stsp static const struct got_error *
6353 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6354 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6356 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6357 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6358 f372d5cd 2019-10-21 stsp char *editor = NULL;
6359 1601cb9f 2020-09-11 naddy int initial_content_len;
6360 8e7bd50a 2019-08-22 stsp int fd = -1;
6362 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6363 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6364 8e7bd50a 2019-08-22 stsp goto done;
6367 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6368 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6369 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6370 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6371 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6372 8e7bd50a 2019-08-22 stsp goto done;
6375 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6377 8e7bd50a 2019-08-22 stsp goto done;
6379 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6380 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6381 97972933 2020-09-11 stsp goto done;
6384 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6386 8e7bd50a 2019-08-22 stsp goto done;
6387 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6388 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6390 8e7bd50a 2019-08-22 stsp free(initial_content);
6391 8e7bd50a 2019-08-22 stsp free(template);
6392 8e7bd50a 2019-08-22 stsp free(editor);
6394 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6395 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6397 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6398 59f86c76 2020-09-11 stsp if (err == NULL)
6399 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6400 59f86c76 2020-09-11 stsp if (err) {
6401 59f86c76 2020-09-11 stsp free(*tagmsg);
6402 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6404 8e7bd50a 2019-08-22 stsp return err;
6407 8e7bd50a 2019-08-22 stsp static const struct got_error *
6408 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6409 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6411 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6412 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6413 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6414 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6415 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6416 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6417 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6418 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6420 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6423 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6424 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6425 8e7bd50a 2019-08-22 stsp * an unintended typo.
6427 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6428 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6430 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6432 8e7bd50a 2019-08-22 stsp return err;
6434 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6436 84de9106 2020-12-26 stsp goto done;
6438 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6439 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6441 8e7bd50a 2019-08-22 stsp goto done;
6443 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6445 8e7bd50a 2019-08-22 stsp goto done;
6447 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6448 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6449 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6450 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6451 62d463ca 2020-10-20 naddy goto done;
6453 8e7bd50a 2019-08-22 stsp tag_name += 10;
6454 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6455 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6456 62d463ca 2020-10-20 naddy goto done;
6459 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6460 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6461 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6462 8e7bd50a 2019-08-22 stsp goto done;
6463 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6464 8e7bd50a 2019-08-22 stsp goto done;
6466 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6467 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6468 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6469 f372d5cd 2019-10-21 stsp if (err) {
6470 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6471 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6472 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6473 8e7bd50a 2019-08-22 stsp goto done;
6477 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6478 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6479 f372d5cd 2019-10-21 stsp if (err) {
6480 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6481 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6482 8e7bd50a 2019-08-22 stsp goto done;
6485 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6486 f372d5cd 2019-10-21 stsp if (err) {
6487 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6488 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6489 8e7bd50a 2019-08-22 stsp goto done;
6492 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6493 f372d5cd 2019-10-21 stsp if (err) {
6494 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6495 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6496 f372d5cd 2019-10-21 stsp goto done;
6499 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6500 f372d5cd 2019-10-21 stsp if (err) {
6501 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6502 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6503 f372d5cd 2019-10-21 stsp goto done;
6505 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6507 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6508 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6509 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6510 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6511 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6512 f372d5cd 2019-10-21 stsp free(tag_id_str);
6514 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6515 8e7bd50a 2019-08-22 stsp free(commit_id);
6516 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6517 8e7bd50a 2019-08-22 stsp free(refname);
6518 8e7bd50a 2019-08-22 stsp free(tagmsg);
6519 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6520 aba9c984 2019-09-08 stsp free(tagger);
6521 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6522 8e7bd50a 2019-08-22 stsp return err;
6525 8e7bd50a 2019-08-22 stsp static const struct got_error *
6526 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6528 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6529 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6530 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6531 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6532 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6533 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6534 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6536 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6537 8e7bd50a 2019-08-22 stsp switch (ch) {
6539 80106605 2020-02-24 stsp commit_id_arg = optarg;
6542 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6545 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6546 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6547 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6549 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6552 8e7bd50a 2019-08-22 stsp do_list = 1;
6555 8e7bd50a 2019-08-22 stsp usage_tag();
6556 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6560 8e7bd50a 2019-08-22 stsp argc -= optind;
6561 8e7bd50a 2019-08-22 stsp argv += optind;
6563 8e7bd50a 2019-08-22 stsp if (do_list) {
6564 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6566 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6567 8e7bd50a 2019-08-22 stsp if (tagmsg)
6568 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6569 8e7bd50a 2019-08-22 stsp if (argc > 0)
6570 8e7bd50a 2019-08-22 stsp usage_tag();
6571 80106605 2020-02-24 stsp } else if (argc != 1)
6572 8e7bd50a 2019-08-22 stsp usage_tag();
6574 a2887370 2019-08-23 stsp tag_name = argv[0];
6576 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6577 8e7bd50a 2019-08-22 stsp if (do_list) {
6578 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6579 8e7bd50a 2019-08-22 stsp NULL) == -1)
6580 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6582 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6583 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6584 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6587 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6588 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6589 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6590 8e7bd50a 2019-08-22 stsp goto done;
6593 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6594 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6595 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6596 8e7bd50a 2019-08-22 stsp goto done;
6598 8e7bd50a 2019-08-22 stsp error = NULL;
6599 8e7bd50a 2019-08-22 stsp if (worktree) {
6600 8e7bd50a 2019-08-22 stsp repo_path =
6601 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6602 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6603 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6604 8e7bd50a 2019-08-22 stsp if (error)
6605 8e7bd50a 2019-08-22 stsp goto done;
6607 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6608 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6609 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6610 8e7bd50a 2019-08-22 stsp goto done;
6615 8e7bd50a 2019-08-22 stsp if (do_list) {
6616 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6617 c9956ddf 2019-09-08 stsp if (error != NULL)
6618 c9956ddf 2019-09-08 stsp goto done;
6619 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6620 8e7bd50a 2019-08-22 stsp if (error)
6621 8e7bd50a 2019-08-22 stsp goto done;
6622 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6624 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6625 c9956ddf 2019-09-08 stsp if (error)
6626 c9956ddf 2019-09-08 stsp goto done;
6627 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6628 c9956ddf 2019-09-08 stsp if (error != NULL)
6629 c9956ddf 2019-09-08 stsp goto done;
6631 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6632 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6633 8e7bd50a 2019-08-22 stsp if (error)
6634 8e7bd50a 2019-08-22 stsp goto done;
6637 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6638 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6639 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6640 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6641 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6642 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6643 8e7bd50a 2019-08-22 stsp if (error)
6644 8e7bd50a 2019-08-22 stsp goto done;
6645 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6646 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6647 8e7bd50a 2019-08-22 stsp if (error)
6648 8e7bd50a 2019-08-22 stsp goto done;
6649 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6650 8e7bd50a 2019-08-22 stsp free(commit_id);
6651 8e7bd50a 2019-08-22 stsp if (error)
6652 8e7bd50a 2019-08-22 stsp goto done;
6655 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6656 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6659 1d0f4054 2021-06-17 stsp if (repo) {
6660 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6661 1d0f4054 2021-06-17 stsp if (error == NULL)
6662 1d0f4054 2021-06-17 stsp error = close_err;
6664 8e7bd50a 2019-08-22 stsp if (worktree)
6665 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6666 8e7bd50a 2019-08-22 stsp free(cwd);
6667 8e7bd50a 2019-08-22 stsp free(repo_path);
6668 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6669 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6670 8e7bd50a 2019-08-22 stsp return error;
6673 8e7bd50a 2019-08-22 stsp __dead static void
6674 d00136be 2019-03-26 stsp usage_add(void)
6676 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6677 022fae89 2019-12-06 tracey getprogname());
6681 d00136be 2019-03-26 stsp static const struct got_error *
6682 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6684 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6686 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6687 4e68cba3 2019-11-23 stsp return NULL;
6690 4e68cba3 2019-11-23 stsp static const struct got_error *
6691 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6693 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6694 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6695 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6696 1dd54920 2019-05-11 stsp char *cwd = NULL;
6697 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6698 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6699 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6701 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6703 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6704 d00136be 2019-03-26 stsp switch (ch) {
6705 022fae89 2019-12-06 tracey case 'I':
6706 022fae89 2019-12-06 tracey no_ignores = 1;
6709 4e68cba3 2019-11-23 stsp can_recurse = 1;
6712 d00136be 2019-03-26 stsp usage_add();
6713 d00136be 2019-03-26 stsp /* NOTREACHED */
6717 d00136be 2019-03-26 stsp argc -= optind;
6718 d00136be 2019-03-26 stsp argv += optind;
6720 43012d58 2019-07-14 stsp #ifndef PROFILE
6721 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6722 43012d58 2019-07-14 stsp NULL) == -1)
6723 43012d58 2019-07-14 stsp err(1, "pledge");
6725 723c305c 2019-05-11 jcs if (argc < 1)
6726 d00136be 2019-03-26 stsp usage_add();
6728 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6729 d00136be 2019-03-26 stsp if (cwd == NULL) {
6730 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6731 d00136be 2019-03-26 stsp goto done;
6734 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6735 fa51e947 2020-03-27 stsp if (error) {
6736 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6737 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6738 d00136be 2019-03-26 stsp goto done;
6741 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6743 031a5338 2019-03-26 stsp if (error != NULL)
6744 031a5338 2019-03-26 stsp goto done;
6746 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6747 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6748 d00136be 2019-03-26 stsp if (error)
6749 d00136be 2019-03-26 stsp goto done;
6751 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6752 6d022e97 2019-08-04 stsp if (error)
6753 022fae89 2019-12-06 tracey goto done;
6755 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6756 4e68cba3 2019-11-23 stsp char *ondisk_path;
6757 4e68cba3 2019-11-23 stsp struct stat sb;
6758 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6759 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6760 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6761 62d463ca 2020-10-20 naddy pe->path) == -1) {
6762 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6763 4e68cba3 2019-11-23 stsp goto done;
6765 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6766 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6767 4e68cba3 2019-11-23 stsp free(ondisk_path);
6770 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6771 4e68cba3 2019-11-23 stsp ondisk_path);
6772 4e68cba3 2019-11-23 stsp free(ondisk_path);
6773 4e68cba3 2019-11-23 stsp goto done;
6775 4e68cba3 2019-11-23 stsp free(ondisk_path);
6776 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6777 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6778 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6779 4e68cba3 2019-11-23 stsp goto done;
6784 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6785 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6787 1d0f4054 2021-06-17 stsp if (repo) {
6788 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6789 1d0f4054 2021-06-17 stsp if (error == NULL)
6790 1d0f4054 2021-06-17 stsp error = close_err;
6792 d00136be 2019-03-26 stsp if (worktree)
6793 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6794 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6795 1dd54920 2019-05-11 stsp free((char *)pe->path);
6796 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6797 2ec1f75b 2019-03-26 stsp free(cwd);
6798 2ec1f75b 2019-03-26 stsp return error;
6801 2ec1f75b 2019-03-26 stsp __dead static void
6802 648e4ef7 2019-07-09 stsp usage_remove(void)
6804 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6805 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6809 2a06fe5f 2019-08-24 stsp static const struct got_error *
6810 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6811 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6813 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6815 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6816 2a06fe5f 2019-08-24 stsp return NULL;
6817 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6818 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6819 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6820 2a06fe5f 2019-08-24 stsp return NULL;
6823 2ec1f75b 2019-03-26 stsp static const struct got_error *
6824 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6826 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6827 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6828 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6829 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6830 17ed4618 2019-06-02 stsp char *cwd = NULL;
6831 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6832 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6833 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6835 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6837 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6838 2ec1f75b 2019-03-26 stsp switch (ch) {
6840 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6842 70e3e7f5 2019-12-13 tracey case 'k':
6843 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6845 f2a9dc41 2019-12-13 tracey case 'R':
6846 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6849 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6850 766841c2 2020-08-13 stsp switch (optarg[i]) {
6851 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6852 766841c2 2020-08-13 stsp delete_local_mods = 1;
6854 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6857 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6858 766841c2 2020-08-13 stsp optarg[i]);
6861 766841c2 2020-08-13 stsp status_codes = optarg;
6864 f2a9dc41 2019-12-13 tracey usage_remove();
6865 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6869 2ec1f75b 2019-03-26 stsp argc -= optind;
6870 2ec1f75b 2019-03-26 stsp argv += optind;
6872 43012d58 2019-07-14 stsp #ifndef PROFILE
6873 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6874 43012d58 2019-07-14 stsp NULL) == -1)
6875 43012d58 2019-07-14 stsp err(1, "pledge");
6877 17ed4618 2019-06-02 stsp if (argc < 1)
6878 648e4ef7 2019-07-09 stsp usage_remove();
6880 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6881 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6882 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6883 2ec1f75b 2019-03-26 stsp goto done;
6885 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6886 fa51e947 2020-03-27 stsp if (error) {
6887 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6888 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6889 2ec1f75b 2019-03-26 stsp goto done;
6892 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6895 2ec1f75b 2019-03-26 stsp goto done;
6897 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6898 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6899 2ec1f75b 2019-03-26 stsp if (error)
6900 2ec1f75b 2019-03-26 stsp goto done;
6902 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6903 6d022e97 2019-08-04 stsp if (error)
6904 6d022e97 2019-08-04 stsp goto done;
6906 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6907 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6908 f2a9dc41 2019-12-13 tracey struct stat sb;
6909 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6910 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6911 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6912 62d463ca 2020-10-20 naddy pe->path) == -1) {
6913 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6914 f2a9dc41 2019-12-13 tracey goto done;
6916 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6917 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6918 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6919 f2a9dc41 2019-12-13 tracey continue;
6921 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6922 f2a9dc41 2019-12-13 tracey ondisk_path);
6923 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6924 f2a9dc41 2019-12-13 tracey goto done;
6926 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6927 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6928 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6929 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6930 f2a9dc41 2019-12-13 tracey goto done;
6935 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6936 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6937 766841c2 2020-08-13 stsp repo, keep_on_disk);
6939 1d0f4054 2021-06-17 stsp if (repo) {
6940 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6941 1d0f4054 2021-06-17 stsp if (error == NULL)
6942 1d0f4054 2021-06-17 stsp error = close_err;
6944 a129376b 2019-03-28 stsp if (worktree)
6945 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6946 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6947 17ed4618 2019-06-02 stsp free((char *)pe->path);
6948 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6949 a129376b 2019-03-28 stsp free(cwd);
6950 a129376b 2019-03-28 stsp return error;
6953 a129376b 2019-03-28 stsp __dead static void
6954 a129376b 2019-03-28 stsp usage_revert(void)
6956 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6957 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6961 1ee397ad 2019-07-12 stsp static const struct got_error *
6962 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6964 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6965 fb9704af 2020-01-27 stsp return NULL;
6967 a129376b 2019-03-28 stsp while (path[0] == '/')
6969 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6970 33aa809d 2019-08-08 stsp return NULL;
6973 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6974 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6975 33aa809d 2019-08-08 stsp const char *action;
6978 33aa809d 2019-08-08 stsp static const struct got_error *
6979 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6980 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6982 33aa809d 2019-08-08 stsp char *line = NULL;
6983 33aa809d 2019-08-08 stsp size_t linesize = 0;
6984 33aa809d 2019-08-08 stsp ssize_t linelen;
6986 33aa809d 2019-08-08 stsp switch (status) {
6987 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6988 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6990 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6991 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6993 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6994 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6995 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6996 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6997 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6998 33aa809d 2019-08-08 stsp printf("%s", line);
6999 33aa809d 2019-08-08 stsp if (ferror(patch_file))
7000 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
7001 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
7002 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7003 33aa809d 2019-08-08 stsp path, n, nchanges, action);
7006 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
7009 33aa809d 2019-08-08 stsp return NULL;
7012 33aa809d 2019-08-08 stsp static const struct got_error *
7013 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7014 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
7016 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
7017 33aa809d 2019-08-08 stsp char *line = NULL;
7018 33aa809d 2019-08-08 stsp size_t linesize = 0;
7019 33aa809d 2019-08-08 stsp ssize_t linelen;
7020 33aa809d 2019-08-08 stsp int resp = ' ';
7021 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
7023 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
7025 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
7027 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7028 33aa809d 2019-08-08 stsp a->action);
7030 33aa809d 2019-08-08 stsp return err;
7031 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
7032 33aa809d 2019-08-08 stsp if (linelen == -1) {
7033 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
7034 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
7035 33aa809d 2019-08-08 stsp return NULL;
7037 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
7039 33aa809d 2019-08-08 stsp *nl = '\0';
7040 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
7041 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7042 33aa809d 2019-08-08 stsp printf("y\n");
7043 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
7044 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7045 33aa809d 2019-08-08 stsp printf("n\n");
7046 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
7047 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
7048 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7049 33aa809d 2019-08-08 stsp printf("q\n");
7051 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
7052 33aa809d 2019-08-08 stsp free(line);
7053 33aa809d 2019-08-08 stsp return NULL;
7056 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
7057 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7058 33aa809d 2019-08-08 stsp a->action);
7060 33aa809d 2019-08-08 stsp return err;
7061 33aa809d 2019-08-08 stsp resp = getchar();
7062 33aa809d 2019-08-08 stsp if (resp == '\n')
7063 33aa809d 2019-08-08 stsp resp = getchar();
7064 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
7065 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
7066 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7067 33aa809d 2019-08-08 stsp resp = ' ';
7069 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
7070 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7071 33aa809d 2019-08-08 stsp resp = ' ';
7075 33aa809d 2019-08-08 stsp if (resp == 'y')
7076 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7077 33aa809d 2019-08-08 stsp else if (resp == 'n')
7078 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7079 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7080 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7082 1ee397ad 2019-07-12 stsp return NULL;
7086 a129376b 2019-03-28 stsp static const struct got_error *
7087 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
7089 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
7090 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
7091 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
7092 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
7093 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
7094 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
7095 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
7096 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
7097 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
7098 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
7100 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
7102 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7103 a129376b 2019-03-28 stsp switch (ch) {
7105 33aa809d 2019-08-08 stsp pflag = 1;
7108 33aa809d 2019-08-08 stsp patch_script_path = optarg;
7111 0f6d7415 2019-08-08 stsp can_recurse = 1;
7114 a129376b 2019-03-28 stsp usage_revert();
7115 a129376b 2019-03-28 stsp /* NOTREACHED */
7119 a129376b 2019-03-28 stsp argc -= optind;
7120 a129376b 2019-03-28 stsp argv += optind;
7122 43012d58 2019-07-14 stsp #ifndef PROFILE
7123 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7124 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7125 43012d58 2019-07-14 stsp err(1, "pledge");
7127 e20a8b6f 2019-06-04 stsp if (argc < 1)
7128 a129376b 2019-03-28 stsp usage_revert();
7129 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
7130 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7132 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
7133 a129376b 2019-03-28 stsp if (cwd == NULL) {
7134 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7135 a129376b 2019-03-28 stsp goto done;
7137 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
7138 fa51e947 2020-03-27 stsp if (error) {
7139 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7140 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
7141 2ec1f75b 2019-03-26 stsp goto done;
7144 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7146 a129376b 2019-03-28 stsp if (error != NULL)
7147 a129376b 2019-03-28 stsp goto done;
7149 33aa809d 2019-08-08 stsp if (patch_script_path) {
7150 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7151 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7152 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7153 33aa809d 2019-08-08 stsp patch_script_path);
7154 33aa809d 2019-08-08 stsp goto done;
7157 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7158 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7159 a129376b 2019-03-28 stsp if (error)
7160 a129376b 2019-03-28 stsp goto done;
7162 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7163 6d022e97 2019-08-04 stsp if (error)
7164 6d022e97 2019-08-04 stsp goto done;
7166 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7167 0f6d7415 2019-08-08 stsp char *ondisk_path;
7168 0f6d7415 2019-08-08 stsp struct stat sb;
7169 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7170 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7171 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7172 62d463ca 2020-10-20 naddy pe->path) == -1) {
7173 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7174 0f6d7415 2019-08-08 stsp goto done;
7176 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7177 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7178 0f6d7415 2019-08-08 stsp free(ondisk_path);
7181 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7182 0f6d7415 2019-08-08 stsp ondisk_path);
7183 0f6d7415 2019-08-08 stsp free(ondisk_path);
7184 0f6d7415 2019-08-08 stsp goto done;
7186 0f6d7415 2019-08-08 stsp free(ondisk_path);
7187 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7188 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7189 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7190 0f6d7415 2019-08-08 stsp goto done;
7195 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7196 33aa809d 2019-08-08 stsp cpa.action = "revert";
7197 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7198 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7200 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7201 33aa809d 2019-08-08 stsp error == NULL)
7202 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7203 1d0f4054 2021-06-17 stsp if (repo) {
7204 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7205 1d0f4054 2021-06-17 stsp if (error == NULL)
7206 1d0f4054 2021-06-17 stsp error = close_err;
7208 2ec1f75b 2019-03-26 stsp if (worktree)
7209 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7210 2ec1f75b 2019-03-26 stsp free(path);
7211 d00136be 2019-03-26 stsp free(cwd);
7212 6bad629b 2019-02-04 stsp return error;
7215 c4296144 2019-05-09 stsp __dead static void
7216 c4296144 2019-05-09 stsp usage_commit(void)
7218 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7219 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7223 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7224 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7225 28cf319f 2021-01-28 stsp const char *prepared_log;
7226 28cf319f 2021-01-28 stsp int non_interactive;
7227 e2ba3d07 2019-05-13 stsp const char *editor;
7228 e0870e44 2019-05-13 stsp const char *worktree_path;
7229 76d98825 2019-06-03 stsp const char *branch_name;
7230 314a6357 2019-05-13 stsp const char *repo_path;
7231 e0870e44 2019-05-13 stsp char *logmsg_path;
7235 28cf319f 2021-01-28 stsp static const struct got_error *
7236 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7238 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7239 28cf319f 2021-01-28 stsp FILE *f = NULL;
7240 28cf319f 2021-01-28 stsp struct stat sb;
7243 28cf319f 2021-01-28 stsp *logmsg = NULL;
7244 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7246 28cf319f 2021-01-28 stsp f = fopen(path, "r");
7247 28cf319f 2021-01-28 stsp if (f == NULL)
7248 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7250 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7251 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7252 28cf319f 2021-01-28 stsp goto done;
7254 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7255 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7256 28cf319f 2021-01-28 stsp goto done;
7259 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7260 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7261 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7262 28cf319f 2021-01-28 stsp goto done;
7265 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7266 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7267 28cf319f 2021-01-28 stsp if (ferror(f))
7268 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7270 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7271 28cf319f 2021-01-28 stsp goto done;
7273 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7275 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7276 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7277 28cf319f 2021-01-28 stsp if (err) {
7278 28cf319f 2021-01-28 stsp free(*logmsg);
7279 28cf319f 2021-01-28 stsp *logmsg = NULL;
7281 28cf319f 2021-01-28 stsp return err;
7285 33ad4cbe 2019-05-12 jcs static const struct got_error *
7286 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7289 76d98825 2019-06-03 stsp char *initial_content = NULL;
7290 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7291 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7292 e0870e44 2019-05-13 stsp char *template = NULL;
7293 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7294 1601cb9f 2020-09-11 naddy int initial_content_len;
7295 97972933 2020-09-11 stsp int fd = -1;
7296 33ad4cbe 2019-05-12 jcs size_t len;
7298 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7299 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7300 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7301 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7302 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7303 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7304 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7305 33ad4cbe 2019-05-12 jcs return NULL;
7306 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7307 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7309 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7310 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7312 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7314 28cf319f 2021-01-28 stsp goto done;
7316 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7317 28cf319f 2021-01-28 stsp char *msg;
7318 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7320 28cf319f 2021-01-28 stsp goto done;
7321 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7322 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7323 28cf319f 2021-01-28 stsp free(msg);
7324 28cf319f 2021-01-28 stsp goto done;
7326 28cf319f 2021-01-28 stsp free(msg);
7329 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7330 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7331 1601cb9f 2020-09-11 naddy a->branch_name);
7332 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7333 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7334 e0870e44 2019-05-13 stsp goto done;
7337 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7338 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7339 97972933 2020-09-11 stsp goto done;
7342 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7343 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7344 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7345 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7346 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7349 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7350 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7352 76d98825 2019-06-03 stsp free(initial_content);
7353 e0870e44 2019-05-13 stsp free(template);
7355 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7356 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7358 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7359 59f86c76 2020-09-11 stsp if (err == NULL)
7360 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7361 59f86c76 2020-09-11 stsp if (err) {
7362 59f86c76 2020-09-11 stsp free(*logmsg);
7363 59f86c76 2020-09-11 stsp *logmsg = NULL;
7365 33ad4cbe 2019-05-12 jcs return err;
7368 c4296144 2019-05-09 stsp static const struct got_error *
7369 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7371 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7372 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7373 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7374 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7375 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7376 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7377 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7378 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7379 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7380 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7381 f259c4c1 2021-09-24 stsp int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7382 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7384 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7385 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7387 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7388 c4296144 2019-05-09 stsp switch (ch) {
7390 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7391 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7392 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7393 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7394 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7398 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7399 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7400 c4296144 2019-05-09 stsp logmsg = optarg;
7403 28cf319f 2021-01-28 stsp non_interactive = 1;
7406 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7409 c4296144 2019-05-09 stsp usage_commit();
7410 c4296144 2019-05-09 stsp /* NOTREACHED */
7414 c4296144 2019-05-09 stsp argc -= optind;
7415 c4296144 2019-05-09 stsp argv += optind;
7417 43012d58 2019-07-14 stsp #ifndef PROFILE
7418 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7419 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7420 43012d58 2019-07-14 stsp err(1, "pledge");
7422 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7423 c4296144 2019-05-09 stsp if (cwd == NULL) {
7424 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7425 c4296144 2019-05-09 stsp goto done;
7427 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7428 fa51e947 2020-03-27 stsp if (error) {
7429 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7430 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7431 7d5807f4 2019-07-11 stsp goto done;
7434 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7435 c4296144 2019-05-09 stsp if (error)
7436 a698f62e 2019-07-25 stsp goto done;
7437 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7438 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7439 c4296144 2019-05-09 stsp goto done;
7442 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7443 916f288c 2019-07-30 stsp worktree);
7444 5c1e53bc 2019-07-28 stsp if (error)
7445 5c1e53bc 2019-07-28 stsp goto done;
7447 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7448 c9956ddf 2019-09-08 stsp if (error)
7449 c9956ddf 2019-09-08 stsp goto done;
7450 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7451 c9956ddf 2019-09-08 stsp gitconfig_path);
7452 c4296144 2019-05-09 stsp if (error != NULL)
7453 c4296144 2019-05-09 stsp goto done;
7455 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7456 f259c4c1 2021-09-24 stsp if (error)
7457 f259c4c1 2021-09-24 stsp goto done;
7458 f259c4c1 2021-09-24 stsp if (merge_in_progress) {
7459 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_MERGE_BUSY);
7460 f259c4c1 2021-09-24 stsp goto done;
7463 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7464 aba9c984 2019-09-08 stsp if (error)
7465 aba9c984 2019-09-08 stsp return error;
7468 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7469 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7471 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7472 314a6357 2019-05-13 stsp error = get_editor(&editor);
7474 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7475 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7476 c4296144 2019-05-09 stsp if (error)
7477 c4296144 2019-05-09 stsp goto done;
7479 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7480 087fb88c 2019-08-04 stsp if (error)
7481 087fb88c 2019-08-04 stsp goto done;
7483 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7484 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7485 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7486 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7487 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7488 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7489 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7490 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7491 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7492 916f288c 2019-07-30 stsp goto done;
7494 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7496 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7497 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7498 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7499 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7500 e0870e44 2019-05-13 stsp if (error) {
7501 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7502 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7503 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7504 c4296144 2019-05-09 stsp goto done;
7507 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7508 c4296144 2019-05-09 stsp if (error)
7509 c4296144 2019-05-09 stsp goto done;
7510 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7512 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7513 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7514 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7515 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7516 7266f21f 2019-10-21 stsp error == NULL)
7517 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7518 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7519 1d0f4054 2021-06-17 stsp if (repo) {
7520 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7521 1d0f4054 2021-06-17 stsp if (error == NULL)
7522 1d0f4054 2021-06-17 stsp error = close_err;
7524 c4296144 2019-05-09 stsp if (worktree)
7525 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7526 c4296144 2019-05-09 stsp free(cwd);
7527 c4296144 2019-05-09 stsp free(id_str);
7528 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7529 e2ba3d07 2019-05-13 stsp free(editor);
7530 aba9c984 2019-09-08 stsp free(author);
7531 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7532 f8a36e22 2021-08-26 stsp return error;
7535 f8a36e22 2021-08-26 stsp __dead static void
7536 f8a36e22 2021-08-26 stsp usage_send(void)
7538 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7539 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7540 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
7544 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
7545 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
7546 f8a36e22 2021-08-26 stsp int verbosity;
7547 f8a36e22 2021-08-26 stsp int last_ncommits;
7548 f8a36e22 2021-08-26 stsp int last_nobj_total;
7549 f8a36e22 2021-08-26 stsp int last_p_deltify;
7550 f8a36e22 2021-08-26 stsp int last_p_written;
7551 f8a36e22 2021-08-26 stsp int last_p_sent;
7552 f8a36e22 2021-08-26 stsp int printed_something;
7553 f8a36e22 2021-08-26 stsp int sent_something;
7554 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
7557 f8a36e22 2021-08-26 stsp static const struct got_error *
7558 f8a36e22 2021-08-26 stsp send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7559 f8a36e22 2021-08-26 stsp int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7560 f8a36e22 2021-08-26 stsp int success)
7562 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
7563 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
7564 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
7565 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
7566 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
7567 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
7569 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7570 f8a36e22 2021-08-26 stsp return NULL;
7572 f8a36e22 2021-08-26 stsp if (refname) {
7573 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7575 f8a36e22 2021-08-26 stsp if (success) {
7576 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7577 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
7578 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7579 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
7580 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
7581 f8a36e22 2021-08-26 stsp status = "deleted";
7582 27b75514 2021-08-28 stsp a->sent_something = 1;
7588 27b75514 2021-08-28 stsp if (a->printed_something)
7589 27b75514 2021-08-28 stsp putchar('\n');
7590 27b75514 2021-08-28 stsp printf("Server has %s %s", status, refname);
7591 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7592 f8a36e22 2021-08-26 stsp return NULL;
7595 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7596 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7597 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7598 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7600 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
7601 f8a36e22 2021-08-26 stsp print_searching = 1;
7602 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
7605 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
7606 f8a36e22 2021-08-26 stsp print_searching = 1;
7607 f8a36e22 2021-08-26 stsp print_total = 1;
7608 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
7611 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7612 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7613 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7614 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7615 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
7618 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
7619 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
7620 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
7621 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
7622 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
7623 f8a36e22 2021-08-26 stsp print_searching = 1;
7624 f8a36e22 2021-08-26 stsp print_total = 1;
7625 f8a36e22 2021-08-26 stsp print_deltify = 1;
7628 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
7629 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
7630 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
7631 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
7632 f8a36e22 2021-08-26 stsp print_searching = 1;
7633 f8a36e22 2021-08-26 stsp print_total = 1;
7634 f8a36e22 2021-08-26 stsp print_deltify = 1;
7635 f8a36e22 2021-08-26 stsp print_written = 1;
7640 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
7641 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
7642 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
7643 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
7644 f8a36e22 2021-08-26 stsp print_searching = 1;
7645 f8a36e22 2021-08-26 stsp print_total = 1;
7646 f8a36e22 2021-08-26 stsp print_deltify = 1;
7647 f8a36e22 2021-08-26 stsp print_written = 1;
7648 f8a36e22 2021-08-26 stsp print_sent = 1;
7650 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7653 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
7654 f8a36e22 2021-08-26 stsp print_sent)
7655 f8a36e22 2021-08-26 stsp printf("\r");
7656 f8a36e22 2021-08-26 stsp if (print_searching)
7657 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
7658 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
7659 f8a36e22 2021-08-26 stsp if (print_total)
7660 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
7661 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
7662 f8a36e22 2021-08-26 stsp if (print_deltify)
7663 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
7664 f8a36e22 2021-08-26 stsp if (print_sent)
7665 f8a36e22 2021-08-26 stsp printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7666 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
7667 f8a36e22 2021-08-26 stsp else if (print_written)
7668 f8a36e22 2021-08-26 stsp printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7669 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
7670 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
7671 f8a36e22 2021-08-26 stsp print_written || print_sent) {
7672 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7673 f8a36e22 2021-08-26 stsp fflush(stdout);
7675 f8a36e22 2021-08-26 stsp return NULL;
7678 f8a36e22 2021-08-26 stsp static const struct got_error *
7679 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7681 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
7682 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
7683 f8a36e22 2021-08-26 stsp const char *remote_name;
7684 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
7685 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
7686 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
7687 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7688 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
7689 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
7690 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7691 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
7692 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
7693 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
7694 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
7695 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
7696 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
7697 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
7698 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7699 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
7700 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
7701 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
7702 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
7703 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
7704 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
7706 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
7707 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
7708 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
7709 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
7710 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
7711 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
7713 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7714 f8a36e22 2021-08-26 stsp switch (ch) {
7716 f8a36e22 2021-08-26 stsp send_all_branches = 1;
7719 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
7720 f8a36e22 2021-08-26 stsp if (error)
7721 f8a36e22 2021-08-26 stsp return error;
7722 f8a36e22 2021-08-26 stsp nbranches++;
7725 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
7726 f8a36e22 2021-08-26 stsp if (error)
7727 f8a36e22 2021-08-26 stsp return error;
7730 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
7733 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
7734 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7735 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
7737 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
7740 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
7741 f8a36e22 2021-08-26 stsp if (error)
7742 f8a36e22 2021-08-26 stsp return error;
7746 f8a36e22 2021-08-26 stsp send_all_tags = 1;
7749 f8a36e22 2021-08-26 stsp if (verbosity < 0)
7750 f8a36e22 2021-08-26 stsp verbosity = 0;
7751 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
7752 f8a36e22 2021-08-26 stsp verbosity++;
7755 f8a36e22 2021-08-26 stsp verbosity = -1;
7758 f8a36e22 2021-08-26 stsp usage_send();
7759 f8a36e22 2021-08-26 stsp /* NOTREACHED */
7762 f8a36e22 2021-08-26 stsp argc -= optind;
7763 f8a36e22 2021-08-26 stsp argv += optind;
7765 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
7766 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
7767 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
7768 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
7771 f8a36e22 2021-08-26 stsp if (argc == 0)
7772 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7773 f8a36e22 2021-08-26 stsp else if (argc == 1)
7774 f8a36e22 2021-08-26 stsp remote_name = argv[0];
7776 f8a36e22 2021-08-26 stsp usage_send();
7778 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
7779 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
7780 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
7781 f8a36e22 2021-08-26 stsp goto done;
7784 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7785 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
7786 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7787 f8a36e22 2021-08-26 stsp goto done;
7789 f8a36e22 2021-08-26 stsp error = NULL;
7790 f8a36e22 2021-08-26 stsp if (worktree) {
7791 f8a36e22 2021-08-26 stsp repo_path =
7792 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
7793 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7794 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7795 f8a36e22 2021-08-26 stsp if (error)
7796 f8a36e22 2021-08-26 stsp goto done;
7798 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
7799 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7800 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7801 f8a36e22 2021-08-26 stsp goto done;
7806 f8a36e22 2021-08-26 stsp error = got_repo_open(&repo, repo_path, NULL);
7807 f8a36e22 2021-08-26 stsp if (error)
7808 f8a36e22 2021-08-26 stsp goto done;
7810 f8a36e22 2021-08-26 stsp if (worktree) {
7811 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
7812 f8a36e22 2021-08-26 stsp if (worktree_conf) {
7813 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7814 f8a36e22 2021-08-26 stsp worktree_conf);
7815 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7816 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7817 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7823 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7824 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
7825 f8a36e22 2021-08-26 stsp if (repo_conf) {
7826 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7827 f8a36e22 2021-08-26 stsp repo_conf);
7828 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7829 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7830 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7836 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7837 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7838 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7839 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7840 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7845 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7846 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7847 f8a36e22 2021-08-26 stsp goto done;
7850 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7851 6480c871 2021-08-30 stsp &repo_name, remote->send_url);
7852 f8a36e22 2021-08-26 stsp if (error)
7853 f8a36e22 2021-08-26 stsp goto done;
7855 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
7856 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7857 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7858 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
7859 f8a36e22 2021-08-26 stsp err(1, "pledge");
7861 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
7862 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
7863 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7864 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7865 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
7866 f8a36e22 2021-08-26 stsp err(1, "pledge");
7868 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
7869 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
7870 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7871 f8a36e22 2021-08-26 stsp goto done;
7873 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7874 f8a36e22 2021-08-26 stsp goto done;
7877 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
7878 d65a88a2 2021-09-05 stsp if (error)
7879 d65a88a2 2021-09-05 stsp goto done;
7881 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7882 f8a36e22 2021-08-26 stsp if (error)
7883 f8a36e22 2021-08-26 stsp goto done;
7885 f8a36e22 2021-08-26 stsp if (send_all_branches) {
7886 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
7887 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7888 f8a36e22 2021-08-26 stsp if (error)
7889 f8a36e22 2021-08-26 stsp goto done;
7890 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
7891 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
7892 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
7893 f8a36e22 2021-08-26 stsp branchname, NULL);
7894 f8a36e22 2021-08-26 stsp if (error)
7895 f8a36e22 2021-08-26 stsp goto done;
7896 f8a36e22 2021-08-26 stsp nbranches++;
7898 eac1df47 2021-09-01 stsp } else if (nbranches == 0) {
7899 eac1df47 2021-09-01 stsp for (i = 0; i < remote->nsend_branches; i++) {
7900 eac1df47 2021-09-01 stsp got_pathlist_append(&branches,
7901 eac1df47 2021-09-01 stsp remote->send_branches[i], NULL);
7905 f8a36e22 2021-08-26 stsp if (send_all_tags) {
7906 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
7907 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7908 f8a36e22 2021-08-26 stsp if (error)
7909 f8a36e22 2021-08-26 stsp goto done;
7910 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
7911 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
7912 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
7913 f8a36e22 2021-08-26 stsp tagname, NULL);
7914 f8a36e22 2021-08-26 stsp if (error)
7915 f8a36e22 2021-08-26 stsp goto done;
7921 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
7922 f8a36e22 2021-08-26 stsp * with 'got send -d'.
7923 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
7925 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
7926 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7928 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
7929 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
7930 f8a36e22 2021-08-26 stsp s = strdup(branchname);
7931 f8a36e22 2021-08-26 stsp if (s == NULL) {
7932 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7933 f8a36e22 2021-08-26 stsp goto done;
7936 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7937 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
7938 f8a36e22 2021-08-26 stsp goto done;
7941 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7942 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
7944 f8a36e22 2021-08-26 stsp if (error)
7945 f8a36e22 2021-08-26 stsp goto done;
7946 f8a36e22 2021-08-26 stsp ndelete_branches++;
7949 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
7950 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
7951 f8a36e22 2021-08-26 stsp if (worktree)
7952 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
7953 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
7955 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7956 f8a36e22 2021-08-26 stsp if (error)
7957 f8a36e22 2021-08-26 stsp goto done;
7958 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
7959 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7960 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
7961 f8a36e22 2021-08-26 stsp if (error)
7962 f8a36e22 2021-08-26 stsp goto done;
7964 f8a36e22 2021-08-26 stsp ref = head_ref;
7965 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
7967 f8a36e22 2021-08-26 stsp if (error)
7968 f8a36e22 2021-08-26 stsp goto done;
7969 f8a36e22 2021-08-26 stsp nbranches++;
7972 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
7973 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7974 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
7976 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7977 f8a36e22 2021-08-26 stsp server_path, verbosity);
7978 f8a36e22 2021-08-26 stsp if (error)
7979 f8a36e22 2021-08-26 stsp goto done;
7981 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
7982 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
7983 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
7984 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
7985 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
7986 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
7987 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7988 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7989 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
7990 f8a36e22 2021-08-26 stsp if (spa.printed_something)
7991 f8a36e22 2021-08-26 stsp putchar('\n');
7992 f8a36e22 2021-08-26 stsp if (error)
7993 f8a36e22 2021-08-26 stsp goto done;
7994 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
7995 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
7997 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
7998 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
7999 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
8000 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8001 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
8003 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8004 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
8005 f8a36e22 2021-08-26 stsp if (repo) {
8006 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
8007 f8a36e22 2021-08-26 stsp if (error == NULL)
8008 f8a36e22 2021-08-26 stsp error = close_err;
8010 f8a36e22 2021-08-26 stsp if (worktree)
8011 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
8013 f8a36e22 2021-08-26 stsp got_ref_close(ref);
8014 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
8015 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
8016 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
8017 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
8018 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
8019 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
8020 f8a36e22 2021-08-26 stsp free((char *)pe->path);
8021 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
8022 f8a36e22 2021-08-26 stsp free(cwd);
8023 f8a36e22 2021-08-26 stsp free(repo_path);
8024 f8a36e22 2021-08-26 stsp free(proto);
8025 f8a36e22 2021-08-26 stsp free(host);
8026 f8a36e22 2021-08-26 stsp free(port);
8027 f8a36e22 2021-08-26 stsp free(server_path);
8028 f8a36e22 2021-08-26 stsp free(repo_name);
8029 234035bc 2019-06-01 stsp return error;
8032 234035bc 2019-06-01 stsp __dead static void
8033 234035bc 2019-06-01 stsp usage_cherrypick(void)
8035 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8039 234035bc 2019-06-01 stsp static const struct got_error *
8040 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
8042 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
8043 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
8044 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
8045 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
8046 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
8047 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
8048 234035bc 2019-06-01 stsp struct got_object_qid *pid;
8050 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8052 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8053 234035bc 2019-06-01 stsp switch (ch) {
8055 234035bc 2019-06-01 stsp usage_cherrypick();
8056 234035bc 2019-06-01 stsp /* NOTREACHED */
8060 234035bc 2019-06-01 stsp argc -= optind;
8061 234035bc 2019-06-01 stsp argv += optind;
8063 43012d58 2019-07-14 stsp #ifndef PROFILE
8064 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8065 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8066 43012d58 2019-07-14 stsp err(1, "pledge");
8068 234035bc 2019-06-01 stsp if (argc != 1)
8069 234035bc 2019-06-01 stsp usage_cherrypick();
8071 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
8072 234035bc 2019-06-01 stsp if (cwd == NULL) {
8073 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
8074 234035bc 2019-06-01 stsp goto done;
8076 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
8077 fa51e947 2020-03-27 stsp if (error) {
8078 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8079 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
8081 234035bc 2019-06-01 stsp goto done;
8084 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8086 234035bc 2019-06-01 stsp if (error != NULL)
8087 234035bc 2019-06-01 stsp goto done;
8089 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8090 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8091 234035bc 2019-06-01 stsp if (error)
8092 234035bc 2019-06-01 stsp goto done;
8094 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8095 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8096 234035bc 2019-06-01 stsp if (error != NULL) {
8097 234035bc 2019-06-01 stsp struct got_reference *ref;
8098 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8099 234035bc 2019-06-01 stsp goto done;
8100 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8101 234035bc 2019-06-01 stsp if (error != NULL)
8102 234035bc 2019-06-01 stsp goto done;
8103 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
8104 234035bc 2019-06-01 stsp got_ref_close(ref);
8105 234035bc 2019-06-01 stsp if (error != NULL)
8106 234035bc 2019-06-01 stsp goto done;
8108 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
8109 234035bc 2019-06-01 stsp if (error)
8110 234035bc 2019-06-01 stsp goto done;
8112 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8113 234035bc 2019-06-01 stsp if (error)
8114 234035bc 2019-06-01 stsp goto done;
8115 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8116 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8117 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8118 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
8120 234035bc 2019-06-01 stsp if (error != NULL)
8121 234035bc 2019-06-01 stsp goto done;
8123 9627c110 2020-04-18 stsp if (upa.did_something)
8124 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
8125 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
8127 234035bc 2019-06-01 stsp if (commit)
8128 234035bc 2019-06-01 stsp got_object_commit_close(commit);
8129 234035bc 2019-06-01 stsp free(commit_id_str);
8130 234035bc 2019-06-01 stsp if (worktree)
8131 234035bc 2019-06-01 stsp got_worktree_close(worktree);
8132 1d0f4054 2021-06-17 stsp if (repo) {
8133 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8134 1d0f4054 2021-06-17 stsp if (error == NULL)
8135 1d0f4054 2021-06-17 stsp error = close_err;
8137 c4296144 2019-05-09 stsp return error;
8140 5ef14e63 2019-06-02 stsp __dead static void
8141 5ef14e63 2019-06-02 stsp usage_backout(void)
8143 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8147 5ef14e63 2019-06-02 stsp static const struct got_error *
8148 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
8150 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
8151 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
8152 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
8153 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
8154 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
8155 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
8156 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
8158 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8160 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8161 5ef14e63 2019-06-02 stsp switch (ch) {
8163 5ef14e63 2019-06-02 stsp usage_backout();
8164 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8168 5ef14e63 2019-06-02 stsp argc -= optind;
8169 5ef14e63 2019-06-02 stsp argv += optind;
8171 43012d58 2019-07-14 stsp #ifndef PROFILE
8172 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8173 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8174 43012d58 2019-07-14 stsp err(1, "pledge");
8176 5ef14e63 2019-06-02 stsp if (argc != 1)
8177 5ef14e63 2019-06-02 stsp usage_backout();
8179 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
8180 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
8181 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
8182 5ef14e63 2019-06-02 stsp goto done;
8184 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
8185 fa51e947 2020-03-27 stsp if (error) {
8186 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8187 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
8188 5ef14e63 2019-06-02 stsp goto done;
8191 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8193 5ef14e63 2019-06-02 stsp if (error != NULL)
8194 5ef14e63 2019-06-02 stsp goto done;
8196 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8197 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8198 5ef14e63 2019-06-02 stsp if (error)
8199 5ef14e63 2019-06-02 stsp goto done;
8201 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8202 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8203 5ef14e63 2019-06-02 stsp if (error != NULL) {
8204 5ef14e63 2019-06-02 stsp struct got_reference *ref;
8205 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8206 5ef14e63 2019-06-02 stsp goto done;
8207 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8208 5ef14e63 2019-06-02 stsp if (error != NULL)
8209 5ef14e63 2019-06-02 stsp goto done;
8210 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
8211 5ef14e63 2019-06-02 stsp got_ref_close(ref);
8212 5ef14e63 2019-06-02 stsp if (error != NULL)
8213 5ef14e63 2019-06-02 stsp goto done;
8215 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
8216 5ef14e63 2019-06-02 stsp if (error)
8217 5ef14e63 2019-06-02 stsp goto done;
8219 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8220 5ef14e63 2019-06-02 stsp if (error)
8221 5ef14e63 2019-06-02 stsp goto done;
8222 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8223 5ef14e63 2019-06-02 stsp if (pid == NULL) {
8224 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
8225 5ef14e63 2019-06-02 stsp goto done;
8228 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8229 f259c4c1 2021-09-24 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id,
8230 f259c4c1 2021-09-24 stsp repo, update_progress, &upa, check_cancelled, NULL);
8231 5ef14e63 2019-06-02 stsp if (error != NULL)
8232 5ef14e63 2019-06-02 stsp goto done;
8234 9627c110 2020-04-18 stsp if (upa.did_something)
8235 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
8236 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
8238 5ef14e63 2019-06-02 stsp if (commit)
8239 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
8240 5ef14e63 2019-06-02 stsp free(commit_id_str);
8241 818c7501 2019-07-11 stsp if (worktree)
8242 818c7501 2019-07-11 stsp got_worktree_close(worktree);
8243 1d0f4054 2021-06-17 stsp if (repo) {
8244 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8245 1d0f4054 2021-06-17 stsp if (error == NULL)
8246 1d0f4054 2021-06-17 stsp error = close_err;
8248 818c7501 2019-07-11 stsp return error;
8251 818c7501 2019-07-11 stsp __dead static void
8252 818c7501 2019-07-11 stsp usage_rebase(void)
8254 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8255 af54c8f8 2019-07-11 stsp getprogname());
8260 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8263 0ebf8283 2019-07-24 stsp size_t len;
8265 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
8266 0ebf8283 2019-07-24 stsp if (len > limit)
8267 0ebf8283 2019-07-24 stsp len = limit;
8268 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
8269 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
8271 0ebf8283 2019-07-24 stsp *nl = '\0';
8274 0ebf8283 2019-07-24 stsp static const struct got_error *
8275 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8277 5943eee2 2019-08-13 stsp const struct got_error *err;
8278 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
8279 5943eee2 2019-08-13 stsp const char *s;
8281 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8283 5943eee2 2019-08-13 stsp return err;
8285 5943eee2 2019-08-13 stsp s = logmsg0;
8286 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8289 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
8290 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
8291 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
8292 5943eee2 2019-08-13 stsp goto done;
8295 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8297 5943eee2 2019-08-13 stsp free(logmsg0);
8298 a0ea4fc0 2020-02-28 stsp return err;
8301 a0ea4fc0 2020-02-28 stsp static const struct got_error *
8302 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8304 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
8305 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
8306 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
8308 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8310 a0ea4fc0 2020-02-28 stsp return err;
8312 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8314 a0ea4fc0 2020-02-28 stsp goto done;
8316 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8318 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8320 a0ea4fc0 2020-02-28 stsp goto done;
8322 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8324 a0ea4fc0 2020-02-28 stsp free(id_str);
8325 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
8326 a0ea4fc0 2020-02-28 stsp free(logmsg);
8327 5943eee2 2019-08-13 stsp return err;
8330 818c7501 2019-07-11 stsp static const struct got_error *
8331 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
8332 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
8334 818c7501 2019-07-11 stsp const struct got_error *err;
8335 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8337 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8339 818c7501 2019-07-11 stsp goto done;
8341 ff0d2220 2019-07-11 stsp if (new_id) {
8342 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
8344 ff0d2220 2019-07-11 stsp goto done;
8347 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
8348 ff0d2220 2019-07-11 stsp if (new_id_str)
8349 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
8351 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8353 0ebf8283 2019-07-24 stsp goto done;
8355 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
8356 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8358 818c7501 2019-07-11 stsp free(old_id_str);
8359 818c7501 2019-07-11 stsp free(new_id_str);
8360 272a1371 2020-02-28 stsp free(logmsg);
8361 818c7501 2019-07-11 stsp return err;
8364 1ee397ad 2019-07-12 stsp static const struct got_error *
8365 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8366 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
8367 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
8368 e600f124 2021-03-21 stsp int create_backup)
8370 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
8371 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
8372 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
8375 818c7501 2019-07-11 stsp static const struct got_error *
8376 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
8377 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8378 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
8379 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8381 ff0d2220 2019-07-11 stsp const struct got_error *error;
8382 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
8383 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
8385 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8386 ff0d2220 2019-07-11 stsp if (error)
8387 ff0d2220 2019-07-11 stsp return error;
8389 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8390 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
8391 ff0d2220 2019-07-11 stsp if (error) {
8392 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8393 ff0d2220 2019-07-11 stsp goto done;
8394 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
8396 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
8397 ff0d2220 2019-07-11 stsp free(new_commit_id);
8400 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8401 ff0d2220 2019-07-11 stsp return error;
8404 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
8405 64c6d990 2019-07-11 stsp const char *path_prefix;
8406 64c6d990 2019-07-11 stsp size_t len;
8407 8ca9bd68 2019-07-25 stsp int errcode;
8410 64c6d990 2019-07-11 stsp static const struct got_error *
8411 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8412 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
8413 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
8414 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
8416 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8418 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8419 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8420 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
8422 64c6d990 2019-07-11 stsp return NULL;
8425 64c6d990 2019-07-11 stsp static const struct got_error *
8426 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
8427 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
8428 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
8430 64c6d990 2019-07-11 stsp const struct got_error *err;
8431 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8432 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
8433 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
8435 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8436 64c6d990 2019-07-11 stsp return NULL;
8438 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8440 64c6d990 2019-07-11 stsp goto done;
8442 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8444 64c6d990 2019-07-11 stsp goto done;
8446 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
8447 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
8449 64c6d990 2019-07-11 stsp goto done;
8451 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
8452 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
8454 64c6d990 2019-07-11 stsp goto done;
8456 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
8457 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
8458 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
8459 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
8460 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
8461 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
8462 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
8464 64c6d990 2019-07-11 stsp if (tree1)
8465 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
8466 64c6d990 2019-07-11 stsp if (tree2)
8467 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
8468 64c6d990 2019-07-11 stsp if (commit)
8469 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
8470 64c6d990 2019-07-11 stsp if (parent_commit)
8471 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
8472 64c6d990 2019-07-11 stsp return err;
8475 ff0d2220 2019-07-11 stsp static const struct got_error *
8476 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
8477 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
8478 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8479 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
8480 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
8482 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
8483 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
8484 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
8485 af61c510 2019-07-19 stsp struct got_object_qid *qid;
8486 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
8488 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8490 af61c510 2019-07-19 stsp return err;
8492 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8493 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8495 af61c510 2019-07-19 stsp goto done;
8496 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8497 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
8498 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
8499 af61c510 2019-07-19 stsp if (err) {
8500 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
8501 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
8502 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
8503 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
8504 af61c510 2019-07-19 stsp "been reached?!?");
8506 ee780d5c 2020-01-04 stsp goto done;
8508 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
8509 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
8511 af61c510 2019-07-19 stsp goto done;
8513 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8515 af61c510 2019-07-19 stsp goto done;
8516 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
8517 af61c510 2019-07-19 stsp commit_id = parent_id;
8521 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8522 af61c510 2019-07-19 stsp return err;
8525 e600f124 2021-03-21 stsp static const struct got_error *
8526 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8528 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8529 e600f124 2021-03-21 stsp time_t committer_time;
8530 e600f124 2021-03-21 stsp struct tm tm;
8531 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
8532 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
8533 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
8535 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
8536 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
8537 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
8538 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8539 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
8541 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
8542 e600f124 2021-03-21 stsp if (author0 == NULL)
8543 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
8544 e600f124 2021-03-21 stsp author = author0;
8545 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
8546 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
8547 e600f124 2021-03-21 stsp author = smallerthan + 1;
8548 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
8550 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8552 e600f124 2021-03-21 stsp goto done;
8553 e600f124 2021-03-21 stsp logmsg = logmsg0;
8554 e600f124 2021-03-21 stsp while (*logmsg == '\n')
8556 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
8557 e600f124 2021-03-21 stsp if (newline)
8558 e600f124 2021-03-21 stsp *newline = '\0';
8560 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
8561 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
8562 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
8564 e600f124 2021-03-21 stsp free(author0);
8565 e600f124 2021-03-21 stsp free(logmsg0);
8566 643b85bc 2021-07-16 stsp return err;
8569 643b85bc 2021-07-16 stsp static const struct got_error *
8570 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8571 643b85bc 2021-07-16 stsp struct got_repository *repo)
8573 643b85bc 2021-07-16 stsp const struct got_error *err;
8574 643b85bc 2021-07-16 stsp char *id_str;
8576 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8578 643b85bc 2021-07-16 stsp return err;
8580 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8582 643b85bc 2021-07-16 stsp goto done;
8584 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8586 643b85bc 2021-07-16 stsp free(id_str);
8587 e600f124 2021-03-21 stsp return err;
8590 e600f124 2021-03-21 stsp static const struct got_error *
8591 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
8592 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8593 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
8594 e600f124 2021-03-21 stsp struct got_repository *repo)
8596 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8597 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
8598 e600f124 2021-03-21 stsp char *refs_str = NULL;
8599 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
8600 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
8601 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
8602 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
8603 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
8604 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
8605 e600f124 2021-03-21 stsp char *custom_refs_str;
8607 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8608 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
8610 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8611 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
8613 e600f124 2021-03-21 stsp goto done;
8615 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8617 e600f124 2021-03-21 stsp goto done;
8619 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8620 e600f124 2021-03-21 stsp if (refs) {
8621 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8623 e600f124 2021-03-21 stsp goto done;
8626 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8628 e600f124 2021-03-21 stsp goto done;
8630 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8632 e600f124 2021-03-21 stsp goto done;
8634 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8635 4e91ef15 2021-09-26 stsp old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
8637 e600f124 2021-03-21 stsp goto done;
8639 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8640 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8641 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
8642 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8643 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
8644 e600f124 2021-03-21 stsp free(refs_str);
8645 e600f124 2021-03-21 stsp refs_str = NULL;
8647 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8649 e600f124 2021-03-21 stsp goto done;
8651 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8653 e600f124 2021-03-21 stsp goto done;
8655 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8657 e600f124 2021-03-21 stsp goto done;
8659 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8660 e600f124 2021-03-21 stsp if (refs) {
8661 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8663 e600f124 2021-03-21 stsp goto done;
8665 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8666 e600f124 2021-03-21 stsp yca_id_str,
8667 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8668 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8671 e600f124 2021-03-21 stsp free(custom_refs_str);
8672 e600f124 2021-03-21 stsp free(new_commit_id);
8673 e600f124 2021-03-21 stsp free(refs_str);
8674 e600f124 2021-03-21 stsp free(yca_id);
8675 e600f124 2021-03-21 stsp free(yca_id_str);
8676 e600f124 2021-03-21 stsp free(yca_brief_str);
8677 e600f124 2021-03-21 stsp if (new_commit)
8678 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8679 e600f124 2021-03-21 stsp if (yca_commit)
8680 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8682 e600f124 2021-03-21 stsp return NULL;
8685 af61c510 2019-07-19 stsp static const struct got_error *
8686 643b85bc 2021-07-16 stsp process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8687 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8689 e600f124 2021-03-21 stsp const struct got_error *err;
8690 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8691 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8692 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8693 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8694 e600f124 2021-03-21 stsp char *branch_name = NULL;
8695 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8696 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8697 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8699 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8700 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8702 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8704 e600f124 2021-03-21 stsp return err;
8706 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8708 e600f124 2021-03-21 stsp goto done;
8710 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8711 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8712 e600f124 2021-03-21 stsp wanted_branch_name += 11;
8715 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8716 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
8718 e600f124 2021-03-21 stsp goto done;
8720 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
8721 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
8722 e600f124 2021-03-21 stsp char *slash;
8724 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
8728 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
8732 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
8733 e600f124 2021-03-21 stsp old_commit_id);
8737 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
8738 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
8739 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
8741 e600f124 2021-03-21 stsp while (refname[0] == '/')
8742 e600f124 2021-03-21 stsp refname++;
8744 e600f124 2021-03-21 stsp branch_name = strdup(refname);
8745 e600f124 2021-03-21 stsp if (branch_name == NULL) {
8746 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
8749 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
8750 e600f124 2021-03-21 stsp if (slash) {
8751 e600f124 2021-03-21 stsp *slash = '\0';
8752 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
8755 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
8756 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
8757 9e822917 2021-03-23 stsp wanted_branch_found = 1;
8758 643b85bc 2021-07-16 stsp if (delete) {
8759 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
8760 643b85bc 2021-07-16 stsp old_commit_id, repo);
8762 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
8763 abc59930 2021-09-05 naddy old_commit_id, old_commit, refs_idmap,
8770 e600f124 2021-03-21 stsp free(old_commit_id);
8771 e600f124 2021-03-21 stsp old_commit_id = NULL;
8772 e600f124 2021-03-21 stsp free(branch_name);
8773 e600f124 2021-03-21 stsp branch_name = NULL;
8774 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8775 e600f124 2021-03-21 stsp old_commit = NULL;
8778 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
8779 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
8780 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8783 e600f124 2021-03-21 stsp if (refs_idmap)
8784 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8785 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8786 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8787 e600f124 2021-03-21 stsp free(old_commit_id);
8788 e600f124 2021-03-21 stsp free(branch_name);
8789 e600f124 2021-03-21 stsp if (old_commit)
8790 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8791 e600f124 2021-03-21 stsp return err;
8794 e600f124 2021-03-21 stsp static const struct got_error *
8795 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8797 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8798 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8799 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8800 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8801 818c7501 2019-07-11 stsp char *cwd = NULL;
8802 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8803 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8804 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8805 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8806 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8807 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8808 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8809 f259c4c1 2021-09-24 stsp int histedit_in_progress = 0, merge_in_progress = 0;
8810 f259c4c1 2021-09-24 stsp int create_backup = 1, list_backups = 0, delete_backups = 0;
8811 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8812 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8813 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8814 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8815 1fa49072 2021-09-28 stsp struct got_update_progress_arg upa;
8817 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
8818 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8819 1fa49072 2021-09-28 stsp memset(&upa, 0, sizeof(upa));
8821 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
8822 818c7501 2019-07-11 stsp switch (ch) {
8824 818c7501 2019-07-11 stsp abort_rebase = 1;
8827 818c7501 2019-07-11 stsp continue_rebase = 1;
8830 e600f124 2021-03-21 stsp list_backups = 1;
8833 643b85bc 2021-07-16 stsp delete_backups = 1;
8836 818c7501 2019-07-11 stsp usage_rebase();
8837 818c7501 2019-07-11 stsp /* NOTREACHED */
8841 818c7501 2019-07-11 stsp argc -= optind;
8842 818c7501 2019-07-11 stsp argv += optind;
8844 43012d58 2019-07-14 stsp #ifndef PROFILE
8845 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8846 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8847 43012d58 2019-07-14 stsp err(1, "pledge");
8849 e600f124 2021-03-21 stsp if (list_backups) {
8850 e600f124 2021-03-21 stsp if (abort_rebase)
8851 e600f124 2021-03-21 stsp option_conflict('l', 'a');
8852 e600f124 2021-03-21 stsp if (continue_rebase)
8853 e600f124 2021-03-21 stsp option_conflict('l', 'c');
8854 643b85bc 2021-07-16 stsp if (delete_backups)
8855 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8856 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
8857 643b85bc 2021-07-16 stsp usage_rebase();
8858 643b85bc 2021-07-16 stsp } else if (delete_backups) {
8859 643b85bc 2021-07-16 stsp if (abort_rebase)
8860 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
8861 643b85bc 2021-07-16 stsp if (continue_rebase)
8862 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
8863 643b85bc 2021-07-16 stsp if (list_backups)
8864 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8865 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
8866 818c7501 2019-07-11 stsp usage_rebase();
8868 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
8869 e600f124 2021-03-21 stsp usage_rebase();
8870 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
8871 e600f124 2021-03-21 stsp if (argc != 0)
8872 e600f124 2021-03-21 stsp usage_rebase();
8873 e600f124 2021-03-21 stsp } else if (argc != 1)
8874 e600f124 2021-03-21 stsp usage_rebase();
8877 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
8878 818c7501 2019-07-11 stsp if (cwd == NULL) {
8879 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
8880 818c7501 2019-07-11 stsp goto done;
8882 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
8883 fa51e947 2020-03-27 stsp if (error) {
8884 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8885 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
8886 e600f124 2021-03-21 stsp goto done;
8888 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8889 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
8890 e600f124 2021-03-21 stsp "rebase", cwd);
8891 e600f124 2021-03-21 stsp goto done;
8895 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
8896 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8897 818c7501 2019-07-11 stsp if (error != NULL)
8898 818c7501 2019-07-11 stsp goto done;
8900 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8901 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
8902 7ef62c4e 2020-02-24 stsp if (error)
8903 7ef62c4e 2020-02-24 stsp goto done;
8905 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8906 643b85bc 2021-07-16 stsp error = process_backup_refs(
8907 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8908 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
8909 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
8912 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8913 7ef62c4e 2020-02-24 stsp worktree);
8914 818c7501 2019-07-11 stsp if (error)
8915 7ef62c4e 2020-02-24 stsp goto done;
8916 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
8917 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8918 f259c4c1 2021-09-24 stsp goto done;
8921 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress,
8922 f259c4c1 2021-09-24 stsp worktree, repo);
8923 f259c4c1 2021-09-24 stsp if (error)
8924 f259c4c1 2021-09-24 stsp goto done;
8925 f259c4c1 2021-09-24 stsp if (merge_in_progress) {
8926 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_MERGE_BUSY);
8927 818c7501 2019-07-11 stsp goto done;
8930 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8931 818c7501 2019-07-11 stsp if (error)
8932 818c7501 2019-07-11 stsp goto done;
8934 f6794adc 2019-07-23 stsp if (abort_rebase) {
8935 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8936 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8937 f6794adc 2019-07-23 stsp goto done;
8939 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8940 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8941 3e3a69f1 2019-07-25 stsp worktree, repo);
8942 818c7501 2019-07-11 stsp if (error)
8943 818c7501 2019-07-11 stsp goto done;
8944 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
8945 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
8946 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
8947 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
8948 818c7501 2019-07-11 stsp if (error)
8949 818c7501 2019-07-11 stsp goto done;
8950 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8951 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
8952 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
8955 818c7501 2019-07-11 stsp if (continue_rebase) {
8956 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8957 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8958 f6794adc 2019-07-23 stsp goto done;
8960 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8961 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8962 3e3a69f1 2019-07-25 stsp worktree, repo);
8963 818c7501 2019-07-11 stsp if (error)
8964 818c7501 2019-07-11 stsp goto done;
8966 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8967 01757395 2019-07-12 stsp resume_commit_id, repo);
8968 818c7501 2019-07-11 stsp if (error)
8969 818c7501 2019-07-11 stsp goto done;
8971 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
8972 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
8973 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
8974 818c7501 2019-07-11 stsp goto done;
8977 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
8978 818c7501 2019-07-11 stsp if (error != NULL)
8979 818c7501 2019-07-11 stsp goto done;
8982 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8983 ff0d2220 2019-07-11 stsp if (error)
8984 ff0d2220 2019-07-11 stsp goto done;
8986 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
8987 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
8989 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
8990 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8991 4e91ef15 2021-09-26 stsp base_commit_id, branch_head_commit_id, 1, repo,
8992 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8993 818c7501 2019-07-11 stsp if (error)
8994 818c7501 2019-07-11 stsp goto done;
8995 818c7501 2019-07-11 stsp if (yca_id == NULL) {
8996 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
8997 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
8998 818c7501 2019-07-11 stsp "with work tree's branch");
8999 818c7501 2019-07-11 stsp goto done;
9002 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
9003 a51a74b3 2019-07-27 stsp if (error) {
9004 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
9005 a51a74b3 2019-07-27 stsp goto done;
9006 a51a74b3 2019-07-27 stsp error = NULL;
9008 df3ed485 2021-01-31 stsp static char msg[128];
9009 df3ed485 2021-01-31 stsp snprintf(msg, sizeof(msg),
9010 df3ed485 2021-01-31 stsp "%s is already based on %s",
9011 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
9012 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
9013 df3ed485 2021-01-31 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
9014 a51a74b3 2019-07-27 stsp goto done;
9016 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
9017 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
9018 818c7501 2019-07-11 stsp if (error)
9019 818c7501 2019-07-11 stsp goto done;
9022 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
9023 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9024 818c7501 2019-07-11 stsp if (error)
9025 818c7501 2019-07-11 stsp goto done;
9027 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9028 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9029 fc66b545 2019-08-12 stsp if (pid == NULL) {
9030 fc66b545 2019-08-12 stsp if (!continue_rebase) {
9031 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
9032 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
9033 fc66b545 2019-08-12 stsp if (error)
9034 fc66b545 2019-08-12 stsp goto done;
9035 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
9036 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
9037 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
9040 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
9041 fc66b545 2019-08-12 stsp goto done;
9043 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
9044 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
9045 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
9046 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9047 818c7501 2019-07-11 stsp commit = NULL;
9048 818c7501 2019-07-11 stsp if (error)
9049 818c7501 2019-07-11 stsp goto done;
9051 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
9052 38b0338b 2019-11-29 stsp if (continue_rebase) {
9053 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
9054 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
9055 e600f124 2021-03-21 stsp create_backup);
9056 38b0338b 2019-11-29 stsp goto done;
9058 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
9059 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
9060 38b0338b 2019-11-29 stsp char *id_str;
9061 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
9062 38b0338b 2019-11-29 stsp new_base_branch);
9063 38b0338b 2019-11-29 stsp if (error)
9064 38b0338b 2019-11-29 stsp goto done;
9065 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
9066 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
9067 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
9068 38b0338b 2019-11-29 stsp free(id_str);
9069 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
9070 38b0338b 2019-11-29 stsp new_head_commit_id);
9071 38b0338b 2019-11-29 stsp if (error)
9072 38b0338b 2019-11-29 stsp goto done;
9073 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
9074 e600f124 2021-03-21 stsp create_backup = 0;
9078 818c7501 2019-07-11 stsp pid = NULL;
9079 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
9081 818c7501 2019-07-11 stsp commit_id = qid->id;
9082 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
9083 818c7501 2019-07-11 stsp pid = qid;
9085 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9086 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
9087 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
9088 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9089 818c7501 2019-07-11 stsp if (error)
9090 818c7501 2019-07-11 stsp goto done;
9092 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
9093 1fa49072 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
9094 1fa49072 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
9095 1fa49072 2021-09-28 stsp if (upa.conflicts > 0) {
9096 1fa49072 2021-09-28 stsp error = show_rebase_merge_conflict(qid->id,
9098 1fa49072 2021-09-28 stsp if (error)
9099 1fa49072 2021-09-28 stsp goto done;
9101 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9105 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
9106 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
9107 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9108 818c7501 2019-07-11 stsp if (error)
9109 818c7501 2019-07-11 stsp goto done;
9112 1fa49072 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
9113 1fa49072 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
9114 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
9115 818c7501 2019-07-11 stsp if (error)
9116 818c7501 2019-07-11 stsp goto done;
9117 1fa49072 2021-09-28 stsp if (upa.conflicts > 0 && upa.missing == 0 &&
9118 1fa49072 2021-09-28 stsp upa.not_deleted == 0 && upa.unversioned == 0) {
9119 1fa49072 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9120 1fa49072 2021-09-28 stsp "conflicts must be resolved before rebasing "
9121 1fa49072 2021-09-28 stsp "can continue");
9122 1fa49072 2021-09-28 stsp } else if (upa.conflicts > 0) {
9123 1fa49072 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9124 1fa49072 2021-09-28 stsp "conflicts must be resolved before rebasing "
9125 1fa49072 2021-09-28 stsp "can continue; changes destined for some "
9126 1fa49072 2021-09-28 stsp "files were not yet merged and should be "
9127 1fa49072 2021-09-28 stsp "merged manually if required before the "
9128 1fa49072 2021-09-28 stsp "rebase operation is continued");
9130 1fa49072 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9131 1fa49072 2021-09-28 stsp "changes destined for some files were not "
9132 1fa49072 2021-09-28 stsp "yet merged and should be merged manually "
9133 1fa49072 2021-09-28 stsp "if required before the rebase operation "
9134 1fa49072 2021-09-28 stsp "is continued");
9137 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
9138 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
9140 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
9141 818c7501 2019-07-11 stsp free(branch_head_commit_id);
9142 818c7501 2019-07-11 stsp free(resume_commit_id);
9143 818c7501 2019-07-11 stsp free(yca_id);
9144 818c7501 2019-07-11 stsp if (commit)
9145 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9146 818c7501 2019-07-11 stsp if (branch)
9147 818c7501 2019-07-11 stsp got_ref_close(branch);
9148 818c7501 2019-07-11 stsp if (new_base_branch)
9149 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
9150 818c7501 2019-07-11 stsp if (tmp_branch)
9151 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
9152 5ef14e63 2019-06-02 stsp if (worktree)
9153 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
9154 1d0f4054 2021-06-17 stsp if (repo) {
9155 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9156 1d0f4054 2021-06-17 stsp if (error == NULL)
9157 1d0f4054 2021-06-17 stsp error = close_err;
9159 5ef14e63 2019-06-02 stsp return error;
9162 0ebf8283 2019-07-24 stsp __dead static void
9163 0ebf8283 2019-07-24 stsp usage_histedit(void)
9165 b93c7142 2021-10-01 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
9166 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9167 643b85bc 2021-07-16 stsp getprogname());
9171 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
9172 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
9173 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
9174 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
9175 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
9177 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
9178 0ebf8283 2019-07-24 stsp unsigned char code;
9179 0ebf8283 2019-07-24 stsp const char *name;
9180 0ebf8283 2019-07-24 stsp const char *desc;
9181 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
9182 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
9183 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9184 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9185 82997472 2020-01-29 stsp "be used" },
9186 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9187 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
9188 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
9191 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
9192 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
9193 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
9194 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9195 0ebf8283 2019-07-24 stsp char *logmsg;
9197 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9199 0ebf8283 2019-07-24 stsp static const struct got_error *
9200 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9201 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9203 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9204 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
9205 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9208 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9210 0ebf8283 2019-07-24 stsp goto done;
9212 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9214 0ebf8283 2019-07-24 stsp goto done;
9216 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
9218 0ebf8283 2019-07-24 stsp goto done;
9220 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9221 0ebf8283 2019-07-24 stsp if (n < 0)
9222 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9224 0ebf8283 2019-07-24 stsp if (commit)
9225 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9226 0ebf8283 2019-07-24 stsp free(id_str);
9227 0ebf8283 2019-07-24 stsp free(logmsg);
9228 0ebf8283 2019-07-24 stsp return err;
9231 0ebf8283 2019-07-24 stsp static const struct got_error *
9232 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
9233 b93c7142 2021-10-01 stsp FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
9234 b93c7142 2021-10-01 stsp struct got_repository *repo)
9236 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9237 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
9238 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
9240 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9241 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9243 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9244 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
9245 b93c7142 2021-10-01 stsp if (edit_only)
9246 b93c7142 2021-10-01 stsp histedit_cmd = "edit";
9247 b93c7142 2021-10-01 stsp else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9248 466785b9 2020-12-10 jrick histedit_cmd = "fold";
9249 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9252 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
9253 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9254 083957f4 2020-02-24 stsp if (n < 0) {
9255 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
9261 0ebf8283 2019-07-24 stsp return err;
9264 0ebf8283 2019-07-24 stsp static const struct got_error *
9265 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
9266 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
9268 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9271 514f2ffe 2020-01-29 stsp char *id_str;
9272 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9274 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
9275 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
9277 514f2ffe 2020-01-29 stsp return err;
9279 514f2ffe 2020-01-29 stsp n = fprintf(f,
9280 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
9281 514f2ffe 2020-01-29 stsp "# commit %s\n"
9282 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
9283 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
9284 514f2ffe 2020-01-29 stsp if (n < 0) {
9285 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9286 514f2ffe 2020-01-29 stsp goto done;
9289 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
9290 514f2ffe 2020-01-29 stsp if (n < 0) {
9291 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9292 514f2ffe 2020-01-29 stsp goto done;
9295 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9296 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9297 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9298 0ebf8283 2019-07-24 stsp cmd->desc);
9299 0ebf8283 2019-07-24 stsp if (n < 0) {
9300 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9305 514f2ffe 2020-01-29 stsp free(id_str);
9306 0ebf8283 2019-07-24 stsp return err;
9309 0ebf8283 2019-07-24 stsp static const struct got_error *
9310 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9312 0ebf8283 2019-07-24 stsp static char msg[42];
9315 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9317 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
9318 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9320 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9323 0ebf8283 2019-07-24 stsp static const struct got_error *
9324 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9325 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
9327 0ebf8283 2019-07-24 stsp const struct got_error *err;
9328 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
9329 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
9331 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9333 0ebf8283 2019-07-24 stsp return err;
9335 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9337 0ebf8283 2019-07-24 stsp goto done;
9339 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9341 5943eee2 2019-08-13 stsp goto done;
9342 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9343 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9344 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
9345 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9348 0ebf8283 2019-07-24 stsp if (folded_commit)
9349 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
9350 0ebf8283 2019-07-24 stsp free(id_str);
9351 5943eee2 2019-08-13 stsp free(folded_logmsg);
9352 0ebf8283 2019-07-24 stsp return err;
9355 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
9356 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
9358 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9360 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
9361 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9362 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
9363 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9364 3f9de99f 2019-07-24 stsp folded = prev;
9365 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
9368 0ebf8283 2019-07-24 stsp return folded;
9371 0ebf8283 2019-07-24 stsp static const struct got_error *
9372 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9373 0ebf8283 2019-07-24 stsp struct got_repository *repo)
9375 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9376 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9377 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9378 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9379 1601cb9f 2020-09-11 naddy int logmsg_len;
9381 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9383 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9385 0ebf8283 2019-07-24 stsp return err;
9387 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
9388 0ebf8283 2019-07-24 stsp if (folded) {
9389 0ebf8283 2019-07-24 stsp while (folded != hle) {
9390 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9391 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9394 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9395 0ebf8283 2019-07-24 stsp logmsg, repo);
9397 0ebf8283 2019-07-24 stsp goto done;
9398 0ebf8283 2019-07-24 stsp free(logmsg);
9399 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9400 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9404 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9406 0ebf8283 2019-07-24 stsp goto done;
9407 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9409 5943eee2 2019-08-13 stsp goto done;
9410 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
9411 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
9412 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
9413 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
9414 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9415 0ebf8283 2019-07-24 stsp goto done;
9417 0ebf8283 2019-07-24 stsp free(logmsg);
9418 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9420 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9422 0ebf8283 2019-07-24 stsp goto done;
9424 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
9425 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
9427 0ebf8283 2019-07-24 stsp goto done;
9429 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9430 0ebf8283 2019-07-24 stsp close(fd);
9432 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9434 0ebf8283 2019-07-24 stsp goto done;
9436 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9437 0d5bb276 2020-12-15 stsp logmsg_len, 0);
9438 0ebf8283 2019-07-24 stsp if (err) {
9439 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9440 0ebf8283 2019-07-24 stsp goto done;
9441 71392a05 2020-12-13 stsp err = NULL;
9442 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
9443 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
9444 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
9447 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9448 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
9449 0ebf8283 2019-07-24 stsp free(logmsg_path);
9450 0ebf8283 2019-07-24 stsp free(logmsg);
9451 5943eee2 2019-08-13 stsp free(orig_logmsg);
9452 0ebf8283 2019-07-24 stsp free(editor);
9453 0ebf8283 2019-07-24 stsp if (commit)
9454 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9455 0ebf8283 2019-07-24 stsp return err;
9458 0ebf8283 2019-07-24 stsp static const struct got_error *
9459 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
9460 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9462 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9463 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
9464 6059809a 2020-12-17 stsp size_t i, size;
9465 0ebf8283 2019-07-24 stsp ssize_t len;
9466 6059809a 2020-12-17 stsp int lineno = 0;
9467 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9468 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
9469 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
9471 0ebf8283 2019-07-24 stsp for (;;) {
9472 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
9473 0ebf8283 2019-07-24 stsp if (len == -1) {
9474 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
9475 0ebf8283 2019-07-24 stsp if (feof(f))
9477 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
9478 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
9483 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9485 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
9486 0ebf8283 2019-07-24 stsp free(line);
9487 0ebf8283 2019-07-24 stsp line = NULL;
9490 0ebf8283 2019-07-24 stsp cmd = NULL;
9491 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9492 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
9493 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9494 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
9495 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
9498 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9503 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9504 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9507 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9509 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
9510 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
9511 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
9514 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9515 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9519 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
9520 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
9521 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9525 0ebf8283 2019-07-24 stsp free(line);
9526 0ebf8283 2019-07-24 stsp line = NULL;
9530 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9532 0ebf8283 2019-07-24 stsp *end = '\0';
9534 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
9535 0ebf8283 2019-07-24 stsp if (err) {
9536 0ebf8283 2019-07-24 stsp /* override error code */
9537 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9541 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
9542 0ebf8283 2019-07-24 stsp if (hle == NULL) {
9543 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
9546 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
9547 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
9548 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
9549 0ebf8283 2019-07-24 stsp commit_id = NULL;
9550 0ebf8283 2019-07-24 stsp free(line);
9551 0ebf8283 2019-07-24 stsp line = NULL;
9552 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9555 0ebf8283 2019-07-24 stsp free(line);
9556 0ebf8283 2019-07-24 stsp free(commit_id);
9557 0ebf8283 2019-07-24 stsp return err;
9560 0ebf8283 2019-07-24 stsp static const struct got_error *
9561 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
9562 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
9564 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
9565 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
9566 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9567 5b87815e 2020-03-05 stsp static char msg[92];
9568 bfce7f83 2019-07-27 stsp char *id_str;
9570 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
9571 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9572 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
9573 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9574 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9576 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9577 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
9578 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9579 5b87815e 2020-03-05 stsp if (hle == hle2)
9581 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9582 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9584 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9586 5b87815e 2020-03-05 stsp return err;
9587 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
9588 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
9589 5b87815e 2020-03-05 stsp free(id_str);
9590 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9594 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9595 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9596 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9599 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9600 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9602 bfce7f83 2019-07-27 stsp return err;
9603 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
9604 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
9605 bfce7f83 2019-07-27 stsp free(id_str);
9606 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9610 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9611 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9612 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9613 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
9615 bfce7f83 2019-07-27 stsp return NULL;
9618 bfce7f83 2019-07-27 stsp static const struct got_error *
9619 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
9620 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
9621 bfce7f83 2019-07-27 stsp struct got_repository *repo)
9623 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9624 0ebf8283 2019-07-24 stsp char *editor;
9625 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9627 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9629 0ebf8283 2019-07-24 stsp return err;
9631 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
9632 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
9633 0ebf8283 2019-07-24 stsp goto done;
9636 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9637 0ebf8283 2019-07-24 stsp if (f == NULL) {
9638 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
9639 0ebf8283 2019-07-24 stsp goto done;
9641 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9643 bfce7f83 2019-07-27 stsp goto done;
9645 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9647 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9648 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9649 0ebf8283 2019-07-24 stsp free(editor);
9650 0ebf8283 2019-07-24 stsp return err;
9653 0ebf8283 2019-07-24 stsp static const struct got_error *
9654 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9655 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
9656 514f2ffe 2020-01-29 stsp struct got_repository *);
9658 0ebf8283 2019-07-24 stsp static const struct got_error *
9659 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
9660 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
9661 b93c7142 2021-10-01 stsp int edit_logmsg_only, int fold_only, int edit_only,
9662 b93c7142 2021-10-01 stsp struct got_repository *repo)
9664 0ebf8283 2019-07-24 stsp const struct got_error *err;
9665 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9666 0ebf8283 2019-07-24 stsp char *path = NULL;
9668 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
9670 0ebf8283 2019-07-24 stsp return err;
9672 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
9674 0ebf8283 2019-07-24 stsp goto done;
9676 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9677 b93c7142 2021-10-01 stsp fold_only, edit_only, repo);
9679 0ebf8283 2019-07-24 stsp goto done;
9681 b93c7142 2021-10-01 stsp if (edit_logmsg_only || fold_only || edit_only) {
9682 083957f4 2020-02-24 stsp rewind(f);
9683 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9685 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
9686 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
9687 0ebf8283 2019-07-24 stsp goto done;
9690 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
9691 083957f4 2020-02-24 stsp if (err) {
9692 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9693 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9694 083957f4 2020-02-24 stsp goto done;
9695 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
9696 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
9700 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9701 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9702 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
9703 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
9704 0ebf8283 2019-07-24 stsp free(path);
9705 0ebf8283 2019-07-24 stsp return err;
9708 0ebf8283 2019-07-24 stsp static const struct got_error *
9709 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
9710 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9712 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9713 0ebf8283 2019-07-24 stsp char *path = NULL;
9714 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9715 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9716 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9718 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
9720 0ebf8283 2019-07-24 stsp return err;
9722 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
9723 0ebf8283 2019-07-24 stsp if (f == NULL) {
9724 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9725 0ebf8283 2019-07-24 stsp goto done;
9727 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9728 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9733 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9734 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
9735 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
9736 0ebf8283 2019-07-24 stsp if (n < 0) {
9737 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9743 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9744 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9745 0ebf8283 2019-07-24 stsp free(path);
9746 0ebf8283 2019-07-24 stsp if (commit)
9747 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9748 0ebf8283 2019-07-24 stsp return err;
9752 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
9754 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9756 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
9757 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
9758 bfce7f83 2019-07-27 stsp free(hle);
9762 0ebf8283 2019-07-24 stsp static const struct got_error *
9763 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
9764 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
9766 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9767 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9769 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9770 0ebf8283 2019-07-24 stsp if (f == NULL) {
9771 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9772 0ebf8283 2019-07-24 stsp goto done;
9775 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9777 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9778 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9779 0ebf8283 2019-07-24 stsp return err;
9782 0ebf8283 2019-07-24 stsp static const struct got_error *
9783 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9784 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
9785 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
9787 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
9788 0ebf8283 2019-07-24 stsp int resp = ' ';
9790 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
9791 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9792 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
9793 0ebf8283 2019-07-24 stsp resp = getchar();
9794 a61a4414 2019-08-07 stsp if (resp == '\n')
9795 a61a4414 2019-08-07 stsp resp = getchar();
9796 426ebf2e 2019-07-25 stsp if (resp == 'c') {
9797 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9798 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
9800 426ebf2e 2019-07-25 stsp if (err) {
9801 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9802 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9804 bfce7f83 2019-07-27 stsp prev_err = err;
9805 426ebf2e 2019-07-25 stsp resp = ' ';
9809 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
9810 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9811 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
9812 b93c7142 2021-10-01 stsp commits, branch_name, 0, 0, 0, repo);
9813 426ebf2e 2019-07-25 stsp if (err) {
9814 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9815 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9817 bfce7f83 2019-07-27 stsp prev_err = err;
9818 426ebf2e 2019-07-25 stsp resp = ' ';
9822 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
9823 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9826 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
9829 0ebf8283 2019-07-24 stsp return err;
9832 0ebf8283 2019-07-24 stsp static const struct got_error *
9833 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
9834 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9835 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
9837 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9838 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9839 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9840 3e3a69f1 2019-07-25 stsp branch, repo);
9843 0ebf8283 2019-07-24 stsp static const struct got_error *
9844 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
9845 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9847 0ebf8283 2019-07-24 stsp const struct got_error *err;
9848 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9850 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
9852 0ebf8283 2019-07-24 stsp goto done;
9854 0ebf8283 2019-07-24 stsp if (new_id) {
9855 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
9857 0ebf8283 2019-07-24 stsp goto done;
9860 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
9861 0ebf8283 2019-07-24 stsp if (new_id_str)
9862 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
9864 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9865 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
9866 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
9867 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9868 0ebf8283 2019-07-24 stsp goto done;
9870 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
9872 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9874 0ebf8283 2019-07-24 stsp goto done;
9877 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
9878 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
9879 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
9880 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
9881 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9883 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
9884 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
9885 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9892 0ebf8283 2019-07-24 stsp free(old_id_str);
9893 0ebf8283 2019-07-24 stsp free(new_id_str);
9894 0ebf8283 2019-07-24 stsp return err;
9897 0ebf8283 2019-07-24 stsp static const struct got_error *
9898 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
9899 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9900 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9901 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
9903 0ebf8283 2019-07-24 stsp const struct got_error *err;
9904 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9905 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
9907 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9908 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
9909 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9911 0ebf8283 2019-07-24 stsp return err;
9914 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9916 0ebf8283 2019-07-24 stsp return err;
9918 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9919 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
9920 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
9921 0ebf8283 2019-07-24 stsp if (err) {
9922 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9923 0ebf8283 2019-07-24 stsp goto done;
9924 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
9926 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
9927 0ebf8283 2019-07-24 stsp free(new_commit_id);
9930 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9931 0ebf8283 2019-07-24 stsp return err;
9934 0ebf8283 2019-07-24 stsp static const struct got_error *
9935 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
9936 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9938 0ebf8283 2019-07-24 stsp const struct got_error *error;
9939 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9941 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9943 0ebf8283 2019-07-24 stsp if (error)
9944 0ebf8283 2019-07-24 stsp return error;
9946 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9947 0ebf8283 2019-07-24 stsp if (error)
9948 0ebf8283 2019-07-24 stsp return error;
9950 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
9951 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9952 0ebf8283 2019-07-24 stsp return error;
9955 ab20a43a 2020-01-29 stsp static const struct got_error *
9956 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
9957 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
9958 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9959 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9961 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
9963 ab20a43a 2020-01-29 stsp switch (status) {
9964 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9965 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9966 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9967 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
9968 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9969 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9974 ab20a43a 2020-01-29 stsp switch (staged_status) {
9975 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9976 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9977 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9978 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9979 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9984 ab20a43a 2020-01-29 stsp return NULL;
9987 0ebf8283 2019-07-24 stsp static const struct got_error *
9988 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
9990 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
9991 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
9992 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9993 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
9994 0ebf8283 2019-07-24 stsp char *cwd = NULL;
9995 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
9996 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
9997 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
9998 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
9999 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
10000 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10001 f259c4c1 2021-09-24 stsp int ch, rebase_in_progress = 0, merge_in_progress = 0;
10002 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10003 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10004 b93c7142 2021-10-01 stsp int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10005 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
10006 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
10007 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
10008 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
10009 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
10010 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
10011 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
10012 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
10014 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
10015 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
10016 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
10017 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10019 b93c7142 2021-10-01 stsp while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
10020 0ebf8283 2019-07-24 stsp switch (ch) {
10021 0ebf8283 2019-07-24 stsp case 'a':
10022 0ebf8283 2019-07-24 stsp abort_edit = 1;
10024 0ebf8283 2019-07-24 stsp case 'c':
10025 0ebf8283 2019-07-24 stsp continue_edit = 1;
10027 b93c7142 2021-10-01 stsp case 'e':
10028 b93c7142 2021-10-01 stsp edit_only = 1;
10030 466785b9 2020-12-10 jrick case 'f':
10031 466785b9 2020-12-10 jrick fold_only = 1;
10033 0ebf8283 2019-07-24 stsp case 'F':
10034 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
10036 083957f4 2020-02-24 stsp case 'm':
10037 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
10039 e600f124 2021-03-21 stsp case 'l':
10040 e600f124 2021-03-21 stsp list_backups = 1;
10042 643b85bc 2021-07-16 stsp case 'X':
10043 643b85bc 2021-07-16 stsp delete_backups = 1;
10046 0ebf8283 2019-07-24 stsp usage_histedit();
10047 0ebf8283 2019-07-24 stsp /* NOTREACHED */
10051 0ebf8283 2019-07-24 stsp argc -= optind;
10052 0ebf8283 2019-07-24 stsp argv += optind;
10054 0ebf8283 2019-07-24 stsp #ifndef PROFILE
10055 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10056 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
10057 0ebf8283 2019-07-24 stsp err(1, "pledge");
10059 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
10060 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
10061 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
10062 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
10063 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
10064 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
10065 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
10066 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
10067 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
10068 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
10069 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
10070 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
10071 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
10072 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
10073 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
10074 b3805337 2020-12-13 stsp option_conflict('F', 'f');
10075 b93c7142 2021-10-01 stsp if (abort_edit && edit_only)
10076 b93c7142 2021-10-01 stsp option_conflict('a', 'e');
10077 b93c7142 2021-10-01 stsp if (continue_edit && edit_only)
10078 b93c7142 2021-10-01 stsp option_conflict('c', 'e');
10079 b93c7142 2021-10-01 stsp if (edit_only && edit_logmsg_only)
10080 b93c7142 2021-10-01 stsp option_conflict('e', 'm');
10081 b93c7142 2021-10-01 stsp if (edit_script_path && edit_only)
10082 b93c7142 2021-10-01 stsp option_conflict('F', 'e');
10083 e600f124 2021-03-21 stsp if (list_backups) {
10084 e600f124 2021-03-21 stsp if (abort_edit)
10085 e600f124 2021-03-21 stsp option_conflict('l', 'a');
10086 e600f124 2021-03-21 stsp if (continue_edit)
10087 e600f124 2021-03-21 stsp option_conflict('l', 'c');
10088 e600f124 2021-03-21 stsp if (edit_script_path)
10089 e600f124 2021-03-21 stsp option_conflict('l', 'F');
10090 e600f124 2021-03-21 stsp if (edit_logmsg_only)
10091 e600f124 2021-03-21 stsp option_conflict('l', 'm');
10092 e600f124 2021-03-21 stsp if (fold_only)
10093 e600f124 2021-03-21 stsp option_conflict('l', 'f');
10094 b93c7142 2021-10-01 stsp if (edit_only)
10095 b93c7142 2021-10-01 stsp option_conflict('l', 'e');
10096 643b85bc 2021-07-16 stsp if (delete_backups)
10097 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
10098 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
10099 e600f124 2021-03-21 stsp usage_histedit();
10100 643b85bc 2021-07-16 stsp } else if (delete_backups) {
10101 643b85bc 2021-07-16 stsp if (abort_edit)
10102 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
10103 643b85bc 2021-07-16 stsp if (continue_edit)
10104 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
10105 643b85bc 2021-07-16 stsp if (edit_script_path)
10106 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
10107 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
10108 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
10109 643b85bc 2021-07-16 stsp if (fold_only)
10110 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
10111 b93c7142 2021-10-01 stsp if (edit_only)
10112 b93c7142 2021-10-01 stsp option_conflict('X', 'e');
10113 643b85bc 2021-07-16 stsp if (list_backups)
10114 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
10115 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
10116 643b85bc 2021-07-16 stsp usage_histedit();
10117 e600f124 2021-03-21 stsp } else if (argc != 0)
10118 0ebf8283 2019-07-24 stsp usage_histedit();
10121 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
10122 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
10123 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
10124 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
10125 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
10126 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
10129 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
10130 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
10131 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
10132 0ebf8283 2019-07-24 stsp goto done;
10134 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
10135 fa51e947 2020-03-27 stsp if (error) {
10136 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10137 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
10138 e600f124 2021-03-21 stsp goto done;
10140 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10141 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
10142 e600f124 2021-03-21 stsp "histedit", cwd);
10143 e600f124 2021-03-21 stsp goto done;
10147 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10148 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
10149 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
10151 e600f124 2021-03-21 stsp if (error != NULL)
10152 e600f124 2021-03-21 stsp goto done;
10153 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10154 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
10155 e600f124 2021-03-21 stsp if (error)
10156 e600f124 2021-03-21 stsp goto done;
10157 643b85bc 2021-07-16 stsp error = process_backup_refs(
10158 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10159 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
10160 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
10163 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10165 0ebf8283 2019-07-24 stsp if (error != NULL)
10166 0ebf8283 2019-07-24 stsp goto done;
10168 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10169 0ebf8283 2019-07-24 stsp if (error)
10170 0ebf8283 2019-07-24 stsp goto done;
10171 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
10172 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
10173 f259c4c1 2021-09-24 stsp goto done;
10176 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10178 f259c4c1 2021-09-24 stsp if (error)
10179 f259c4c1 2021-09-24 stsp goto done;
10180 f259c4c1 2021-09-24 stsp if (merge_in_progress) {
10181 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_MERGE_BUSY);
10182 0ebf8283 2019-07-24 stsp goto done;
10185 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10186 0ebf8283 2019-07-24 stsp if (error)
10187 0ebf8283 2019-07-24 stsp goto done;
10189 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
10190 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10191 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
10192 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
10193 083957f4 2020-02-24 stsp "before the -m option can be used");
10194 083957f4 2020-02-24 stsp goto done;
10196 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
10197 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10198 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
10199 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
10200 466785b9 2020-12-10 jrick "before the -f option can be used");
10201 466785b9 2020-12-10 jrick goto done;
10203 b93c7142 2021-10-01 stsp if (edit_in_progress && edit_only) {
10204 b93c7142 2021-10-01 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10205 b93c7142 2021-10-01 stsp "histedit operation is in progress in this "
10206 b93c7142 2021-10-01 stsp "work tree and must be continued or aborted "
10207 b93c7142 2021-10-01 stsp "before the -e option can be used");
10208 b93c7142 2021-10-01 stsp goto done;
10211 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
10212 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10213 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10214 3e3a69f1 2019-07-25 stsp worktree, repo);
10215 0ebf8283 2019-07-24 stsp if (error)
10216 0ebf8283 2019-07-24 stsp goto done;
10217 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10218 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10219 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
10220 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
10221 0ebf8283 2019-07-24 stsp if (error)
10222 0ebf8283 2019-07-24 stsp goto done;
10223 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
10224 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10225 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10226 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
10227 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
10228 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10229 0ebf8283 2019-07-24 stsp goto done;
10232 0ebf8283 2019-07-24 stsp if (continue_edit) {
10233 0ebf8283 2019-07-24 stsp char *path;
10235 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
10236 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10237 0ebf8283 2019-07-24 stsp goto done;
10240 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
10241 0ebf8283 2019-07-24 stsp if (error)
10242 0ebf8283 2019-07-24 stsp goto done;
10244 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
10245 0ebf8283 2019-07-24 stsp free(path);
10246 0ebf8283 2019-07-24 stsp if (error)
10247 0ebf8283 2019-07-24 stsp goto done;
10249 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10250 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10251 3e3a69f1 2019-07-25 stsp worktree, repo);
10252 0ebf8283 2019-07-24 stsp if (error)
10253 0ebf8283 2019-07-24 stsp goto done;
10255 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10256 0ebf8283 2019-07-24 stsp if (error)
10257 0ebf8283 2019-07-24 stsp goto done;
10259 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10260 0ebf8283 2019-07-24 stsp head_commit_id);
10261 0ebf8283 2019-07-24 stsp if (error)
10262 0ebf8283 2019-07-24 stsp goto done;
10263 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10264 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10265 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10266 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10267 3aac7cf7 2019-07-25 stsp goto done;
10269 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10270 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
10271 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10272 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10273 0ebf8283 2019-07-24 stsp commit = NULL;
10274 0ebf8283 2019-07-24 stsp if (error)
10275 0ebf8283 2019-07-24 stsp goto done;
10277 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
10278 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
10279 0ebf8283 2019-07-24 stsp goto done;
10282 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
10283 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10284 0ebf8283 2019-07-24 stsp if (error != NULL)
10285 0ebf8283 2019-07-24 stsp goto done;
10287 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10288 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10289 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
10290 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
10291 c7d20a3f 2019-07-30 stsp goto done;
10294 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10295 bfce7f83 2019-07-27 stsp got_ref_close(branch);
10296 bfce7f83 2019-07-27 stsp branch = NULL;
10297 0ebf8283 2019-07-24 stsp if (error)
10298 0ebf8283 2019-07-24 stsp goto done;
10300 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10301 0ebf8283 2019-07-24 stsp head_commit_id);
10302 0ebf8283 2019-07-24 stsp if (error)
10303 0ebf8283 2019-07-24 stsp goto done;
10304 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10305 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10306 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10307 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10308 3aac7cf7 2019-07-25 stsp goto done;
10310 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10311 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
10312 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
10313 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10314 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10315 0ebf8283 2019-07-24 stsp commit = NULL;
10316 0ebf8283 2019-07-24 stsp if (error)
10317 0ebf8283 2019-07-24 stsp goto done;
10319 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
10320 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10321 c5996fff 2020-01-29 stsp goto done;
10324 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10325 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
10326 bfce7f83 2019-07-27 stsp if (error)
10327 bfce7f83 2019-07-27 stsp goto done;
10329 0ebf8283 2019-07-24 stsp if (edit_script_path) {
10330 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
10331 0ebf8283 2019-07-24 stsp edit_script_path, repo);
10332 6ba2bea2 2019-07-27 stsp if (error) {
10333 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10334 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10335 9627c110 2020-04-18 stsp update_progress, &upa);
10336 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10337 0ebf8283 2019-07-24 stsp goto done;
10340 514f2ffe 2020-01-29 stsp const char *branch_name;
10341 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
10342 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
10343 514f2ffe 2020-01-29 stsp branch_name += 11;
10344 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
10345 b93c7142 2021-10-01 stsp branch_name, edit_logmsg_only, fold_only,
10346 b93c7142 2021-10-01 stsp edit_only, repo);
10347 6ba2bea2 2019-07-27 stsp if (error) {
10348 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10349 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10350 9627c110 2020-04-18 stsp update_progress, &upa);
10351 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10352 0ebf8283 2019-07-24 stsp goto done;
10357 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10359 6ba2bea2 2019-07-27 stsp if (error) {
10360 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10361 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10362 9627c110 2020-04-18 stsp update_progress, &upa);
10363 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10364 0ebf8283 2019-07-24 stsp goto done;
10369 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
10370 4ec14e60 2019-08-28 hiltjo if (error)
10371 0ebf8283 2019-07-24 stsp goto done;
10373 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10374 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
10375 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
10376 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
10377 0ebf8283 2019-07-24 stsp continue;
10379 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
10380 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10381 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
10382 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
10384 ab20a43a 2020-01-29 stsp if (error)
10385 ab20a43a 2020-01-29 stsp goto done;
10387 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10388 ab20a43a 2020-01-29 stsp int have_changes = 0;
10390 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
10391 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
10392 ab20a43a 2020-01-29 stsp if (error)
10393 ab20a43a 2020-01-29 stsp goto done;
10394 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
10395 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
10396 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
10397 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
10398 ab20a43a 2020-01-29 stsp if (error) {
10399 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
10400 ab20a43a 2020-01-29 stsp goto done;
10401 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
10402 ab20a43a 2020-01-29 stsp goto done;
10404 ab20a43a 2020-01-29 stsp if (have_changes) {
10405 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
10406 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
10407 ab20a43a 2020-01-29 stsp if (error)
10408 ab20a43a 2020-01-29 stsp goto done;
10410 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
10411 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
10412 ab20a43a 2020-01-29 stsp if (error)
10413 ab20a43a 2020-01-29 stsp goto done;
10414 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
10415 ab20a43a 2020-01-29 stsp hle, NULL);
10416 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
10417 ab20a43a 2020-01-29 stsp commit = NULL;
10418 ab20a43a 2020-01-29 stsp if (error)
10419 ab20a43a 2020-01-29 stsp goto done;
10422 0ebf8283 2019-07-24 stsp continue;
10425 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10426 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10427 0ebf8283 2019-07-24 stsp if (error)
10428 0ebf8283 2019-07-24 stsp goto done;
10429 0ebf8283 2019-07-24 stsp continue;
10432 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10433 0ebf8283 2019-07-24 stsp hle->commit_id);
10434 0ebf8283 2019-07-24 stsp if (error)
10435 0ebf8283 2019-07-24 stsp goto done;
10436 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10437 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10439 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
10440 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
10441 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10442 0ebf8283 2019-07-24 stsp if (error)
10443 0ebf8283 2019-07-24 stsp goto done;
10444 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10445 0ebf8283 2019-07-24 stsp commit = NULL;
10447 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10448 cd33da48 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
10449 cd33da48 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
10450 cd33da48 2021-09-28 stsp if (upa.conflicts > 0) {
10451 cd33da48 2021-09-28 stsp error = show_rebase_merge_conflict(
10452 cd33da48 2021-09-28 stsp hle->commit_id, repo);
10453 cd33da48 2021-09-28 stsp if (error)
10454 cd33da48 2021-09-28 stsp goto done;
10456 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10460 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10461 0ebf8283 2019-07-24 stsp char *id_str;
10462 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
10463 0ebf8283 2019-07-24 stsp if (error)
10464 0ebf8283 2019-07-24 stsp goto done;
10465 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
10467 0ebf8283 2019-07-24 stsp free(id_str);
10468 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10469 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
10470 3e3a69f1 2019-07-25 stsp fileindex);
10471 0ebf8283 2019-07-24 stsp goto done;
10474 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10475 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10476 0ebf8283 2019-07-24 stsp if (error)
10477 0ebf8283 2019-07-24 stsp goto done;
10478 0ebf8283 2019-07-24 stsp continue;
10481 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
10482 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
10483 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10484 0ebf8283 2019-07-24 stsp if (error)
10485 0ebf8283 2019-07-24 stsp goto done;
10488 cd33da48 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
10489 cd33da48 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
10490 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
10491 0ebf8283 2019-07-24 stsp if (error)
10492 0ebf8283 2019-07-24 stsp goto done;
10493 cd33da48 2021-09-28 stsp if (upa.conflicts > 0 && upa.missing == 0 &&
10494 cd33da48 2021-09-28 stsp upa.not_deleted == 0 && upa.unversioned == 0) {
10495 cd33da48 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10496 cd33da48 2021-09-28 stsp "conflicts must be resolved before histedit "
10497 cd33da48 2021-09-28 stsp "can continue");
10498 cd33da48 2021-09-28 stsp } else if (upa.conflicts > 0) {
10499 cd33da48 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10500 cd33da48 2021-09-28 stsp "conflicts must be resolved before histedit "
10501 cd33da48 2021-09-28 stsp "can continue; changes destined for some "
10502 cd33da48 2021-09-28 stsp "files were not yet merged and should be "
10503 cd33da48 2021-09-28 stsp "merged manually if required before the "
10504 cd33da48 2021-09-28 stsp "histedit operation is continued");
10506 cd33da48 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10507 cd33da48 2021-09-28 stsp "changes destined for some files were not "
10508 cd33da48 2021-09-28 stsp "yet merged and should be merged manually "
10509 cd33da48 2021-09-28 stsp "if required before the histedit operation "
10510 cd33da48 2021-09-28 stsp "is continued");
10513 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10514 3e3a69f1 2019-07-25 stsp branch, repo);
10516 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
10517 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
10518 0ebf8283 2019-07-24 stsp free(head_commit_id);
10519 0ebf8283 2019-07-24 stsp free(base_commit_id);
10520 0ebf8283 2019-07-24 stsp free(resume_commit_id);
10521 0ebf8283 2019-07-24 stsp if (commit)
10522 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10523 0ebf8283 2019-07-24 stsp if (branch)
10524 0ebf8283 2019-07-24 stsp got_ref_close(branch);
10525 0ebf8283 2019-07-24 stsp if (tmp_branch)
10526 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
10527 0ebf8283 2019-07-24 stsp if (worktree)
10528 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
10529 1d0f4054 2021-06-17 stsp if (repo) {
10530 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10531 1d0f4054 2021-06-17 stsp if (error == NULL)
10532 1d0f4054 2021-06-17 stsp error = close_err;
10534 2822a352 2019-10-15 stsp return error;
10537 2822a352 2019-10-15 stsp __dead static void
10538 2822a352 2019-10-15 stsp usage_integrate(void)
10540 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10544 2822a352 2019-10-15 stsp static const struct got_error *
10545 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10547 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
10548 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
10549 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
10550 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10551 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
10552 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10553 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
10554 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10556 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10558 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10559 2822a352 2019-10-15 stsp switch (ch) {
10561 2822a352 2019-10-15 stsp usage_integrate();
10562 2822a352 2019-10-15 stsp /* NOTREACHED */
10566 2822a352 2019-10-15 stsp argc -= optind;
10567 2822a352 2019-10-15 stsp argv += optind;
10569 2822a352 2019-10-15 stsp if (argc != 1)
10570 2822a352 2019-10-15 stsp usage_integrate();
10571 2822a352 2019-10-15 stsp branch_arg = argv[0];
10572 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
10573 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10574 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
10575 2822a352 2019-10-15 stsp err(1, "pledge");
10577 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
10578 2822a352 2019-10-15 stsp if (cwd == NULL) {
10579 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
10580 2822a352 2019-10-15 stsp goto done;
10583 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
10584 fa51e947 2020-03-27 stsp if (error) {
10585 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10586 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
10588 2822a352 2019-10-15 stsp goto done;
10591 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
10592 2822a352 2019-10-15 stsp if (error)
10593 2822a352 2019-10-15 stsp goto done;
10595 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10597 2822a352 2019-10-15 stsp if (error != NULL)
10598 2822a352 2019-10-15 stsp goto done;
10600 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10601 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
10602 2822a352 2019-10-15 stsp if (error)
10603 2822a352 2019-10-15 stsp goto done;
10605 f259c4c1 2021-09-24 stsp error = check_merge_in_progress(worktree, repo);
10606 f259c4c1 2021-09-24 stsp if (error)
10607 f259c4c1 2021-09-24 stsp goto done;
10609 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10610 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
10611 2822a352 2019-10-15 stsp goto done;
10614 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10615 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
10616 2822a352 2019-10-15 stsp if (error)
10617 2822a352 2019-10-15 stsp goto done;
10619 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
10620 2822a352 2019-10-15 stsp if (refname == NULL) {
10621 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10622 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10623 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10624 2822a352 2019-10-15 stsp goto done;
10626 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
10627 2822a352 2019-10-15 stsp if (base_refname == NULL) {
10628 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10629 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10630 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10631 2822a352 2019-10-15 stsp goto done;
10634 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
10635 2822a352 2019-10-15 stsp if (error)
10636 2822a352 2019-10-15 stsp goto done;
10638 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10639 2822a352 2019-10-15 stsp if (error)
10640 2822a352 2019-10-15 stsp goto done;
10642 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10643 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
10644 2822a352 2019-10-15 stsp "specified branch has already been integrated");
10645 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10646 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10647 2822a352 2019-10-15 stsp goto done;
10650 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10651 2822a352 2019-10-15 stsp if (error) {
10652 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
10653 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
10654 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10655 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10656 2822a352 2019-10-15 stsp goto done;
10659 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10660 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
10661 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
10662 2822a352 2019-10-15 stsp check_cancelled, NULL);
10663 2822a352 2019-10-15 stsp if (error)
10664 2822a352 2019-10-15 stsp goto done;
10666 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
10667 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10669 1d0f4054 2021-06-17 stsp if (repo) {
10670 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10671 1d0f4054 2021-06-17 stsp if (error == NULL)
10672 1d0f4054 2021-06-17 stsp error = close_err;
10674 2822a352 2019-10-15 stsp if (worktree)
10675 2822a352 2019-10-15 stsp got_worktree_close(worktree);
10676 2822a352 2019-10-15 stsp free(cwd);
10677 2822a352 2019-10-15 stsp free(base_commit_id);
10678 2822a352 2019-10-15 stsp free(commit_id);
10679 2822a352 2019-10-15 stsp free(refname);
10680 2822a352 2019-10-15 stsp free(base_refname);
10681 f259c4c1 2021-09-24 stsp return error;
10684 f259c4c1 2021-09-24 stsp __dead static void
10685 f259c4c1 2021-09-24 stsp usage_merge(void)
10687 088449d3 2021-09-26 stsp fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
10688 f259c4c1 2021-09-24 stsp getprogname());
10692 f259c4c1 2021-09-24 stsp static const struct got_error *
10693 f259c4c1 2021-09-24 stsp cmd_merge(int argc, char *argv[])
10695 f259c4c1 2021-09-24 stsp const struct got_error *error = NULL;
10696 f259c4c1 2021-09-24 stsp struct got_worktree *worktree = NULL;
10697 f259c4c1 2021-09-24 stsp struct got_repository *repo = NULL;
10698 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex = NULL;
10699 f259c4c1 2021-09-24 stsp char *cwd = NULL, *id_str = NULL, *author = NULL;
10700 f259c4c1 2021-09-24 stsp struct got_reference *branch = NULL, *wt_branch = NULL;
10701 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *yca_id = NULL;
10702 f259c4c1 2021-09-24 stsp struct got_object_id *wt_branch_tip = NULL;
10703 f259c4c1 2021-09-24 stsp int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
10704 088449d3 2021-09-26 stsp int interrupt_merge = 0;
10705 f259c4c1 2021-09-24 stsp struct got_update_progress_arg upa;
10706 f259c4c1 2021-09-24 stsp struct got_object_id *merge_commit_id = NULL;
10707 f259c4c1 2021-09-24 stsp char *branch_name = NULL;
10709 f259c4c1 2021-09-24 stsp memset(&upa, 0, sizeof(upa));
10711 088449d3 2021-09-26 stsp while ((ch = getopt(argc, argv, "acn")) != -1) {
10712 f259c4c1 2021-09-24 stsp switch (ch) {
10713 f259c4c1 2021-09-24 stsp case 'a':
10714 f259c4c1 2021-09-24 stsp abort_merge = 1;
10716 f259c4c1 2021-09-24 stsp case 'c':
10717 f259c4c1 2021-09-24 stsp continue_merge = 1;
10719 088449d3 2021-09-26 stsp case 'n':
10720 088449d3 2021-09-26 stsp interrupt_merge = 1;
10723 f259c4c1 2021-09-24 stsp usage_rebase();
10724 f259c4c1 2021-09-24 stsp /* NOTREACHED */
10728 f259c4c1 2021-09-24 stsp argc -= optind;
10729 f259c4c1 2021-09-24 stsp argv += optind;
10731 f259c4c1 2021-09-24 stsp #ifndef PROFILE
10732 f259c4c1 2021-09-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10733 f259c4c1 2021-09-24 stsp "unveil", NULL) == -1)
10734 f259c4c1 2021-09-24 stsp err(1, "pledge");
10737 f259c4c1 2021-09-24 stsp if (abort_merge && continue_merge)
10738 f259c4c1 2021-09-24 stsp option_conflict('a', 'c');
10739 f259c4c1 2021-09-24 stsp if (abort_merge || continue_merge) {
10740 f259c4c1 2021-09-24 stsp if (argc != 0)
10741 f259c4c1 2021-09-24 stsp usage_merge();
10742 f259c4c1 2021-09-24 stsp } else if (argc != 1)
10743 f259c4c1 2021-09-24 stsp usage_merge();
10745 f259c4c1 2021-09-24 stsp cwd = getcwd(NULL, 0);
10746 f259c4c1 2021-09-24 stsp if (cwd == NULL) {
10747 f259c4c1 2021-09-24 stsp error = got_error_from_errno("getcwd");
10748 f259c4c1 2021-09-24 stsp goto done;
10751 f259c4c1 2021-09-24 stsp error = got_worktree_open(&worktree, cwd);
10752 f259c4c1 2021-09-24 stsp if (error) {
10753 f259c4c1 2021-09-24 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10754 f259c4c1 2021-09-24 stsp error = wrap_not_worktree_error(error,
10755 f259c4c1 2021-09-24 stsp "merge", cwd);
10756 f259c4c1 2021-09-24 stsp goto done;
10759 f259c4c1 2021-09-24 stsp error = got_repo_open(&repo,
10760 f259c4c1 2021-09-24 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
10761 f259c4c1 2021-09-24 stsp if (error != NULL)
10762 f259c4c1 2021-09-24 stsp goto done;
10764 f259c4c1 2021-09-24 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10765 f259c4c1 2021-09-24 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
10766 f259c4c1 2021-09-24 stsp if (error)
10767 f259c4c1 2021-09-24 stsp goto done;
10769 f259c4c1 2021-09-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
10770 f259c4c1 2021-09-24 stsp if (error)
10771 f259c4c1 2021-09-24 stsp goto done;
10773 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10775 f259c4c1 2021-09-24 stsp if (error)
10776 f259c4c1 2021-09-24 stsp goto done;
10778 f259c4c1 2021-09-24 stsp if (abort_merge) {
10779 f259c4c1 2021-09-24 stsp if (!merge_in_progress) {
10780 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_NOT_MERGING);
10781 f259c4c1 2021-09-24 stsp goto done;
10783 f259c4c1 2021-09-24 stsp error = got_worktree_merge_continue(&branch_name,
10784 f259c4c1 2021-09-24 stsp &branch_tip, &fileindex, worktree, repo);
10785 f259c4c1 2021-09-24 stsp if (error)
10786 f259c4c1 2021-09-24 stsp goto done;
10787 f259c4c1 2021-09-24 stsp error = got_worktree_merge_abort(worktree, fileindex, repo,
10788 f259c4c1 2021-09-24 stsp update_progress, &upa);
10789 f259c4c1 2021-09-24 stsp if (error)
10790 f259c4c1 2021-09-24 stsp goto done;
10791 f259c4c1 2021-09-24 stsp printf("Merge of %s aborted\n", branch_name);
10792 f259c4c1 2021-09-24 stsp goto done; /* nothing else to do */
10795 f259c4c1 2021-09-24 stsp error = get_author(&author, repo, worktree);
10796 f259c4c1 2021-09-24 stsp if (error)
10797 f259c4c1 2021-09-24 stsp goto done;
10799 f259c4c1 2021-09-24 stsp if (continue_merge) {
10800 f259c4c1 2021-09-24 stsp if (!merge_in_progress) {
10801 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_NOT_MERGING);
10802 f259c4c1 2021-09-24 stsp goto done;
10804 f259c4c1 2021-09-24 stsp error = got_worktree_merge_continue(&branch_name,
10805 f259c4c1 2021-09-24 stsp &branch_tip, &fileindex, worktree, repo);
10806 f259c4c1 2021-09-24 stsp if (error)
10807 f259c4c1 2021-09-24 stsp goto done;
10809 f259c4c1 2021-09-24 stsp error = got_ref_open(&branch, repo, argv[0], 0);
10810 f259c4c1 2021-09-24 stsp if (error != NULL)
10811 f259c4c1 2021-09-24 stsp goto done;
10812 f259c4c1 2021-09-24 stsp branch_name = strdup(got_ref_get_name(branch));
10813 f259c4c1 2021-09-24 stsp if (branch_name == NULL) {
10814 f259c4c1 2021-09-24 stsp error = got_error_from_errno("strdup");
10815 f259c4c1 2021-09-24 stsp goto done;
10817 f259c4c1 2021-09-24 stsp error = got_ref_resolve(&branch_tip, repo, branch);
10818 f259c4c1 2021-09-24 stsp if (error)
10819 f259c4c1 2021-09-24 stsp goto done;
10822 f259c4c1 2021-09-24 stsp error = got_ref_open(&wt_branch, repo,
10823 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10824 f259c4c1 2021-09-24 stsp if (error)
10825 f259c4c1 2021-09-24 stsp goto done;
10826 f259c4c1 2021-09-24 stsp error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
10827 f259c4c1 2021-09-24 stsp if (error)
10828 f259c4c1 2021-09-24 stsp goto done;
10829 f259c4c1 2021-09-24 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10830 4e91ef15 2021-09-26 stsp wt_branch_tip, branch_tip, 0, repo,
10831 f259c4c1 2021-09-24 stsp check_cancelled, NULL);
10832 4e91ef15 2021-09-26 stsp if (error && error->code != GOT_ERR_ANCESTRY)
10833 f259c4c1 2021-09-24 stsp goto done;
10835 f259c4c1 2021-09-24 stsp if (!continue_merge) {
10836 f259c4c1 2021-09-24 stsp error = check_path_prefix(wt_branch_tip, branch_tip,
10837 f259c4c1 2021-09-24 stsp got_worktree_get_path_prefix(worktree),
10838 f259c4c1 2021-09-24 stsp GOT_ERR_MERGE_PATH, repo);
10839 f259c4c1 2021-09-24 stsp if (error)
10840 f259c4c1 2021-09-24 stsp goto done;
10841 4e91ef15 2021-09-26 stsp if (yca_id) {
10842 4e91ef15 2021-09-26 stsp error = check_same_branch(wt_branch_tip, branch,
10843 4e91ef15 2021-09-26 stsp yca_id, repo);
10844 4e91ef15 2021-09-26 stsp if (error) {
10845 4e91ef15 2021-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
10846 4e91ef15 2021-09-26 stsp goto done;
10847 4e91ef15 2021-09-26 stsp error = NULL;
10849 4e91ef15 2021-09-26 stsp static char msg[512];
10850 4e91ef15 2021-09-26 stsp snprintf(msg, sizeof(msg),
10851 4e91ef15 2021-09-26 stsp "cannot create a merge commit because "
10852 4e91ef15 2021-09-26 stsp "%s is based on %s; %s can be integrated "
10853 4e91ef15 2021-09-26 stsp "with 'got integrate' instead", branch_name,
10854 4e91ef15 2021-09-26 stsp got_worktree_get_head_ref_name(worktree),
10855 4e91ef15 2021-09-26 stsp branch_name);
10856 4e91ef15 2021-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
10857 f259c4c1 2021-09-24 stsp goto done;
10860 f259c4c1 2021-09-24 stsp error = got_worktree_merge_prepare(&fileindex, worktree,
10861 f259c4c1 2021-09-24 stsp branch, repo);
10862 f259c4c1 2021-09-24 stsp if (error)
10863 f259c4c1 2021-09-24 stsp goto done;
10865 f259c4c1 2021-09-24 stsp error = got_worktree_merge_branch(worktree, fileindex,
10866 f259c4c1 2021-09-24 stsp yca_id, branch_tip, repo, update_progress, &upa,
10867 f259c4c1 2021-09-24 stsp check_cancelled, NULL);
10868 f259c4c1 2021-09-24 stsp if (error)
10869 f259c4c1 2021-09-24 stsp goto done;
10870 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10871 4e91ef15 2021-09-26 stsp if (!upa.did_something) {
10872 4e91ef15 2021-09-26 stsp error = got_worktree_merge_abort(worktree, fileindex,
10873 4e91ef15 2021-09-26 stsp repo, update_progress, &upa);
10874 4e91ef15 2021-09-26 stsp if (error)
10875 4e91ef15 2021-09-26 stsp goto done;
10876 4e91ef15 2021-09-26 stsp printf("Already up-to-date\n");
10877 4e91ef15 2021-09-26 stsp goto done;
10881 088449d3 2021-09-26 stsp if (interrupt_merge) {
10882 f259c4c1 2021-09-24 stsp error = got_worktree_merge_postpone(worktree, fileindex);
10883 f259c4c1 2021-09-24 stsp if (error)
10884 f259c4c1 2021-09-24 stsp goto done;
10885 088449d3 2021-09-26 stsp printf("Merge of %s interrupted on request\n", branch_name);
10886 c1b05723 2021-09-28 stsp } else if (upa.conflicts > 0 || upa.missing > 0 ||
10887 c1b05723 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
10888 088449d3 2021-09-26 stsp error = got_worktree_merge_postpone(worktree, fileindex);
10889 088449d3 2021-09-26 stsp if (error)
10890 088449d3 2021-09-26 stsp goto done;
10891 c1b05723 2021-09-28 stsp if (upa.conflicts > 0 && upa.missing == 0 &&
10892 c1b05723 2021-09-28 stsp upa.not_deleted == 0 && upa.unversioned == 0) {
10893 f259c4c1 2021-09-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10894 f259c4c1 2021-09-24 stsp "conflicts must be resolved before merging "
10895 f259c4c1 2021-09-24 stsp "can continue");
10896 f259c4c1 2021-09-24 stsp } else if (upa.conflicts > 0) {
10897 f259c4c1 2021-09-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10898 f259c4c1 2021-09-24 stsp "conflicts must be resolved before merging "
10899 c1b05723 2021-09-28 stsp "can continue; changes destined for some "
10900 1acd48bc 2021-09-24 stsp "files were not yet merged and "
10901 f259c4c1 2021-09-24 stsp "should be merged manually if required before the "
10902 f259c4c1 2021-09-24 stsp "merge operation is continued");
10904 f259c4c1 2021-09-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10905 c1b05723 2021-09-28 stsp "changes destined for some "
10906 f259c4c1 2021-09-24 stsp "files were not yet merged and should be "
10907 f259c4c1 2021-09-24 stsp "merged manually if required before the "
10908 f259c4c1 2021-09-24 stsp "merge operation is continued");
10910 f259c4c1 2021-09-24 stsp goto done;
10912 f259c4c1 2021-09-24 stsp error = got_worktree_merge_commit(&merge_commit_id, worktree,
10913 0ff8d236 2021-09-28 stsp fileindex, author, NULL, 1, branch_tip, branch_name,
10914 0ff8d236 2021-09-28 stsp repo, continue_merge ? print_status : NULL, NULL);
10915 f259c4c1 2021-09-24 stsp if (error)
10916 f259c4c1 2021-09-24 stsp goto done;
10917 f259c4c1 2021-09-24 stsp error = got_worktree_merge_complete(worktree, fileindex, repo);
10918 f259c4c1 2021-09-24 stsp if (error)
10919 f259c4c1 2021-09-24 stsp goto done;
10920 f259c4c1 2021-09-24 stsp error = got_object_id_str(&id_str, merge_commit_id);
10921 f259c4c1 2021-09-24 stsp if (error)
10922 f259c4c1 2021-09-24 stsp goto done;
10923 f259c4c1 2021-09-24 stsp printf("Merged %s into %s: %s\n", branch_name,
10924 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(worktree),
10929 f259c4c1 2021-09-24 stsp free(id_str);
10930 f259c4c1 2021-09-24 stsp free(merge_commit_id);
10931 f259c4c1 2021-09-24 stsp free(author);
10932 f259c4c1 2021-09-24 stsp free(branch_tip);
10933 f259c4c1 2021-09-24 stsp free(branch_name);
10934 f259c4c1 2021-09-24 stsp free(yca_id);
10935 f259c4c1 2021-09-24 stsp if (branch)
10936 f259c4c1 2021-09-24 stsp got_ref_close(branch);
10937 f259c4c1 2021-09-24 stsp if (wt_branch)
10938 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
10939 f259c4c1 2021-09-24 stsp if (worktree)
10940 f259c4c1 2021-09-24 stsp got_worktree_close(worktree);
10941 f259c4c1 2021-09-24 stsp if (repo) {
10942 f259c4c1 2021-09-24 stsp const struct got_error *close_err = got_repo_close(repo);
10943 f259c4c1 2021-09-24 stsp if (error == NULL)
10944 f259c4c1 2021-09-24 stsp error = close_err;
10946 715dc77e 2019-08-03 stsp return error;
10949 715dc77e 2019-08-03 stsp __dead static void
10950 715dc77e 2019-08-03 stsp usage_stage(void)
10952 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10953 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
10954 715dc77e 2019-08-03 stsp getprogname());
10958 715dc77e 2019-08-03 stsp static const struct got_error *
10959 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
10960 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
10961 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10962 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
10964 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
10965 408b4ebc 2019-08-03 stsp char *id_str = NULL;
10967 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
10968 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
10969 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
10970 408b4ebc 2019-08-03 stsp return NULL;
10972 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
10973 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
10974 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
10976 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
10978 408b4ebc 2019-08-03 stsp return err;
10980 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
10981 408b4ebc 2019-08-03 stsp free(id_str);
10982 dc424a06 2019-08-07 stsp return NULL;
10985 dc424a06 2019-08-07 stsp static const struct got_error *
10986 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
10988 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
10989 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
10990 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
10991 715dc77e 2019-08-03 stsp char *cwd = NULL;
10992 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
10993 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
10994 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10995 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
10996 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10997 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10999 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
11001 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
11002 715dc77e 2019-08-03 stsp switch (ch) {
11003 408b4ebc 2019-08-03 stsp case 'l':
11004 408b4ebc 2019-08-03 stsp list_stage = 1;
11006 dc424a06 2019-08-07 stsp case 'p':
11007 dc424a06 2019-08-07 stsp pflag = 1;
11009 dc424a06 2019-08-07 stsp case 'F':
11010 dc424a06 2019-08-07 stsp patch_script_path = optarg;
11012 35213c7c 2020-07-23 stsp case 'S':
11013 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
11016 715dc77e 2019-08-03 stsp usage_stage();
11017 715dc77e 2019-08-03 stsp /* NOTREACHED */
11021 715dc77e 2019-08-03 stsp argc -= optind;
11022 715dc77e 2019-08-03 stsp argv += optind;
11024 715dc77e 2019-08-03 stsp #ifndef PROFILE
11025 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11026 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
11027 715dc77e 2019-08-03 stsp err(1, "pledge");
11029 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
11030 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
11031 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
11032 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
11034 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
11035 715dc77e 2019-08-03 stsp if (cwd == NULL) {
11036 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
11037 715dc77e 2019-08-03 stsp goto done;
11040 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
11041 fa51e947 2020-03-27 stsp if (error) {
11042 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11043 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
11044 715dc77e 2019-08-03 stsp goto done;
11047 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11049 715dc77e 2019-08-03 stsp if (error != NULL)
11050 715dc77e 2019-08-03 stsp goto done;
11052 dc424a06 2019-08-07 stsp if (patch_script_path) {
11053 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
11054 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
11055 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
11056 dc424a06 2019-08-07 stsp patch_script_path);
11057 dc424a06 2019-08-07 stsp goto done;
11060 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11061 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
11062 715dc77e 2019-08-03 stsp if (error)
11063 715dc77e 2019-08-03 stsp goto done;
11065 f259c4c1 2021-09-24 stsp error = check_merge_in_progress(worktree, repo);
11066 f259c4c1 2021-09-24 stsp if (error)
11067 f259c4c1 2021-09-24 stsp goto done;
11069 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11070 6d022e97 2019-08-04 stsp if (error)
11071 6d022e97 2019-08-04 stsp goto done;
11073 6d022e97 2019-08-04 stsp if (list_stage)
11074 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
11075 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
11077 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
11078 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
11079 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
11080 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
11081 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
11082 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
11085 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
11086 2e1f37b0 2019-08-08 stsp error == NULL)
11087 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
11088 1d0f4054 2021-06-17 stsp if (repo) {
11089 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11090 1d0f4054 2021-06-17 stsp if (error == NULL)
11091 1d0f4054 2021-06-17 stsp error = close_err;
11093 ad493afc 2019-08-03 stsp if (worktree)
11094 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
11095 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
11096 ad493afc 2019-08-03 stsp free((char *)pe->path);
11097 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
11098 ad493afc 2019-08-03 stsp free(cwd);
11099 ad493afc 2019-08-03 stsp return error;
11102 ad493afc 2019-08-03 stsp __dead static void
11103 ad493afc 2019-08-03 stsp usage_unstage(void)
11105 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
11106 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
11107 ad493afc 2019-08-03 stsp getprogname());
11112 ad493afc 2019-08-03 stsp static const struct got_error *
11113 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
11115 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
11116 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
11117 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
11118 ad493afc 2019-08-03 stsp char *cwd = NULL;
11119 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
11120 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
11121 9627c110 2020-04-18 stsp int ch, pflag = 0;
11122 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
11123 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
11124 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
11125 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
11127 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
11129 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
11130 ad493afc 2019-08-03 stsp switch (ch) {
11131 2e1f37b0 2019-08-08 stsp case 'p':
11132 2e1f37b0 2019-08-08 stsp pflag = 1;
11134 2e1f37b0 2019-08-08 stsp case 'F':
11135 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
11138 ad493afc 2019-08-03 stsp usage_unstage();
11139 ad493afc 2019-08-03 stsp /* NOTREACHED */
11143 ad493afc 2019-08-03 stsp argc -= optind;
11144 ad493afc 2019-08-03 stsp argv += optind;
11146 ad493afc 2019-08-03 stsp #ifndef PROFILE
11147 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11148 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
11149 ad493afc 2019-08-03 stsp err(1, "pledge");
11151 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
11152 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
11154 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
11155 ad493afc 2019-08-03 stsp if (cwd == NULL) {
11156 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
11157 ad493afc 2019-08-03 stsp goto done;
11160 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
11161 fa51e947 2020-03-27 stsp if (error) {
11162 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11163 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
11164 ad493afc 2019-08-03 stsp goto done;
11167 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11169 ad493afc 2019-08-03 stsp if (error != NULL)
11170 ad493afc 2019-08-03 stsp goto done;
11172 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
11173 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
11174 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
11175 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
11176 2e1f37b0 2019-08-08 stsp patch_script_path);
11177 2e1f37b0 2019-08-08 stsp goto done;
11181 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11182 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
11183 ad493afc 2019-08-03 stsp if (error)
11184 ad493afc 2019-08-03 stsp goto done;
11186 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11187 ad493afc 2019-08-03 stsp if (error)
11188 ad493afc 2019-08-03 stsp goto done;
11190 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
11191 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
11192 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
11193 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
11194 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
11195 9627c110 2020-04-18 stsp if (!error)
11196 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
11198 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
11199 2e1f37b0 2019-08-08 stsp error == NULL)
11200 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
11201 1d0f4054 2021-06-17 stsp if (repo) {
11202 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11203 1d0f4054 2021-06-17 stsp if (error == NULL)
11204 1d0f4054 2021-06-17 stsp error = close_err;
11206 715dc77e 2019-08-03 stsp if (worktree)
11207 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
11208 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
11209 715dc77e 2019-08-03 stsp free((char *)pe->path);
11210 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
11211 715dc77e 2019-08-03 stsp free(cwd);
11212 01073a5d 2019-08-22 stsp return error;
11215 01073a5d 2019-08-22 stsp __dead static void
11216 01073a5d 2019-08-22 stsp usage_cat(void)
11218 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11219 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
11223 01073a5d 2019-08-22 stsp static const struct got_error *
11224 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11226 01073a5d 2019-08-22 stsp const struct got_error *err;
11227 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
11229 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
11231 01073a5d 2019-08-22 stsp return err;
11233 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
11234 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
11235 01073a5d 2019-08-22 stsp return err;
11238 01073a5d 2019-08-22 stsp static const struct got_error *
11239 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11241 01073a5d 2019-08-22 stsp const struct got_error *err;
11242 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
11243 56e0773d 2019-11-28 stsp int nentries, i;
11245 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
11247 01073a5d 2019-08-22 stsp return err;
11249 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
11250 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
11251 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
11252 01073a5d 2019-08-22 stsp char *id_str;
11253 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
11255 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
11256 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
11259 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
11260 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
11261 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
11262 01073a5d 2019-08-22 stsp free(id_str);
11265 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
11266 01073a5d 2019-08-22 stsp return err;
11269 01073a5d 2019-08-22 stsp static const struct got_error *
11270 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11272 01073a5d 2019-08-22 stsp const struct got_error *err;
11273 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
11274 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
11275 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
11276 01073a5d 2019-08-22 stsp char *id_str = NULL;
11277 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
11279 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
11281 01073a5d 2019-08-22 stsp return err;
11283 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11285 01073a5d 2019-08-22 stsp goto done;
11287 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
11288 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
11289 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
11290 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
11291 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
11292 01073a5d 2019-08-22 stsp char *pid_str;
11293 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
11295 01073a5d 2019-08-22 stsp goto done;
11296 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
11297 01073a5d 2019-08-22 stsp free(pid_str);
11299 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
11300 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
11301 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
11303 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
11304 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
11305 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
11307 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
11308 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
11309 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
11311 01073a5d 2019-08-22 stsp free(id_str);
11312 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
11313 01073a5d 2019-08-22 stsp return err;
11316 01073a5d 2019-08-22 stsp static const struct got_error *
11317 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11319 01073a5d 2019-08-22 stsp const struct got_error *err;
11320 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
11321 01073a5d 2019-08-22 stsp char *id_str = NULL;
11322 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
11324 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
11326 01073a5d 2019-08-22 stsp return err;
11328 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11330 01073a5d 2019-08-22 stsp goto done;
11332 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11334 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
11335 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11336 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11337 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
11339 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11340 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11341 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
11343 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11344 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11345 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
11347 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11348 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11349 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
11355 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
11356 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
11358 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
11359 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
11360 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
11362 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
11363 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
11364 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
11366 01073a5d 2019-08-22 stsp free(id_str);
11367 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
11368 01073a5d 2019-08-22 stsp return err;
11371 01073a5d 2019-08-22 stsp static const struct got_error *
11372 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
11374 01073a5d 2019-08-22 stsp const struct got_error *error;
11375 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
11376 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
11377 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
11378 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
11379 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
11380 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
11381 84de9106 2020-12-26 stsp struct got_reflist_head refs;
11383 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
11385 01073a5d 2019-08-22 stsp #ifndef PROFILE
11386 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11387 01073a5d 2019-08-22 stsp NULL) == -1)
11388 01073a5d 2019-08-22 stsp err(1, "pledge");
11391 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
11392 01073a5d 2019-08-22 stsp switch (ch) {
11393 896e9b6f 2019-08-26 stsp case 'c':
11394 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
11396 01073a5d 2019-08-22 stsp case 'r':
11397 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
11398 01073a5d 2019-08-22 stsp if (repo_path == NULL)
11399 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
11401 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
11403 896e9b6f 2019-08-26 stsp case 'P':
11404 896e9b6f 2019-08-26 stsp force_path = 1;
11407 01073a5d 2019-08-22 stsp usage_cat();
11408 01073a5d 2019-08-22 stsp /* NOTREACHED */
11412 01073a5d 2019-08-22 stsp argc -= optind;
11413 01073a5d 2019-08-22 stsp argv += optind;
11415 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
11416 01073a5d 2019-08-22 stsp if (cwd == NULL) {
11417 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
11418 01073a5d 2019-08-22 stsp goto done;
11420 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
11421 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
11422 01073a5d 2019-08-22 stsp goto done;
11423 01073a5d 2019-08-22 stsp if (worktree) {
11424 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11425 01073a5d 2019-08-22 stsp repo_path = strdup(
11426 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
11427 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11428 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
11429 01073a5d 2019-08-22 stsp goto done;
11434 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11435 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
11436 01073a5d 2019-08-22 stsp if (repo_path == NULL)
11437 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
11440 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
11441 01073a5d 2019-08-22 stsp free(repo_path);
11442 01073a5d 2019-08-22 stsp if (error != NULL)
11443 01073a5d 2019-08-22 stsp goto done;
11445 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11446 896e9b6f 2019-08-26 stsp if (error)
11447 896e9b6f 2019-08-26 stsp goto done;
11449 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11450 84de9106 2020-12-26 stsp if (error)
11451 84de9106 2020-12-26 stsp goto done;
11453 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
11454 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
11455 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
11456 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11457 01073a5d 2019-08-22 stsp if (error)
11458 01073a5d 2019-08-22 stsp goto done;
11460 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
11461 896e9b6f 2019-08-26 stsp if (force_path) {
11462 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
11463 896e9b6f 2019-08-26 stsp argv[i]);
11464 896e9b6f 2019-08-26 stsp if (error)
11467 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
11468 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11470 896e9b6f 2019-08-26 stsp if (error) {
11471 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11472 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
11474 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
11475 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
11476 896e9b6f 2019-08-26 stsp if (error)
11481 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
11482 01073a5d 2019-08-22 stsp if (error)
11485 01073a5d 2019-08-22 stsp switch (obj_type) {
11486 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11487 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
11489 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11490 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
11492 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11493 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
11495 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11496 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
11499 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
11502 01073a5d 2019-08-22 stsp if (error)
11504 01073a5d 2019-08-22 stsp free(label);
11505 01073a5d 2019-08-22 stsp label = NULL;
11506 01073a5d 2019-08-22 stsp free(id);
11507 01073a5d 2019-08-22 stsp id = NULL;
11510 01073a5d 2019-08-22 stsp free(label);
11511 01073a5d 2019-08-22 stsp free(id);
11512 896e9b6f 2019-08-26 stsp free(commit_id);
11513 01073a5d 2019-08-22 stsp if (worktree)
11514 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
11515 01073a5d 2019-08-22 stsp if (repo) {
11516 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11517 01073a5d 2019-08-22 stsp if (error == NULL)
11518 1d0f4054 2021-06-17 stsp error = close_err;
11520 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11521 b2118c49 2020-07-28 stsp return error;
11524 b2118c49 2020-07-28 stsp __dead static void
11525 b2118c49 2020-07-28 stsp usage_info(void)
11527 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11528 b2118c49 2020-07-28 stsp getprogname());
11532 b2118c49 2020-07-28 stsp static const struct got_error *
11533 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11534 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11535 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
11537 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
11538 b2118c49 2020-07-28 stsp char *id_str = NULL;
11539 b2118c49 2020-07-28 stsp char datebuf[128];
11540 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
11541 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
11542 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11545 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
11546 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
11548 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
11549 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
11550 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
11551 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
11552 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
11555 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
11556 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
11557 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
11558 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
11559 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
11560 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11561 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
11562 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
11564 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11566 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
11567 b2118c49 2020-07-28 stsp if (tm == NULL)
11568 b2118c49 2020-07-28 stsp return NULL;
11569 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11570 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
11571 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
11573 b2118c49 2020-07-28 stsp if (blob_id) {
11574 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11576 b2118c49 2020-07-28 stsp return err;
11577 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
11578 b2118c49 2020-07-28 stsp free(id_str);
11581 b2118c49 2020-07-28 stsp if (staged_blob_id) {
11582 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
11584 b2118c49 2020-07-28 stsp return err;
11585 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
11586 b2118c49 2020-07-28 stsp free(id_str);
11589 b2118c49 2020-07-28 stsp if (commit_id) {
11590 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
11592 b2118c49 2020-07-28 stsp return err;
11593 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
11594 b2118c49 2020-07-28 stsp free(id_str);
11597 b2118c49 2020-07-28 stsp return NULL;
11600 b2118c49 2020-07-28 stsp static const struct got_error *
11601 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11603 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
11604 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
11605 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
11606 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
11607 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11608 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
11609 b2118c49 2020-07-28 stsp int ch, show_files = 0;
11611 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11613 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11614 b2118c49 2020-07-28 stsp switch (ch) {
11616 b2118c49 2020-07-28 stsp usage_info();
11617 b2118c49 2020-07-28 stsp /* NOTREACHED */
11621 b2118c49 2020-07-28 stsp argc -= optind;
11622 b2118c49 2020-07-28 stsp argv += optind;
11624 b2118c49 2020-07-28 stsp #ifndef PROFILE
11625 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11626 b2118c49 2020-07-28 stsp NULL) == -1)
11627 b2118c49 2020-07-28 stsp err(1, "pledge");
11629 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
11630 b2118c49 2020-07-28 stsp if (cwd == NULL) {
11631 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
11632 b2118c49 2020-07-28 stsp goto done;
11635 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
11636 b2118c49 2020-07-28 stsp if (error) {
11637 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11638 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
11639 b2118c49 2020-07-28 stsp goto done;
11642 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11643 b2118c49 2020-07-28 stsp if (error)
11644 b2118c49 2020-07-28 stsp goto done;
11646 b2118c49 2020-07-28 stsp if (argc >= 1) {
11647 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
11648 b2118c49 2020-07-28 stsp worktree);
11649 b2118c49 2020-07-28 stsp if (error)
11650 b2118c49 2020-07-28 stsp goto done;
11651 b2118c49 2020-07-28 stsp show_files = 1;
11654 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
11655 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
11656 b2118c49 2020-07-28 stsp if (error)
11657 b2118c49 2020-07-28 stsp goto done;
11659 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
11660 b2118c49 2020-07-28 stsp if (error)
11661 b2118c49 2020-07-28 stsp goto done;
11663 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11664 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
11665 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
11666 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
11667 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
11668 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
11669 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
11670 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11672 b2118c49 2020-07-28 stsp if (show_files) {
11673 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11674 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11675 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
11676 b2118c49 2020-07-28 stsp continue;
11678 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
11679 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
11681 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
11682 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
11684 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
11685 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
11686 b2118c49 2020-07-28 stsp if (error)
11687 b2118c49 2020-07-28 stsp goto done;
11688 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11689 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
11690 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
11696 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
11697 b2118c49 2020-07-28 stsp free((char *)pe->path);
11698 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
11699 b2118c49 2020-07-28 stsp free(cwd);
11700 b2118c49 2020-07-28 stsp free(id_str);
11701 b2118c49 2020-07-28 stsp free(uuidstr);
11702 0ebf8283 2019-07-24 stsp return error;