Blame


1 5c860e29 2018-03-12 stsp /*
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>
5 5c860e29 2018-03-12 stsp *
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.
9 5c860e29 2018-03-12 stsp *
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.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 f42b1b34 2018-03-12 stsp
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
42 5c860e29 2018-03-12 stsp
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
58 5c860e29 2018-03-12 stsp
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 5c860e29 2018-03-12 stsp #endif
62 99437157 2018-11-11 stsp
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
65 99437157 2018-11-11 stsp
66 99437157 2018-11-11 stsp static void
67 99437157 2018-11-11 stsp catch_sigint(int signo)
68 99437157 2018-11-11 stsp {
69 99437157 2018-11-11 stsp sigint_received = 1;
70 99437157 2018-11-11 stsp }
71 99437157 2018-11-11 stsp
72 99437157 2018-11-11 stsp static void
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
74 99437157 2018-11-11 stsp {
75 99437157 2018-11-11 stsp sigpipe_received = 1;
76 99437157 2018-11-11 stsp }
77 5c860e29 2018-03-12 stsp
78 99437157 2018-11-11 stsp
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 5c860e29 2018-03-12 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
84 5c860e29 2018-03-12 stsp };
85 5c860e29 2018-03-12 stsp
86 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
106 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
107 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
108 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
109 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
110 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
111 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
112 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
114 5c860e29 2018-03-12 stsp
115 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
116 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
117 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
118 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
120 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
123 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
124 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
126 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
127 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
128 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
129 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
130 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
131 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
132 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
133 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
134 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
135 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
136 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
137 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
138 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
139 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
140 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
141 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
142 5c860e29 2018-03-12 stsp
143 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
144 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
145 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
146 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
147 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
148 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
149 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
150 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
151 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
152 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
153 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
154 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
155 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
156 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
157 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
158 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
159 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
160 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
161 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
162 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
164 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
165 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
166 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
167 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
168 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
169 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
170 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
171 5c860e29 2018-03-12 stsp };
172 ce5b7c56 2019-07-09 stsp
173 ce5b7c56 2019-07-09 stsp static void
174 ce5b7c56 2019-07-09 stsp list_commands(void)
175 ce5b7c56 2019-07-09 stsp {
176 ce5b7c56 2019-07-09 stsp int i;
177 ce5b7c56 2019-07-09 stsp
178 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
179 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
180 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
181 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
182 ce5b7c56 2019-07-09 stsp }
183 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
184 ce5b7c56 2019-07-09 stsp }
185 5c860e29 2018-03-12 stsp
186 5c860e29 2018-03-12 stsp int
187 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
188 5c860e29 2018-03-12 stsp {
189 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
190 5c860e29 2018-03-12 stsp unsigned int i;
191 5c860e29 2018-03-12 stsp int ch;
192 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
193 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
194 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
195 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
196 83cd27f8 2020-01-13 stsp };
197 5c860e29 2018-03-12 stsp
198 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
199 5c860e29 2018-03-12 stsp
200 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 5c860e29 2018-03-12 stsp switch (ch) {
202 1b6b95a8 2018-03-12 stsp case 'h':
203 1b6b95a8 2018-03-12 stsp hflag = 1;
204 53ccebc2 2019-07-30 stsp break;
205 53ccebc2 2019-07-30 stsp case 'V':
206 53ccebc2 2019-07-30 stsp Vflag = 1;
207 1b6b95a8 2018-03-12 stsp break;
208 5c860e29 2018-03-12 stsp default:
209 ce5b7c56 2019-07-09 stsp usage(hflag);
210 5c860e29 2018-03-12 stsp /* NOTREACHED */
211 5c860e29 2018-03-12 stsp }
212 5c860e29 2018-03-12 stsp }
213 5c860e29 2018-03-12 stsp
214 5c860e29 2018-03-12 stsp argc -= optind;
215 5c860e29 2018-03-12 stsp argv += optind;
216 1e70621d 2018-03-27 stsp optind = 0;
217 53ccebc2 2019-07-30 stsp
218 53ccebc2 2019-07-30 stsp if (Vflag) {
219 53ccebc2 2019-07-30 stsp got_version_print_str();
220 53ccebc2 2019-07-30 stsp return 1;
221 53ccebc2 2019-07-30 stsp }
222 5c860e29 2018-03-12 stsp
223 5c860e29 2018-03-12 stsp if (argc <= 0)
224 ce5b7c56 2019-07-09 stsp usage(hflag);
225 5c860e29 2018-03-12 stsp
226 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
227 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
228 99437157 2018-11-11 stsp
229 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
230 d7d4f210 2018-03-12 stsp const struct got_error *error;
231 d7d4f210 2018-03-12 stsp
232 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
233 5c860e29 2018-03-12 stsp
234 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
236 5c860e29 2018-03-12 stsp continue;
237 5c860e29 2018-03-12 stsp
238 1b6b95a8 2018-03-12 stsp if (hflag)
239 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
240 1b6b95a8 2018-03-12 stsp
241 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
242 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
243 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
244 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
245 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
246 70015d7a 2019-11-08 stsp !(sigint_received &&
247 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
249 d7d4f210 2018-03-12 stsp return 1;
250 d7d4f210 2018-03-12 stsp }
251 d7d4f210 2018-03-12 stsp
252 d7d4f210 2018-03-12 stsp return 0;
253 5c860e29 2018-03-12 stsp }
254 5c860e29 2018-03-12 stsp
255 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 ce5b7c56 2019-07-09 stsp list_commands();
257 5c860e29 2018-03-12 stsp return 1;
258 5c860e29 2018-03-12 stsp }
259 5c860e29 2018-03-12 stsp
260 4ed7e80c 2018-05-20 stsp __dead static void
261 ce5b7c56 2019-07-09 stsp usage(int hflag)
262 5c860e29 2018-03-12 stsp {
263 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
264 53ccebc2 2019-07-30 stsp getprogname());
265 ce5b7c56 2019-07-09 stsp if (hflag)
266 ce5b7c56 2019-07-09 stsp list_commands();
267 5c860e29 2018-03-12 stsp exit(1);
268 5c860e29 2018-03-12 stsp }
269 5c860e29 2018-03-12 stsp
270 0266afb7 2019-01-04 stsp static const struct got_error *
271 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
272 e2ba3d07 2019-05-13 stsp {
273 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
274 e2ba3d07 2019-05-13 stsp const char *editor;
275 8920fa04 2019-08-18 stsp
276 8920fa04 2019-08-18 stsp *abspath = NULL;
277 e2ba3d07 2019-05-13 stsp
278 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
279 e2ba3d07 2019-05-13 stsp if (editor == NULL)
280 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
281 e2ba3d07 2019-05-13 stsp
282 0ee7065d 2019-05-13 stsp if (editor) {
283 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
284 0ee7065d 2019-05-13 stsp if (err)
285 0ee7065d 2019-05-13 stsp return err;
286 0ee7065d 2019-05-13 stsp }
287 e2ba3d07 2019-05-13 stsp
288 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
289 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
290 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
291 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
292 0ee7065d 2019-05-13 stsp }
293 0ee7065d 2019-05-13 stsp
294 e2ba3d07 2019-05-13 stsp return NULL;
295 e2ba3d07 2019-05-13 stsp }
296 e2ba3d07 2019-05-13 stsp
297 e2ba3d07 2019-05-13 stsp static const struct got_error *
298 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
299 c530dc23 2019-07-23 stsp const char *worktree_path)
300 0266afb7 2019-01-04 stsp {
301 163ce85a 2019-05-13 stsp const struct got_error *err;
302 0266afb7 2019-01-04 stsp
303 37c06ea4 2019-07-15 stsp #ifdef PROFILE
304 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
305 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
306 37c06ea4 2019-07-15 stsp #endif
307 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
309 0266afb7 2019-01-04 stsp
310 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
312 0266afb7 2019-01-04 stsp
313 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
315 0266afb7 2019-01-04 stsp
316 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
317 163ce85a 2019-05-13 stsp if (err != NULL)
318 163ce85a 2019-05-13 stsp return err;
319 0266afb7 2019-01-04 stsp
320 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
322 0266afb7 2019-01-04 stsp
323 0266afb7 2019-01-04 stsp return NULL;
324 2c7829a4 2019-06-17 stsp }
325 2c7829a4 2019-06-17 stsp
326 2c7829a4 2019-06-17 stsp __dead static void
327 2c7829a4 2019-06-17 stsp usage_init(void)
328 2c7829a4 2019-06-17 stsp {
329 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
330 2c7829a4 2019-06-17 stsp exit(1);
331 2c7829a4 2019-06-17 stsp }
332 2c7829a4 2019-06-17 stsp
333 2c7829a4 2019-06-17 stsp static const struct got_error *
334 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
335 2c7829a4 2019-06-17 stsp {
336 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
337 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
338 2c7829a4 2019-06-17 stsp int ch;
339 2c7829a4 2019-06-17 stsp
340 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
341 2c7829a4 2019-06-17 stsp switch (ch) {
342 2c7829a4 2019-06-17 stsp default:
343 2c7829a4 2019-06-17 stsp usage_init();
344 2c7829a4 2019-06-17 stsp /* NOTREACHED */
345 2c7829a4 2019-06-17 stsp }
346 2c7829a4 2019-06-17 stsp }
347 2c7829a4 2019-06-17 stsp
348 2c7829a4 2019-06-17 stsp argc -= optind;
349 2c7829a4 2019-06-17 stsp argv += optind;
350 2c7829a4 2019-06-17 stsp
351 2c7829a4 2019-06-17 stsp #ifndef PROFILE
352 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
353 2c7829a4 2019-06-17 stsp err(1, "pledge");
354 2c7829a4 2019-06-17 stsp #endif
355 2c7829a4 2019-06-17 stsp if (argc != 1)
356 bc20e173 2019-06-17 stsp usage_init();
357 2c7829a4 2019-06-17 stsp
358 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
359 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
360 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
361 2c7829a4 2019-06-17 stsp
362 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
363 2c7829a4 2019-06-17 stsp
364 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
365 2c7829a4 2019-06-17 stsp if (error &&
366 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
367 2c7829a4 2019-06-17 stsp goto done;
368 2c7829a4 2019-06-17 stsp
369 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
370 2c7829a4 2019-06-17 stsp if (error)
371 2c7829a4 2019-06-17 stsp goto done;
372 2c7829a4 2019-06-17 stsp
373 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
374 3ce1b845 2019-07-15 stsp done:
375 3ce1b845 2019-07-15 stsp free(repo_path);
376 3ce1b845 2019-07-15 stsp return error;
377 3ce1b845 2019-07-15 stsp }
378 3ce1b845 2019-07-15 stsp
379 3ce1b845 2019-07-15 stsp __dead static void
380 3ce1b845 2019-07-15 stsp usage_import(void)
381 3ce1b845 2019-07-15 stsp {
382 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
384 3ce1b845 2019-07-15 stsp exit(1);
385 3ce1b845 2019-07-15 stsp }
386 3ce1b845 2019-07-15 stsp
387 3ce1b845 2019-07-15 stsp int
388 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
389 3ce1b845 2019-07-15 stsp {
390 3ce1b845 2019-07-15 stsp pid_t pid;
391 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
392 3ce1b845 2019-07-15 stsp int st = -1;
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
395 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
396 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
399 3ce1b845 2019-07-15 stsp case -1:
400 3ce1b845 2019-07-15 stsp goto doneediting;
401 3ce1b845 2019-07-15 stsp case 0:
402 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
403 3ce1b845 2019-07-15 stsp _exit(127);
404 3ce1b845 2019-07-15 stsp }
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
407 3ce1b845 2019-07-15 stsp if (errno != EINTR)
408 3ce1b845 2019-07-15 stsp break;
409 3ce1b845 2019-07-15 stsp
410 3ce1b845 2019-07-15 stsp doneediting:
411 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
412 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
413 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
416 3ce1b845 2019-07-15 stsp errno = EINTR;
417 3ce1b845 2019-07-15 stsp return -1;
418 3ce1b845 2019-07-15 stsp }
419 3ce1b845 2019-07-15 stsp
420 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
421 3ce1b845 2019-07-15 stsp }
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp static const struct got_error *
424 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 3ce1b845 2019-07-15 stsp const char *initial_content)
426 3ce1b845 2019-07-15 stsp {
427 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
428 3ce1b845 2019-07-15 stsp char buf[1024];
429 3ce1b845 2019-07-15 stsp struct stat st, st2;
430 3ce1b845 2019-07-15 stsp FILE *fp;
431 3ce1b845 2019-07-15 stsp int content_changed = 0;
432 3ce1b845 2019-07-15 stsp size_t len;
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp *logmsg = NULL;
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
437 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
438 3ce1b845 2019-07-15 stsp
439 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
440 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
441 3ce1b845 2019-07-15 stsp
442 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
448 3ce1b845 2019-07-15 stsp
449 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
450 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
451 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
452 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
453 3ce1b845 2019-07-15 stsp len = 0;
454 3ce1b845 2019-07-15 stsp
455 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
456 3ce1b845 2019-07-15 stsp if (fp == NULL) {
457 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
458 3ce1b845 2019-07-15 stsp goto done;
459 3ce1b845 2019-07-15 stsp }
460 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
461 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
462 3ce1b845 2019-07-15 stsp content_changed = 1;
463 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
465 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
466 3ce1b845 2019-07-15 stsp }
467 3ce1b845 2019-07-15 stsp fclose(fp);
468 3ce1b845 2019-07-15 stsp
469 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
471 3ce1b845 2019-07-15 stsp len--;
472 3ce1b845 2019-07-15 stsp }
473 3ce1b845 2019-07-15 stsp
474 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
475 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
477 3ce1b845 2019-07-15 stsp done:
478 3ce1b845 2019-07-15 stsp if (err) {
479 3ce1b845 2019-07-15 stsp free(*logmsg);
480 3ce1b845 2019-07-15 stsp *logmsg = NULL;
481 3ce1b845 2019-07-15 stsp }
482 3ce1b845 2019-07-15 stsp return err;
483 3ce1b845 2019-07-15 stsp }
484 3ce1b845 2019-07-15 stsp
485 3ce1b845 2019-07-15 stsp static const struct got_error *
486 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
488 3ce1b845 2019-07-15 stsp {
489 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
490 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
491 1601cb9f 2020-09-11 naddy int initial_content_len;
492 97972933 2020-09-11 stsp int fd = -1;
493 3ce1b845 2019-07-15 stsp
494 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
495 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
496 1601cb9f 2020-09-11 naddy branch_name);
497 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
498 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
499 3ce1b845 2019-07-15 stsp
500 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
501 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
502 3ce1b845 2019-07-15 stsp if (err)
503 3ce1b845 2019-07-15 stsp goto done;
504 3ce1b845 2019-07-15 stsp
505 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
506 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
507 97972933 2020-09-11 stsp goto done;
508 97972933 2020-09-11 stsp }
509 3ce1b845 2019-07-15 stsp
510 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
511 3ce1b845 2019-07-15 stsp done:
512 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
513 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
514 3ce1b845 2019-07-15 stsp free(initial_content);
515 59f86c76 2020-09-11 stsp if (err) {
516 59f86c76 2020-09-11 stsp free(*logmsg_path);
517 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
518 59f86c76 2020-09-11 stsp }
519 3ce1b845 2019-07-15 stsp return err;
520 3ce1b845 2019-07-15 stsp }
521 3ce1b845 2019-07-15 stsp
522 3ce1b845 2019-07-15 stsp static const struct got_error *
523 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
524 3ce1b845 2019-07-15 stsp {
525 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
526 3ce1b845 2019-07-15 stsp return NULL;
527 3ce1b845 2019-07-15 stsp }
528 3ce1b845 2019-07-15 stsp
529 3ce1b845 2019-07-15 stsp static const struct got_error *
530 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
531 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
532 84792843 2019-08-09 stsp {
533 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
534 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
535 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
536 84792843 2019-08-09 stsp
537 84792843 2019-08-09 stsp *author = NULL;
538 aba9c984 2019-09-08 stsp
539 50b0790e 2020-09-11 stsp if (worktree)
540 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
541 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
542 50b0790e 2020-09-11 stsp
543 50b0790e 2020-09-11 stsp /*
544 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
545 50b0790e 2020-09-11 stsp * significant to least significant:
546 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
547 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
548 50b0790e 2020-09-11 stsp * 3) repository's git config file
549 50b0790e 2020-09-11 stsp * 4) environment variables
550 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
551 50b0790e 2020-09-11 stsp */
552 50b0790e 2020-09-11 stsp
553 50b0790e 2020-09-11 stsp if (worktree_conf)
554 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
555 50b0790e 2020-09-11 stsp if (got_author == NULL)
556 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
557 84792843 2019-08-09 stsp if (got_author == NULL) {
558 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
559 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
560 c9956ddf 2019-09-08 stsp if (name && email) {
561 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
562 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
563 c9956ddf 2019-09-08 stsp return NULL;
564 c9956ddf 2019-09-08 stsp }
565 257add31 2020-09-09 stsp
566 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
567 257add31 2020-09-09 stsp if (got_author == NULL) {
568 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
569 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
570 257add31 2020-09-09 stsp repo);
571 257add31 2020-09-09 stsp if (name && email) {
572 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
573 257add31 2020-09-09 stsp == -1)
574 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
575 257add31 2020-09-09 stsp return NULL;
576 257add31 2020-09-09 stsp }
577 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
578 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
579 257add31 2020-09-09 stsp }
580 84792843 2019-08-09 stsp }
581 84792843 2019-08-09 stsp
582 aba9c984 2019-09-08 stsp *author = strdup(got_author);
583 aba9c984 2019-09-08 stsp if (*author == NULL)
584 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
585 84792843 2019-08-09 stsp
586 84792843 2019-08-09 stsp /*
587 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
588 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
589 84792843 2019-08-09 stsp */
590 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
591 84792843 2019-08-09 stsp got_author++;
592 aba9c984 2019-09-08 stsp if (*got_author != '<') {
593 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
594 aba9c984 2019-09-08 stsp goto done;
595 aba9c984 2019-09-08 stsp }
596 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
597 84792843 2019-08-09 stsp got_author++;
598 aba9c984 2019-09-08 stsp if (*got_author != '@') {
599 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
600 aba9c984 2019-09-08 stsp goto done;
601 aba9c984 2019-09-08 stsp }
602 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
603 84792843 2019-08-09 stsp got_author++;
604 84792843 2019-08-09 stsp if (*got_author != '>')
605 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
606 aba9c984 2019-09-08 stsp done:
607 aba9c984 2019-09-08 stsp if (err) {
608 aba9c984 2019-09-08 stsp free(*author);
609 aba9c984 2019-09-08 stsp *author = NULL;
610 aba9c984 2019-09-08 stsp }
611 aba9c984 2019-09-08 stsp return err;
612 c9956ddf 2019-09-08 stsp }
613 c9956ddf 2019-09-08 stsp
614 c9956ddf 2019-09-08 stsp static const struct got_error *
615 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
616 c9956ddf 2019-09-08 stsp {
617 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
618 c9956ddf 2019-09-08 stsp
619 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
620 c9956ddf 2019-09-08 stsp if (homedir) {
621 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
622 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
623 c9956ddf 2019-09-08 stsp
624 c9956ddf 2019-09-08 stsp }
625 c9956ddf 2019-09-08 stsp return NULL;
626 84792843 2019-08-09 stsp }
627 84792843 2019-08-09 stsp
628 84792843 2019-08-09 stsp static const struct got_error *
629 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
630 3ce1b845 2019-07-15 stsp {
631 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
632 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
633 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
634 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
635 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
636 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
637 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
638 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
639 3ce1b845 2019-07-15 stsp int ch;
640 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
641 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
642 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
643 3ce1b845 2019-07-15 stsp
644 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
645 3ce1b845 2019-07-15 stsp
646 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
647 3ce1b845 2019-07-15 stsp switch (ch) {
648 3ce1b845 2019-07-15 stsp case 'b':
649 3ce1b845 2019-07-15 stsp branch_name = optarg;
650 3ce1b845 2019-07-15 stsp break;
651 3ce1b845 2019-07-15 stsp case 'm':
652 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
653 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
654 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
655 3ce1b845 2019-07-15 stsp goto done;
656 3ce1b845 2019-07-15 stsp }
657 3ce1b845 2019-07-15 stsp break;
658 3ce1b845 2019-07-15 stsp case 'r':
659 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
660 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
661 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
662 9ba1d308 2019-10-21 stsp optarg);
663 3ce1b845 2019-07-15 stsp goto done;
664 3ce1b845 2019-07-15 stsp }
665 3ce1b845 2019-07-15 stsp break;
666 3ce1b845 2019-07-15 stsp case 'I':
667 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
668 3ce1b845 2019-07-15 stsp break;
669 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
670 3ce1b845 2019-07-15 stsp NULL);
671 3ce1b845 2019-07-15 stsp if (error)
672 3ce1b845 2019-07-15 stsp goto done;
673 3ce1b845 2019-07-15 stsp break;
674 3ce1b845 2019-07-15 stsp default:
675 b2b65d18 2019-08-22 stsp usage_import();
676 3ce1b845 2019-07-15 stsp /* NOTREACHED */
677 3ce1b845 2019-07-15 stsp }
678 3ce1b845 2019-07-15 stsp }
679 3ce1b845 2019-07-15 stsp
680 3ce1b845 2019-07-15 stsp argc -= optind;
681 3ce1b845 2019-07-15 stsp argv += optind;
682 3ce1b845 2019-07-15 stsp
683 3ce1b845 2019-07-15 stsp #ifndef PROFILE
684 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
685 aba9c984 2019-09-08 stsp "unveil",
686 3ce1b845 2019-07-15 stsp NULL) == -1)
687 3ce1b845 2019-07-15 stsp err(1, "pledge");
688 3ce1b845 2019-07-15 stsp #endif
689 3ce1b845 2019-07-15 stsp if (argc != 1)
690 3ce1b845 2019-07-15 stsp usage_import();
691 2c7829a4 2019-06-17 stsp
692 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
693 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
694 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
695 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
696 3ce1b845 2019-07-15 stsp }
697 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
698 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
699 c9956ddf 2019-09-08 stsp if (error)
700 c9956ddf 2019-09-08 stsp goto done;
701 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
702 3ce1b845 2019-07-15 stsp if (error)
703 3ce1b845 2019-07-15 stsp goto done;
704 aba9c984 2019-09-08 stsp
705 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
706 aba9c984 2019-09-08 stsp if (error)
707 aba9c984 2019-09-08 stsp return error;
708 e560b7e0 2019-11-28 stsp
709 e560b7e0 2019-11-28 stsp /*
710 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
711 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
712 e560b7e0 2019-11-28 stsp * an unintended typo.
713 e560b7e0 2019-11-28 stsp */
714 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
715 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
716 3ce1b845 2019-07-15 stsp
717 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
718 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
719 3ce1b845 2019-07-15 stsp goto done;
720 3ce1b845 2019-07-15 stsp }
721 3ce1b845 2019-07-15 stsp
722 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
723 3ce1b845 2019-07-15 stsp if (error) {
724 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
725 3ce1b845 2019-07-15 stsp goto done;
726 3ce1b845 2019-07-15 stsp } else {
727 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
728 3ce1b845 2019-07-15 stsp "import target branch already exists");
729 3ce1b845 2019-07-15 stsp goto done;
730 3ce1b845 2019-07-15 stsp }
731 3ce1b845 2019-07-15 stsp
732 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
733 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
734 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
735 3ce1b845 2019-07-15 stsp goto done;
736 3ce1b845 2019-07-15 stsp }
737 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
738 3ce1b845 2019-07-15 stsp
739 3ce1b845 2019-07-15 stsp /*
740 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
741 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
742 3ce1b845 2019-07-15 stsp */
743 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
744 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
745 3ce1b845 2019-07-15 stsp if (error)
746 3ce1b845 2019-07-15 stsp goto done;
747 8e158b01 2019-09-22 stsp free(logmsg);
748 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
749 ef293bdd 2019-10-21 stsp path_dir, refname);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
752 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
753 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
754 3ce1b845 2019-07-15 stsp goto done;
755 ef293bdd 2019-10-21 stsp }
756 3ce1b845 2019-07-15 stsp }
757 3ce1b845 2019-07-15 stsp
758 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
759 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
760 ef293bdd 2019-10-21 stsp if (logmsg_path)
761 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
762 3ce1b845 2019-07-15 stsp goto done;
763 ef293bdd 2019-10-21 stsp }
764 3ce1b845 2019-07-15 stsp
765 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
766 ef293bdd 2019-10-21 stsp if (error) {
767 ef293bdd 2019-10-21 stsp if (logmsg_path)
768 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
769 ef293bdd 2019-10-21 stsp goto done;
770 ef293bdd 2019-10-21 stsp }
771 ef293bdd 2019-10-21 stsp
772 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
773 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
774 ef293bdd 2019-10-21 stsp if (error) {
775 ef293bdd 2019-10-21 stsp if (logmsg_path)
776 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
777 3ce1b845 2019-07-15 stsp goto done;
778 ef293bdd 2019-10-21 stsp }
779 3ce1b845 2019-07-15 stsp
780 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
781 ef293bdd 2019-10-21 stsp if (error) {
782 ef293bdd 2019-10-21 stsp if (logmsg_path)
783 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
784 3ce1b845 2019-07-15 stsp goto done;
785 ef293bdd 2019-10-21 stsp }
786 3ce1b845 2019-07-15 stsp
787 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
788 ef293bdd 2019-10-21 stsp if (error) {
789 ef293bdd 2019-10-21 stsp if (logmsg_path)
790 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
791 3ce1b845 2019-07-15 stsp goto done;
792 ef293bdd 2019-10-21 stsp }
793 3ce1b845 2019-07-15 stsp
794 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
795 ef293bdd 2019-10-21 stsp if (error) {
796 ef293bdd 2019-10-21 stsp if (logmsg_path)
797 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
798 3ce1b845 2019-07-15 stsp goto done;
799 ef293bdd 2019-10-21 stsp }
800 3ce1b845 2019-07-15 stsp
801 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
802 3ce1b845 2019-07-15 stsp if (error) {
803 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
804 ef293bdd 2019-10-21 stsp if (logmsg_path)
805 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
806 3ce1b845 2019-07-15 stsp goto done;
807 ef293bdd 2019-10-21 stsp }
808 3ce1b845 2019-07-15 stsp
809 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
810 3ce1b845 2019-07-15 stsp branch_ref);
811 ef293bdd 2019-10-21 stsp if (error) {
812 ef293bdd 2019-10-21 stsp if (logmsg_path)
813 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
814 3ce1b845 2019-07-15 stsp goto done;
815 ef293bdd 2019-10-21 stsp }
816 3ce1b845 2019-07-15 stsp
817 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
818 ef293bdd 2019-10-21 stsp if (error) {
819 ef293bdd 2019-10-21 stsp if (logmsg_path)
820 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
821 3ce1b845 2019-07-15 stsp goto done;
822 ef293bdd 2019-10-21 stsp }
823 3ce1b845 2019-07-15 stsp }
824 3ce1b845 2019-07-15 stsp
825 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
826 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
827 2c7829a4 2019-06-17 stsp done:
828 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
829 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
830 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
831 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
832 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
833 8e158b01 2019-09-22 stsp free(logmsg);
834 ef293bdd 2019-10-21 stsp free(logmsg_path);
835 2c7829a4 2019-06-17 stsp free(repo_path);
836 3ce1b845 2019-07-15 stsp free(editor);
837 3ce1b845 2019-07-15 stsp free(refname);
838 3ce1b845 2019-07-15 stsp free(new_commit_id);
839 3ce1b845 2019-07-15 stsp free(id_str);
840 aba9c984 2019-09-08 stsp free(author);
841 c9956ddf 2019-09-08 stsp free(gitconfig_path);
842 3ce1b845 2019-07-15 stsp if (branch_ref)
843 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
844 3ce1b845 2019-07-15 stsp if (head_ref)
845 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
846 2c7829a4 2019-06-17 stsp return error;
847 93658fb9 2020-03-18 stsp }
848 93658fb9 2020-03-18 stsp
849 93658fb9 2020-03-18 stsp __dead static void
850 93658fb9 2020-03-18 stsp usage_clone(void)
851 93658fb9 2020-03-18 stsp {
852 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
853 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
854 93658fb9 2020-03-18 stsp exit(1);
855 93658fb9 2020-03-18 stsp }
856 892ac3b6 2020-03-18 stsp
857 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
858 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
859 892ac3b6 2020-03-18 stsp int last_p_indexed;
860 892ac3b6 2020-03-18 stsp int last_p_resolved;
861 68999b92 2020-03-18 stsp int verbosity;
862 04d9a9ec 2020-09-24 stsp
863 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
864 04d9a9ec 2020-09-24 stsp
865 04d9a9ec 2020-09-24 stsp int create_configs;
866 04d9a9ec 2020-09-24 stsp int configs_created;
867 04d9a9ec 2020-09-24 stsp struct {
868 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
869 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
870 04d9a9ec 2020-09-24 stsp const char *proto;
871 04d9a9ec 2020-09-24 stsp const char *host;
872 04d9a9ec 2020-09-24 stsp const char *port;
873 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
874 04d9a9ec 2020-09-24 stsp const char *git_url;
875 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
876 04d9a9ec 2020-09-24 stsp int mirror_references;
877 04d9a9ec 2020-09-24 stsp } config_info;
878 892ac3b6 2020-03-18 stsp };
879 93658fb9 2020-03-18 stsp
880 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
881 93658fb9 2020-03-18 stsp static const struct got_error *
882 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
883 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
884 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
885 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo);
886 04d9a9ec 2020-09-24 stsp
887 04d9a9ec 2020-09-24 stsp static const struct got_error *
888 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
889 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
890 531c3985 2020-03-18 stsp {
891 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
892 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
893 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
894 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
895 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
896 04d9a9ec 2020-09-24 stsp
897 04d9a9ec 2020-09-24 stsp /*
898 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
899 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
900 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
901 04d9a9ec 2020-09-24 stsp * we have all required information.
902 04d9a9ec 2020-09-24 stsp */
903 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
904 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
905 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
906 04d9a9ec 2020-09-24 stsp a->config_info.host, a->config_info.port,
907 04d9a9ec 2020-09-24 stsp a->config_info.remote_repo_path,
908 04d9a9ec 2020-09-24 stsp a->config_info.git_url,
909 04d9a9ec 2020-09-24 stsp a->config_info.fetch_all_branches,
910 04d9a9ec 2020-09-24 stsp a->config_info.mirror_references,
911 04d9a9ec 2020-09-24 stsp a->config_info.symrefs,
912 04d9a9ec 2020-09-24 stsp a->config_info.wanted_branches, a->repo);
913 04d9a9ec 2020-09-24 stsp if (err)
914 04d9a9ec 2020-09-24 stsp return err;
915 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
916 04d9a9ec 2020-09-24 stsp }
917 b2409d58 2020-03-18 stsp
918 68999b92 2020-03-18 stsp if (a->verbosity < 0)
919 68999b92 2020-03-18 stsp return NULL;
920 68999b92 2020-03-18 stsp
921 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
922 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
923 892ac3b6 2020-03-18 stsp fflush(stdout);
924 12d1281e 2020-03-19 stsp return NULL;
925 b2409d58 2020-03-18 stsp }
926 b2409d58 2020-03-18 stsp
927 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
928 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
929 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
930 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
931 892ac3b6 2020-03-18 stsp print_size = 1;
932 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
933 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
934 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
935 892ac3b6 2020-03-18 stsp }
936 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
937 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
938 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
939 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
940 892ac3b6 2020-03-18 stsp print_indexed = 1;
941 892ac3b6 2020-03-18 stsp print_size = 1;
942 892ac3b6 2020-03-18 stsp }
943 61cc1a7a 2020-03-18 stsp }
944 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
945 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
946 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
947 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
948 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
949 892ac3b6 2020-03-18 stsp print_resolved = 1;
950 892ac3b6 2020-03-18 stsp print_indexed = 1;
951 892ac3b6 2020-03-18 stsp print_size = 1;
952 892ac3b6 2020-03-18 stsp }
953 61cc1a7a 2020-03-18 stsp }
954 3168e5da 2020-09-10 stsp
955 d2cdc636 2020-03-18 stsp }
956 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
957 892ac3b6 2020-03-18 stsp printf("\r");
958 892ac3b6 2020-03-18 stsp if (print_size)
959 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
960 d715f13e 2020-03-19 stsp if (print_indexed)
961 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
962 d715f13e 2020-03-19 stsp if (print_resolved)
963 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
964 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
965 892ac3b6 2020-03-18 stsp fflush(stdout);
966 892ac3b6 2020-03-18 stsp
967 531c3985 2020-03-18 stsp return NULL;
968 531c3985 2020-03-18 stsp }
969 531c3985 2020-03-18 stsp
970 531c3985 2020-03-18 stsp static const struct got_error *
971 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
972 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
973 4ba14133 2020-03-20 stsp {
974 4ba14133 2020-03-20 stsp const struct got_error *err;
975 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
976 4ba14133 2020-03-20 stsp
977 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
978 4ba14133 2020-03-20 stsp if (err)
979 4ba14133 2020-03-20 stsp return err;
980 4ba14133 2020-03-20 stsp
981 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
982 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
983 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
984 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
985 6338a6a1 2020-03-21 stsp }
986 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
987 4ba14133 2020-03-20 stsp return err;
988 4ba14133 2020-03-20 stsp }
989 4ba14133 2020-03-20 stsp
990 4ba14133 2020-03-20 stsp static const struct got_error *
991 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
992 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
993 41b0de12 2020-03-21 stsp {
994 41b0de12 2020-03-21 stsp const struct got_error *err;
995 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
996 41b0de12 2020-03-21 stsp
997 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
998 41b0de12 2020-03-21 stsp const char *refname = pe->path;
999 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1000 41b0de12 2020-03-21 stsp
1001 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1002 41b0de12 2020-03-21 stsp }
1003 41b0de12 2020-03-21 stsp
1004 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1005 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1006 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1007 41b0de12 2020-03-21 stsp char *id_str;
1008 41b0de12 2020-03-21 stsp
1009 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1010 41b0de12 2020-03-21 stsp if (err)
1011 41b0de12 2020-03-21 stsp return err;
1012 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1013 41b0de12 2020-03-21 stsp free(id_str);
1014 41b0de12 2020-03-21 stsp }
1015 41b0de12 2020-03-21 stsp
1016 41b0de12 2020-03-21 stsp return NULL;
1017 6338a6a1 2020-03-21 stsp }
1018 6338a6a1 2020-03-21 stsp
1019 6338a6a1 2020-03-21 stsp static const struct got_error *
1020 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1021 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1022 6338a6a1 2020-03-21 stsp {
1023 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1024 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1025 6338a6a1 2020-03-21 stsp char *id_str;
1026 6338a6a1 2020-03-21 stsp
1027 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1028 6338a6a1 2020-03-21 stsp if (err)
1029 6338a6a1 2020-03-21 stsp return err;
1030 6338a6a1 2020-03-21 stsp
1031 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1032 6338a6a1 2020-03-21 stsp if (err)
1033 6338a6a1 2020-03-21 stsp goto done;
1034 6338a6a1 2020-03-21 stsp
1035 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1036 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1037 6338a6a1 2020-03-21 stsp
1038 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1039 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1040 6338a6a1 2020-03-21 stsp done:
1041 6338a6a1 2020-03-21 stsp free(id_str);
1042 6338a6a1 2020-03-21 stsp return err;
1043 0e4002ca 2020-03-21 stsp }
1044 0e4002ca 2020-03-21 stsp
1045 0e4002ca 2020-03-21 stsp static int
1046 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1047 0e4002ca 2020-03-21 stsp {
1048 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1049 0e4002ca 2020-03-21 stsp return 0;
1050 0e4002ca 2020-03-21 stsp refname += 5;
1051 0e4002ca 2020-03-21 stsp
1052 0e4002ca 2020-03-21 stsp /*
1053 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1054 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1055 0e4002ca 2020-03-21 stsp */
1056 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1057 0e4002ca 2020-03-21 stsp return 0;
1058 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1059 0e4002ca 2020-03-21 stsp return 0;
1060 0e4002ca 2020-03-21 stsp
1061 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1062 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1063 0e4002ca 2020-03-21 stsp
1064 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1065 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1066 0e4002ca 2020-03-21 stsp return 1;
1067 0e4002ca 2020-03-21 stsp
1068 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1069 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1070 41b0de12 2020-03-21 stsp }
1071 41b0de12 2020-03-21 stsp
1072 0e4002ca 2020-03-21 stsp static int
1073 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1074 0e4002ca 2020-03-21 stsp {
1075 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1076 0e4002ca 2020-03-21 stsp
1077 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1078 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1079 0e4002ca 2020-03-21 stsp return 1;
1080 0e4002ca 2020-03-21 stsp }
1081 0e4002ca 2020-03-21 stsp
1082 0e4002ca 2020-03-21 stsp return 0;
1083 0e4002ca 2020-03-21 stsp }
1084 0e4002ca 2020-03-21 stsp
1085 41b0de12 2020-03-21 stsp static const struct got_error *
1086 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1087 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1088 0e4002ca 2020-03-21 stsp {
1089 0e4002ca 2020-03-21 stsp const struct got_error *err;
1090 0e4002ca 2020-03-21 stsp char *remote_refname;
1091 0e4002ca 2020-03-21 stsp
1092 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1093 0e4002ca 2020-03-21 stsp refname += 5;
1094 0e4002ca 2020-03-21 stsp
1095 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1096 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1097 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1098 0e4002ca 2020-03-21 stsp
1099 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1100 0e4002ca 2020-03-21 stsp free(remote_refname);
1101 7c0b7f42 2020-09-24 stsp return err;
1102 7c0b7f42 2020-09-24 stsp }
1103 7c0b7f42 2020-09-24 stsp
1104 7c0b7f42 2020-09-24 stsp static const struct got_error *
1105 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1106 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, int fetch_all_branches, int mirror_references,
1107 7c0b7f42 2020-09-24 stsp struct got_repository *repo)
1108 7c0b7f42 2020-09-24 stsp {
1109 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1110 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1111 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1112 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1113 7c0b7f42 2020-09-24 stsp ssize_t n;
1114 7c0b7f42 2020-09-24 stsp
1115 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1116 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1117 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1118 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1119 7c0b7f42 2020-09-24 stsp goto done;
1120 7c0b7f42 2020-09-24 stsp }
1121 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1122 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1123 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1124 7c0b7f42 2020-09-24 stsp goto done;
1125 7c0b7f42 2020-09-24 stsp }
1126 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1127 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1128 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1129 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1130 7c0b7f42 2020-09-24 stsp "%s%s%s"
1131 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1132 7c0b7f42 2020-09-24 stsp "%s"
1133 7c0b7f42 2020-09-24 stsp "}\n",
1134 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1135 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1136 7c0b7f42 2020-09-24 stsp remote_repo_path,
1137 7c0b7f42 2020-09-24 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1138 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1139 7c0b7f42 2020-09-24 stsp goto done;
1140 7c0b7f42 2020-09-24 stsp }
1141 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1142 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1143 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1144 7c0b7f42 2020-09-24 stsp goto done;
1145 7c0b7f42 2020-09-24 stsp }
1146 7c0b7f42 2020-09-24 stsp
1147 7c0b7f42 2020-09-24 stsp done:
1148 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1149 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1150 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1151 7c0b7f42 2020-09-24 stsp return err;
1152 7c0b7f42 2020-09-24 stsp }
1153 7c0b7f42 2020-09-24 stsp
1154 7c0b7f42 2020-09-24 stsp static const struct got_error *
1155 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1156 7c0b7f42 2020-09-24 stsp int fetch_all_branches, int mirror_references, struct got_repository *repo)
1157 7c0b7f42 2020-09-24 stsp {
1158 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1159 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1160 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1161 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1162 7c0b7f42 2020-09-24 stsp ssize_t n;
1163 7c0b7f42 2020-09-24 stsp
1164 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1165 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1166 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1167 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1168 7c0b7f42 2020-09-24 stsp goto done;
1169 7c0b7f42 2020-09-24 stsp }
1170 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1171 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1172 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1173 7c0b7f42 2020-09-24 stsp goto done;
1174 7c0b7f42 2020-09-24 stsp }
1175 7c0b7f42 2020-09-24 stsp if (mirror_references) {
1176 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1177 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1178 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1179 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/*:refs/*\n"
1180 7c0b7f42 2020-09-24 stsp "\tmirror = true\n",
1181 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1182 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1183 7c0b7f42 2020-09-24 stsp goto done;
1184 7c0b7f42 2020-09-24 stsp }
1185 7c0b7f42 2020-09-24 stsp } else if (fetch_all_branches) {
1186 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1187 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1188 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1189 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1190 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1191 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1192 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1193 7c0b7f42 2020-09-24 stsp goto done;
1194 7c0b7f42 2020-09-24 stsp }
1195 7c0b7f42 2020-09-24 stsp } else {
1196 7c0b7f42 2020-09-24 stsp const char *branchname;
1197 7c0b7f42 2020-09-24 stsp
1198 7c0b7f42 2020-09-24 stsp /*
1199 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1200 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1201 7c0b7f42 2020-09-24 stsp */
1202 04d9a9ec 2020-09-24 stsp if (default_branch) {
1203 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1204 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1205 7c0b7f42 2020-09-24 stsp branchname += 11;
1206 7c0b7f42 2020-09-24 stsp } else
1207 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1208 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1209 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1210 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1211 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1212 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1213 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1214 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1215 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1216 7c0b7f42 2020-09-24 stsp goto done;
1217 7c0b7f42 2020-09-24 stsp }
1218 7c0b7f42 2020-09-24 stsp }
1219 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1220 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1221 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1222 7c0b7f42 2020-09-24 stsp goto done;
1223 7c0b7f42 2020-09-24 stsp }
1224 7c0b7f42 2020-09-24 stsp done:
1225 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1226 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1227 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1228 0e4002ca 2020-03-21 stsp return err;
1229 0e4002ca 2020-03-21 stsp }
1230 0e4002ca 2020-03-21 stsp
1231 0e4002ca 2020-03-21 stsp static const struct got_error *
1232 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1233 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1234 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1235 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo)
1236 04d9a9ec 2020-09-24 stsp {
1237 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1238 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1239 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1240 04d9a9ec 2020-09-24 stsp
1241 04d9a9ec 2020-09-24 stsp /*
1242 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1243 04d9a9ec 2020-09-24 stsp * one of those.
1244 04d9a9ec 2020-09-24 stsp */
1245 04d9a9ec 2020-09-24 stsp if (!TAILQ_EMPTY(wanted_branches)) {
1246 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1247 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1248 04d9a9ec 2020-09-24 stsp } else {
1249 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1250 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1251 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1252 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1253 04d9a9ec 2020-09-24 stsp
1254 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1255 04d9a9ec 2020-09-24 stsp continue;
1256 04d9a9ec 2020-09-24 stsp
1257 04d9a9ec 2020-09-24 stsp default_branch = target;
1258 04d9a9ec 2020-09-24 stsp break;
1259 04d9a9ec 2020-09-24 stsp }
1260 04d9a9ec 2020-09-24 stsp }
1261 04d9a9ec 2020-09-24 stsp
1262 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1263 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1264 04d9a9ec 2020-09-24 stsp fetch_all_branches, mirror_references, repo);
1265 04d9a9ec 2020-09-24 stsp if (err)
1266 04d9a9ec 2020-09-24 stsp return err;
1267 04d9a9ec 2020-09-24 stsp
1268 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1269 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1270 04d9a9ec 2020-09-24 stsp mirror_references, repo);
1271 04d9a9ec 2020-09-24 stsp }
1272 04d9a9ec 2020-09-24 stsp
1273 04d9a9ec 2020-09-24 stsp static const struct got_error *
1274 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1275 93658fb9 2020-03-18 stsp {
1276 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1277 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1278 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1279 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1280 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1281 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1282 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1283 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1284 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1285 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1286 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1287 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1288 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1289 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1290 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1291 93658fb9 2020-03-18 stsp
1292 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1293 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1294 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1295 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1296 d9b4d0c0 2020-03-18 stsp
1297 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1298 93658fb9 2020-03-18 stsp switch (ch) {
1299 659e7fbd 2020-03-20 stsp case 'a':
1300 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1301 4ba14133 2020-03-20 stsp break;
1302 4ba14133 2020-03-20 stsp case 'b':
1303 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1304 4ba14133 2020-03-20 stsp optarg, NULL);
1305 4ba14133 2020-03-20 stsp if (error)
1306 4ba14133 2020-03-20 stsp return error;
1307 659e7fbd 2020-03-20 stsp break;
1308 41b0de12 2020-03-21 stsp case 'l':
1309 41b0de12 2020-03-21 stsp list_refs_only = 1;
1310 41b0de12 2020-03-21 stsp break;
1311 469dd726 2020-03-20 stsp case 'm':
1312 469dd726 2020-03-20 stsp mirror_references = 1;
1313 469dd726 2020-03-20 stsp break;
1314 68999b92 2020-03-18 stsp case 'v':
1315 68999b92 2020-03-18 stsp if (verbosity < 0)
1316 68999b92 2020-03-18 stsp verbosity = 0;
1317 68999b92 2020-03-18 stsp else if (verbosity < 3)
1318 68999b92 2020-03-18 stsp verbosity++;
1319 68999b92 2020-03-18 stsp break;
1320 68999b92 2020-03-18 stsp case 'q':
1321 68999b92 2020-03-18 stsp verbosity = -1;
1322 68999b92 2020-03-18 stsp break;
1323 0e4002ca 2020-03-21 stsp case 'R':
1324 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1325 0e4002ca 2020-03-21 stsp optarg, NULL);
1326 0e4002ca 2020-03-21 stsp if (error)
1327 0e4002ca 2020-03-21 stsp return error;
1328 0e4002ca 2020-03-21 stsp break;
1329 93658fb9 2020-03-18 stsp default:
1330 93658fb9 2020-03-18 stsp usage_clone();
1331 93658fb9 2020-03-18 stsp break;
1332 93658fb9 2020-03-18 stsp }
1333 93658fb9 2020-03-18 stsp }
1334 93658fb9 2020-03-18 stsp argc -= optind;
1335 93658fb9 2020-03-18 stsp argv += optind;
1336 39c64a6a 2020-03-18 stsp
1337 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1338 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1339 41b0de12 2020-03-21 stsp if (list_refs_only) {
1340 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1341 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1342 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1343 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1344 41b0de12 2020-03-21 stsp if (mirror_references)
1345 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1346 41b0de12 2020-03-21 stsp if (verbosity == -1)
1347 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1348 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1349 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1350 41b0de12 2020-03-21 stsp }
1351 4ba14133 2020-03-20 stsp
1352 93658fb9 2020-03-18 stsp uri = argv[0];
1353 9df6f38b 2020-03-18 stsp
1354 9df6f38b 2020-03-18 stsp if (argc == 1)
1355 93658fb9 2020-03-18 stsp dirname = NULL;
1356 9df6f38b 2020-03-18 stsp else if (argc == 2)
1357 93658fb9 2020-03-18 stsp dirname = argv[1];
1358 93658fb9 2020-03-18 stsp else
1359 93658fb9 2020-03-18 stsp usage_clone();
1360 09838ffc 2020-03-18 stsp
1361 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1362 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1363 39c64a6a 2020-03-18 stsp if (error)
1364 09f63084 2020-03-20 stsp goto done;
1365 09f63084 2020-03-20 stsp
1366 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1367 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1368 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1369 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1370 09838ffc 2020-03-18 stsp goto done;
1371 09f63084 2020-03-20 stsp }
1372 09838ffc 2020-03-18 stsp
1373 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1374 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1375 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1376 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1377 39c64a6a 2020-03-18 stsp err(1, "pledge");
1378 b46f3e71 2020-03-18 stsp #endif
1379 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1380 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1381 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1382 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1383 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1384 39c64a6a 2020-03-18 stsp err(1, "pledge");
1385 b46f3e71 2020-03-18 stsp #endif
1386 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1387 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1388 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1389 39c64a6a 2020-03-18 stsp goto done;
1390 39c64a6a 2020-03-18 stsp } else {
1391 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1392 39c64a6a 2020-03-18 stsp goto done;
1393 39c64a6a 2020-03-18 stsp }
1394 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1395 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1396 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1397 bb64b798 2020-03-18 stsp goto done;
1398 bb64b798 2020-03-18 stsp }
1399 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1400 bb64b798 2020-03-18 stsp } else
1401 bb64b798 2020-03-18 stsp repo_path = dirname;
1402 bb64b798 2020-03-18 stsp
1403 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1404 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1405 2751fe64 2020-09-24 stsp if (error &&
1406 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1407 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1408 41b0de12 2020-03-21 stsp goto done;
1409 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1410 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1411 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1412 41b0de12 2020-03-21 stsp goto done;
1413 2751fe64 2020-09-24 stsp }
1414 41b0de12 2020-03-21 stsp }
1415 bb64b798 2020-03-18 stsp
1416 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1417 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1418 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1419 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1420 ee448f5f 2020-03-18 stsp goto done;
1421 ee448f5f 2020-03-18 stsp }
1422 ee448f5f 2020-03-18 stsp }
1423 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1424 ee448f5f 2020-03-18 stsp if (error)
1425 ee448f5f 2020-03-18 stsp goto done;
1426 f79e6490 2020-04-19 stsp
1427 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1428 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1429 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1430 ee448f5f 2020-03-18 stsp
1431 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1432 9c52365f 2020-03-21 stsp server_path, verbosity);
1433 39c64a6a 2020-03-18 stsp if (error)
1434 bb64b798 2020-03-18 stsp goto done;
1435 bb64b798 2020-03-18 stsp
1436 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1437 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1438 2751fe64 2020-09-24 stsp if (error)
1439 2751fe64 2020-09-24 stsp goto done;
1440 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1441 2751fe64 2020-09-24 stsp if (error)
1442 2751fe64 2020-09-24 stsp goto done;
1443 2751fe64 2020-09-24 stsp }
1444 2751fe64 2020-09-24 stsp
1445 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1446 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1447 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1448 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1449 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1450 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1451 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1452 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1453 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1454 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1455 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1456 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1457 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1458 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1459 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1460 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1461 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1462 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1463 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1464 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1465 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1466 39c64a6a 2020-03-18 stsp if (error)
1467 d9b4d0c0 2020-03-18 stsp goto done;
1468 d9b4d0c0 2020-03-18 stsp
1469 41b0de12 2020-03-21 stsp if (list_refs_only) {
1470 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1471 41b0de12 2020-03-21 stsp goto done;
1472 41b0de12 2020-03-21 stsp }
1473 41b0de12 2020-03-21 stsp
1474 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1475 39c64a6a 2020-03-18 stsp if (error)
1476 d9b4d0c0 2020-03-18 stsp goto done;
1477 68999b92 2020-03-18 stsp if (verbosity >= 0)
1478 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1479 d9b4d0c0 2020-03-18 stsp free(id_str);
1480 d9b4d0c0 2020-03-18 stsp
1481 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1482 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1483 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1484 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1485 7ebc0570 2020-03-18 stsp char *remote_refname;
1486 0e4002ca 2020-03-21 stsp
1487 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1488 0e4002ca 2020-03-21 stsp !mirror_references) {
1489 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1490 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1491 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1492 0e4002ca 2020-03-21 stsp if (error)
1493 0e4002ca 2020-03-21 stsp goto done;
1494 0e4002ca 2020-03-21 stsp continue;
1495 0e4002ca 2020-03-21 stsp }
1496 668a20f6 2020-03-18 stsp
1497 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1498 39c64a6a 2020-03-18 stsp if (error)
1499 d9b4d0c0 2020-03-18 stsp goto done;
1500 d9b4d0c0 2020-03-18 stsp
1501 469dd726 2020-03-20 stsp if (mirror_references)
1502 469dd726 2020-03-20 stsp continue;
1503 469dd726 2020-03-20 stsp
1504 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1505 7ebc0570 2020-03-18 stsp continue;
1506 7ebc0570 2020-03-18 stsp
1507 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1508 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1509 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1510 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1511 7ebc0570 2020-03-18 stsp goto done;
1512 7ebc0570 2020-03-18 stsp }
1513 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1514 f298ae0f 2020-03-25 stsp free(remote_refname);
1515 39c64a6a 2020-03-18 stsp if (error)
1516 d9b4d0c0 2020-03-18 stsp goto done;
1517 d9b4d0c0 2020-03-18 stsp }
1518 d9b4d0c0 2020-03-18 stsp
1519 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1520 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1521 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1522 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1523 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1524 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1525 d9b4d0c0 2020-03-18 stsp
1526 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1527 d9b4d0c0 2020-03-18 stsp continue;
1528 d9b4d0c0 2020-03-18 stsp
1529 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1530 39c64a6a 2020-03-18 stsp if (error) {
1531 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1532 55330abe 2020-03-20 stsp error = NULL;
1533 d9b4d0c0 2020-03-18 stsp continue;
1534 55330abe 2020-03-20 stsp }
1535 d9b4d0c0 2020-03-18 stsp goto done;
1536 d9b4d0c0 2020-03-18 stsp }
1537 d9b4d0c0 2020-03-18 stsp
1538 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1539 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1540 f298ae0f 2020-03-25 stsp if (error)
1541 f298ae0f 2020-03-25 stsp goto done;
1542 f298ae0f 2020-03-25 stsp
1543 f298ae0f 2020-03-25 stsp if (mirror_references)
1544 f298ae0f 2020-03-25 stsp continue;
1545 f298ae0f 2020-03-25 stsp
1546 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1547 f298ae0f 2020-03-25 stsp continue;
1548 f298ae0f 2020-03-25 stsp
1549 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1550 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1551 f298ae0f 2020-03-25 stsp refname) == -1) {
1552 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1553 f298ae0f 2020-03-25 stsp goto done;
1554 f298ae0f 2020-03-25 stsp }
1555 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1556 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1557 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1558 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1559 f298ae0f 2020-03-25 stsp free(remote_refname);
1560 f298ae0f 2020-03-25 stsp goto done;
1561 f298ae0f 2020-03-25 stsp }
1562 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1563 f298ae0f 2020-03-25 stsp if (error) {
1564 f298ae0f 2020-03-25 stsp free(remote_refname);
1565 f298ae0f 2020-03-25 stsp free(remote_target);
1566 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1567 f298ae0f 2020-03-25 stsp error = NULL;
1568 f298ae0f 2020-03-25 stsp continue;
1569 f298ae0f 2020-03-25 stsp }
1570 f298ae0f 2020-03-25 stsp goto done;
1571 f298ae0f 2020-03-25 stsp }
1572 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1573 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1574 f298ae0f 2020-03-25 stsp free(remote_refname);
1575 f298ae0f 2020-03-25 stsp free(remote_target);
1576 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1577 39c64a6a 2020-03-18 stsp if (error)
1578 d9b4d0c0 2020-03-18 stsp goto done;
1579 4ba14133 2020-03-20 stsp }
1580 4ba14133 2020-03-20 stsp if (pe == NULL) {
1581 4ba14133 2020-03-20 stsp /*
1582 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1583 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1584 4ba14133 2020-03-20 stsp * which could be fetched instead.
1585 4ba14133 2020-03-20 stsp */
1586 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1587 4ba14133 2020-03-20 stsp const char *target = pe->path;
1588 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1589 d9b4d0c0 2020-03-18 stsp
1590 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1591 4ba14133 2020-03-20 stsp if (error) {
1592 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1593 4ba14133 2020-03-20 stsp error = NULL;
1594 4ba14133 2020-03-20 stsp continue;
1595 4ba14133 2020-03-20 stsp }
1596 4ba14133 2020-03-20 stsp goto done;
1597 4ba14133 2020-03-20 stsp }
1598 4ba14133 2020-03-20 stsp
1599 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1600 04d9a9ec 2020-09-24 stsp verbosity, repo);
1601 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1602 4ba14133 2020-03-20 stsp if (error)
1603 4ba14133 2020-03-20 stsp goto done;
1604 4ba14133 2020-03-20 stsp break;
1605 4ba14133 2020-03-20 stsp }
1606 659e7fbd 2020-03-20 stsp }
1607 659e7fbd 2020-03-20 stsp
1608 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1609 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1610 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1611 09838ffc 2020-03-18 stsp done:
1612 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1613 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1614 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1615 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1616 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1617 9c52365f 2020-03-21 stsp }
1618 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1619 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1620 bb64b798 2020-03-18 stsp if (repo)
1621 bb64b798 2020-03-18 stsp got_repo_close(repo);
1622 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1623 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1624 d9b4d0c0 2020-03-18 stsp free(pe->data);
1625 d9b4d0c0 2020-03-18 stsp }
1626 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1627 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1628 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1629 d9b4d0c0 2020-03-18 stsp free(pe->data);
1630 d9b4d0c0 2020-03-18 stsp }
1631 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1632 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1633 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1634 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1635 09838ffc 2020-03-18 stsp free(proto);
1636 09838ffc 2020-03-18 stsp free(host);
1637 09838ffc 2020-03-18 stsp free(port);
1638 09838ffc 2020-03-18 stsp free(server_path);
1639 09838ffc 2020-03-18 stsp free(repo_name);
1640 bb64b798 2020-03-18 stsp free(default_destdir);
1641 b46f3e71 2020-03-18 stsp free(git_url);
1642 39c64a6a 2020-03-18 stsp return error;
1643 7848a0e1 2020-03-19 stsp }
1644 7848a0e1 2020-03-19 stsp
1645 7848a0e1 2020-03-19 stsp static const struct got_error *
1646 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1647 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1648 7848a0e1 2020-03-19 stsp {
1649 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1650 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1651 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1652 7848a0e1 2020-03-19 stsp
1653 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1654 7848a0e1 2020-03-19 stsp if (err)
1655 7848a0e1 2020-03-19 stsp goto done;
1656 7848a0e1 2020-03-19 stsp
1657 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1658 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1659 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1660 88609724 2020-03-21 stsp if (err)
1661 88609724 2020-03-21 stsp goto done;
1662 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1663 88609724 2020-03-21 stsp goto done;
1664 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1665 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1666 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1667 db6d8ad8 2020-03-21 stsp }
1668 db6d8ad8 2020-03-21 stsp goto done;
1669 db6d8ad8 2020-03-21 stsp }
1670 db6d8ad8 2020-03-21 stsp
1671 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1672 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1673 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1674 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1675 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1676 688f11b3 2020-03-21 stsp }
1677 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1678 7848a0e1 2020-03-19 stsp if (err)
1679 7848a0e1 2020-03-19 stsp goto done;
1680 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1681 e8a967e0 2020-03-21 stsp if (err)
1682 e8a967e0 2020-03-21 stsp goto done;
1683 7848a0e1 2020-03-19 stsp } else {
1684 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1685 7848a0e1 2020-03-19 stsp if (err)
1686 7848a0e1 2020-03-19 stsp goto done;
1687 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1688 6338a6a1 2020-03-21 stsp goto done;
1689 6338a6a1 2020-03-21 stsp
1690 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1691 6338a6a1 2020-03-21 stsp if (err)
1692 6338a6a1 2020-03-21 stsp goto done;
1693 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1694 6338a6a1 2020-03-21 stsp if (err)
1695 6338a6a1 2020-03-21 stsp goto done;
1696 7848a0e1 2020-03-19 stsp }
1697 6338a6a1 2020-03-21 stsp
1698 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1699 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1700 6338a6a1 2020-03-21 stsp new_id_str);
1701 7848a0e1 2020-03-19 stsp done:
1702 7848a0e1 2020-03-19 stsp free(old_id);
1703 7848a0e1 2020-03-19 stsp free(new_id_str);
1704 7848a0e1 2020-03-19 stsp return err;
1705 2ab43947 2020-03-18 stsp }
1706 f1bcca34 2020-03-25 stsp
1707 f1bcca34 2020-03-25 stsp static const struct got_error *
1708 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1709 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1710 f1bcca34 2020-03-25 stsp {
1711 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1712 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1713 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1714 f1bcca34 2020-03-25 stsp
1715 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1716 bcf34b0e 2020-03-26 stsp if (err) {
1717 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1718 bcf34b0e 2020-03-26 stsp return err;
1719 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1720 bcf34b0e 2020-03-26 stsp if (err)
1721 bcf34b0e 2020-03-26 stsp goto done;
1722 2ab43947 2020-03-18 stsp
1723 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1724 bcf34b0e 2020-03-26 stsp if (err)
1725 bcf34b0e 2020-03-26 stsp goto done;
1726 f1bcca34 2020-03-25 stsp
1727 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1728 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1729 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1730 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1731 bcf34b0e 2020-03-26 stsp } else {
1732 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1733 f1bcca34 2020-03-25 stsp
1734 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1735 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1736 bcf34b0e 2020-03-26 stsp goto done;
1737 bcf34b0e 2020-03-26 stsp
1738 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1739 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1740 bcf34b0e 2020-03-26 stsp if (err)
1741 bcf34b0e 2020-03-26 stsp goto done;
1742 bcf34b0e 2020-03-26 stsp
1743 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1744 bcf34b0e 2020-03-26 stsp if (err)
1745 bcf34b0e 2020-03-26 stsp goto done;
1746 bcf34b0e 2020-03-26 stsp
1747 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1748 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1749 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1750 bcf34b0e 2020-03-26 stsp
1751 bcf34b0e 2020-03-26 stsp }
1752 f1bcca34 2020-03-25 stsp done:
1753 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1754 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1755 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1756 bcf34b0e 2020-03-26 stsp err = unlock_err;
1757 bcf34b0e 2020-03-26 stsp }
1758 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1759 f1bcca34 2020-03-25 stsp return err;
1760 f1bcca34 2020-03-25 stsp }
1761 f1bcca34 2020-03-25 stsp
1762 2ab43947 2020-03-18 stsp __dead static void
1763 7848a0e1 2020-03-19 stsp usage_fetch(void)
1764 7848a0e1 2020-03-19 stsp {
1765 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1766 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1767 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1768 13f12b09 2020-03-21 stsp getprogname());
1769 7848a0e1 2020-03-19 stsp exit(1);
1770 7848a0e1 2020-03-19 stsp }
1771 7848a0e1 2020-03-19 stsp
1772 7848a0e1 2020-03-19 stsp static const struct got_error *
1773 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1774 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1775 f21ec2f0 2020-03-21 stsp {
1776 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1777 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1778 3789fd73 2020-03-26 stsp char *id_str = NULL;
1779 3789fd73 2020-03-26 stsp
1780 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1781 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1782 3789fd73 2020-03-26 stsp if (err)
1783 3789fd73 2020-03-26 stsp return err;
1784 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1785 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1786 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1787 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1788 3789fd73 2020-03-26 stsp }
1789 3789fd73 2020-03-26 stsp } else {
1790 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1791 3789fd73 2020-03-26 stsp if (err)
1792 3789fd73 2020-03-26 stsp return err;
1793 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1794 3789fd73 2020-03-26 stsp if (err)
1795 3789fd73 2020-03-26 stsp goto done;
1796 3168e5da 2020-09-10 stsp
1797 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1798 3789fd73 2020-03-26 stsp if (err)
1799 3789fd73 2020-03-26 stsp goto done;
1800 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1801 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1802 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1803 3789fd73 2020-03-26 stsp }
1804 3789fd73 2020-03-26 stsp }
1805 3789fd73 2020-03-26 stsp done:
1806 3789fd73 2020-03-26 stsp free(id);
1807 3789fd73 2020-03-26 stsp free(id_str);
1808 3789fd73 2020-03-26 stsp return NULL;
1809 3789fd73 2020-03-26 stsp }
1810 3789fd73 2020-03-26 stsp
1811 3789fd73 2020-03-26 stsp static const struct got_error *
1812 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1813 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1814 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1815 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1816 3789fd73 2020-03-26 stsp {
1817 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1818 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1819 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1820 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1821 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1822 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1823 f21ec2f0 2020-03-21 stsp
1824 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1825 f21ec2f0 2020-03-21 stsp
1826 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1827 3789fd73 2020-03-26 stsp == -1)
1828 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1829 3789fd73 2020-03-26 stsp
1830 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1831 f21ec2f0 2020-03-21 stsp if (err)
1832 3789fd73 2020-03-26 stsp goto done;
1833 f21ec2f0 2020-03-21 stsp
1834 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1835 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1836 f21ec2f0 2020-03-21 stsp
1837 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1838 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1839 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1840 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1841 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1842 3789fd73 2020-03-26 stsp continue;
1843 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1844 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1845 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1846 3789fd73 2020-03-26 stsp goto done;
1847 3789fd73 2020-03-26 stsp }
1848 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1849 3789fd73 2020-03-26 stsp continue;
1850 3789fd73 2020-03-26 stsp }
1851 f21ec2f0 2020-03-21 stsp
1852 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1853 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1854 f21ec2f0 2020-03-21 stsp break;
1855 f21ec2f0 2020-03-21 stsp }
1856 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1857 f21ec2f0 2020-03-21 stsp continue;
1858 f21ec2f0 2020-03-21 stsp
1859 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1860 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1861 3789fd73 2020-03-26 stsp break;
1862 3789fd73 2020-03-26 stsp }
1863 3789fd73 2020-03-26 stsp if (pe != NULL)
1864 3789fd73 2020-03-26 stsp continue;
1865 f21ec2f0 2020-03-21 stsp
1866 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1867 f21ec2f0 2020-03-21 stsp if (err)
1868 f21ec2f0 2020-03-21 stsp break;
1869 3789fd73 2020-03-26 stsp
1870 3789fd73 2020-03-26 stsp if (local_refname) {
1871 3789fd73 2020-03-26 stsp struct got_reference *ref;
1872 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1873 3789fd73 2020-03-26 stsp if (err) {
1874 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1875 3789fd73 2020-03-26 stsp break;
1876 3789fd73 2020-03-26 stsp free(local_refname);
1877 3789fd73 2020-03-26 stsp local_refname = NULL;
1878 3789fd73 2020-03-26 stsp continue;
1879 3789fd73 2020-03-26 stsp }
1880 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1881 3789fd73 2020-03-26 stsp if (err)
1882 3789fd73 2020-03-26 stsp break;
1883 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1884 3789fd73 2020-03-26 stsp got_ref_close(ref);
1885 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1886 3789fd73 2020-03-26 stsp err = unlock_err;
1887 3789fd73 2020-03-26 stsp break;
1888 3789fd73 2020-03-26 stsp }
1889 3789fd73 2020-03-26 stsp
1890 3789fd73 2020-03-26 stsp free(local_refname);
1891 3789fd73 2020-03-26 stsp local_refname = NULL;
1892 6338a6a1 2020-03-21 stsp }
1893 f21ec2f0 2020-03-21 stsp }
1894 3789fd73 2020-03-26 stsp done:
1895 3789fd73 2020-03-26 stsp free(remote_namespace);
1896 3789fd73 2020-03-26 stsp free(local_refname);
1897 0e4002ca 2020-03-21 stsp return err;
1898 0e4002ca 2020-03-21 stsp }
1899 0e4002ca 2020-03-21 stsp
1900 0e4002ca 2020-03-21 stsp static const struct got_error *
1901 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1902 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1903 0e4002ca 2020-03-21 stsp {
1904 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1905 0e4002ca 2020-03-21 stsp char *remote_refname;
1906 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1907 0e4002ca 2020-03-21 stsp
1908 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1909 0e4002ca 2020-03-21 stsp refname += 5;
1910 0e4002ca 2020-03-21 stsp
1911 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1912 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1913 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1914 f21ec2f0 2020-03-21 stsp
1915 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1916 0e4002ca 2020-03-21 stsp if (err) {
1917 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1918 0e4002ca 2020-03-21 stsp goto done;
1919 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1920 0e4002ca 2020-03-21 stsp } else {
1921 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1922 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1923 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1924 9f142382 2020-03-21 stsp err = unlock_err;
1925 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1926 0e4002ca 2020-03-21 stsp }
1927 0e4002ca 2020-03-21 stsp done:
1928 0e4002ca 2020-03-21 stsp free(remote_refname);
1929 f21ec2f0 2020-03-21 stsp return err;
1930 f21ec2f0 2020-03-21 stsp }
1931 f21ec2f0 2020-03-21 stsp
1932 f21ec2f0 2020-03-21 stsp static const struct got_error *
1933 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1934 7848a0e1 2020-03-19 stsp {
1935 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1936 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1937 7848a0e1 2020-03-19 stsp const char *remote_name;
1938 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1939 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1940 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1941 7848a0e1 2020-03-19 stsp int nremotes;
1942 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1943 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1944 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1945 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1946 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1947 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1948 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1949 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1950 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1951 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1952 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1953 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1954 7848a0e1 2020-03-19 stsp
1955 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1956 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1957 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1958 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1959 7848a0e1 2020-03-19 stsp
1960 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1961 7848a0e1 2020-03-19 stsp switch (ch) {
1962 659e7fbd 2020-03-20 stsp case 'a':
1963 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1964 4ba14133 2020-03-20 stsp break;
1965 4ba14133 2020-03-20 stsp case 'b':
1966 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1967 4ba14133 2020-03-20 stsp optarg, NULL);
1968 4ba14133 2020-03-20 stsp if (error)
1969 4ba14133 2020-03-20 stsp return error;
1970 41b0de12 2020-03-21 stsp break;
1971 f21ec2f0 2020-03-21 stsp case 'd':
1972 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1973 f21ec2f0 2020-03-21 stsp break;
1974 41b0de12 2020-03-21 stsp case 'l':
1975 41b0de12 2020-03-21 stsp list_refs_only = 1;
1976 659e7fbd 2020-03-20 stsp break;
1977 7848a0e1 2020-03-19 stsp case 'r':
1978 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1979 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1980 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1981 7848a0e1 2020-03-19 stsp optarg);
1982 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1983 7848a0e1 2020-03-19 stsp break;
1984 db6d8ad8 2020-03-21 stsp case 't':
1985 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1986 db6d8ad8 2020-03-21 stsp break;
1987 7848a0e1 2020-03-19 stsp case 'v':
1988 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1989 7848a0e1 2020-03-19 stsp verbosity = 0;
1990 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1991 7848a0e1 2020-03-19 stsp verbosity++;
1992 7848a0e1 2020-03-19 stsp break;
1993 7848a0e1 2020-03-19 stsp case 'q':
1994 7848a0e1 2020-03-19 stsp verbosity = -1;
1995 7848a0e1 2020-03-19 stsp break;
1996 0e4002ca 2020-03-21 stsp case 'R':
1997 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1998 0e4002ca 2020-03-21 stsp optarg, NULL);
1999 0e4002ca 2020-03-21 stsp if (error)
2000 0e4002ca 2020-03-21 stsp return error;
2001 0e4002ca 2020-03-21 stsp break;
2002 7848a0e1 2020-03-19 stsp default:
2003 7848a0e1 2020-03-19 stsp usage_fetch();
2004 7848a0e1 2020-03-19 stsp break;
2005 7848a0e1 2020-03-19 stsp }
2006 7848a0e1 2020-03-19 stsp }
2007 7848a0e1 2020-03-19 stsp argc -= optind;
2008 7848a0e1 2020-03-19 stsp argv += optind;
2009 7848a0e1 2020-03-19 stsp
2010 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2011 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
2012 41b0de12 2020-03-21 stsp if (list_refs_only) {
2013 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2014 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
2015 41b0de12 2020-03-21 stsp if (fetch_all_branches)
2016 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
2017 f21ec2f0 2020-03-21 stsp if (delete_refs)
2018 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
2019 41b0de12 2020-03-21 stsp if (verbosity == -1)
2020 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
2021 41b0de12 2020-03-21 stsp }
2022 4ba14133 2020-03-20 stsp
2023 7848a0e1 2020-03-19 stsp if (argc == 0)
2024 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2025 7848a0e1 2020-03-19 stsp else if (argc == 1)
2026 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2027 7848a0e1 2020-03-19 stsp else
2028 7848a0e1 2020-03-19 stsp usage_fetch();
2029 7848a0e1 2020-03-19 stsp
2030 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2031 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2032 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2033 7848a0e1 2020-03-19 stsp goto done;
2034 7848a0e1 2020-03-19 stsp }
2035 7848a0e1 2020-03-19 stsp
2036 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2037 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2038 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2039 7848a0e1 2020-03-19 stsp goto done;
2040 7848a0e1 2020-03-19 stsp else
2041 7848a0e1 2020-03-19 stsp error = NULL;
2042 7848a0e1 2020-03-19 stsp if (worktree) {
2043 7848a0e1 2020-03-19 stsp repo_path =
2044 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2045 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2046 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2047 7848a0e1 2020-03-19 stsp if (error)
2048 7848a0e1 2020-03-19 stsp goto done;
2049 7848a0e1 2020-03-19 stsp } else {
2050 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2051 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2052 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2053 7848a0e1 2020-03-19 stsp goto done;
2054 7848a0e1 2020-03-19 stsp }
2055 7848a0e1 2020-03-19 stsp }
2056 7848a0e1 2020-03-19 stsp }
2057 7848a0e1 2020-03-19 stsp
2058 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2059 7848a0e1 2020-03-19 stsp if (error)
2060 7848a0e1 2020-03-19 stsp goto done;
2061 7848a0e1 2020-03-19 stsp
2062 50b0790e 2020-09-11 stsp if (worktree) {
2063 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2064 50b0790e 2020-09-11 stsp if (worktree_conf) {
2065 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2066 50b0790e 2020-09-11 stsp worktree_conf);
2067 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2068 50b0790e 2020-09-11 stsp remote = &remotes[i];
2069 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2070 50b0790e 2020-09-11 stsp break;
2071 50b0790e 2020-09-11 stsp }
2072 50b0790e 2020-09-11 stsp }
2073 7848a0e1 2020-03-19 stsp }
2074 50b0790e 2020-09-11 stsp if (remote == NULL) {
2075 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2076 50b0790e 2020-09-11 stsp if (repo_conf) {
2077 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2078 50b0790e 2020-09-11 stsp repo_conf);
2079 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2080 50b0790e 2020-09-11 stsp remote = &remotes[i];
2081 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2082 50b0790e 2020-09-11 stsp break;
2083 50b0790e 2020-09-11 stsp }
2084 50b0790e 2020-09-11 stsp }
2085 50b0790e 2020-09-11 stsp }
2086 50b0790e 2020-09-11 stsp if (remote == NULL) {
2087 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2088 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2089 257add31 2020-09-09 stsp remote = &remotes[i];
2090 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
2091 257add31 2020-09-09 stsp break;
2092 257add31 2020-09-09 stsp }
2093 7848a0e1 2020-03-19 stsp }
2094 50b0790e 2020-09-11 stsp if (remote == NULL) {
2095 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2096 50b0790e 2020-09-11 stsp goto done;
2097 50b0790e 2020-09-11 stsp }
2098 7848a0e1 2020-03-19 stsp
2099 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2100 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2101 7848a0e1 2020-03-19 stsp if (error)
2102 7848a0e1 2020-03-19 stsp goto done;
2103 7848a0e1 2020-03-19 stsp
2104 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2105 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2106 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2107 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2108 7848a0e1 2020-03-19 stsp err(1, "pledge");
2109 7848a0e1 2020-03-19 stsp #endif
2110 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2111 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2112 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2113 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2114 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2115 7848a0e1 2020-03-19 stsp err(1, "pledge");
2116 7848a0e1 2020-03-19 stsp #endif
2117 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2118 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2119 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2120 7848a0e1 2020-03-19 stsp goto done;
2121 7848a0e1 2020-03-19 stsp } else {
2122 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2123 7848a0e1 2020-03-19 stsp goto done;
2124 7848a0e1 2020-03-19 stsp }
2125 7848a0e1 2020-03-19 stsp
2126 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2127 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2128 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2129 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2130 7848a0e1 2020-03-19 stsp goto done;
2131 7848a0e1 2020-03-19 stsp }
2132 7848a0e1 2020-03-19 stsp }
2133 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2134 7848a0e1 2020-03-19 stsp if (error)
2135 7848a0e1 2020-03-19 stsp goto done;
2136 f79e6490 2020-04-19 stsp
2137 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2138 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2139 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2140 7848a0e1 2020-03-19 stsp
2141 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2142 9c52365f 2020-03-21 stsp server_path, verbosity);
2143 7848a0e1 2020-03-19 stsp if (error)
2144 7848a0e1 2020-03-19 stsp goto done;
2145 7848a0e1 2020-03-19 stsp
2146 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2147 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2148 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2149 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2150 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2151 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2152 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2153 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2154 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2155 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2156 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2157 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2158 7848a0e1 2020-03-19 stsp if (error)
2159 7848a0e1 2020-03-19 stsp goto done;
2160 7848a0e1 2020-03-19 stsp
2161 41b0de12 2020-03-21 stsp if (list_refs_only) {
2162 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2163 41b0de12 2020-03-21 stsp goto done;
2164 41b0de12 2020-03-21 stsp }
2165 41b0de12 2020-03-21 stsp
2166 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2167 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2168 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2169 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2170 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2171 984065c8 2020-03-19 stsp if (error)
2172 984065c8 2020-03-19 stsp goto done;
2173 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2174 984065c8 2020-03-19 stsp free(id_str);
2175 984065c8 2020-03-19 stsp id_str = NULL;
2176 984065c8 2020-03-19 stsp }
2177 7848a0e1 2020-03-19 stsp
2178 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2179 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2180 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2181 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2182 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2183 7848a0e1 2020-03-19 stsp char *remote_refname;
2184 7848a0e1 2020-03-19 stsp
2185 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2186 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2187 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2188 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2189 0e4002ca 2020-03-21 stsp if (error)
2190 0e4002ca 2020-03-21 stsp goto done;
2191 0e4002ca 2020-03-21 stsp continue;
2192 0e4002ca 2020-03-21 stsp }
2193 0e4002ca 2020-03-21 stsp
2194 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2195 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2196 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2197 7848a0e1 2020-03-19 stsp if (error) {
2198 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2199 7848a0e1 2020-03-19 stsp goto done;
2200 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2201 6338a6a1 2020-03-21 stsp repo);
2202 7848a0e1 2020-03-19 stsp if (error)
2203 7848a0e1 2020-03-19 stsp goto done;
2204 7848a0e1 2020-03-19 stsp } else {
2205 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2206 db6d8ad8 2020-03-21 stsp verbosity, repo);
2207 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2208 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2209 9f142382 2020-03-21 stsp error = unlock_err;
2210 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2211 7848a0e1 2020-03-19 stsp if (error)
2212 7848a0e1 2020-03-19 stsp goto done;
2213 7848a0e1 2020-03-19 stsp }
2214 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2215 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2216 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2217 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2218 7848a0e1 2020-03-19 stsp goto done;
2219 7848a0e1 2020-03-19 stsp }
2220 7848a0e1 2020-03-19 stsp
2221 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2222 7848a0e1 2020-03-19 stsp if (error) {
2223 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2224 7848a0e1 2020-03-19 stsp goto done;
2225 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2226 688f11b3 2020-03-21 stsp verbosity, repo);
2227 7848a0e1 2020-03-19 stsp if (error)
2228 7848a0e1 2020-03-19 stsp goto done;
2229 7848a0e1 2020-03-19 stsp } else {
2230 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2231 db6d8ad8 2020-03-21 stsp verbosity, repo);
2232 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2233 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2234 9f142382 2020-03-21 stsp error = unlock_err;
2235 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2236 7848a0e1 2020-03-19 stsp if (error)
2237 7848a0e1 2020-03-19 stsp goto done;
2238 7848a0e1 2020-03-19 stsp }
2239 2ec30c80 2020-03-20 stsp
2240 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2241 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2242 2ec30c80 2020-03-20 stsp if (error) {
2243 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2244 2ec30c80 2020-03-20 stsp goto done;
2245 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2246 6338a6a1 2020-03-21 stsp repo);
2247 2ec30c80 2020-03-20 stsp if (error)
2248 2ec30c80 2020-03-20 stsp goto done;
2249 9f142382 2020-03-21 stsp } else {
2250 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2251 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2252 9f142382 2020-03-21 stsp error = unlock_err;
2253 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2254 9f142382 2020-03-21 stsp }
2255 7848a0e1 2020-03-19 stsp }
2256 7848a0e1 2020-03-19 stsp }
2257 f1bcca34 2020-03-25 stsp if (delete_refs) {
2258 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2259 3789fd73 2020-03-26 stsp verbosity, repo);
2260 f1bcca34 2020-03-25 stsp if (error)
2261 f1bcca34 2020-03-25 stsp goto done;
2262 f1bcca34 2020-03-25 stsp }
2263 f1bcca34 2020-03-25 stsp
2264 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2265 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2266 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2267 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2268 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2269 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2270 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2271 f1bcca34 2020-03-25 stsp
2272 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2273 f1bcca34 2020-03-25 stsp continue;
2274 f1bcca34 2020-03-25 stsp
2275 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2276 f1bcca34 2020-03-25 stsp continue;
2277 f1bcca34 2020-03-25 stsp
2278 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2279 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2280 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2281 f1bcca34 2020-03-25 stsp goto done;
2282 f1bcca34 2020-03-25 stsp }
2283 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2284 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2285 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2286 f1bcca34 2020-03-25 stsp free(remote_refname);
2287 f1bcca34 2020-03-25 stsp goto done;
2288 f1bcca34 2020-03-25 stsp }
2289 f1bcca34 2020-03-25 stsp
2290 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2291 f1bcca34 2020-03-25 stsp 0);
2292 f1bcca34 2020-03-25 stsp if (error) {
2293 f1bcca34 2020-03-25 stsp free(remote_refname);
2294 f1bcca34 2020-03-25 stsp free(remote_target);
2295 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2296 f1bcca34 2020-03-25 stsp error = NULL;
2297 f1bcca34 2020-03-25 stsp continue;
2298 f1bcca34 2020-03-25 stsp }
2299 f1bcca34 2020-03-25 stsp goto done;
2300 f1bcca34 2020-03-25 stsp }
2301 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2302 f1bcca34 2020-03-25 stsp verbosity, repo);
2303 f1bcca34 2020-03-25 stsp free(remote_refname);
2304 f1bcca34 2020-03-25 stsp free(remote_target);
2305 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2306 f1bcca34 2020-03-25 stsp if (error)
2307 f1bcca34 2020-03-25 stsp goto done;
2308 f1bcca34 2020-03-25 stsp }
2309 f1bcca34 2020-03-25 stsp }
2310 7848a0e1 2020-03-19 stsp done:
2311 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2312 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2313 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2314 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2315 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2316 9c52365f 2020-03-21 stsp }
2317 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2318 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2319 7848a0e1 2020-03-19 stsp if (repo)
2320 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2321 7848a0e1 2020-03-19 stsp if (worktree)
2322 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2323 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2324 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2325 7848a0e1 2020-03-19 stsp free(pe->data);
2326 7848a0e1 2020-03-19 stsp }
2327 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2328 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2329 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2330 7848a0e1 2020-03-19 stsp free(pe->data);
2331 7848a0e1 2020-03-19 stsp }
2332 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2333 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2334 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2335 7848a0e1 2020-03-19 stsp free(id_str);
2336 7848a0e1 2020-03-19 stsp free(cwd);
2337 7848a0e1 2020-03-19 stsp free(repo_path);
2338 7848a0e1 2020-03-19 stsp free(pack_hash);
2339 7848a0e1 2020-03-19 stsp free(proto);
2340 7848a0e1 2020-03-19 stsp free(host);
2341 7848a0e1 2020-03-19 stsp free(port);
2342 7848a0e1 2020-03-19 stsp free(server_path);
2343 7848a0e1 2020-03-19 stsp free(repo_name);
2344 7848a0e1 2020-03-19 stsp return error;
2345 7848a0e1 2020-03-19 stsp }
2346 7848a0e1 2020-03-19 stsp
2347 7848a0e1 2020-03-19 stsp
2348 7848a0e1 2020-03-19 stsp __dead static void
2349 2ab43947 2020-03-18 stsp usage_checkout(void)
2350 2ab43947 2020-03-18 stsp {
2351 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2352 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2353 2ab43947 2020-03-18 stsp exit(1);
2354 2ab43947 2020-03-18 stsp }
2355 2ab43947 2020-03-18 stsp
2356 2ab43947 2020-03-18 stsp static void
2357 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2358 2ab43947 2020-03-18 stsp {
2359 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2360 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2361 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2362 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2363 2ab43947 2020-03-18 stsp getprogname());
2364 2ab43947 2020-03-18 stsp }
2365 2ab43947 2020-03-18 stsp
2366 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2367 2ab43947 2020-03-18 stsp const char *worktree_path;
2368 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2369 2ab43947 2020-03-18 stsp };
2370 2ab43947 2020-03-18 stsp
2371 2ab43947 2020-03-18 stsp static const struct got_error *
2372 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2373 2ab43947 2020-03-18 stsp {
2374 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2375 2ab43947 2020-03-18 stsp
2376 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2377 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2378 2ab43947 2020-03-18 stsp return NULL;
2379 2ab43947 2020-03-18 stsp
2380 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2381 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2382 2ab43947 2020-03-18 stsp return NULL;
2383 2ab43947 2020-03-18 stsp }
2384 2ab43947 2020-03-18 stsp
2385 2ab43947 2020-03-18 stsp while (path[0] == '/')
2386 2ab43947 2020-03-18 stsp path++;
2387 2ab43947 2020-03-18 stsp
2388 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2389 2ab43947 2020-03-18 stsp return NULL;
2390 2ab43947 2020-03-18 stsp }
2391 2ab43947 2020-03-18 stsp
2392 2ab43947 2020-03-18 stsp static const struct got_error *
2393 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2394 2ab43947 2020-03-18 stsp {
2395 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2396 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2397 2ab43947 2020-03-18 stsp return NULL;
2398 2ab43947 2020-03-18 stsp }
2399 2ab43947 2020-03-18 stsp
2400 2ab43947 2020-03-18 stsp static const struct got_error *
2401 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2402 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2403 2ab43947 2020-03-18 stsp struct got_repository *repo)
2404 2ab43947 2020-03-18 stsp {
2405 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2406 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2407 2ab43947 2020-03-18 stsp
2408 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2409 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2410 2ab43947 2020-03-18 stsp if (err)
2411 2ab43947 2020-03-18 stsp return err;
2412 2ab43947 2020-03-18 stsp
2413 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2414 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2415 2ab43947 2020-03-18 stsp
2416 2ab43947 2020-03-18 stsp /*
2417 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2418 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2419 2ab43947 2020-03-18 stsp *
2420 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2421 2ab43947 2020-03-18 stsp *
2422 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2423 2ab43947 2020-03-18 stsp * \ /
2424 2ab43947 2020-03-18 stsp * C E
2425 2ab43947 2020-03-18 stsp * \ /
2426 2ab43947 2020-03-18 stsp * B (yca)
2427 2ab43947 2020-03-18 stsp * |
2428 2ab43947 2020-03-18 stsp * A
2429 2ab43947 2020-03-18 stsp *
2430 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2431 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2432 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2433 2ab43947 2020-03-18 stsp */
2434 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2435 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2436 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2437 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2438 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2439 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2440 2ab43947 2020-03-18 stsp
2441 2ab43947 2020-03-18 stsp free(yca_id);
2442 2ab43947 2020-03-18 stsp return NULL;
2443 2ab43947 2020-03-18 stsp }
2444 2ab43947 2020-03-18 stsp
2445 2ab43947 2020-03-18 stsp static const struct got_error *
2446 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2447 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2448 2ab43947 2020-03-18 stsp struct got_repository *repo)
2449 2ab43947 2020-03-18 stsp {
2450 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2451 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2452 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2453 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2454 2ab43947 2020-03-18 stsp
2455 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2456 2ab43947 2020-03-18 stsp if (err)
2457 2ab43947 2020-03-18 stsp goto done;
2458 2ab43947 2020-03-18 stsp
2459 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2460 2ab43947 2020-03-18 stsp is_same_branch = 1;
2461 2ab43947 2020-03-18 stsp goto done;
2462 2ab43947 2020-03-18 stsp }
2463 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2464 2ab43947 2020-03-18 stsp is_same_branch = 1;
2465 2ab43947 2020-03-18 stsp goto done;
2466 2ab43947 2020-03-18 stsp }
2467 2ab43947 2020-03-18 stsp
2468 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2469 2ab43947 2020-03-18 stsp if (err)
2470 2ab43947 2020-03-18 stsp goto done;
2471 2ab43947 2020-03-18 stsp
2472 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2473 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2474 2ab43947 2020-03-18 stsp if (err)
2475 2ab43947 2020-03-18 stsp goto done;
2476 2ab43947 2020-03-18 stsp
2477 2ab43947 2020-03-18 stsp for (;;) {
2478 2ab43947 2020-03-18 stsp struct got_object_id *id;
2479 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2480 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2481 2ab43947 2020-03-18 stsp if (err) {
2482 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2483 2ab43947 2020-03-18 stsp err = NULL;
2484 2ab43947 2020-03-18 stsp break;
2485 2ab43947 2020-03-18 stsp }
2486 2ab43947 2020-03-18 stsp
2487 2ab43947 2020-03-18 stsp if (id) {
2488 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2489 2ab43947 2020-03-18 stsp break;
2490 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2491 2ab43947 2020-03-18 stsp is_same_branch = 1;
2492 2ab43947 2020-03-18 stsp break;
2493 2ab43947 2020-03-18 stsp }
2494 2ab43947 2020-03-18 stsp }
2495 2ab43947 2020-03-18 stsp }
2496 2ab43947 2020-03-18 stsp done:
2497 2ab43947 2020-03-18 stsp if (graph)
2498 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2499 2ab43947 2020-03-18 stsp free(head_commit_id);
2500 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2501 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2502 2ab43947 2020-03-18 stsp return err;
2503 a367ff0f 2019-05-14 stsp }
2504 8069f636 2019-01-12 stsp
2505 4ed7e80c 2018-05-20 stsp static const struct got_error *
2506 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2507 4b6c9460 2020-03-05 stsp {
2508 4b6c9460 2020-03-05 stsp static char msg[512];
2509 4b6c9460 2020-03-05 stsp const char *branch_name;
2510 4b6c9460 2020-03-05 stsp
2511 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2512 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2513 4b6c9460 2020-03-05 stsp else
2514 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2515 4b6c9460 2020-03-05 stsp
2516 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2517 4b6c9460 2020-03-05 stsp branch_name += 11;
2518 4b6c9460 2020-03-05 stsp
2519 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2520 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2521 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2522 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2523 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2524 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2525 4b6c9460 2020-03-05 stsp
2526 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2527 4b6c9460 2020-03-05 stsp }
2528 4b6c9460 2020-03-05 stsp
2529 4b6c9460 2020-03-05 stsp static const struct got_error *
2530 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2531 c09a553d 2018-03-12 stsp {
2532 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2533 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2534 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2535 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2536 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2537 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2538 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2539 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2540 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2541 c47340da 2020-09-20 stsp char *cwd = NULL, *path = NULL;
2542 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2543 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2544 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2545 f2ea84fa 2019-07-27 stsp
2546 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2547 c09a553d 2018-03-12 stsp
2548 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2549 0bb8a95e 2018-03-12 stsp switch (ch) {
2550 08573d5b 2019-05-14 stsp case 'b':
2551 08573d5b 2019-05-14 stsp branch_name = optarg;
2552 08573d5b 2019-05-14 stsp break;
2553 8069f636 2019-01-12 stsp case 'c':
2554 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2555 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2556 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2557 bb51a5b4 2020-01-13 stsp break;
2558 bb51a5b4 2020-01-13 stsp case 'E':
2559 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2560 8069f636 2019-01-12 stsp break;
2561 0bb8a95e 2018-03-12 stsp case 'p':
2562 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2563 0bb8a95e 2018-03-12 stsp break;
2564 0bb8a95e 2018-03-12 stsp default:
2565 2deda0b9 2019-03-07 stsp usage_checkout();
2566 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2567 0bb8a95e 2018-03-12 stsp }
2568 0bb8a95e 2018-03-12 stsp }
2569 0bb8a95e 2018-03-12 stsp
2570 0bb8a95e 2018-03-12 stsp argc -= optind;
2571 0bb8a95e 2018-03-12 stsp argv += optind;
2572 0bb8a95e 2018-03-12 stsp
2573 6715a751 2018-03-16 stsp #ifndef PROFILE
2574 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2575 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2576 c09a553d 2018-03-12 stsp err(1, "pledge");
2577 6715a751 2018-03-16 stsp #endif
2578 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2579 c47340da 2020-09-20 stsp char *base, *dotgit;
2580 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2581 76089277 2018-04-01 stsp if (repo_path == NULL)
2582 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2583 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2584 76089277 2018-04-01 stsp if (cwd == NULL) {
2585 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2586 76089277 2018-04-01 stsp goto done;
2587 76089277 2018-04-01 stsp }
2588 c47340da 2020-09-20 stsp if (path_prefix[0])
2589 c47340da 2020-09-20 stsp path = strdup(path_prefix);
2590 c47340da 2020-09-20 stsp else
2591 c47340da 2020-09-20 stsp path = strdup(repo_path);
2592 c47340da 2020-09-20 stsp if (path == NULL) {
2593 c47340da 2020-09-20 stsp error = got_error_from_errno("strdup");
2594 c47340da 2020-09-20 stsp goto done;
2595 c47340da 2020-09-20 stsp }
2596 c47340da 2020-09-20 stsp base = basename(path);
2597 c47340da 2020-09-20 stsp if (base == NULL) {
2598 c47340da 2020-09-20 stsp error = got_error_from_errno2("basename", path);
2599 c47340da 2020-09-20 stsp goto done;
2600 76089277 2018-04-01 stsp }
2601 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2602 c09a553d 2018-03-12 stsp if (dotgit)
2603 c09a553d 2018-03-12 stsp *dotgit = '\0';
2604 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2605 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2606 76089277 2018-04-01 stsp goto done;
2607 c09a553d 2018-03-12 stsp }
2608 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2609 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2610 76089277 2018-04-01 stsp if (repo_path == NULL) {
2611 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2612 76089277 2018-04-01 stsp goto done;
2613 76089277 2018-04-01 stsp }
2614 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2615 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2616 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2617 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2618 b4b3a7dd 2019-07-22 stsp argv[1]);
2619 b4b3a7dd 2019-07-22 stsp goto done;
2620 b4b3a7dd 2019-07-22 stsp }
2621 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2622 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2623 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2624 b4b3a7dd 2019-07-22 stsp goto done;
2625 b4b3a7dd 2019-07-22 stsp }
2626 76089277 2018-04-01 stsp }
2627 c09a553d 2018-03-12 stsp } else
2628 c09a553d 2018-03-12 stsp usage_checkout();
2629 c09a553d 2018-03-12 stsp
2630 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2631 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2632 13bfb272 2019-05-10 jcs
2633 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2634 c09a553d 2018-03-12 stsp if (error != NULL)
2635 c02c541e 2019-03-29 stsp goto done;
2636 c02c541e 2019-03-29 stsp
2637 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2638 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2639 c530dc23 2019-07-23 stsp if (error) {
2640 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2641 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2642 c530dc23 2019-07-23 stsp goto done;
2643 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2644 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2645 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2646 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2647 c530dc23 2019-07-23 stsp goto done;
2648 c530dc23 2019-07-23 stsp }
2649 c530dc23 2019-07-23 stsp }
2650 c530dc23 2019-07-23 stsp
2651 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2652 c02c541e 2019-03-29 stsp if (error)
2653 c09a553d 2018-03-12 stsp goto done;
2654 8069f636 2019-01-12 stsp
2655 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2656 c09a553d 2018-03-12 stsp if (error != NULL)
2657 c09a553d 2018-03-12 stsp goto done;
2658 c09a553d 2018-03-12 stsp
2659 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2660 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2661 c09a553d 2018-03-12 stsp goto done;
2662 c09a553d 2018-03-12 stsp
2663 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2664 c09a553d 2018-03-12 stsp if (error != NULL)
2665 c09a553d 2018-03-12 stsp goto done;
2666 c09a553d 2018-03-12 stsp
2667 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2668 e5dc7198 2018-12-29 stsp path_prefix);
2669 e5dc7198 2018-12-29 stsp if (error != NULL)
2670 e5dc7198 2018-12-29 stsp goto done;
2671 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2672 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2673 49520a32 2018-12-29 stsp goto done;
2674 49520a32 2018-12-29 stsp }
2675 49520a32 2018-12-29 stsp
2676 8069f636 2019-01-12 stsp if (commit_id_str) {
2677 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2678 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2679 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2680 30837e32 2019-07-25 stsp if (error)
2681 8069f636 2019-01-12 stsp goto done;
2682 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2683 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2684 8069f636 2019-01-12 stsp if (error != NULL) {
2685 8069f636 2019-01-12 stsp free(commit_id);
2686 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2687 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2688 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2689 4b6c9460 2020-03-05 stsp }
2690 8069f636 2019-01-12 stsp goto done;
2691 8069f636 2019-01-12 stsp }
2692 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2693 4b6c9460 2020-03-05 stsp if (error) {
2694 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2695 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2696 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2697 4b6c9460 2020-03-05 stsp }
2698 45d344f6 2019-05-14 stsp goto done;
2699 4b6c9460 2020-03-05 stsp }
2700 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2701 8069f636 2019-01-12 stsp commit_id);
2702 8069f636 2019-01-12 stsp free(commit_id);
2703 8069f636 2019-01-12 stsp if (error)
2704 8069f636 2019-01-12 stsp goto done;
2705 8069f636 2019-01-12 stsp }
2706 8069f636 2019-01-12 stsp
2707 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2708 f2ea84fa 2019-07-27 stsp if (error)
2709 f2ea84fa 2019-07-27 stsp goto done;
2710 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2711 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2712 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2713 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2714 c09a553d 2018-03-12 stsp if (error != NULL)
2715 c09a553d 2018-03-12 stsp goto done;
2716 c09a553d 2018-03-12 stsp
2717 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2718 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2719 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2720 c09a553d 2018-03-12 stsp done:
2721 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2722 8069f636 2019-01-12 stsp free(commit_id_str);
2723 76089277 2018-04-01 stsp free(repo_path);
2724 507dc3bb 2018-12-29 stsp free(worktree_path);
2725 c47340da 2020-09-20 stsp free(cwd);
2726 c47340da 2020-09-20 stsp free(path);
2727 507dc3bb 2018-12-29 stsp return error;
2728 9627c110 2020-04-18 stsp }
2729 9627c110 2020-04-18 stsp
2730 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2731 9627c110 2020-04-18 stsp int did_something;
2732 9627c110 2020-04-18 stsp int conflicts;
2733 9627c110 2020-04-18 stsp int obstructed;
2734 9627c110 2020-04-18 stsp int not_updated;
2735 9627c110 2020-04-18 stsp };
2736 9627c110 2020-04-18 stsp
2737 9627c110 2020-04-18 stsp void
2738 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2739 9627c110 2020-04-18 stsp {
2740 9627c110 2020-04-18 stsp if (!upa->did_something)
2741 9627c110 2020-04-18 stsp return;
2742 9627c110 2020-04-18 stsp
2743 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2744 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2745 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2746 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2747 9627c110 2020-04-18 stsp upa->obstructed);
2748 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2749 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2750 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2751 507dc3bb 2018-12-29 stsp }
2752 507dc3bb 2018-12-29 stsp
2753 507dc3bb 2018-12-29 stsp __dead static void
2754 507dc3bb 2018-12-29 stsp usage_update(void)
2755 507dc3bb 2018-12-29 stsp {
2756 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2757 507dc3bb 2018-12-29 stsp getprogname());
2758 507dc3bb 2018-12-29 stsp exit(1);
2759 507dc3bb 2018-12-29 stsp }
2760 507dc3bb 2018-12-29 stsp
2761 1ee397ad 2019-07-12 stsp static const struct got_error *
2762 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2763 507dc3bb 2018-12-29 stsp {
2764 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2765 784955db 2019-01-12 stsp
2766 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2767 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2768 1ee397ad 2019-07-12 stsp return NULL;
2769 507dc3bb 2018-12-29 stsp
2770 9627c110 2020-04-18 stsp upa->did_something = 1;
2771 a484d721 2019-06-10 stsp
2772 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2773 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2774 1ee397ad 2019-07-12 stsp return NULL;
2775 a484d721 2019-06-10 stsp
2776 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2777 9627c110 2020-04-18 stsp upa->conflicts++;
2778 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2779 9627c110 2020-04-18 stsp upa->obstructed++;
2780 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2781 9627c110 2020-04-18 stsp upa->not_updated++;
2782 9627c110 2020-04-18 stsp
2783 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2784 507dc3bb 2018-12-29 stsp path++;
2785 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2786 1ee397ad 2019-07-12 stsp return NULL;
2787 be7061eb 2018-12-30 stsp }
2788 be7061eb 2018-12-30 stsp
2789 be7061eb 2018-12-30 stsp static const struct got_error *
2790 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2791 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2792 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2793 a1fb16d8 2019-05-24 stsp {
2794 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2795 a1fb16d8 2019-05-24 stsp char *base_id_str;
2796 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2797 a1fb16d8 2019-05-24 stsp
2798 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2799 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2800 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2801 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2802 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2803 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2804 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2805 a1fb16d8 2019-05-24 stsp }
2806 a1fb16d8 2019-05-24 stsp
2807 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2808 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2809 a1fb16d8 2019-05-24 stsp if (err) {
2810 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2811 a1fb16d8 2019-05-24 stsp return err;
2812 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2813 a1fb16d8 2019-05-24 stsp }
2814 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2815 a1fb16d8 2019-05-24 stsp return NULL;
2816 a1fb16d8 2019-05-24 stsp
2817 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2818 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2819 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2820 a1fb16d8 2019-05-24 stsp if (err)
2821 a1fb16d8 2019-05-24 stsp return err;
2822 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2823 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2824 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2825 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2826 0ebf8283 2019-07-24 stsp return NULL;
2827 0ebf8283 2019-07-24 stsp }
2828 0ebf8283 2019-07-24 stsp
2829 0ebf8283 2019-07-24 stsp static const struct got_error *
2830 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2831 0ebf8283 2019-07-24 stsp {
2832 0ebf8283 2019-07-24 stsp const struct got_error *err;
2833 0ebf8283 2019-07-24 stsp int in_progress;
2834 0ebf8283 2019-07-24 stsp
2835 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2836 0ebf8283 2019-07-24 stsp if (err)
2837 0ebf8283 2019-07-24 stsp return err;
2838 0ebf8283 2019-07-24 stsp if (in_progress)
2839 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2840 0ebf8283 2019-07-24 stsp
2841 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2842 0ebf8283 2019-07-24 stsp if (err)
2843 0ebf8283 2019-07-24 stsp return err;
2844 0ebf8283 2019-07-24 stsp if (in_progress)
2845 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2846 0ebf8283 2019-07-24 stsp
2847 a1fb16d8 2019-05-24 stsp return NULL;
2848 a5edda0a 2019-07-27 stsp }
2849 a5edda0a 2019-07-27 stsp
2850 a5edda0a 2019-07-27 stsp static const struct got_error *
2851 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2852 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2853 a5edda0a 2019-07-27 stsp {
2854 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2855 a5edda0a 2019-07-27 stsp char *path;
2856 a5edda0a 2019-07-27 stsp int i;
2857 a5edda0a 2019-07-27 stsp
2858 a5edda0a 2019-07-27 stsp if (argc == 0) {
2859 a5edda0a 2019-07-27 stsp path = strdup("");
2860 a5edda0a 2019-07-27 stsp if (path == NULL)
2861 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2862 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2863 a5edda0a 2019-07-27 stsp }
2864 a5edda0a 2019-07-27 stsp
2865 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2866 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2867 a5edda0a 2019-07-27 stsp if (err)
2868 a5edda0a 2019-07-27 stsp break;
2869 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2870 a5edda0a 2019-07-27 stsp if (err) {
2871 a5edda0a 2019-07-27 stsp free(path);
2872 a5edda0a 2019-07-27 stsp break;
2873 a5edda0a 2019-07-27 stsp }
2874 a5edda0a 2019-07-27 stsp }
2875 a5edda0a 2019-07-27 stsp
2876 a5edda0a 2019-07-27 stsp return err;
2877 a1fb16d8 2019-05-24 stsp }
2878 a1fb16d8 2019-05-24 stsp
2879 a1fb16d8 2019-05-24 stsp static const struct got_error *
2880 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2881 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2882 fa51e947 2020-03-27 stsp {
2883 fa51e947 2020-03-27 stsp const struct got_error *err;
2884 fa51e947 2020-03-27 stsp struct got_repository *repo;
2885 fa51e947 2020-03-27 stsp static char msg[512];
2886 fa51e947 2020-03-27 stsp
2887 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2888 fa51e947 2020-03-27 stsp if (err)
2889 fa51e947 2020-03-27 stsp return orig_err;
2890 fa51e947 2020-03-27 stsp
2891 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2892 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2893 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2894 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2895 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2896 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2897 fa51e947 2020-03-27 stsp got_repo_close(repo);
2898 fa51e947 2020-03-27 stsp return err;
2899 fa51e947 2020-03-27 stsp }
2900 fa51e947 2020-03-27 stsp
2901 fa51e947 2020-03-27 stsp static const struct got_error *
2902 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2903 507dc3bb 2018-12-29 stsp {
2904 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2905 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2906 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2907 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2908 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2909 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2910 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2911 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2912 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2913 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2914 9627c110 2020-04-18 stsp int ch;
2915 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2916 507dc3bb 2018-12-29 stsp
2917 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2918 f2ea84fa 2019-07-27 stsp
2919 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2920 507dc3bb 2018-12-29 stsp switch (ch) {
2921 024e9686 2019-05-14 stsp case 'b':
2922 024e9686 2019-05-14 stsp branch_name = optarg;
2923 024e9686 2019-05-14 stsp break;
2924 507dc3bb 2018-12-29 stsp case 'c':
2925 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2926 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2927 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2928 507dc3bb 2018-12-29 stsp break;
2929 507dc3bb 2018-12-29 stsp default:
2930 2deda0b9 2019-03-07 stsp usage_update();
2931 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2932 507dc3bb 2018-12-29 stsp }
2933 507dc3bb 2018-12-29 stsp }
2934 507dc3bb 2018-12-29 stsp
2935 507dc3bb 2018-12-29 stsp argc -= optind;
2936 507dc3bb 2018-12-29 stsp argv += optind;
2937 507dc3bb 2018-12-29 stsp
2938 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2939 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2940 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2941 507dc3bb 2018-12-29 stsp err(1, "pledge");
2942 507dc3bb 2018-12-29 stsp #endif
2943 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2944 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2945 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2946 c4cdcb68 2019-04-03 stsp goto done;
2947 c4cdcb68 2019-04-03 stsp }
2948 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2949 fa51e947 2020-03-27 stsp if (error) {
2950 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2951 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2952 fa51e947 2020-03-27 stsp worktree_path);
2953 7d5807f4 2019-07-11 stsp goto done;
2954 fa51e947 2020-03-27 stsp }
2955 7d5807f4 2019-07-11 stsp
2956 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2957 f2ea84fa 2019-07-27 stsp if (error)
2958 f2ea84fa 2019-07-27 stsp goto done;
2959 507dc3bb 2018-12-29 stsp
2960 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2961 c9956ddf 2019-09-08 stsp NULL);
2962 507dc3bb 2018-12-29 stsp if (error != NULL)
2963 507dc3bb 2018-12-29 stsp goto done;
2964 507dc3bb 2018-12-29 stsp
2965 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2966 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2967 087fb88c 2019-08-04 stsp if (error)
2968 087fb88c 2019-08-04 stsp goto done;
2969 087fb88c 2019-08-04 stsp
2970 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2971 0266afb7 2019-01-04 stsp if (error)
2972 0266afb7 2019-01-04 stsp goto done;
2973 0266afb7 2019-01-04 stsp
2974 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2975 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2976 024e9686 2019-05-14 stsp if (error != NULL)
2977 024e9686 2019-05-14 stsp goto done;
2978 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2979 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2980 9c4b8182 2019-01-02 stsp if (error != NULL)
2981 9c4b8182 2019-01-02 stsp goto done;
2982 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2983 507dc3bb 2018-12-29 stsp if (error != NULL)
2984 507dc3bb 2018-12-29 stsp goto done;
2985 507dc3bb 2018-12-29 stsp } else {
2986 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2987 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2988 04f57cb3 2019-07-25 stsp free(commit_id_str);
2989 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2990 30837e32 2019-07-25 stsp if (error)
2991 a297e751 2019-07-11 stsp goto done;
2992 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2993 a297e751 2019-07-11 stsp if (error)
2994 507dc3bb 2018-12-29 stsp goto done;
2995 507dc3bb 2018-12-29 stsp }
2996 35c965b2 2018-12-29 stsp
2997 a1fb16d8 2019-05-24 stsp if (branch_name) {
2998 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2999 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3000 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3001 f2ea84fa 2019-07-27 stsp continue;
3002 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3003 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3004 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3005 024e9686 2019-05-14 stsp goto done;
3006 024e9686 2019-05-14 stsp }
3007 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3008 024e9686 2019-05-14 stsp if (error)
3009 024e9686 2019-05-14 stsp goto done;
3010 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3011 3aef623b 2019-10-15 stsp repo);
3012 a367ff0f 2019-05-14 stsp free(head_commit_id);
3013 024e9686 2019-05-14 stsp if (error != NULL)
3014 024e9686 2019-05-14 stsp goto done;
3015 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3016 a367ff0f 2019-05-14 stsp if (error)
3017 a367ff0f 2019-05-14 stsp goto done;
3018 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3019 024e9686 2019-05-14 stsp if (error)
3020 024e9686 2019-05-14 stsp goto done;
3021 024e9686 2019-05-14 stsp } else {
3022 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3023 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3024 a367ff0f 2019-05-14 stsp if (error != NULL) {
3025 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3026 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3027 024e9686 2019-05-14 stsp goto done;
3028 a367ff0f 2019-05-14 stsp }
3029 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3030 a367ff0f 2019-05-14 stsp if (error)
3031 a367ff0f 2019-05-14 stsp goto done;
3032 024e9686 2019-05-14 stsp }
3033 507dc3bb 2018-12-29 stsp
3034 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3035 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3036 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3037 507dc3bb 2018-12-29 stsp commit_id);
3038 507dc3bb 2018-12-29 stsp if (error)
3039 507dc3bb 2018-12-29 stsp goto done;
3040 507dc3bb 2018-12-29 stsp }
3041 507dc3bb 2018-12-29 stsp
3042 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3043 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3044 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3045 507dc3bb 2018-12-29 stsp if (error != NULL)
3046 507dc3bb 2018-12-29 stsp goto done;
3047 9c4b8182 2019-01-02 stsp
3048 9627c110 2020-04-18 stsp if (upa.did_something)
3049 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3050 784955db 2019-01-12 stsp else
3051 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3052 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3053 507dc3bb 2018-12-29 stsp done:
3054 c09a553d 2018-03-12 stsp free(worktree_path);
3055 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3056 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3057 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3058 507dc3bb 2018-12-29 stsp free(commit_id);
3059 9c4b8182 2019-01-02 stsp free(commit_id_str);
3060 c09a553d 2018-03-12 stsp return error;
3061 c09a553d 2018-03-12 stsp }
3062 c09a553d 2018-03-12 stsp
3063 f42b1b34 2018-03-12 stsp static const struct got_error *
3064 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3065 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3066 63035f9f 2019-10-06 stsp struct got_repository *repo)
3067 5c860e29 2018-03-12 stsp {
3068 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3069 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3070 79109fed 2018-03-27 stsp
3071 44392932 2019-08-25 stsp if (blob_id1) {
3072 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3073 44392932 2019-08-25 stsp if (err)
3074 44392932 2019-08-25 stsp goto done;
3075 44392932 2019-08-25 stsp }
3076 79109fed 2018-03-27 stsp
3077 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3078 44392932 2019-08-25 stsp if (err)
3079 44392932 2019-08-25 stsp goto done;
3080 79109fed 2018-03-27 stsp
3081 44392932 2019-08-25 stsp while (path[0] == '/')
3082 44392932 2019-08-25 stsp path++;
3083 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
3084 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
3085 44392932 2019-08-25 stsp done:
3086 44392932 2019-08-25 stsp if (blob1)
3087 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3088 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3089 44392932 2019-08-25 stsp return err;
3090 44392932 2019-08-25 stsp }
3091 44392932 2019-08-25 stsp
3092 44392932 2019-08-25 stsp static const struct got_error *
3093 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3094 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3095 63035f9f 2019-10-06 stsp struct got_repository *repo)
3096 44392932 2019-08-25 stsp {
3097 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3098 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3099 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3100 44392932 2019-08-25 stsp
3101 44392932 2019-08-25 stsp if (tree_id1) {
3102 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3103 79109fed 2018-03-27 stsp if (err)
3104 44392932 2019-08-25 stsp goto done;
3105 79109fed 2018-03-27 stsp }
3106 79109fed 2018-03-27 stsp
3107 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3108 0f2b3dca 2018-12-22 stsp if (err)
3109 0f2b3dca 2018-12-22 stsp goto done;
3110 0f2b3dca 2018-12-22 stsp
3111 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3112 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3113 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3114 44392932 2019-08-25 stsp while (path[0] == '/')
3115 44392932 2019-08-25 stsp path++;
3116 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3117 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3118 0f2b3dca 2018-12-22 stsp done:
3119 79109fed 2018-03-27 stsp if (tree1)
3120 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3121 366e0a5f 2019-10-10 stsp if (tree2)
3122 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3123 44392932 2019-08-25 stsp return err;
3124 44392932 2019-08-25 stsp }
3125 44392932 2019-08-25 stsp
3126 44392932 2019-08-25 stsp static const struct got_error *
3127 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3128 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3129 0208f208 2020-05-05 stsp {
3130 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3131 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3132 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3133 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3134 0208f208 2020-05-05 stsp
3135 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3136 0208f208 2020-05-05 stsp if (qid != NULL) {
3137 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3138 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3139 0208f208 2020-05-05 stsp qid->id);
3140 0208f208 2020-05-05 stsp if (err)
3141 0208f208 2020-05-05 stsp return err;
3142 0208f208 2020-05-05 stsp
3143 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3144 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3145 0208f208 2020-05-05 stsp
3146 0208f208 2020-05-05 stsp }
3147 0208f208 2020-05-05 stsp
3148 0208f208 2020-05-05 stsp if (tree_id1) {
3149 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3150 0208f208 2020-05-05 stsp if (err)
3151 0208f208 2020-05-05 stsp goto done;
3152 0208f208 2020-05-05 stsp }
3153 0208f208 2020-05-05 stsp
3154 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3155 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3156 0208f208 2020-05-05 stsp if (err)
3157 0208f208 2020-05-05 stsp goto done;
3158 0208f208 2020-05-05 stsp
3159 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3160 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3161 0208f208 2020-05-05 stsp done:
3162 0208f208 2020-05-05 stsp if (tree1)
3163 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3164 0208f208 2020-05-05 stsp if (tree2)
3165 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3166 0208f208 2020-05-05 stsp return err;
3167 0208f208 2020-05-05 stsp }
3168 0208f208 2020-05-05 stsp
3169 0208f208 2020-05-05 stsp static const struct got_error *
3170 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3171 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3172 44392932 2019-08-25 stsp {
3173 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3174 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3175 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3176 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3177 44392932 2019-08-25 stsp struct got_object_qid *qid;
3178 44392932 2019-08-25 stsp
3179 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3180 44392932 2019-08-25 stsp if (qid != NULL) {
3181 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3182 44392932 2019-08-25 stsp qid->id);
3183 44392932 2019-08-25 stsp if (err)
3184 44392932 2019-08-25 stsp return err;
3185 44392932 2019-08-25 stsp }
3186 44392932 2019-08-25 stsp
3187 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3188 44392932 2019-08-25 stsp int obj_type;
3189 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3190 44392932 2019-08-25 stsp if (err)
3191 44392932 2019-08-25 stsp goto done;
3192 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3193 44392932 2019-08-25 stsp if (err) {
3194 44392932 2019-08-25 stsp free(obj_id2);
3195 44392932 2019-08-25 stsp goto done;
3196 44392932 2019-08-25 stsp }
3197 44392932 2019-08-25 stsp if (pcommit) {
3198 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3199 44392932 2019-08-25 stsp qid->id, path);
3200 44392932 2019-08-25 stsp if (err) {
3201 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3202 2e8c69d1 2020-05-04 stsp free(obj_id2);
3203 2e8c69d1 2020-05-04 stsp goto done;
3204 2e8c69d1 2020-05-04 stsp }
3205 2e8c69d1 2020-05-04 stsp } else {
3206 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3207 2e8c69d1 2020-05-04 stsp if (err) {
3208 2e8c69d1 2020-05-04 stsp free(obj_id2);
3209 2e8c69d1 2020-05-04 stsp goto done;
3210 2e8c69d1 2020-05-04 stsp }
3211 44392932 2019-08-25 stsp }
3212 44392932 2019-08-25 stsp }
3213 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3214 44392932 2019-08-25 stsp if (err) {
3215 44392932 2019-08-25 stsp free(obj_id2);
3216 44392932 2019-08-25 stsp goto done;
3217 44392932 2019-08-25 stsp }
3218 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3219 44392932 2019-08-25 stsp switch (obj_type) {
3220 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3221 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3222 63035f9f 2019-10-06 stsp 0, repo);
3223 44392932 2019-08-25 stsp break;
3224 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3225 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3226 63035f9f 2019-10-06 stsp 0, repo);
3227 44392932 2019-08-25 stsp break;
3228 44392932 2019-08-25 stsp default:
3229 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3230 44392932 2019-08-25 stsp break;
3231 44392932 2019-08-25 stsp }
3232 44392932 2019-08-25 stsp free(obj_id1);
3233 44392932 2019-08-25 stsp free(obj_id2);
3234 44392932 2019-08-25 stsp } else {
3235 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3236 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3237 44392932 2019-08-25 stsp if (err)
3238 44392932 2019-08-25 stsp goto done;
3239 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3240 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3241 44392932 2019-08-25 stsp if (err)
3242 44392932 2019-08-25 stsp goto done;
3243 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3244 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3245 44392932 2019-08-25 stsp }
3246 44392932 2019-08-25 stsp done:
3247 0f2b3dca 2018-12-22 stsp free(id_str1);
3248 0f2b3dca 2018-12-22 stsp free(id_str2);
3249 44392932 2019-08-25 stsp if (pcommit)
3250 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3251 79109fed 2018-03-27 stsp return err;
3252 79109fed 2018-03-27 stsp }
3253 79109fed 2018-03-27 stsp
3254 4bb494d5 2018-06-16 stsp static char *
3255 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3256 6c281f94 2018-06-11 stsp {
3257 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3258 09867e48 2019-08-13 stsp char *p, *s;
3259 09867e48 2019-08-13 stsp
3260 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3261 09867e48 2019-08-13 stsp if (tm == NULL)
3262 09867e48 2019-08-13 stsp return NULL;
3263 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3264 09867e48 2019-08-13 stsp if (s == NULL)
3265 09867e48 2019-08-13 stsp return NULL;
3266 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3267 6c281f94 2018-06-11 stsp if (p)
3268 6c281f94 2018-06-11 stsp *p = '\0';
3269 6c281f94 2018-06-11 stsp return s;
3270 6c281f94 2018-06-11 stsp }
3271 dc424a06 2019-08-07 stsp
3272 6841bf13 2019-11-29 kn static const struct got_error *
3273 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3274 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3275 6841bf13 2019-11-29 kn {
3276 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3277 6841bf13 2019-11-29 kn regmatch_t regmatch;
3278 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3279 6841bf13 2019-11-29 kn
3280 6841bf13 2019-11-29 kn *have_match = 0;
3281 6841bf13 2019-11-29 kn
3282 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3283 6841bf13 2019-11-29 kn if (err)
3284 6841bf13 2019-11-29 kn return err;
3285 6841bf13 2019-11-29 kn
3286 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3287 6841bf13 2019-11-29 kn if (err)
3288 6841bf13 2019-11-29 kn goto done;
3289 6841bf13 2019-11-29 kn
3290 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3291 6841bf13 2019-11-29 kn *have_match = 1;
3292 6841bf13 2019-11-29 kn done:
3293 6841bf13 2019-11-29 kn free(id_str);
3294 6841bf13 2019-11-29 kn free(logmsg);
3295 6841bf13 2019-11-29 kn return err;
3296 6841bf13 2019-11-29 kn }
3297 6841bf13 2019-11-29 kn
3298 0208f208 2020-05-05 stsp static void
3299 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3300 0208f208 2020-05-05 stsp regex_t *regex)
3301 0208f208 2020-05-05 stsp {
3302 0208f208 2020-05-05 stsp regmatch_t regmatch;
3303 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3304 0208f208 2020-05-05 stsp
3305 0208f208 2020-05-05 stsp *have_match = 0;
3306 0208f208 2020-05-05 stsp
3307 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3308 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3309 0208f208 2020-05-05 stsp *have_match = 1;
3310 0208f208 2020-05-05 stsp break;
3311 0208f208 2020-05-05 stsp }
3312 0208f208 2020-05-05 stsp }
3313 0208f208 2020-05-05 stsp }
3314 0208f208 2020-05-05 stsp
3315 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3316 6c281f94 2018-06-11 stsp
3317 79109fed 2018-03-27 stsp static const struct got_error *
3318 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3319 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3320 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3321 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3322 79109fed 2018-03-27 stsp {
3323 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3324 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3325 6c281f94 2018-06-11 stsp char datebuf[26];
3326 45d799e2 2018-12-23 stsp time_t committer_time;
3327 45d799e2 2018-12-23 stsp const char *author, *committer;
3328 199a4027 2019-02-02 stsp char *refs_str = NULL;
3329 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3330 5c860e29 2018-03-12 stsp
3331 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3332 199a4027 2019-02-02 stsp char *s;
3333 199a4027 2019-02-02 stsp const char *name;
3334 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3335 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3336 a436ad14 2019-08-13 stsp int cmp;
3337 a436ad14 2019-08-13 stsp
3338 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3339 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3340 d9498b20 2019-02-05 stsp continue;
3341 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3342 199a4027 2019-02-02 stsp name += 5;
3343 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3344 7143d404 2019-03-12 stsp continue;
3345 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3346 e34f9ed6 2019-02-02 stsp name += 6;
3347 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3348 141c2bff 2019-02-04 stsp name += 8;
3349 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3350 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3351 4343a07f 2020-04-24 stsp continue;
3352 4343a07f 2020-04-24 stsp }
3353 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3354 48cae60d 2020-09-22 stsp if (err)
3355 48cae60d 2020-09-22 stsp return err;
3356 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3357 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3358 5d844a1e 2019-08-13 stsp if (err) {
3359 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3360 48cae60d 2020-09-22 stsp free(ref_id);
3361 5d844a1e 2019-08-13 stsp return err;
3362 48cae60d 2020-09-22 stsp }
3363 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3364 5d844a1e 2019-08-13 stsp err = NULL;
3365 5d844a1e 2019-08-13 stsp tag = NULL;
3366 5d844a1e 2019-08-13 stsp }
3367 a436ad14 2019-08-13 stsp }
3368 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3369 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3370 48cae60d 2020-09-22 stsp free(ref_id);
3371 a436ad14 2019-08-13 stsp if (tag)
3372 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3373 a436ad14 2019-08-13 stsp if (cmp != 0)
3374 a436ad14 2019-08-13 stsp continue;
3375 199a4027 2019-02-02 stsp s = refs_str;
3376 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3377 199a4027 2019-02-02 stsp name) == -1) {
3378 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3379 199a4027 2019-02-02 stsp free(s);
3380 34ca4898 2019-08-13 stsp return err;
3381 199a4027 2019-02-02 stsp }
3382 199a4027 2019-02-02 stsp free(s);
3383 199a4027 2019-02-02 stsp }
3384 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3385 8bf5b3c9 2018-03-17 stsp if (err)
3386 8bf5b3c9 2018-03-17 stsp return err;
3387 788c352e 2018-06-16 stsp
3388 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3389 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3390 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3391 832c249c 2018-06-10 stsp free(id_str);
3392 d3d493d7 2019-02-21 stsp id_str = NULL;
3393 d3d493d7 2019-02-21 stsp free(refs_str);
3394 d3d493d7 2019-02-21 stsp refs_str = NULL;
3395 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3396 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3397 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3398 09867e48 2019-08-13 stsp if (datestr)
3399 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3400 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3401 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3402 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3403 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3404 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3405 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3406 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3407 3fe1abad 2018-06-10 stsp int n = 1;
3408 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3409 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3410 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3411 a0603db2 2018-06-10 stsp if (err)
3412 a0603db2 2018-06-10 stsp return err;
3413 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3414 a0603db2 2018-06-10 stsp free(id_str);
3415 a0603db2 2018-06-10 stsp }
3416 a0603db2 2018-06-10 stsp }
3417 832c249c 2018-06-10 stsp
3418 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3419 5943eee2 2019-08-13 stsp if (err)
3420 5943eee2 2019-08-13 stsp return err;
3421 8bf5b3c9 2018-03-17 stsp
3422 621015ac 2018-07-23 stsp logmsg = logmsg0;
3423 832c249c 2018-06-10 stsp do {
3424 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3425 832c249c 2018-06-10 stsp if (line)
3426 832c249c 2018-06-10 stsp printf(" %s\n", line);
3427 832c249c 2018-06-10 stsp } while (line);
3428 621015ac 2018-07-23 stsp free(logmsg0);
3429 832c249c 2018-06-10 stsp
3430 0208f208 2020-05-05 stsp if (changed_paths) {
3431 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3432 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3433 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3434 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3435 0208f208 2020-05-05 stsp }
3436 0208f208 2020-05-05 stsp printf("\n");
3437 0208f208 2020-05-05 stsp }
3438 971751ac 2018-03-27 stsp if (show_patch) {
3439 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3440 971751ac 2018-03-27 stsp if (err == 0)
3441 971751ac 2018-03-27 stsp printf("\n");
3442 971751ac 2018-03-27 stsp }
3443 07862c20 2018-09-15 stsp
3444 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3445 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3446 07862c20 2018-09-15 stsp return err;
3447 f42b1b34 2018-03-12 stsp }
3448 5c860e29 2018-03-12 stsp
3449 f42b1b34 2018-03-12 stsp static const struct got_error *
3450 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3451 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3452 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3453 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3454 f42b1b34 2018-03-12 stsp {
3455 f42b1b34 2018-03-12 stsp const struct got_error *err;
3456 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3457 6841bf13 2019-11-29 kn regex_t regex;
3458 6841bf13 2019-11-29 kn int have_match;
3459 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3460 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3461 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3462 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3463 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3464 dbec59df 2020-04-18 stsp
3465 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3466 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3467 372ccdbb 2018-06-10 stsp
3468 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3469 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3470 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3471 6841bf13 2019-11-29 kn
3472 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3473 f42b1b34 2018-03-12 stsp if (err)
3474 f42b1b34 2018-03-12 stsp return err;
3475 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3476 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3477 372ccdbb 2018-06-10 stsp if (err)
3478 fcc85cad 2018-07-22 stsp goto done;
3479 656b1f76 2019-05-11 jcs for (;;) {
3480 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3481 8bf5b3c9 2018-03-17 stsp
3482 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3483 84453469 2018-11-11 stsp break;
3484 84453469 2018-11-11 stsp
3485 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3486 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3487 372ccdbb 2018-06-10 stsp if (err) {
3488 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3489 9ba79e04 2018-06-11 stsp err = NULL;
3490 ee780d5c 2020-01-04 stsp break;
3491 7e665116 2018-04-02 stsp }
3492 b43fbaa0 2018-06-11 stsp if (id == NULL)
3493 7e665116 2018-04-02 stsp break;
3494 b43fbaa0 2018-06-11 stsp
3495 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3496 b43fbaa0 2018-06-11 stsp if (err)
3497 fcc85cad 2018-07-22 stsp break;
3498 6841bf13 2019-11-29 kn
3499 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3500 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3501 0208f208 2020-05-05 stsp if (err)
3502 0208f208 2020-05-05 stsp break;
3503 0208f208 2020-05-05 stsp }
3504 0208f208 2020-05-05 stsp
3505 6841bf13 2019-11-29 kn if (search_pattern) {
3506 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3507 6841bf13 2019-11-29 kn if (err) {
3508 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3509 6841bf13 2019-11-29 kn break;
3510 6841bf13 2019-11-29 kn }
3511 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3512 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3513 0208f208 2020-05-05 stsp &changed_paths, &regex);
3514 6841bf13 2019-11-29 kn if (have_match == 0) {
3515 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3516 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3517 0208f208 2020-05-05 stsp free((char *)pe->path);
3518 0208f208 2020-05-05 stsp free(pe->data);
3519 0208f208 2020-05-05 stsp }
3520 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3521 6841bf13 2019-11-29 kn continue;
3522 6841bf13 2019-11-29 kn }
3523 6841bf13 2019-11-29 kn }
3524 6841bf13 2019-11-29 kn
3525 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3526 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3527 dbec59df 2020-04-18 stsp if (err)
3528 dbec59df 2020-04-18 stsp break;
3529 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3530 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3531 dbec59df 2020-04-18 stsp } else {
3532 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3533 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3534 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3535 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3536 dbec59df 2020-04-18 stsp if (err)
3537 dbec59df 2020-04-18 stsp break;
3538 dbec59df 2020-04-18 stsp }
3539 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3540 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3541 7e665116 2018-04-02 stsp break;
3542 0208f208 2020-05-05 stsp
3543 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3544 0208f208 2020-05-05 stsp free((char *)pe->path);
3545 0208f208 2020-05-05 stsp free(pe->data);
3546 0208f208 2020-05-05 stsp }
3547 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3548 31cedeaf 2018-09-15 stsp }
3549 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3550 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3551 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3552 dbec59df 2020-04-18 stsp if (err)
3553 dbec59df 2020-04-18 stsp break;
3554 502b9684 2020-07-31 stsp if (show_changed_paths) {
3555 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3556 502b9684 2020-07-31 stsp commit, repo);
3557 502b9684 2020-07-31 stsp if (err)
3558 502b9684 2020-07-31 stsp break;
3559 502b9684 2020-07-31 stsp }
3560 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3561 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3562 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3563 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3564 dbec59df 2020-04-18 stsp if (err)
3565 dbec59df 2020-04-18 stsp break;
3566 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3567 502b9684 2020-07-31 stsp free((char *)pe->path);
3568 502b9684 2020-07-31 stsp free(pe->data);
3569 502b9684 2020-07-31 stsp }
3570 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3571 dbec59df 2020-04-18 stsp }
3572 dbec59df 2020-04-18 stsp }
3573 fcc85cad 2018-07-22 stsp done:
3574 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3575 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3576 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3577 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3578 dbec59df 2020-04-18 stsp }
3579 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3580 0208f208 2020-05-05 stsp free((char *)pe->path);
3581 0208f208 2020-05-05 stsp free(pe->data);
3582 0208f208 2020-05-05 stsp }
3583 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3584 6841bf13 2019-11-29 kn if (search_pattern)
3585 6841bf13 2019-11-29 kn regfree(&regex);
3586 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3587 f42b1b34 2018-03-12 stsp return err;
3588 f42b1b34 2018-03-12 stsp }
3589 5c860e29 2018-03-12 stsp
3590 4ed7e80c 2018-05-20 stsp __dead static void
3591 6f3d1eb0 2018-03-12 stsp usage_log(void)
3592 6f3d1eb0 2018-03-12 stsp {
3593 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3594 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3595 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3596 6f3d1eb0 2018-03-12 stsp exit(1);
3597 b1ebc001 2019-08-13 stsp }
3598 b1ebc001 2019-08-13 stsp
3599 b1ebc001 2019-08-13 stsp static int
3600 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3601 b1ebc001 2019-08-13 stsp {
3602 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3603 b1ebc001 2019-08-13 stsp long long n;
3604 b1ebc001 2019-08-13 stsp const char *errstr;
3605 b1ebc001 2019-08-13 stsp
3606 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3607 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3608 b1ebc001 2019-08-13 stsp return 0;
3609 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3610 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3611 b1ebc001 2019-08-13 stsp return 0;
3612 b1ebc001 2019-08-13 stsp return n;
3613 d1fe46f9 2020-04-18 stsp }
3614 d1fe46f9 2020-04-18 stsp
3615 d1fe46f9 2020-04-18 stsp static const struct got_error *
3616 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3617 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3618 d1fe46f9 2020-04-18 stsp {
3619 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3620 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3621 d1fe46f9 2020-04-18 stsp
3622 d1fe46f9 2020-04-18 stsp *id = NULL;
3623 d1fe46f9 2020-04-18 stsp
3624 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3625 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3626 d1fe46f9 2020-04-18 stsp int obj_type;
3627 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3628 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3629 d1fe46f9 2020-04-18 stsp if (err)
3630 d1fe46f9 2020-04-18 stsp return err;
3631 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3632 d1fe46f9 2020-04-18 stsp if (err)
3633 d1fe46f9 2020-04-18 stsp return err;
3634 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3635 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3636 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3637 d1fe46f9 2020-04-18 stsp if (err)
3638 d1fe46f9 2020-04-18 stsp return err;
3639 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3640 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3641 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3642 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3643 d1fe46f9 2020-04-18 stsp }
3644 d1fe46f9 2020-04-18 stsp free(*id);
3645 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3646 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3647 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3648 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3649 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3650 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3651 d1fe46f9 2020-04-18 stsp if (err)
3652 d1fe46f9 2020-04-18 stsp return err;
3653 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3654 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3655 d1fe46f9 2020-04-18 stsp } else {
3656 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3657 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3658 d1fe46f9 2020-04-18 stsp }
3659 d1fe46f9 2020-04-18 stsp
3660 d1fe46f9 2020-04-18 stsp return err;
3661 6f3d1eb0 2018-03-12 stsp }
3662 6f3d1eb0 2018-03-12 stsp
3663 4ed7e80c 2018-05-20 stsp static const struct got_error *
3664 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3665 f42b1b34 2018-03-12 stsp {
3666 f42b1b34 2018-03-12 stsp const struct got_error *error;
3667 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3668 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3669 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3670 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3671 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3672 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3673 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3674 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3675 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3676 64a96a6d 2018-04-01 stsp const char *errstr;
3677 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3678 1b3893a2 2019-03-18 stsp
3679 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3680 5c860e29 2018-03-12 stsp
3681 6715a751 2018-03-16 stsp #ifndef PROFILE
3682 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3683 6098196c 2019-01-04 stsp NULL)
3684 ad242220 2018-09-08 stsp == -1)
3685 f42b1b34 2018-03-12 stsp err(1, "pledge");
3686 6715a751 2018-03-16 stsp #endif
3687 79109fed 2018-03-27 stsp
3688 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3689 b1ebc001 2019-08-13 stsp
3690 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3691 79109fed 2018-03-27 stsp switch (ch) {
3692 79109fed 2018-03-27 stsp case 'p':
3693 79109fed 2018-03-27 stsp show_patch = 1;
3694 d142fc45 2018-04-01 stsp break;
3695 0208f208 2020-05-05 stsp case 'P':
3696 0208f208 2020-05-05 stsp show_changed_paths = 1;
3697 0208f208 2020-05-05 stsp break;
3698 d142fc45 2018-04-01 stsp case 'c':
3699 d142fc45 2018-04-01 stsp start_commit = optarg;
3700 64a96a6d 2018-04-01 stsp break;
3701 c0cc5c62 2018-10-18 stsp case 'C':
3702 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3703 4a8520aa 2018-10-18 stsp &errstr);
3704 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3705 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3706 c0cc5c62 2018-10-18 stsp break;
3707 64a96a6d 2018-04-01 stsp case 'l':
3708 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3709 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3710 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3711 cc54c501 2019-07-15 stsp break;
3712 48c8c60d 2020-01-27 stsp case 'b':
3713 48c8c60d 2020-01-27 stsp log_branches = 1;
3714 a0603db2 2018-06-10 stsp break;
3715 04ca23f4 2018-07-16 stsp case 'r':
3716 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3717 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3718 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3719 9ba1d308 2019-10-21 stsp optarg);
3720 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3721 04ca23f4 2018-07-16 stsp break;
3722 dbec59df 2020-04-18 stsp case 'R':
3723 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3724 dbec59df 2020-04-18 stsp break;
3725 6841bf13 2019-11-29 kn case 's':
3726 6841bf13 2019-11-29 kn search_pattern = optarg;
3727 6841bf13 2019-11-29 kn break;
3728 d1fe46f9 2020-04-18 stsp case 'x':
3729 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3730 d1fe46f9 2020-04-18 stsp break;
3731 79109fed 2018-03-27 stsp default:
3732 2deda0b9 2019-03-07 stsp usage_log();
3733 79109fed 2018-03-27 stsp /* NOTREACHED */
3734 79109fed 2018-03-27 stsp }
3735 79109fed 2018-03-27 stsp }
3736 79109fed 2018-03-27 stsp
3737 79109fed 2018-03-27 stsp argc -= optind;
3738 79109fed 2018-03-27 stsp argv += optind;
3739 f42b1b34 2018-03-12 stsp
3740 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3741 dc1edbfa 2019-11-29 kn diff_context = 3;
3742 dc1edbfa 2019-11-29 kn else if (!show_patch)
3743 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3744 dc1edbfa 2019-11-29 kn
3745 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3746 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3747 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3748 04ca23f4 2018-07-16 stsp goto done;
3749 04ca23f4 2018-07-16 stsp }
3750 cffc0aa4 2019-02-05 stsp
3751 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3752 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3753 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3754 50f2fada 2020-04-24 stsp goto done;
3755 50f2fada 2020-04-24 stsp error = NULL;
3756 50f2fada 2020-04-24 stsp }
3757 cffc0aa4 2019-02-05 stsp
3758 e7301579 2019-03-18 stsp if (argc == 0) {
3759 cbd1af7a 2019-03-18 stsp path = strdup("");
3760 e7301579 2019-03-18 stsp if (path == NULL) {
3761 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3762 cbd1af7a 2019-03-18 stsp goto done;
3763 e7301579 2019-03-18 stsp }
3764 e7301579 2019-03-18 stsp } else if (argc == 1) {
3765 e7301579 2019-03-18 stsp if (worktree) {
3766 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3767 e7301579 2019-03-18 stsp argv[0]);
3768 e7301579 2019-03-18 stsp if (error)
3769 e7301579 2019-03-18 stsp goto done;
3770 e7301579 2019-03-18 stsp } else {
3771 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3772 e7301579 2019-03-18 stsp if (path == NULL) {
3773 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3774 e7301579 2019-03-18 stsp goto done;
3775 e7301579 2019-03-18 stsp }
3776 e7301579 2019-03-18 stsp }
3777 cbd1af7a 2019-03-18 stsp } else
3778 cbd1af7a 2019-03-18 stsp usage_log();
3779 cbd1af7a 2019-03-18 stsp
3780 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3781 5486daa2 2019-05-11 stsp repo_path = worktree ?
3782 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3783 5486daa2 2019-05-11 stsp }
3784 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3785 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3786 cffc0aa4 2019-02-05 stsp goto done;
3787 04ca23f4 2018-07-16 stsp }
3788 6098196c 2019-01-04 stsp
3789 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3790 d7d4f210 2018-03-12 stsp if (error != NULL)
3791 04ca23f4 2018-07-16 stsp goto done;
3792 f42b1b34 2018-03-12 stsp
3793 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3794 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3795 c02c541e 2019-03-29 stsp if (error)
3796 c02c541e 2019-03-29 stsp goto done;
3797 c02c541e 2019-03-29 stsp
3798 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3799 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3800 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3801 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3802 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3803 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3804 3235492e 2018-04-01 stsp if (error != NULL)
3805 d1fe46f9 2020-04-18 stsp goto done;
3806 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3807 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3808 3235492e 2018-04-01 stsp if (error != NULL)
3809 d1fe46f9 2020-04-18 stsp goto done;
3810 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3811 d1fe46f9 2020-04-18 stsp start_id);
3812 d1fe46f9 2020-04-18 stsp if (error != NULL)
3813 d1fe46f9 2020-04-18 stsp goto done;
3814 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3815 3235492e 2018-04-01 stsp } else {
3816 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3817 d1fe46f9 2020-04-18 stsp if (error != NULL)
3818 d1fe46f9 2020-04-18 stsp goto done;
3819 3235492e 2018-04-01 stsp }
3820 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3821 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3822 d1fe46f9 2020-04-18 stsp if (error != NULL)
3823 d1fe46f9 2020-04-18 stsp goto done;
3824 d1fe46f9 2020-04-18 stsp }
3825 04ca23f4 2018-07-16 stsp
3826 deeabeae 2019-08-27 stsp if (worktree) {
3827 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3828 deeabeae 2019-08-27 stsp char *p;
3829 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3830 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3831 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3832 deeabeae 2019-08-27 stsp goto done;
3833 deeabeae 2019-08-27 stsp }
3834 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3835 deeabeae 2019-08-27 stsp free(p);
3836 deeabeae 2019-08-27 stsp } else
3837 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3838 04ca23f4 2018-07-16 stsp if (error != NULL)
3839 04ca23f4 2018-07-16 stsp goto done;
3840 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3841 04ca23f4 2018-07-16 stsp free(path);
3842 04ca23f4 2018-07-16 stsp path = in_repo_path;
3843 04ca23f4 2018-07-16 stsp }
3844 199a4027 2019-02-02 stsp
3845 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3846 199a4027 2019-02-02 stsp if (error)
3847 199a4027 2019-02-02 stsp goto done;
3848 04ca23f4 2018-07-16 stsp
3849 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3850 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3851 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3852 04ca23f4 2018-07-16 stsp done:
3853 04ca23f4 2018-07-16 stsp free(path);
3854 04ca23f4 2018-07-16 stsp free(repo_path);
3855 04ca23f4 2018-07-16 stsp free(cwd);
3856 cffc0aa4 2019-02-05 stsp if (worktree)
3857 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3858 ad242220 2018-09-08 stsp if (repo) {
3859 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3860 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3861 ad242220 2018-09-08 stsp if (error == NULL)
3862 ad242220 2018-09-08 stsp error = repo_error;
3863 ad242220 2018-09-08 stsp }
3864 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3865 8bf5b3c9 2018-03-17 stsp return error;
3866 5c860e29 2018-03-12 stsp }
3867 5c860e29 2018-03-12 stsp
3868 4ed7e80c 2018-05-20 stsp __dead static void
3869 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3870 3f8b7d6a 2018-04-01 stsp {
3871 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3872 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3873 3f8b7d6a 2018-04-01 stsp exit(1);
3874 b00d56cd 2018-04-01 stsp }
3875 b00d56cd 2018-04-01 stsp
3876 b72f483a 2019-02-05 stsp struct print_diff_arg {
3877 b72f483a 2019-02-05 stsp struct got_repository *repo;
3878 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3879 b72f483a 2019-02-05 stsp int diff_context;
3880 3fc0c068 2019-02-10 stsp const char *id_str;
3881 3fc0c068 2019-02-10 stsp int header_shown;
3882 98eaaa12 2019-08-03 stsp int diff_staged;
3883 63035f9f 2019-10-06 stsp int ignore_whitespace;
3884 b72f483a 2019-02-05 stsp };
3885 39449a05 2020-07-23 stsp
3886 39449a05 2020-07-23 stsp /*
3887 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3888 39449a05 2020-07-23 stsp * it as content to the diff engine.
3889 39449a05 2020-07-23 stsp */
3890 39449a05 2020-07-23 stsp static const struct got_error *
3891 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3892 39449a05 2020-07-23 stsp const char *abspath)
3893 39449a05 2020-07-23 stsp {
3894 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3895 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3896 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3897 39449a05 2020-07-23 stsp
3898 39449a05 2020-07-23 stsp *fd = -1;
3899 39449a05 2020-07-23 stsp
3900 39449a05 2020-07-23 stsp if (dirfd != -1) {
3901 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3902 39449a05 2020-07-23 stsp if (target_len == -1)
3903 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3904 39449a05 2020-07-23 stsp } else {
3905 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3906 39449a05 2020-07-23 stsp if (target_len == -1)
3907 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3908 39449a05 2020-07-23 stsp }
3909 39449a05 2020-07-23 stsp
3910 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3911 39449a05 2020-07-23 stsp if (*fd == -1)
3912 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3913 39449a05 2020-07-23 stsp
3914 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3915 39449a05 2020-07-23 stsp if (outlen == -1) {
3916 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3917 39449a05 2020-07-23 stsp goto done;
3918 39449a05 2020-07-23 stsp }
3919 39449a05 2020-07-23 stsp
3920 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3921 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3922 39449a05 2020-07-23 stsp goto done;
3923 39449a05 2020-07-23 stsp }
3924 39449a05 2020-07-23 stsp done:
3925 39449a05 2020-07-23 stsp if (err) {
3926 39449a05 2020-07-23 stsp close(*fd);
3927 39449a05 2020-07-23 stsp *fd = -1;
3928 39449a05 2020-07-23 stsp }
3929 39449a05 2020-07-23 stsp return err;
3930 39449a05 2020-07-23 stsp }
3931 b72f483a 2019-02-05 stsp
3932 4ed7e80c 2018-05-20 stsp static const struct got_error *
3933 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3934 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3935 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3936 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3937 b72f483a 2019-02-05 stsp {
3938 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3939 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3940 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3941 12463d8b 2019-12-13 stsp int fd = -1;
3942 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3943 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3944 b72f483a 2019-02-05 stsp struct stat sb;
3945 b72f483a 2019-02-05 stsp
3946 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3947 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3948 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3949 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3950 98eaaa12 2019-08-03 stsp return NULL;
3951 98eaaa12 2019-08-03 stsp } else {
3952 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3953 98eaaa12 2019-08-03 stsp return NULL;
3954 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3955 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3956 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3957 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3958 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3959 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3960 98eaaa12 2019-08-03 stsp return NULL;
3961 98eaaa12 2019-08-03 stsp }
3962 3fc0c068 2019-02-10 stsp
3963 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3964 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3965 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3966 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3967 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3968 3fc0c068 2019-02-10 stsp }
3969 b72f483a 2019-02-05 stsp
3970 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3971 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3972 98eaaa12 2019-08-03 stsp switch (staged_status) {
3973 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3974 98eaaa12 2019-08-03 stsp label1 = path;
3975 98eaaa12 2019-08-03 stsp label2 = path;
3976 98eaaa12 2019-08-03 stsp break;
3977 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3978 98eaaa12 2019-08-03 stsp label2 = path;
3979 98eaaa12 2019-08-03 stsp break;
3980 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3981 98eaaa12 2019-08-03 stsp label1 = path;
3982 98eaaa12 2019-08-03 stsp break;
3983 98eaaa12 2019-08-03 stsp default:
3984 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3985 98eaaa12 2019-08-03 stsp }
3986 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3987 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3988 63035f9f 2019-10-06 stsp a->repo, stdout);
3989 98eaaa12 2019-08-03 stsp }
3990 98eaaa12 2019-08-03 stsp
3991 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3992 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3993 4ce46740 2019-08-08 stsp char *id_str;
3994 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3995 408b4ebc 2019-08-03 stsp 8192);
3996 4ce46740 2019-08-08 stsp if (err)
3997 4ce46740 2019-08-08 stsp goto done;
3998 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3999 4ce46740 2019-08-08 stsp if (err)
4000 4ce46740 2019-08-08 stsp goto done;
4001 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4002 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4003 4ce46740 2019-08-08 stsp free(id_str);
4004 4ce46740 2019-08-08 stsp goto done;
4005 4ce46740 2019-08-08 stsp }
4006 4ce46740 2019-08-08 stsp free(id_str);
4007 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4008 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4009 4ce46740 2019-08-08 stsp if (err)
4010 4ce46740 2019-08-08 stsp goto done;
4011 4ce46740 2019-08-08 stsp }
4012 d00136be 2019-03-26 stsp
4013 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4014 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4015 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4016 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4017 2ec1f75b 2019-03-26 stsp goto done;
4018 2ec1f75b 2019-03-26 stsp }
4019 b72f483a 2019-02-05 stsp
4020 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4021 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4022 12463d8b 2019-12-13 stsp if (fd == -1) {
4023 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4024 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4025 39449a05 2020-07-23 stsp abspath);
4026 39449a05 2020-07-23 stsp goto done;
4027 39449a05 2020-07-23 stsp }
4028 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4029 39449a05 2020-07-23 stsp de_name, abspath);
4030 39449a05 2020-07-23 stsp if (err)
4031 39449a05 2020-07-23 stsp goto done;
4032 12463d8b 2019-12-13 stsp }
4033 12463d8b 2019-12-13 stsp } else {
4034 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4035 12463d8b 2019-12-13 stsp if (fd == -1) {
4036 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4037 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4038 39449a05 2020-07-23 stsp abspath);
4039 39449a05 2020-07-23 stsp goto done;
4040 39449a05 2020-07-23 stsp }
4041 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4042 39449a05 2020-07-23 stsp de_name, abspath);
4043 39449a05 2020-07-23 stsp if (err)
4044 39449a05 2020-07-23 stsp goto done;
4045 12463d8b 2019-12-13 stsp }
4046 12463d8b 2019-12-13 stsp }
4047 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4048 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4049 2ec1f75b 2019-03-26 stsp goto done;
4050 2ec1f75b 2019-03-26 stsp }
4051 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4052 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4053 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4054 2ec1f75b 2019-03-26 stsp goto done;
4055 2ec1f75b 2019-03-26 stsp }
4056 12463d8b 2019-12-13 stsp fd = -1;
4057 2ec1f75b 2019-03-26 stsp } else
4058 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4059 b72f483a 2019-02-05 stsp
4060 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4061 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
4062 b72f483a 2019-02-05 stsp done:
4063 b72f483a 2019-02-05 stsp if (blob1)
4064 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4065 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4066 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4067 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4068 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4069 b72f483a 2019-02-05 stsp free(abspath);
4070 927df6b7 2019-02-10 stsp return err;
4071 927df6b7 2019-02-10 stsp }
4072 927df6b7 2019-02-10 stsp
4073 927df6b7 2019-02-10 stsp static const struct got_error *
4074 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4075 b00d56cd 2018-04-01 stsp {
4076 b00d56cd 2018-04-01 stsp const struct got_error *error;
4077 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4078 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4079 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4080 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4081 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4082 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4083 15a94983 2018-12-23 stsp int type1, type2;
4084 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4085 c0cc5c62 2018-10-18 stsp const char *errstr;
4086 927df6b7 2019-02-10 stsp char *path = NULL;
4087 b00d56cd 2018-04-01 stsp
4088 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4089 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4090 25eccc22 2019-01-04 stsp NULL) == -1)
4091 b00d56cd 2018-04-01 stsp err(1, "pledge");
4092 b00d56cd 2018-04-01 stsp #endif
4093 b00d56cd 2018-04-01 stsp
4094 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
4095 b00d56cd 2018-04-01 stsp switch (ch) {
4096 c0cc5c62 2018-10-18 stsp case 'C':
4097 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4098 dfcab68b 2019-11-29 kn &errstr);
4099 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4100 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4101 c0cc5c62 2018-10-18 stsp break;
4102 b72f483a 2019-02-05 stsp case 'r':
4103 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4104 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4105 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4106 9ba1d308 2019-10-21 stsp optarg);
4107 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4108 b72f483a 2019-02-05 stsp break;
4109 98eaaa12 2019-08-03 stsp case 's':
4110 98eaaa12 2019-08-03 stsp diff_staged = 1;
4111 63035f9f 2019-10-06 stsp break;
4112 63035f9f 2019-10-06 stsp case 'w':
4113 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4114 98eaaa12 2019-08-03 stsp break;
4115 b00d56cd 2018-04-01 stsp default:
4116 2deda0b9 2019-03-07 stsp usage_diff();
4117 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4118 b00d56cd 2018-04-01 stsp }
4119 b00d56cd 2018-04-01 stsp }
4120 b00d56cd 2018-04-01 stsp
4121 b00d56cd 2018-04-01 stsp argc -= optind;
4122 b00d56cd 2018-04-01 stsp argv += optind;
4123 b00d56cd 2018-04-01 stsp
4124 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4125 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4126 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4127 b72f483a 2019-02-05 stsp goto done;
4128 b72f483a 2019-02-05 stsp }
4129 927df6b7 2019-02-10 stsp if (argc <= 1) {
4130 b72f483a 2019-02-05 stsp if (repo_path)
4131 b72f483a 2019-02-05 stsp errx(1,
4132 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4133 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4134 fa51e947 2020-03-27 stsp if (error) {
4135 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4136 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4137 fa51e947 2020-03-27 stsp cwd);
4138 1f03b8da 2020-03-20 stsp goto done;
4139 fa51e947 2020-03-27 stsp }
4140 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4141 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4142 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4143 927df6b7 2019-02-10 stsp goto done;
4144 927df6b7 2019-02-10 stsp }
4145 927df6b7 2019-02-10 stsp if (argc == 1) {
4146 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4147 cbd1af7a 2019-03-18 stsp argv[0]);
4148 927df6b7 2019-02-10 stsp if (error)
4149 927df6b7 2019-02-10 stsp goto done;
4150 927df6b7 2019-02-10 stsp } else {
4151 927df6b7 2019-02-10 stsp path = strdup("");
4152 927df6b7 2019-02-10 stsp if (path == NULL) {
4153 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4154 927df6b7 2019-02-10 stsp goto done;
4155 927df6b7 2019-02-10 stsp }
4156 927df6b7 2019-02-10 stsp }
4157 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4158 98eaaa12 2019-08-03 stsp if (diff_staged)
4159 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4160 98eaaa12 2019-08-03 stsp "objects in repository");
4161 15a94983 2018-12-23 stsp id_str1 = argv[0];
4162 15a94983 2018-12-23 stsp id_str2 = argv[1];
4163 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4164 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4165 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4166 30db809c 2019-06-05 stsp goto done;
4167 1f03b8da 2020-03-20 stsp if (worktree) {
4168 1f03b8da 2020-03-20 stsp repo_path = strdup(
4169 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4170 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4171 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4172 1f03b8da 2020-03-20 stsp goto done;
4173 1f03b8da 2020-03-20 stsp }
4174 1f03b8da 2020-03-20 stsp } else {
4175 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4176 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4177 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4178 1f03b8da 2020-03-20 stsp goto done;
4179 1f03b8da 2020-03-20 stsp }
4180 30db809c 2019-06-05 stsp }
4181 30db809c 2019-06-05 stsp }
4182 b00d56cd 2018-04-01 stsp } else
4183 b00d56cd 2018-04-01 stsp usage_diff();
4184 b00d56cd 2018-04-01 stsp
4185 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4186 76089277 2018-04-01 stsp free(repo_path);
4187 b00d56cd 2018-04-01 stsp if (error != NULL)
4188 b00d56cd 2018-04-01 stsp goto done;
4189 b00d56cd 2018-04-01 stsp
4190 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4191 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4192 c02c541e 2019-03-29 stsp if (error)
4193 c02c541e 2019-03-29 stsp goto done;
4194 c02c541e 2019-03-29 stsp
4195 30db809c 2019-06-05 stsp if (argc <= 1) {
4196 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4197 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4198 b72f483a 2019-02-05 stsp char *id_str;
4199 72ea6654 2019-07-27 stsp
4200 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4201 72ea6654 2019-07-27 stsp
4202 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4203 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4204 b72f483a 2019-02-05 stsp if (error)
4205 b72f483a 2019-02-05 stsp goto done;
4206 b72f483a 2019-02-05 stsp arg.repo = repo;
4207 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4208 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4209 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4210 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4211 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4212 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4213 b72f483a 2019-02-05 stsp
4214 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4215 72ea6654 2019-07-27 stsp if (error)
4216 72ea6654 2019-07-27 stsp goto done;
4217 72ea6654 2019-07-27 stsp
4218 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4219 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4220 b72f483a 2019-02-05 stsp free(id_str);
4221 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4222 b72f483a 2019-02-05 stsp goto done;
4223 b72f483a 2019-02-05 stsp }
4224 b72f483a 2019-02-05 stsp
4225 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4226 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4227 0adb7196 2019-08-11 stsp if (error)
4228 0adb7196 2019-08-11 stsp goto done;
4229 b00d56cd 2018-04-01 stsp
4230 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4231 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4232 0adb7196 2019-08-11 stsp if (error)
4233 0adb7196 2019-08-11 stsp goto done;
4234 b00d56cd 2018-04-01 stsp
4235 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4236 15a94983 2018-12-23 stsp if (error)
4237 15a94983 2018-12-23 stsp goto done;
4238 15a94983 2018-12-23 stsp
4239 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4240 15a94983 2018-12-23 stsp if (error)
4241 15a94983 2018-12-23 stsp goto done;
4242 15a94983 2018-12-23 stsp
4243 15a94983 2018-12-23 stsp if (type1 != type2) {
4244 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4245 b00d56cd 2018-04-01 stsp goto done;
4246 b00d56cd 2018-04-01 stsp }
4247 b00d56cd 2018-04-01 stsp
4248 15a94983 2018-12-23 stsp switch (type1) {
4249 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4250 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4251 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4252 b00d56cd 2018-04-01 stsp break;
4253 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4254 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4255 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4256 b00d56cd 2018-04-01 stsp break;
4257 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4258 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4259 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4260 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4261 b00d56cd 2018-04-01 stsp break;
4262 b00d56cd 2018-04-01 stsp default:
4263 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4264 b00d56cd 2018-04-01 stsp }
4265 b00d56cd 2018-04-01 stsp done:
4266 5e70831e 2019-05-28 stsp free(label1);
4267 5e70831e 2019-05-28 stsp free(label2);
4268 15a94983 2018-12-23 stsp free(id1);
4269 15a94983 2018-12-23 stsp free(id2);
4270 927df6b7 2019-02-10 stsp free(path);
4271 b72f483a 2019-02-05 stsp if (worktree)
4272 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4273 ad242220 2018-09-08 stsp if (repo) {
4274 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4275 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4276 ad242220 2018-09-08 stsp if (error == NULL)
4277 ad242220 2018-09-08 stsp error = repo_error;
4278 ad242220 2018-09-08 stsp }
4279 b00d56cd 2018-04-01 stsp return error;
4280 404c43c4 2018-06-21 stsp }
4281 404c43c4 2018-06-21 stsp
4282 404c43c4 2018-06-21 stsp __dead static void
4283 404c43c4 2018-06-21 stsp usage_blame(void)
4284 404c43c4 2018-06-21 stsp {
4285 9270e621 2019-02-05 stsp fprintf(stderr,
4286 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4287 404c43c4 2018-06-21 stsp getprogname());
4288 404c43c4 2018-06-21 stsp exit(1);
4289 b00d56cd 2018-04-01 stsp }
4290 b00d56cd 2018-04-01 stsp
4291 28315671 2019-08-14 stsp struct blame_line {
4292 28315671 2019-08-14 stsp int annotated;
4293 28315671 2019-08-14 stsp char *id_str;
4294 82f6abb8 2019-08-14 stsp char *committer;
4295 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4296 28315671 2019-08-14 stsp };
4297 28315671 2019-08-14 stsp
4298 28315671 2019-08-14 stsp struct blame_cb_args {
4299 28315671 2019-08-14 stsp struct blame_line *lines;
4300 28315671 2019-08-14 stsp int nlines;
4301 7ef28ff8 2019-08-14 stsp int nlines_prec;
4302 28315671 2019-08-14 stsp int lineno_cur;
4303 28315671 2019-08-14 stsp off_t *line_offsets;
4304 28315671 2019-08-14 stsp FILE *f;
4305 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4306 28315671 2019-08-14 stsp };
4307 28315671 2019-08-14 stsp
4308 404c43c4 2018-06-21 stsp static const struct got_error *
4309 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4310 28315671 2019-08-14 stsp {
4311 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4312 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4313 28315671 2019-08-14 stsp struct blame_line *bline;
4314 82f6abb8 2019-08-14 stsp char *line = NULL;
4315 28315671 2019-08-14 stsp size_t linesize = 0;
4316 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4317 28315671 2019-08-14 stsp off_t offset;
4318 bcb49d15 2019-08-14 stsp struct tm tm;
4319 bcb49d15 2019-08-14 stsp time_t committer_time;
4320 28315671 2019-08-14 stsp
4321 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4322 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4323 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4324 28315671 2019-08-14 stsp
4325 28315671 2019-08-14 stsp if (sigint_received)
4326 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4327 28315671 2019-08-14 stsp
4328 2839f8b9 2019-08-18 stsp if (lineno == -1)
4329 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4330 2839f8b9 2019-08-18 stsp
4331 28315671 2019-08-14 stsp /* Annotate this line. */
4332 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4333 28315671 2019-08-14 stsp if (bline->annotated)
4334 28315671 2019-08-14 stsp return NULL;
4335 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4336 28315671 2019-08-14 stsp if (err)
4337 28315671 2019-08-14 stsp return err;
4338 82f6abb8 2019-08-14 stsp
4339 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4340 82f6abb8 2019-08-14 stsp if (err)
4341 82f6abb8 2019-08-14 stsp goto done;
4342 82f6abb8 2019-08-14 stsp
4343 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4344 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4345 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4346 bcb49d15 2019-08-14 stsp goto done;
4347 bcb49d15 2019-08-14 stsp }
4348 bcb49d15 2019-08-14 stsp
4349 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4350 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4351 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4352 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4353 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4354 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4355 82f6abb8 2019-08-14 stsp goto done;
4356 82f6abb8 2019-08-14 stsp }
4357 28315671 2019-08-14 stsp bline->annotated = 1;
4358 28315671 2019-08-14 stsp
4359 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4360 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4361 28315671 2019-08-14 stsp if (!bline->annotated)
4362 82f6abb8 2019-08-14 stsp goto done;
4363 28315671 2019-08-14 stsp
4364 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4365 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4366 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4367 82f6abb8 2019-08-14 stsp goto done;
4368 82f6abb8 2019-08-14 stsp }
4369 28315671 2019-08-14 stsp
4370 28315671 2019-08-14 stsp while (bline->annotated) {
4371 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4372 82f6abb8 2019-08-14 stsp size_t len;
4373 82f6abb8 2019-08-14 stsp
4374 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4375 28315671 2019-08-14 stsp if (ferror(a->f))
4376 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4377 28315671 2019-08-14 stsp break;
4378 28315671 2019-08-14 stsp }
4379 82f6abb8 2019-08-14 stsp
4380 82f6abb8 2019-08-14 stsp committer = bline->committer;
4381 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4382 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4383 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4384 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4385 82f6abb8 2019-08-14 stsp if (at)
4386 82f6abb8 2019-08-14 stsp *at = '\0';
4387 82f6abb8 2019-08-14 stsp len = strlen(committer);
4388 82f6abb8 2019-08-14 stsp if (len >= 9)
4389 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4390 28315671 2019-08-14 stsp
4391 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4392 28315671 2019-08-14 stsp if (nl)
4393 28315671 2019-08-14 stsp *nl = '\0';
4394 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4395 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4396 28315671 2019-08-14 stsp
4397 28315671 2019-08-14 stsp a->lineno_cur++;
4398 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4399 28315671 2019-08-14 stsp }
4400 82f6abb8 2019-08-14 stsp done:
4401 82f6abb8 2019-08-14 stsp if (commit)
4402 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4403 28315671 2019-08-14 stsp free(line);
4404 28315671 2019-08-14 stsp return err;
4405 28315671 2019-08-14 stsp }
4406 28315671 2019-08-14 stsp
4407 28315671 2019-08-14 stsp static const struct got_error *
4408 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4409 404c43c4 2018-06-21 stsp {
4410 404c43c4 2018-06-21 stsp const struct got_error *error;
4411 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4412 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4413 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4414 0587e10c 2020-07-23 stsp char *link_target = NULL;
4415 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4416 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4417 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4418 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4419 28315671 2019-08-14 stsp struct blame_cb_args bca;
4420 28315671 2019-08-14 stsp int ch, obj_type, i;
4421 b02560ec 2019-08-19 stsp size_t filesize;
4422 90f3c347 2019-08-19 stsp
4423 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4424 404c43c4 2018-06-21 stsp
4425 404c43c4 2018-06-21 stsp #ifndef PROFILE
4426 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4427 36e2fb66 2019-01-04 stsp NULL) == -1)
4428 404c43c4 2018-06-21 stsp err(1, "pledge");
4429 404c43c4 2018-06-21 stsp #endif
4430 404c43c4 2018-06-21 stsp
4431 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4432 404c43c4 2018-06-21 stsp switch (ch) {
4433 404c43c4 2018-06-21 stsp case 'c':
4434 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4435 404c43c4 2018-06-21 stsp break;
4436 66bea077 2018-08-02 stsp case 'r':
4437 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4438 66bea077 2018-08-02 stsp if (repo_path == NULL)
4439 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4440 9ba1d308 2019-10-21 stsp optarg);
4441 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4442 66bea077 2018-08-02 stsp break;
4443 404c43c4 2018-06-21 stsp default:
4444 2deda0b9 2019-03-07 stsp usage_blame();
4445 404c43c4 2018-06-21 stsp /* NOTREACHED */
4446 404c43c4 2018-06-21 stsp }
4447 404c43c4 2018-06-21 stsp }
4448 404c43c4 2018-06-21 stsp
4449 404c43c4 2018-06-21 stsp argc -= optind;
4450 404c43c4 2018-06-21 stsp argv += optind;
4451 404c43c4 2018-06-21 stsp
4452 a39318fd 2018-08-02 stsp if (argc == 1)
4453 404c43c4 2018-06-21 stsp path = argv[0];
4454 a39318fd 2018-08-02 stsp else
4455 404c43c4 2018-06-21 stsp usage_blame();
4456 404c43c4 2018-06-21 stsp
4457 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4458 66bea077 2018-08-02 stsp if (cwd == NULL) {
4459 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4460 66bea077 2018-08-02 stsp goto done;
4461 66bea077 2018-08-02 stsp }
4462 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4463 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4464 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4465 66bea077 2018-08-02 stsp goto done;
4466 0c06baac 2019-02-05 stsp else
4467 0c06baac 2019-02-05 stsp error = NULL;
4468 0c06baac 2019-02-05 stsp if (worktree) {
4469 0c06baac 2019-02-05 stsp repo_path =
4470 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4471 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4472 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4473 1f03b8da 2020-03-20 stsp if (error)
4474 1f03b8da 2020-03-20 stsp goto done;
4475 1f03b8da 2020-03-20 stsp }
4476 0c06baac 2019-02-05 stsp } else {
4477 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4478 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4479 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4480 0c06baac 2019-02-05 stsp goto done;
4481 0c06baac 2019-02-05 stsp }
4482 66bea077 2018-08-02 stsp }
4483 66bea077 2018-08-02 stsp }
4484 36e2fb66 2019-01-04 stsp
4485 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4486 c02c541e 2019-03-29 stsp if (error != NULL)
4487 36e2fb66 2019-01-04 stsp goto done;
4488 66bea077 2018-08-02 stsp
4489 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4490 c02c541e 2019-03-29 stsp if (error)
4491 404c43c4 2018-06-21 stsp goto done;
4492 404c43c4 2018-06-21 stsp
4493 0c06baac 2019-02-05 stsp if (worktree) {
4494 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4495 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4496 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4497 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4498 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4499 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4500 6efaaa2d 2019-02-05 stsp path) == -1) {
4501 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4502 0c06baac 2019-02-05 stsp goto done;
4503 0c06baac 2019-02-05 stsp }
4504 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4505 0c06baac 2019-02-05 stsp free(p);
4506 0c06baac 2019-02-05 stsp } else {
4507 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4508 0c06baac 2019-02-05 stsp }
4509 0c06baac 2019-02-05 stsp if (error)
4510 66bea077 2018-08-02 stsp goto done;
4511 66bea077 2018-08-02 stsp
4512 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4513 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4514 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4515 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4516 404c43c4 2018-06-21 stsp if (error != NULL)
4517 66bea077 2018-08-02 stsp goto done;
4518 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4519 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4520 404c43c4 2018-06-21 stsp if (error != NULL)
4521 66bea077 2018-08-02 stsp goto done;
4522 404c43c4 2018-06-21 stsp } else {
4523 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4524 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4525 30837e32 2019-07-25 stsp if (error)
4526 66bea077 2018-08-02 stsp goto done;
4527 404c43c4 2018-06-21 stsp }
4528 404c43c4 2018-06-21 stsp
4529 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4530 0587e10c 2020-07-23 stsp commit_id, repo);
4531 0587e10c 2020-07-23 stsp if (error)
4532 0587e10c 2020-07-23 stsp goto done;
4533 0587e10c 2020-07-23 stsp
4534 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4535 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4536 28315671 2019-08-14 stsp if (error)
4537 28315671 2019-08-14 stsp goto done;
4538 28315671 2019-08-14 stsp
4539 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4540 28315671 2019-08-14 stsp if (error)
4541 28315671 2019-08-14 stsp goto done;
4542 28315671 2019-08-14 stsp
4543 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4544 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4545 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4546 28315671 2019-08-14 stsp goto done;
4547 28315671 2019-08-14 stsp }
4548 28315671 2019-08-14 stsp
4549 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4550 28315671 2019-08-14 stsp if (error)
4551 28315671 2019-08-14 stsp goto done;
4552 28315671 2019-08-14 stsp bca.f = got_opentemp();
4553 28315671 2019-08-14 stsp if (bca.f == NULL) {
4554 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4555 28315671 2019-08-14 stsp goto done;
4556 28315671 2019-08-14 stsp }
4557 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4558 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4559 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4560 28315671 2019-08-14 stsp goto done;
4561 28315671 2019-08-14 stsp
4562 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4563 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4564 b02560ec 2019-08-19 stsp bca.nlines--;
4565 b02560ec 2019-08-19 stsp
4566 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4567 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4568 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4569 28315671 2019-08-14 stsp goto done;
4570 28315671 2019-08-14 stsp }
4571 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4572 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4573 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4574 7ef28ff8 2019-08-14 stsp while (i > 0) {
4575 7ef28ff8 2019-08-14 stsp i /= 10;
4576 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4577 7ef28ff8 2019-08-14 stsp }
4578 82f6abb8 2019-08-14 stsp bca.repo = repo;
4579 7ef28ff8 2019-08-14 stsp
4580 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4581 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4582 404c43c4 2018-06-21 stsp done:
4583 66bea077 2018-08-02 stsp free(in_repo_path);
4584 0587e10c 2020-07-23 stsp free(link_target);
4585 66bea077 2018-08-02 stsp free(repo_path);
4586 66bea077 2018-08-02 stsp free(cwd);
4587 404c43c4 2018-06-21 stsp free(commit_id);
4588 28315671 2019-08-14 stsp free(obj_id);
4589 28315671 2019-08-14 stsp if (blob)
4590 28315671 2019-08-14 stsp got_object_blob_close(blob);
4591 0c06baac 2019-02-05 stsp if (worktree)
4592 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4593 ad242220 2018-09-08 stsp if (repo) {
4594 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4595 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4596 ad242220 2018-09-08 stsp if (error == NULL)
4597 ad242220 2018-09-08 stsp error = repo_error;
4598 ad242220 2018-09-08 stsp }
4599 3affba96 2019-09-22 stsp if (bca.lines) {
4600 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4601 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4602 3affba96 2019-09-22 stsp free(bline->id_str);
4603 3affba96 2019-09-22 stsp free(bline->committer);
4604 3affba96 2019-09-22 stsp }
4605 3affba96 2019-09-22 stsp free(bca.lines);
4606 28315671 2019-08-14 stsp }
4607 28315671 2019-08-14 stsp free(bca.line_offsets);
4608 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4609 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4610 404c43c4 2018-06-21 stsp return error;
4611 5de5890b 2018-10-18 stsp }
4612 5de5890b 2018-10-18 stsp
4613 5de5890b 2018-10-18 stsp __dead static void
4614 5de5890b 2018-10-18 stsp usage_tree(void)
4615 5de5890b 2018-10-18 stsp {
4616 c1669e2e 2019-01-09 stsp fprintf(stderr,
4617 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4618 5de5890b 2018-10-18 stsp getprogname());
4619 5de5890b 2018-10-18 stsp exit(1);
4620 5de5890b 2018-10-18 stsp }
4621 5de5890b 2018-10-18 stsp
4622 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4623 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4624 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4625 c1669e2e 2019-01-09 stsp {
4626 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4627 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4628 848d6979 2019-08-12 stsp const char *modestr = "";
4629 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4630 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4631 5de5890b 2018-10-18 stsp
4632 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4633 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4634 c1669e2e 2019-01-09 stsp path++;
4635 c1669e2e 2019-01-09 stsp
4636 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4637 63c5ca5d 2019-08-24 stsp modestr = "$";
4638 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4639 0d6c6ee3 2020-05-20 stsp int i;
4640 0d6c6ee3 2020-05-20 stsp
4641 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4642 0d6c6ee3 2020-05-20 stsp if (err)
4643 0d6c6ee3 2020-05-20 stsp return err;
4644 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4645 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4646 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4647 0d6c6ee3 2020-05-20 stsp }
4648 0d6c6ee3 2020-05-20 stsp
4649 848d6979 2019-08-12 stsp modestr = "@";
4650 0d6c6ee3 2020-05-20 stsp }
4651 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4652 848d6979 2019-08-12 stsp modestr = "/";
4653 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4654 848d6979 2019-08-12 stsp modestr = "*";
4655 848d6979 2019-08-12 stsp
4656 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4657 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4658 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4659 0d6c6ee3 2020-05-20 stsp
4660 0d6c6ee3 2020-05-20 stsp free(link_target);
4661 0d6c6ee3 2020-05-20 stsp return NULL;
4662 c1669e2e 2019-01-09 stsp }
4663 c1669e2e 2019-01-09 stsp
4664 5de5890b 2018-10-18 stsp static const struct got_error *
4665 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4666 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4667 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4668 5de5890b 2018-10-18 stsp {
4669 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4670 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4671 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4672 56e0773d 2019-11-28 stsp int nentries, i;
4673 5de5890b 2018-10-18 stsp
4674 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4675 5de5890b 2018-10-18 stsp if (err)
4676 5de5890b 2018-10-18 stsp goto done;
4677 5de5890b 2018-10-18 stsp
4678 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4679 5de5890b 2018-10-18 stsp if (err)
4680 5de5890b 2018-10-18 stsp goto done;
4681 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4682 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4683 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4684 5de5890b 2018-10-18 stsp char *id = NULL;
4685 84453469 2018-11-11 stsp
4686 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4687 84453469 2018-11-11 stsp break;
4688 84453469 2018-11-11 stsp
4689 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4690 5de5890b 2018-10-18 stsp if (show_ids) {
4691 5de5890b 2018-10-18 stsp char *id_str;
4692 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4693 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4694 5de5890b 2018-10-18 stsp if (err)
4695 5de5890b 2018-10-18 stsp goto done;
4696 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4697 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4698 5de5890b 2018-10-18 stsp free(id_str);
4699 5de5890b 2018-10-18 stsp goto done;
4700 5de5890b 2018-10-18 stsp }
4701 5de5890b 2018-10-18 stsp free(id_str);
4702 5de5890b 2018-10-18 stsp }
4703 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4704 5de5890b 2018-10-18 stsp free(id);
4705 0d6c6ee3 2020-05-20 stsp if (err)
4706 0d6c6ee3 2020-05-20 stsp goto done;
4707 c1669e2e 2019-01-09 stsp
4708 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4709 c1669e2e 2019-01-09 stsp char *child_path;
4710 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4711 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4712 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4713 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4714 c1669e2e 2019-01-09 stsp goto done;
4715 c1669e2e 2019-01-09 stsp }
4716 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4717 c1669e2e 2019-01-09 stsp root_path, repo);
4718 c1669e2e 2019-01-09 stsp free(child_path);
4719 c1669e2e 2019-01-09 stsp if (err)
4720 c1669e2e 2019-01-09 stsp goto done;
4721 c1669e2e 2019-01-09 stsp }
4722 5de5890b 2018-10-18 stsp }
4723 5de5890b 2018-10-18 stsp done:
4724 5de5890b 2018-10-18 stsp if (tree)
4725 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4726 5de5890b 2018-10-18 stsp free(tree_id);
4727 5de5890b 2018-10-18 stsp return err;
4728 404c43c4 2018-06-21 stsp }
4729 404c43c4 2018-06-21 stsp
4730 5de5890b 2018-10-18 stsp static const struct got_error *
4731 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4732 5de5890b 2018-10-18 stsp {
4733 5de5890b 2018-10-18 stsp const struct got_error *error;
4734 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4735 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4736 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4737 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4738 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4739 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4740 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4741 5de5890b 2018-10-18 stsp int ch;
4742 5de5890b 2018-10-18 stsp
4743 5de5890b 2018-10-18 stsp #ifndef PROFILE
4744 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4745 0f8d269b 2019-01-04 stsp NULL) == -1)
4746 5de5890b 2018-10-18 stsp err(1, "pledge");
4747 5de5890b 2018-10-18 stsp #endif
4748 5de5890b 2018-10-18 stsp
4749 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4750 5de5890b 2018-10-18 stsp switch (ch) {
4751 5de5890b 2018-10-18 stsp case 'c':
4752 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4753 5de5890b 2018-10-18 stsp break;
4754 5de5890b 2018-10-18 stsp case 'r':
4755 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4756 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4757 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4758 9ba1d308 2019-10-21 stsp optarg);
4759 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4760 5de5890b 2018-10-18 stsp break;
4761 5de5890b 2018-10-18 stsp case 'i':
4762 5de5890b 2018-10-18 stsp show_ids = 1;
4763 5de5890b 2018-10-18 stsp break;
4764 c1669e2e 2019-01-09 stsp case 'R':
4765 c1669e2e 2019-01-09 stsp recurse = 1;
4766 c1669e2e 2019-01-09 stsp break;
4767 5de5890b 2018-10-18 stsp default:
4768 2deda0b9 2019-03-07 stsp usage_tree();
4769 5de5890b 2018-10-18 stsp /* NOTREACHED */
4770 5de5890b 2018-10-18 stsp }
4771 5de5890b 2018-10-18 stsp }
4772 5de5890b 2018-10-18 stsp
4773 5de5890b 2018-10-18 stsp argc -= optind;
4774 5de5890b 2018-10-18 stsp argv += optind;
4775 5de5890b 2018-10-18 stsp
4776 5de5890b 2018-10-18 stsp if (argc == 1)
4777 5de5890b 2018-10-18 stsp path = argv[0];
4778 5de5890b 2018-10-18 stsp else if (argc > 1)
4779 5de5890b 2018-10-18 stsp usage_tree();
4780 5de5890b 2018-10-18 stsp else
4781 7a2c19d6 2019-02-05 stsp path = NULL;
4782 7a2c19d6 2019-02-05 stsp
4783 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4784 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4785 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4786 9bf7a39b 2019-02-05 stsp goto done;
4787 9bf7a39b 2019-02-05 stsp }
4788 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4789 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4790 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4791 7a2c19d6 2019-02-05 stsp goto done;
4792 7a2c19d6 2019-02-05 stsp else
4793 7a2c19d6 2019-02-05 stsp error = NULL;
4794 7a2c19d6 2019-02-05 stsp if (worktree) {
4795 7a2c19d6 2019-02-05 stsp repo_path =
4796 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4797 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4798 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4799 7a2c19d6 2019-02-05 stsp if (error)
4800 7a2c19d6 2019-02-05 stsp goto done;
4801 7a2c19d6 2019-02-05 stsp } else {
4802 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4803 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4804 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4805 7a2c19d6 2019-02-05 stsp goto done;
4806 7a2c19d6 2019-02-05 stsp }
4807 5de5890b 2018-10-18 stsp }
4808 5de5890b 2018-10-18 stsp }
4809 5de5890b 2018-10-18 stsp
4810 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4811 5de5890b 2018-10-18 stsp if (error != NULL)
4812 c02c541e 2019-03-29 stsp goto done;
4813 c02c541e 2019-03-29 stsp
4814 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4815 c02c541e 2019-03-29 stsp if (error)
4816 5de5890b 2018-10-18 stsp goto done;
4817 5de5890b 2018-10-18 stsp
4818 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4819 9bf7a39b 2019-02-05 stsp if (worktree) {
4820 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4821 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4822 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4823 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4824 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4825 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4826 9bf7a39b 2019-02-05 stsp goto done;
4827 9bf7a39b 2019-02-05 stsp }
4828 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4829 9bf7a39b 2019-02-05 stsp free(p);
4830 9bf7a39b 2019-02-05 stsp if (error)
4831 9bf7a39b 2019-02-05 stsp goto done;
4832 9bf7a39b 2019-02-05 stsp } else
4833 9bf7a39b 2019-02-05 stsp path = "/";
4834 9bf7a39b 2019-02-05 stsp }
4835 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4836 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4837 9bf7a39b 2019-02-05 stsp if (error != NULL)
4838 9bf7a39b 2019-02-05 stsp goto done;
4839 9bf7a39b 2019-02-05 stsp }
4840 5de5890b 2018-10-18 stsp
4841 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4842 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4843 4e0a20a4 2020-03-23 tracey if (worktree)
4844 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4845 4e0a20a4 2020-03-23 tracey else
4846 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4847 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4848 5de5890b 2018-10-18 stsp if (error != NULL)
4849 5de5890b 2018-10-18 stsp goto done;
4850 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4851 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4852 5de5890b 2018-10-18 stsp if (error != NULL)
4853 5de5890b 2018-10-18 stsp goto done;
4854 5de5890b 2018-10-18 stsp } else {
4855 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4856 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4857 30837e32 2019-07-25 stsp if (error)
4858 5de5890b 2018-10-18 stsp goto done;
4859 5de5890b 2018-10-18 stsp }
4860 5de5890b 2018-10-18 stsp
4861 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4862 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4863 5de5890b 2018-10-18 stsp done:
4864 5de5890b 2018-10-18 stsp free(in_repo_path);
4865 5de5890b 2018-10-18 stsp free(repo_path);
4866 5de5890b 2018-10-18 stsp free(cwd);
4867 5de5890b 2018-10-18 stsp free(commit_id);
4868 7a2c19d6 2019-02-05 stsp if (worktree)
4869 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4870 5de5890b 2018-10-18 stsp if (repo) {
4871 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4872 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4873 5de5890b 2018-10-18 stsp if (error == NULL)
4874 5de5890b 2018-10-18 stsp error = repo_error;
4875 5de5890b 2018-10-18 stsp }
4876 5de5890b 2018-10-18 stsp return error;
4877 5de5890b 2018-10-18 stsp }
4878 5de5890b 2018-10-18 stsp
4879 6bad629b 2019-02-04 stsp __dead static void
4880 6bad629b 2019-02-04 stsp usage_status(void)
4881 6bad629b 2019-02-04 stsp {
4882 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4883 081470ac 2020-08-13 stsp getprogname());
4884 6bad629b 2019-02-04 stsp exit(1);
4885 6bad629b 2019-02-04 stsp }
4886 5c860e29 2018-03-12 stsp
4887 b72f483a 2019-02-05 stsp static const struct got_error *
4888 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4889 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4890 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4891 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4892 6bad629b 2019-02-04 stsp {
4893 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4894 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4895 081470ac 2020-08-13 stsp if (arg) {
4896 081470ac 2020-08-13 stsp char *status_codes = arg;
4897 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4898 081470ac 2020-08-13 stsp int i;
4899 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4900 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4901 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4902 081470ac 2020-08-13 stsp break;
4903 081470ac 2020-08-13 stsp }
4904 081470ac 2020-08-13 stsp if (i == ncodes)
4905 081470ac 2020-08-13 stsp return NULL;
4906 081470ac 2020-08-13 stsp }
4907 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4908 b72f483a 2019-02-05 stsp return NULL;
4909 6bad629b 2019-02-04 stsp }
4910 5c860e29 2018-03-12 stsp
4911 6bad629b 2019-02-04 stsp static const struct got_error *
4912 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4913 6bad629b 2019-02-04 stsp {
4914 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4915 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4916 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4917 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4918 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4919 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4920 081470ac 2020-08-13 stsp int ch, i;
4921 5c860e29 2018-03-12 stsp
4922 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4923 72ea6654 2019-07-27 stsp
4924 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4925 6bad629b 2019-02-04 stsp switch (ch) {
4926 081470ac 2020-08-13 stsp case 's':
4927 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4928 081470ac 2020-08-13 stsp switch (optarg[i]) {
4929 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4930 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4931 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4932 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4933 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4934 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4935 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4936 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4937 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4938 081470ac 2020-08-13 stsp break;
4939 081470ac 2020-08-13 stsp default:
4940 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
4941 081470ac 2020-08-13 stsp optarg[i]);
4942 081470ac 2020-08-13 stsp }
4943 081470ac 2020-08-13 stsp }
4944 081470ac 2020-08-13 stsp status_codes = optarg;
4945 081470ac 2020-08-13 stsp break;
4946 5c860e29 2018-03-12 stsp default:
4947 2deda0b9 2019-03-07 stsp usage_status();
4948 6bad629b 2019-02-04 stsp /* NOTREACHED */
4949 5c860e29 2018-03-12 stsp }
4950 5c860e29 2018-03-12 stsp }
4951 5c860e29 2018-03-12 stsp
4952 6bad629b 2019-02-04 stsp argc -= optind;
4953 6bad629b 2019-02-04 stsp argv += optind;
4954 5c860e29 2018-03-12 stsp
4955 6bad629b 2019-02-04 stsp #ifndef PROFILE
4956 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4957 6bad629b 2019-02-04 stsp NULL) == -1)
4958 6bad629b 2019-02-04 stsp err(1, "pledge");
4959 f42b1b34 2018-03-12 stsp #endif
4960 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4961 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4962 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4963 927df6b7 2019-02-10 stsp goto done;
4964 927df6b7 2019-02-10 stsp }
4965 927df6b7 2019-02-10 stsp
4966 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4967 fa51e947 2020-03-27 stsp if (error) {
4968 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4969 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4970 a5edda0a 2019-07-27 stsp goto done;
4971 fa51e947 2020-03-27 stsp }
4972 6bad629b 2019-02-04 stsp
4973 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4974 c9956ddf 2019-09-08 stsp NULL);
4975 6bad629b 2019-02-04 stsp if (error != NULL)
4976 6bad629b 2019-02-04 stsp goto done;
4977 6bad629b 2019-02-04 stsp
4978 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4979 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4980 087fb88c 2019-08-04 stsp if (error)
4981 087fb88c 2019-08-04 stsp goto done;
4982 087fb88c 2019-08-04 stsp
4983 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4984 6bad629b 2019-02-04 stsp if (error)
4985 6bad629b 2019-02-04 stsp goto done;
4986 6bad629b 2019-02-04 stsp
4987 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4988 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
4989 6bad629b 2019-02-04 stsp done:
4990 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4991 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4992 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4993 927df6b7 2019-02-10 stsp free(cwd);
4994 d0eebce4 2019-03-11 stsp return error;
4995 d0eebce4 2019-03-11 stsp }
4996 d0eebce4 2019-03-11 stsp
4997 d0eebce4 2019-03-11 stsp __dead static void
4998 d0eebce4 2019-03-11 stsp usage_ref(void)
4999 d0eebce4 2019-03-11 stsp {
5000 d0eebce4 2019-03-11 stsp fprintf(stderr,
5001 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5002 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5003 d0eebce4 2019-03-11 stsp getprogname());
5004 d0eebce4 2019-03-11 stsp exit(1);
5005 d0eebce4 2019-03-11 stsp }
5006 d0eebce4 2019-03-11 stsp
5007 d0eebce4 2019-03-11 stsp static const struct got_error *
5008 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5009 d0eebce4 2019-03-11 stsp {
5010 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5011 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5012 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5013 d0eebce4 2019-03-11 stsp
5014 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
5015 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5016 d0eebce4 2019-03-11 stsp if (err)
5017 d0eebce4 2019-03-11 stsp return err;
5018 d0eebce4 2019-03-11 stsp
5019 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5020 d0eebce4 2019-03-11 stsp char *refstr;
5021 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5022 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5023 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5024 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5025 d0eebce4 2019-03-11 stsp free(refstr);
5026 d0eebce4 2019-03-11 stsp }
5027 d0eebce4 2019-03-11 stsp
5028 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5029 d0eebce4 2019-03-11 stsp return NULL;
5030 d0eebce4 2019-03-11 stsp }
5031 d0eebce4 2019-03-11 stsp
5032 d0eebce4 2019-03-11 stsp static const struct got_error *
5033 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5034 d0eebce4 2019-03-11 stsp {
5035 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5036 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5037 d0eebce4 2019-03-11 stsp
5038 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5039 d0eebce4 2019-03-11 stsp if (err)
5040 d0eebce4 2019-03-11 stsp return err;
5041 d0eebce4 2019-03-11 stsp
5042 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5043 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5044 d0eebce4 2019-03-11 stsp return err;
5045 d0eebce4 2019-03-11 stsp }
5046 d0eebce4 2019-03-11 stsp
5047 d0eebce4 2019-03-11 stsp static const struct got_error *
5048 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5049 d0eebce4 2019-03-11 stsp {
5050 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5051 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5052 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5053 d1644381 2019-07-14 stsp
5054 d1644381 2019-07-14 stsp /*
5055 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5056 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5057 d1644381 2019-07-14 stsp * an unintended typo.
5058 d1644381 2019-07-14 stsp */
5059 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5060 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5061 d0eebce4 2019-03-11 stsp
5062 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5063 dd88155e 2019-06-29 stsp repo);
5064 d83d9d5c 2019-05-13 stsp if (err) {
5065 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5066 d0eebce4 2019-03-11 stsp
5067 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5068 d83d9d5c 2019-05-13 stsp return err;
5069 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5070 d83d9d5c 2019-05-13 stsp if (err)
5071 d83d9d5c 2019-05-13 stsp return err;
5072 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5073 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5074 d83d9d5c 2019-05-13 stsp if (err)
5075 d83d9d5c 2019-05-13 stsp return err;
5076 d83d9d5c 2019-05-13 stsp }
5077 d83d9d5c 2019-05-13 stsp
5078 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5079 d0eebce4 2019-03-11 stsp if (err)
5080 d0eebce4 2019-03-11 stsp goto done;
5081 d0eebce4 2019-03-11 stsp
5082 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5083 d0eebce4 2019-03-11 stsp done:
5084 d0eebce4 2019-03-11 stsp if (ref)
5085 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5086 d0eebce4 2019-03-11 stsp free(id);
5087 d1c1ae5f 2019-08-12 stsp return err;
5088 d1c1ae5f 2019-08-12 stsp }
5089 d1c1ae5f 2019-08-12 stsp
5090 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5091 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5092 d1c1ae5f 2019-08-12 stsp {
5093 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5094 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5095 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5096 d1c1ae5f 2019-08-12 stsp
5097 d1c1ae5f 2019-08-12 stsp /*
5098 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5099 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5100 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5101 d1c1ae5f 2019-08-12 stsp */
5102 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5103 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5104 d1c1ae5f 2019-08-12 stsp
5105 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5106 d1c1ae5f 2019-08-12 stsp if (err)
5107 d1c1ae5f 2019-08-12 stsp return err;
5108 d1c1ae5f 2019-08-12 stsp
5109 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5110 d1c1ae5f 2019-08-12 stsp if (err)
5111 d1c1ae5f 2019-08-12 stsp goto done;
5112 d1c1ae5f 2019-08-12 stsp
5113 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5114 d1c1ae5f 2019-08-12 stsp done:
5115 d1c1ae5f 2019-08-12 stsp if (target_ref)
5116 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5117 d1c1ae5f 2019-08-12 stsp if (ref)
5118 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5119 d0eebce4 2019-03-11 stsp return err;
5120 d0eebce4 2019-03-11 stsp }
5121 d0eebce4 2019-03-11 stsp
5122 d0eebce4 2019-03-11 stsp static const struct got_error *
5123 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5124 d0eebce4 2019-03-11 stsp {
5125 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5126 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5127 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5128 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5129 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5130 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5131 b2070a3f 2020-03-22 stsp char *refname = NULL;
5132 d0eebce4 2019-03-11 stsp
5133 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5134 d0eebce4 2019-03-11 stsp switch (ch) {
5135 e31abbf2 2020-03-22 stsp case 'c':
5136 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5137 e31abbf2 2020-03-22 stsp break;
5138 d0eebce4 2019-03-11 stsp case 'd':
5139 e31abbf2 2020-03-22 stsp do_delete = 1;
5140 d0eebce4 2019-03-11 stsp break;
5141 d0eebce4 2019-03-11 stsp case 'r':
5142 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5143 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5144 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5145 9ba1d308 2019-10-21 stsp optarg);
5146 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5147 d0eebce4 2019-03-11 stsp break;
5148 d0eebce4 2019-03-11 stsp case 'l':
5149 d0eebce4 2019-03-11 stsp do_list = 1;
5150 d1c1ae5f 2019-08-12 stsp break;
5151 d1c1ae5f 2019-08-12 stsp case 's':
5152 e31abbf2 2020-03-22 stsp symref_target = optarg;
5153 d0eebce4 2019-03-11 stsp break;
5154 d0eebce4 2019-03-11 stsp default:
5155 d0eebce4 2019-03-11 stsp usage_ref();
5156 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5157 d0eebce4 2019-03-11 stsp }
5158 d0eebce4 2019-03-11 stsp }
5159 d0eebce4 2019-03-11 stsp
5160 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5161 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5162 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5163 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5164 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5165 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5166 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5167 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5168 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5169 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5170 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5171 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5172 d0eebce4 2019-03-11 stsp
5173 d0eebce4 2019-03-11 stsp argc -= optind;
5174 d0eebce4 2019-03-11 stsp argv += optind;
5175 d0eebce4 2019-03-11 stsp
5176 e31abbf2 2020-03-22 stsp if (do_list) {
5177 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5178 d0eebce4 2019-03-11 stsp usage_ref();
5179 b2070a3f 2020-03-22 stsp if (argc == 1) {
5180 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5181 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5182 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5183 b2070a3f 2020-03-22 stsp goto done;
5184 b2070a3f 2020-03-22 stsp }
5185 b2070a3f 2020-03-22 stsp }
5186 e31abbf2 2020-03-22 stsp } else {
5187 e31abbf2 2020-03-22 stsp if (argc != 1)
5188 e31abbf2 2020-03-22 stsp usage_ref();
5189 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5190 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5191 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5192 b2070a3f 2020-03-22 stsp goto done;
5193 b2070a3f 2020-03-22 stsp }
5194 e31abbf2 2020-03-22 stsp }
5195 b2070a3f 2020-03-22 stsp
5196 b2070a3f 2020-03-22 stsp if (refname)
5197 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5198 d0eebce4 2019-03-11 stsp
5199 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5200 e0b57350 2019-03-12 stsp if (do_list) {
5201 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5202 e0b57350 2019-03-12 stsp NULL) == -1)
5203 e0b57350 2019-03-12 stsp err(1, "pledge");
5204 e0b57350 2019-03-12 stsp } else {
5205 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5206 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5207 e0b57350 2019-03-12 stsp err(1, "pledge");
5208 e0b57350 2019-03-12 stsp }
5209 d0eebce4 2019-03-11 stsp #endif
5210 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5211 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5212 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5213 d0eebce4 2019-03-11 stsp goto done;
5214 d0eebce4 2019-03-11 stsp }
5215 d0eebce4 2019-03-11 stsp
5216 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5217 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5218 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5219 d0eebce4 2019-03-11 stsp goto done;
5220 d0eebce4 2019-03-11 stsp else
5221 d0eebce4 2019-03-11 stsp error = NULL;
5222 d0eebce4 2019-03-11 stsp if (worktree) {
5223 d0eebce4 2019-03-11 stsp repo_path =
5224 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5225 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5226 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5227 d0eebce4 2019-03-11 stsp if (error)
5228 d0eebce4 2019-03-11 stsp goto done;
5229 d0eebce4 2019-03-11 stsp } else {
5230 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5231 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5232 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5233 d0eebce4 2019-03-11 stsp goto done;
5234 d0eebce4 2019-03-11 stsp }
5235 d0eebce4 2019-03-11 stsp }
5236 d0eebce4 2019-03-11 stsp }
5237 d0eebce4 2019-03-11 stsp
5238 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5239 d0eebce4 2019-03-11 stsp if (error != NULL)
5240 d0eebce4 2019-03-11 stsp goto done;
5241 d0eebce4 2019-03-11 stsp
5242 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5243 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5244 c02c541e 2019-03-29 stsp if (error)
5245 c02c541e 2019-03-29 stsp goto done;
5246 c02c541e 2019-03-29 stsp
5247 d0eebce4 2019-03-11 stsp if (do_list)
5248 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5249 e31abbf2 2020-03-22 stsp else if (do_delete)
5250 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5251 e31abbf2 2020-03-22 stsp else if (symref_target)
5252 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5253 e31abbf2 2020-03-22 stsp else {
5254 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5255 e31abbf2 2020-03-22 stsp usage_ref();
5256 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5257 e31abbf2 2020-03-22 stsp }
5258 4e759de4 2019-06-26 stsp done:
5259 b2070a3f 2020-03-22 stsp free(refname);
5260 4e759de4 2019-06-26 stsp if (repo)
5261 4e759de4 2019-06-26 stsp got_repo_close(repo);
5262 4e759de4 2019-06-26 stsp if (worktree)
5263 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5264 4e759de4 2019-06-26 stsp free(cwd);
5265 4e759de4 2019-06-26 stsp free(repo_path);
5266 4e759de4 2019-06-26 stsp return error;
5267 4e759de4 2019-06-26 stsp }
5268 4e759de4 2019-06-26 stsp
5269 4e759de4 2019-06-26 stsp __dead static void
5270 4e759de4 2019-06-26 stsp usage_branch(void)
5271 4e759de4 2019-06-26 stsp {
5272 4e759de4 2019-06-26 stsp fprintf(stderr,
5273 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5274 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5275 4e759de4 2019-06-26 stsp exit(1);
5276 4e759de4 2019-06-26 stsp }
5277 4e759de4 2019-06-26 stsp
5278 4e759de4 2019-06-26 stsp static const struct got_error *
5279 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5280 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5281 4e99b47e 2019-10-04 stsp {
5282 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5283 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5284 4e99b47e 2019-10-04 stsp char *refstr;
5285 4e99b47e 2019-10-04 stsp
5286 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5287 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5288 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5289 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5290 4e99b47e 2019-10-04 stsp
5291 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5292 4e99b47e 2019-10-04 stsp if (err)
5293 4e99b47e 2019-10-04 stsp return err;
5294 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5295 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5296 4e99b47e 2019-10-04 stsp marker = "* ";
5297 4e99b47e 2019-10-04 stsp else
5298 4e99b47e 2019-10-04 stsp marker = "~ ";
5299 4e99b47e 2019-10-04 stsp free(id);
5300 4e99b47e 2019-10-04 stsp }
5301 4e99b47e 2019-10-04 stsp
5302 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5303 4e99b47e 2019-10-04 stsp refname += 11;
5304 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5305 4e99b47e 2019-10-04 stsp refname += 18;
5306 4e99b47e 2019-10-04 stsp
5307 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5308 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5309 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5310 4e99b47e 2019-10-04 stsp
5311 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5312 4e99b47e 2019-10-04 stsp free(refstr);
5313 4e99b47e 2019-10-04 stsp return NULL;
5314 4e99b47e 2019-10-04 stsp }
5315 4e99b47e 2019-10-04 stsp
5316 4e99b47e 2019-10-04 stsp static const struct got_error *
5317 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5318 ad89fa31 2019-10-04 stsp {
5319 ad89fa31 2019-10-04 stsp const char *refname;
5320 ad89fa31 2019-10-04 stsp
5321 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5322 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5323 ad89fa31 2019-10-04 stsp
5324 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5325 ad89fa31 2019-10-04 stsp
5326 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5327 ad89fa31 2019-10-04 stsp refname += 11;
5328 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5329 ad89fa31 2019-10-04 stsp refname += 18;
5330 ad89fa31 2019-10-04 stsp
5331 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5332 ad89fa31 2019-10-04 stsp
5333 ad89fa31 2019-10-04 stsp return NULL;
5334 ad89fa31 2019-10-04 stsp }
5335 ad89fa31 2019-10-04 stsp
5336 ad89fa31 2019-10-04 stsp static const struct got_error *
5337 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5338 4e759de4 2019-06-26 stsp {
5339 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5340 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5341 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5342 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5343 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5344 4e759de4 2019-06-26 stsp
5345 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5346 ba882ee3 2019-07-11 stsp
5347 4e99b47e 2019-10-04 stsp if (worktree) {
5348 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5349 4e99b47e 2019-10-04 stsp worktree);
5350 4e99b47e 2019-10-04 stsp if (err)
5351 4e99b47e 2019-10-04 stsp return err;
5352 4e99b47e 2019-10-04 stsp
5353 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5354 4e99b47e 2019-10-04 stsp worktree);
5355 4e99b47e 2019-10-04 stsp if (err)
5356 4e99b47e 2019-10-04 stsp return err;
5357 4e99b47e 2019-10-04 stsp
5358 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5359 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5360 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5361 4e99b47e 2019-10-04 stsp if (err)
5362 4e99b47e 2019-10-04 stsp return err;
5363 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5364 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5365 4e99b47e 2019-10-04 stsp }
5366 4e99b47e 2019-10-04 stsp }
5367 4e99b47e 2019-10-04 stsp
5368 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5369 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5370 4e759de4 2019-06-26 stsp if (err)
5371 4e759de4 2019-06-26 stsp return err;
5372 4e759de4 2019-06-26 stsp
5373 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5374 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5375 4e759de4 2019-06-26 stsp
5376 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5377 4e759de4 2019-06-26 stsp return NULL;
5378 4e759de4 2019-06-26 stsp }
5379 4e759de4 2019-06-26 stsp
5380 4e759de4 2019-06-26 stsp static const struct got_error *
5381 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5382 45cd4e47 2019-08-25 stsp const char *branch_name)
5383 4e759de4 2019-06-26 stsp {
5384 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5385 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5386 4e759de4 2019-06-26 stsp char *refname;
5387 4e759de4 2019-06-26 stsp
5388 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5389 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5390 4e759de4 2019-06-26 stsp
5391 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5392 4e759de4 2019-06-26 stsp if (err)
5393 4e759de4 2019-06-26 stsp goto done;
5394 4e759de4 2019-06-26 stsp
5395 45cd4e47 2019-08-25 stsp if (worktree &&
5396 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5397 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5398 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5399 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5400 45cd4e47 2019-08-25 stsp goto done;
5401 45cd4e47 2019-08-25 stsp }
5402 45cd4e47 2019-08-25 stsp
5403 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5404 4e759de4 2019-06-26 stsp done:
5405 45cd4e47 2019-08-25 stsp if (ref)
5406 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5407 4e759de4 2019-06-26 stsp free(refname);
5408 4e759de4 2019-06-26 stsp return err;
5409 4e759de4 2019-06-26 stsp }
5410 4e759de4 2019-06-26 stsp
5411 4e759de4 2019-06-26 stsp static const struct got_error *
5412 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5413 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5414 4e759de4 2019-06-26 stsp {
5415 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5416 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5417 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5418 d3f84d51 2019-07-11 stsp
5419 d3f84d51 2019-07-11 stsp /*
5420 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5421 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5422 d3f84d51 2019-07-11 stsp * an unintended typo.
5423 d3f84d51 2019-07-11 stsp */
5424 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5425 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5426 4e759de4 2019-06-26 stsp
5427 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5428 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5429 4e759de4 2019-06-26 stsp goto done;
5430 4e759de4 2019-06-26 stsp }
5431 4e759de4 2019-06-26 stsp
5432 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5433 4e759de4 2019-06-26 stsp if (err == NULL) {
5434 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5435 4e759de4 2019-06-26 stsp goto done;
5436 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5437 4e759de4 2019-06-26 stsp goto done;
5438 4e759de4 2019-06-26 stsp
5439 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5440 4e759de4 2019-06-26 stsp if (err)
5441 4e759de4 2019-06-26 stsp goto done;
5442 4e759de4 2019-06-26 stsp
5443 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5444 d0eebce4 2019-03-11 stsp done:
5445 4e759de4 2019-06-26 stsp if (ref)
5446 4e759de4 2019-06-26 stsp got_ref_close(ref);
5447 4e759de4 2019-06-26 stsp free(base_refname);
5448 4e759de4 2019-06-26 stsp free(refname);
5449 4e759de4 2019-06-26 stsp return err;
5450 4e759de4 2019-06-26 stsp }
5451 4e759de4 2019-06-26 stsp
5452 4e759de4 2019-06-26 stsp static const struct got_error *
5453 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5454 4e759de4 2019-06-26 stsp {
5455 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5456 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5457 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5458 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5459 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5460 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5461 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5462 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5463 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5464 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5465 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5466 4e759de4 2019-06-26 stsp
5467 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5468 da76fce2 2020-02-24 stsp
5469 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5470 4e759de4 2019-06-26 stsp switch (ch) {
5471 a74f7e83 2019-11-10 stsp case 'c':
5472 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5473 a74f7e83 2019-11-10 stsp break;
5474 4e759de4 2019-06-26 stsp case 'd':
5475 4e759de4 2019-06-26 stsp delref = optarg;
5476 4e759de4 2019-06-26 stsp break;
5477 4e759de4 2019-06-26 stsp case 'r':
5478 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5479 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5480 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5481 9ba1d308 2019-10-21 stsp optarg);
5482 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5483 4e759de4 2019-06-26 stsp break;
5484 4e759de4 2019-06-26 stsp case 'l':
5485 4e759de4 2019-06-26 stsp do_list = 1;
5486 da76fce2 2020-02-24 stsp break;
5487 da76fce2 2020-02-24 stsp case 'n':
5488 da76fce2 2020-02-24 stsp do_update = 0;
5489 4e759de4 2019-06-26 stsp break;
5490 4e759de4 2019-06-26 stsp default:
5491 4e759de4 2019-06-26 stsp usage_branch();
5492 4e759de4 2019-06-26 stsp /* NOTREACHED */
5493 4e759de4 2019-06-26 stsp }
5494 4e759de4 2019-06-26 stsp }
5495 4e759de4 2019-06-26 stsp
5496 4e759de4 2019-06-26 stsp if (do_list && delref)
5497 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5498 4e759de4 2019-06-26 stsp
5499 4e759de4 2019-06-26 stsp argc -= optind;
5500 4e759de4 2019-06-26 stsp argv += optind;
5501 ad89fa31 2019-10-04 stsp
5502 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5503 ad89fa31 2019-10-04 stsp do_show = 1;
5504 4e759de4 2019-06-26 stsp
5505 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5506 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5507 a74f7e83 2019-11-10 stsp
5508 4e759de4 2019-06-26 stsp if (do_list || delref) {
5509 4e759de4 2019-06-26 stsp if (argc > 0)
5510 4e759de4 2019-06-26 stsp usage_branch();
5511 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5512 4e759de4 2019-06-26 stsp usage_branch();
5513 4e759de4 2019-06-26 stsp
5514 4e759de4 2019-06-26 stsp #ifndef PROFILE
5515 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5516 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5517 4e759de4 2019-06-26 stsp NULL) == -1)
5518 4e759de4 2019-06-26 stsp err(1, "pledge");
5519 4e759de4 2019-06-26 stsp } else {
5520 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5521 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5522 4e759de4 2019-06-26 stsp err(1, "pledge");
5523 4e759de4 2019-06-26 stsp }
5524 4e759de4 2019-06-26 stsp #endif
5525 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5526 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5527 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5528 4e759de4 2019-06-26 stsp goto done;
5529 4e759de4 2019-06-26 stsp }
5530 4e759de4 2019-06-26 stsp
5531 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5532 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5533 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5534 4e759de4 2019-06-26 stsp goto done;
5535 4e759de4 2019-06-26 stsp else
5536 4e759de4 2019-06-26 stsp error = NULL;
5537 4e759de4 2019-06-26 stsp if (worktree) {
5538 4e759de4 2019-06-26 stsp repo_path =
5539 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5540 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5541 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5542 4e759de4 2019-06-26 stsp if (error)
5543 4e759de4 2019-06-26 stsp goto done;
5544 4e759de4 2019-06-26 stsp } else {
5545 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5546 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5547 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5548 4e759de4 2019-06-26 stsp goto done;
5549 4e759de4 2019-06-26 stsp }
5550 4e759de4 2019-06-26 stsp }
5551 4e759de4 2019-06-26 stsp }
5552 4e759de4 2019-06-26 stsp
5553 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5554 4e759de4 2019-06-26 stsp if (error != NULL)
5555 4e759de4 2019-06-26 stsp goto done;
5556 4e759de4 2019-06-26 stsp
5557 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5558 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5559 4e759de4 2019-06-26 stsp if (error)
5560 4e759de4 2019-06-26 stsp goto done;
5561 4e759de4 2019-06-26 stsp
5562 ad89fa31 2019-10-04 stsp if (do_show)
5563 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5564 ad89fa31 2019-10-04 stsp else if (do_list)
5565 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5566 4e759de4 2019-06-26 stsp else if (delref)
5567 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5568 4e759de4 2019-06-26 stsp else {
5569 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5570 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5571 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5572 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5573 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5574 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5575 a74f7e83 2019-11-10 stsp if (error)
5576 a74f7e83 2019-11-10 stsp goto done;
5577 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5578 da76fce2 2020-02-24 stsp if (error)
5579 da76fce2 2020-02-24 stsp goto done;
5580 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5581 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5582 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5583 da76fce2 2020-02-24 stsp
5584 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5585 da76fce2 2020-02-24 stsp if (error)
5586 da76fce2 2020-02-24 stsp goto done;
5587 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5588 da76fce2 2020-02-24 stsp worktree);
5589 da76fce2 2020-02-24 stsp if (error)
5590 da76fce2 2020-02-24 stsp goto done;
5591 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5592 da76fce2 2020-02-24 stsp == -1) {
5593 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5594 da76fce2 2020-02-24 stsp goto done;
5595 da76fce2 2020-02-24 stsp }
5596 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5597 da76fce2 2020-02-24 stsp free(branch_refname);
5598 da76fce2 2020-02-24 stsp if (error)
5599 da76fce2 2020-02-24 stsp goto done;
5600 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5601 da76fce2 2020-02-24 stsp repo);
5602 da76fce2 2020-02-24 stsp if (error)
5603 da76fce2 2020-02-24 stsp goto done;
5604 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5605 da76fce2 2020-02-24 stsp commit_id);
5606 da76fce2 2020-02-24 stsp if (error)
5607 da76fce2 2020-02-24 stsp goto done;
5608 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5609 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5610 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5611 9627c110 2020-04-18 stsp NULL);
5612 da76fce2 2020-02-24 stsp if (error)
5613 da76fce2 2020-02-24 stsp goto done;
5614 9627c110 2020-04-18 stsp if (upa.did_something)
5615 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5616 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5617 da76fce2 2020-02-24 stsp }
5618 4e759de4 2019-06-26 stsp }
5619 4e759de4 2019-06-26 stsp done:
5620 da76fce2 2020-02-24 stsp if (ref)
5621 da76fce2 2020-02-24 stsp got_ref_close(ref);
5622 d0eebce4 2019-03-11 stsp if (repo)
5623 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5624 d0eebce4 2019-03-11 stsp if (worktree)
5625 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5626 d0eebce4 2019-03-11 stsp free(cwd);
5627 d0eebce4 2019-03-11 stsp free(repo_path);
5628 da76fce2 2020-02-24 stsp free(commit_id);
5629 da76fce2 2020-02-24 stsp free(commit_id_str);
5630 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5631 da76fce2 2020-02-24 stsp free((char *)pe->path);
5632 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5633 d00136be 2019-03-26 stsp return error;
5634 d00136be 2019-03-26 stsp }
5635 d00136be 2019-03-26 stsp
5636 8e7bd50a 2019-08-22 stsp
5637 d00136be 2019-03-26 stsp __dead static void
5638 8e7bd50a 2019-08-22 stsp usage_tag(void)
5639 8e7bd50a 2019-08-22 stsp {
5640 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5641 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5642 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5643 8e7bd50a 2019-08-22 stsp exit(1);
5644 b8bad2ba 2019-08-23 stsp }
5645 b8bad2ba 2019-08-23 stsp
5646 b8bad2ba 2019-08-23 stsp #if 0
5647 b8bad2ba 2019-08-23 stsp static const struct got_error *
5648 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5649 b8bad2ba 2019-08-23 stsp {
5650 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5651 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5652 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5653 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5654 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5655 b8bad2ba 2019-08-23 stsp
5656 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5657 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5658 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5659 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5660 b8bad2ba 2019-08-23 stsp if (err)
5661 b8bad2ba 2019-08-23 stsp return err;
5662 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5663 b8bad2ba 2019-08-23 stsp continue;
5664 b8bad2ba 2019-08-23 stsp } else {
5665 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5666 b8bad2ba 2019-08-23 stsp if (err)
5667 b8bad2ba 2019-08-23 stsp break;
5668 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5669 b8bad2ba 2019-08-23 stsp free(re_id);
5670 b8bad2ba 2019-08-23 stsp if (err)
5671 b8bad2ba 2019-08-23 stsp break;
5672 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5673 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5674 b8bad2ba 2019-08-23 stsp }
5675 b8bad2ba 2019-08-23 stsp
5676 b8bad2ba 2019-08-23 stsp while (se) {
5677 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5678 b8bad2ba 2019-08-23 stsp if (err)
5679 b8bad2ba 2019-08-23 stsp break;
5680 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5681 b8bad2ba 2019-08-23 stsp free(se_id);
5682 b8bad2ba 2019-08-23 stsp if (err)
5683 b8bad2ba 2019-08-23 stsp break;
5684 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5685 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5686 b8bad2ba 2019-08-23 stsp
5687 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5688 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5689 b8bad2ba 2019-08-23 stsp if (err)
5690 b8bad2ba 2019-08-23 stsp return err;
5691 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5692 b8bad2ba 2019-08-23 stsp break;
5693 b8bad2ba 2019-08-23 stsp }
5694 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5695 b8bad2ba 2019-08-23 stsp continue;
5696 b8bad2ba 2019-08-23 stsp }
5697 b8bad2ba 2019-08-23 stsp }
5698 b8bad2ba 2019-08-23 stsp done:
5699 b8bad2ba 2019-08-23 stsp return err;
5700 8e7bd50a 2019-08-22 stsp }
5701 b8bad2ba 2019-08-23 stsp #endif
5702 b8bad2ba 2019-08-23 stsp
5703 b8bad2ba 2019-08-23 stsp static const struct got_error *
5704 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5705 8e7bd50a 2019-08-22 stsp {
5706 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5707 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5708 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5709 8e7bd50a 2019-08-22 stsp
5710 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5711 8e7bd50a 2019-08-22 stsp
5712 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5713 8e7bd50a 2019-08-22 stsp if (err)
5714 8e7bd50a 2019-08-22 stsp return err;
5715 8e7bd50a 2019-08-22 stsp
5716 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5717 8e7bd50a 2019-08-22 stsp const char *refname;
5718 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5719 8e7bd50a 2019-08-22 stsp char datebuf[26];
5720 d4efa91b 2020-01-14 stsp const char *tagger;
5721 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5722 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5723 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5724 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5725 8e7bd50a 2019-08-22 stsp
5726 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5727 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5728 8e7bd50a 2019-08-22 stsp continue;
5729 8e7bd50a 2019-08-22 stsp refname += 10;
5730 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5731 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5732 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5733 8e7bd50a 2019-08-22 stsp break;
5734 8e7bd50a 2019-08-22 stsp }
5735 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5736 8e7bd50a 2019-08-22 stsp free(refstr);
5737 8e7bd50a 2019-08-22 stsp
5738 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5739 8e7bd50a 2019-08-22 stsp if (err)
5740 8e7bd50a 2019-08-22 stsp break;
5741 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5742 d4efa91b 2020-01-14 stsp if (err) {
5743 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5744 d4efa91b 2020-01-14 stsp free(id);
5745 d4efa91b 2020-01-14 stsp break;
5746 d4efa91b 2020-01-14 stsp }
5747 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5748 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5749 d4efa91b 2020-01-14 stsp if (err) {
5750 d4efa91b 2020-01-14 stsp free(id);
5751 d4efa91b 2020-01-14 stsp break;
5752 d4efa91b 2020-01-14 stsp }
5753 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5754 d4efa91b 2020-01-14 stsp tagger_time =
5755 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5756 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5757 d4efa91b 2020-01-14 stsp free(id);
5758 d4efa91b 2020-01-14 stsp if (err)
5759 d4efa91b 2020-01-14 stsp break;
5760 d4efa91b 2020-01-14 stsp } else {
5761 d4efa91b 2020-01-14 stsp free(id);
5762 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5763 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5764 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5765 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5766 d4efa91b 2020-01-14 stsp if (err)
5767 d4efa91b 2020-01-14 stsp break;
5768 d4efa91b 2020-01-14 stsp }
5769 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5770 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5771 2417344c 2019-08-23 stsp if (datestr)
5772 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5773 d4efa91b 2020-01-14 stsp if (commit)
5774 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5775 d4efa91b 2020-01-14 stsp else {
5776 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5777 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5778 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5779 d4efa91b 2020-01-14 stsp id_str);
5780 d4efa91b 2020-01-14 stsp break;
5781 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5782 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5783 d4efa91b 2020-01-14 stsp id_str);
5784 d4efa91b 2020-01-14 stsp break;
5785 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5786 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5787 d4efa91b 2020-01-14 stsp id_str);
5788 d4efa91b 2020-01-14 stsp break;
5789 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5790 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5791 d4efa91b 2020-01-14 stsp id_str);
5792 d4efa91b 2020-01-14 stsp break;
5793 d4efa91b 2020-01-14 stsp default:
5794 d4efa91b 2020-01-14 stsp break;
5795 d4efa91b 2020-01-14 stsp }
5796 8e7bd50a 2019-08-22 stsp }
5797 8e7bd50a 2019-08-22 stsp free(id_str);
5798 d4efa91b 2020-01-14 stsp if (commit) {
5799 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5800 d4efa91b 2020-01-14 stsp if (err)
5801 d4efa91b 2020-01-14 stsp break;
5802 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5803 d4efa91b 2020-01-14 stsp } else {
5804 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5805 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5806 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5807 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5808 d4efa91b 2020-01-14 stsp break;
5809 d4efa91b 2020-01-14 stsp }
5810 8e7bd50a 2019-08-22 stsp }
5811 8e7bd50a 2019-08-22 stsp
5812 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5813 8e7bd50a 2019-08-22 stsp do {
5814 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5815 8e7bd50a 2019-08-22 stsp if (line)
5816 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5817 8e7bd50a 2019-08-22 stsp } while (line);
5818 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5819 8e7bd50a 2019-08-22 stsp }
5820 8e7bd50a 2019-08-22 stsp
5821 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5822 8e7bd50a 2019-08-22 stsp return NULL;
5823 8e7bd50a 2019-08-22 stsp }
5824 8e7bd50a 2019-08-22 stsp
5825 8e7bd50a 2019-08-22 stsp static const struct got_error *
5826 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5827 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5828 8e7bd50a 2019-08-22 stsp {
5829 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5830 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5831 f372d5cd 2019-10-21 stsp char *editor = NULL;
5832 1601cb9f 2020-09-11 naddy int initial_content_len;
5833 8e7bd50a 2019-08-22 stsp int fd = -1;
5834 8e7bd50a 2019-08-22 stsp
5835 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5836 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5837 8e7bd50a 2019-08-22 stsp goto done;
5838 8e7bd50a 2019-08-22 stsp }
5839 8e7bd50a 2019-08-22 stsp
5840 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5841 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5842 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5843 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5844 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5845 8e7bd50a 2019-08-22 stsp goto done;
5846 8e7bd50a 2019-08-22 stsp }
5847 8e7bd50a 2019-08-22 stsp
5848 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5849 8e7bd50a 2019-08-22 stsp if (err)
5850 8e7bd50a 2019-08-22 stsp goto done;
5851 8e7bd50a 2019-08-22 stsp
5852 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5853 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5854 97972933 2020-09-11 stsp goto done;
5855 97972933 2020-09-11 stsp }
5856 8e7bd50a 2019-08-22 stsp
5857 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5858 8e7bd50a 2019-08-22 stsp if (err)
5859 8e7bd50a 2019-08-22 stsp goto done;
5860 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5861 8e7bd50a 2019-08-22 stsp done:
5862 8e7bd50a 2019-08-22 stsp free(initial_content);
5863 8e7bd50a 2019-08-22 stsp free(template);
5864 8e7bd50a 2019-08-22 stsp free(editor);
5865 8e7bd50a 2019-08-22 stsp
5866 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5867 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5868 97972933 2020-09-11 stsp
5869 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5870 59f86c76 2020-09-11 stsp if (err == NULL)
5871 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5872 59f86c76 2020-09-11 stsp if (err) {
5873 59f86c76 2020-09-11 stsp free(*tagmsg);
5874 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5875 8e7bd50a 2019-08-22 stsp }
5876 8e7bd50a 2019-08-22 stsp return err;
5877 8e7bd50a 2019-08-22 stsp }
5878 8e7bd50a 2019-08-22 stsp
5879 8e7bd50a 2019-08-22 stsp static const struct got_error *
5880 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5881 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5882 8e7bd50a 2019-08-22 stsp {
5883 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5884 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5885 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5886 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5887 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5888 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5889 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5890 8e7bd50a 2019-08-22 stsp
5891 8e7bd50a 2019-08-22 stsp /*
5892 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5893 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5894 8e7bd50a 2019-08-22 stsp * an unintended typo.
5895 8e7bd50a 2019-08-22 stsp */
5896 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5897 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5898 8e7bd50a 2019-08-22 stsp
5899 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5900 8e7bd50a 2019-08-22 stsp if (err)
5901 8e7bd50a 2019-08-22 stsp return err;
5902 8e7bd50a 2019-08-22 stsp
5903 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5904 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5905 8e7bd50a 2019-08-22 stsp if (err)
5906 8e7bd50a 2019-08-22 stsp goto done;
5907 8e7bd50a 2019-08-22 stsp
5908 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5909 8e7bd50a 2019-08-22 stsp if (err)
5910 8e7bd50a 2019-08-22 stsp goto done;
5911 8e7bd50a 2019-08-22 stsp
5912 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5913 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5914 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5915 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5916 8e7bd50a 2019-08-22 stsp goto done;
5917 8e7bd50a 2019-08-22 stsp }
5918 8e7bd50a 2019-08-22 stsp tag_name += 10;
5919 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5920 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5921 8e7bd50a 2019-08-22 stsp goto done;
5922 8e7bd50a 2019-08-22 stsp }
5923 8e7bd50a 2019-08-22 stsp
5924 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5925 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5926 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5927 8e7bd50a 2019-08-22 stsp goto done;
5928 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5929 8e7bd50a 2019-08-22 stsp goto done;
5930 8e7bd50a 2019-08-22 stsp
5931 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5932 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5933 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5934 f372d5cd 2019-10-21 stsp if (err) {
5935 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5936 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5937 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5938 8e7bd50a 2019-08-22 stsp goto done;
5939 f372d5cd 2019-10-21 stsp }
5940 8e7bd50a 2019-08-22 stsp }
5941 8e7bd50a 2019-08-22 stsp
5942 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5943 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5944 f372d5cd 2019-10-21 stsp if (err) {
5945 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5946 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5947 8e7bd50a 2019-08-22 stsp goto done;
5948 f372d5cd 2019-10-21 stsp }
5949 8e7bd50a 2019-08-22 stsp
5950 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5951 f372d5cd 2019-10-21 stsp if (err) {
5952 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5953 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5954 8e7bd50a 2019-08-22 stsp goto done;
5955 f372d5cd 2019-10-21 stsp }
5956 8e7bd50a 2019-08-22 stsp
5957 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5958 f372d5cd 2019-10-21 stsp if (err) {
5959 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5960 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5961 f372d5cd 2019-10-21 stsp goto done;
5962 f372d5cd 2019-10-21 stsp }
5963 8e7bd50a 2019-08-22 stsp
5964 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5965 f372d5cd 2019-10-21 stsp if (err) {
5966 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5967 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5968 f372d5cd 2019-10-21 stsp goto done;
5969 8e7bd50a 2019-08-22 stsp }
5970 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5971 8e7bd50a 2019-08-22 stsp done:
5972 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5973 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5974 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5975 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5976 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5977 f372d5cd 2019-10-21 stsp free(tag_id_str);
5978 8e7bd50a 2019-08-22 stsp if (ref)
5979 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5980 8e7bd50a 2019-08-22 stsp free(commit_id);
5981 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5982 8e7bd50a 2019-08-22 stsp free(refname);
5983 8e7bd50a 2019-08-22 stsp free(tagmsg);
5984 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5985 aba9c984 2019-09-08 stsp free(tagger);
5986 8e7bd50a 2019-08-22 stsp return err;
5987 8e7bd50a 2019-08-22 stsp }
5988 8e7bd50a 2019-08-22 stsp
5989 8e7bd50a 2019-08-22 stsp static const struct got_error *
5990 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5991 8e7bd50a 2019-08-22 stsp {
5992 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5993 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5994 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5995 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5996 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5997 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5998 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5999 8e7bd50a 2019-08-22 stsp
6000 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6001 8e7bd50a 2019-08-22 stsp switch (ch) {
6002 80106605 2020-02-24 stsp case 'c':
6003 80106605 2020-02-24 stsp commit_id_arg = optarg;
6004 80106605 2020-02-24 stsp break;
6005 8e7bd50a 2019-08-22 stsp case 'm':
6006 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6007 8e7bd50a 2019-08-22 stsp break;
6008 8e7bd50a 2019-08-22 stsp case 'r':
6009 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6010 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6011 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6012 9ba1d308 2019-10-21 stsp optarg);
6013 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6014 8e7bd50a 2019-08-22 stsp break;
6015 8e7bd50a 2019-08-22 stsp case 'l':
6016 8e7bd50a 2019-08-22 stsp do_list = 1;
6017 8e7bd50a 2019-08-22 stsp break;
6018 8e7bd50a 2019-08-22 stsp default:
6019 8e7bd50a 2019-08-22 stsp usage_tag();
6020 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6021 8e7bd50a 2019-08-22 stsp }
6022 8e7bd50a 2019-08-22 stsp }
6023 8e7bd50a 2019-08-22 stsp
6024 8e7bd50a 2019-08-22 stsp argc -= optind;
6025 8e7bd50a 2019-08-22 stsp argv += optind;
6026 8e7bd50a 2019-08-22 stsp
6027 8e7bd50a 2019-08-22 stsp if (do_list) {
6028 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6029 775ce909 2020-03-22 stsp errx(1,
6030 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6031 8e7bd50a 2019-08-22 stsp if (tagmsg)
6032 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
6033 8e7bd50a 2019-08-22 stsp if (argc > 0)
6034 8e7bd50a 2019-08-22 stsp usage_tag();
6035 80106605 2020-02-24 stsp } else if (argc != 1)
6036 8e7bd50a 2019-08-22 stsp usage_tag();
6037 80106605 2020-02-24 stsp
6038 a2887370 2019-08-23 stsp tag_name = argv[0];
6039 8e7bd50a 2019-08-22 stsp
6040 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6041 8e7bd50a 2019-08-22 stsp if (do_list) {
6042 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6043 8e7bd50a 2019-08-22 stsp NULL) == -1)
6044 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6045 8e7bd50a 2019-08-22 stsp } else {
6046 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6047 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6048 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6049 8e7bd50a 2019-08-22 stsp }
6050 8e7bd50a 2019-08-22 stsp #endif
6051 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6052 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6053 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6054 8e7bd50a 2019-08-22 stsp goto done;
6055 8e7bd50a 2019-08-22 stsp }
6056 8e7bd50a 2019-08-22 stsp
6057 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6058 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6059 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6060 8e7bd50a 2019-08-22 stsp goto done;
6061 8e7bd50a 2019-08-22 stsp else
6062 8e7bd50a 2019-08-22 stsp error = NULL;
6063 8e7bd50a 2019-08-22 stsp if (worktree) {
6064 8e7bd50a 2019-08-22 stsp repo_path =
6065 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6066 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6067 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6068 8e7bd50a 2019-08-22 stsp if (error)
6069 8e7bd50a 2019-08-22 stsp goto done;
6070 8e7bd50a 2019-08-22 stsp } else {
6071 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6072 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6073 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6074 8e7bd50a 2019-08-22 stsp goto done;
6075 8e7bd50a 2019-08-22 stsp }
6076 8e7bd50a 2019-08-22 stsp }
6077 8e7bd50a 2019-08-22 stsp }
6078 8e7bd50a 2019-08-22 stsp
6079 8e7bd50a 2019-08-22 stsp if (do_list) {
6080 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6081 c9956ddf 2019-09-08 stsp if (error != NULL)
6082 c9956ddf 2019-09-08 stsp goto done;
6083 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6084 8e7bd50a 2019-08-22 stsp if (error)
6085 8e7bd50a 2019-08-22 stsp goto done;
6086 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6087 8e7bd50a 2019-08-22 stsp } else {
6088 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6089 c9956ddf 2019-09-08 stsp if (error)
6090 c9956ddf 2019-09-08 stsp goto done;
6091 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6092 c9956ddf 2019-09-08 stsp if (error != NULL)
6093 c9956ddf 2019-09-08 stsp goto done;
6094 c9956ddf 2019-09-08 stsp
6095 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6096 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6097 8e7bd50a 2019-08-22 stsp if (error)
6098 8e7bd50a 2019-08-22 stsp goto done;
6099 8e7bd50a 2019-08-22 stsp }
6100 8e7bd50a 2019-08-22 stsp
6101 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6102 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6103 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6104 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6105 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6106 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6107 8e7bd50a 2019-08-22 stsp if (error)
6108 8e7bd50a 2019-08-22 stsp goto done;
6109 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6110 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6111 8e7bd50a 2019-08-22 stsp if (error)
6112 8e7bd50a 2019-08-22 stsp goto done;
6113 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6114 8e7bd50a 2019-08-22 stsp free(commit_id);
6115 8e7bd50a 2019-08-22 stsp if (error)
6116 8e7bd50a 2019-08-22 stsp goto done;
6117 8e7bd50a 2019-08-22 stsp }
6118 8e7bd50a 2019-08-22 stsp
6119 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6120 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6121 8e7bd50a 2019-08-22 stsp }
6122 8e7bd50a 2019-08-22 stsp done:
6123 8e7bd50a 2019-08-22 stsp if (repo)
6124 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6125 8e7bd50a 2019-08-22 stsp if (worktree)
6126 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6127 8e7bd50a 2019-08-22 stsp free(cwd);
6128 8e7bd50a 2019-08-22 stsp free(repo_path);
6129 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6130 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6131 8e7bd50a 2019-08-22 stsp return error;
6132 8e7bd50a 2019-08-22 stsp }
6133 8e7bd50a 2019-08-22 stsp
6134 8e7bd50a 2019-08-22 stsp __dead static void
6135 d00136be 2019-03-26 stsp usage_add(void)
6136 d00136be 2019-03-26 stsp {
6137 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6138 022fae89 2019-12-06 tracey getprogname());
6139 d00136be 2019-03-26 stsp exit(1);
6140 d00136be 2019-03-26 stsp }
6141 d00136be 2019-03-26 stsp
6142 d00136be 2019-03-26 stsp static const struct got_error *
6143 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6144 4e68cba3 2019-11-23 stsp {
6145 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6146 4e68cba3 2019-11-23 stsp path++;
6147 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6148 4e68cba3 2019-11-23 stsp return NULL;
6149 4e68cba3 2019-11-23 stsp }
6150 4e68cba3 2019-11-23 stsp
6151 4e68cba3 2019-11-23 stsp static const struct got_error *
6152 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6153 d00136be 2019-03-26 stsp {
6154 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6155 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6156 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6157 1dd54920 2019-05-11 stsp char *cwd = NULL;
6158 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6159 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6160 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6161 1dd54920 2019-05-11 stsp
6162 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6163 d00136be 2019-03-26 stsp
6164 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6165 d00136be 2019-03-26 stsp switch (ch) {
6166 022fae89 2019-12-06 tracey case 'I':
6167 022fae89 2019-12-06 tracey no_ignores = 1;
6168 022fae89 2019-12-06 tracey break;
6169 4e68cba3 2019-11-23 stsp case 'R':
6170 4e68cba3 2019-11-23 stsp can_recurse = 1;
6171 4e68cba3 2019-11-23 stsp break;
6172 d00136be 2019-03-26 stsp default:
6173 d00136be 2019-03-26 stsp usage_add();
6174 d00136be 2019-03-26 stsp /* NOTREACHED */
6175 d00136be 2019-03-26 stsp }
6176 d00136be 2019-03-26 stsp }
6177 d00136be 2019-03-26 stsp
6178 d00136be 2019-03-26 stsp argc -= optind;
6179 d00136be 2019-03-26 stsp argv += optind;
6180 d00136be 2019-03-26 stsp
6181 43012d58 2019-07-14 stsp #ifndef PROFILE
6182 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6183 43012d58 2019-07-14 stsp NULL) == -1)
6184 43012d58 2019-07-14 stsp err(1, "pledge");
6185 43012d58 2019-07-14 stsp #endif
6186 723c305c 2019-05-11 jcs if (argc < 1)
6187 d00136be 2019-03-26 stsp usage_add();
6188 d00136be 2019-03-26 stsp
6189 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6190 d00136be 2019-03-26 stsp if (cwd == NULL) {
6191 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6192 d00136be 2019-03-26 stsp goto done;
6193 d00136be 2019-03-26 stsp }
6194 723c305c 2019-05-11 jcs
6195 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6196 fa51e947 2020-03-27 stsp if (error) {
6197 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6198 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6199 d00136be 2019-03-26 stsp goto done;
6200 fa51e947 2020-03-27 stsp }
6201 d00136be 2019-03-26 stsp
6202 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6203 c9956ddf 2019-09-08 stsp NULL);
6204 031a5338 2019-03-26 stsp if (error != NULL)
6205 031a5338 2019-03-26 stsp goto done;
6206 031a5338 2019-03-26 stsp
6207 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6208 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6209 d00136be 2019-03-26 stsp if (error)
6210 d00136be 2019-03-26 stsp goto done;
6211 d00136be 2019-03-26 stsp
6212 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6213 6d022e97 2019-08-04 stsp if (error)
6214 022fae89 2019-12-06 tracey goto done;
6215 022fae89 2019-12-06 tracey
6216 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6217 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6218 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6219 6d022e97 2019-08-04 stsp goto done;
6220 723c305c 2019-05-11 jcs
6221 022fae89 2019-12-06 tracey }
6222 022fae89 2019-12-06 tracey
6223 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6224 4e68cba3 2019-11-23 stsp char *ondisk_path;
6225 4e68cba3 2019-11-23 stsp struct stat sb;
6226 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6227 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6228 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6229 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6230 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6231 4e68cba3 2019-11-23 stsp goto done;
6232 4e68cba3 2019-11-23 stsp }
6233 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6234 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6235 4e68cba3 2019-11-23 stsp free(ondisk_path);
6236 4e68cba3 2019-11-23 stsp continue;
6237 4e68cba3 2019-11-23 stsp }
6238 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6239 4e68cba3 2019-11-23 stsp ondisk_path);
6240 4e68cba3 2019-11-23 stsp free(ondisk_path);
6241 4e68cba3 2019-11-23 stsp goto done;
6242 4e68cba3 2019-11-23 stsp }
6243 4e68cba3 2019-11-23 stsp free(ondisk_path);
6244 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6245 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6246 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6247 4e68cba3 2019-11-23 stsp goto done;
6248 4e68cba3 2019-11-23 stsp }
6249 4e68cba3 2019-11-23 stsp }
6250 4e68cba3 2019-11-23 stsp }
6251 022fae89 2019-12-06 tracey
6252 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6253 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6254 d00136be 2019-03-26 stsp done:
6255 031a5338 2019-03-26 stsp if (repo)
6256 031a5338 2019-03-26 stsp got_repo_close(repo);
6257 d00136be 2019-03-26 stsp if (worktree)
6258 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6259 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6260 1dd54920 2019-05-11 stsp free((char *)pe->path);
6261 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6262 2ec1f75b 2019-03-26 stsp free(cwd);
6263 2ec1f75b 2019-03-26 stsp return error;
6264 2ec1f75b 2019-03-26 stsp }
6265 2ec1f75b 2019-03-26 stsp
6266 2ec1f75b 2019-03-26 stsp __dead static void
6267 648e4ef7 2019-07-09 stsp usage_remove(void)
6268 2ec1f75b 2019-03-26 stsp {
6269 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6270 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6271 2ec1f75b 2019-03-26 stsp exit(1);
6272 2a06fe5f 2019-08-24 stsp }
6273 2a06fe5f 2019-08-24 stsp
6274 2a06fe5f 2019-08-24 stsp static const struct got_error *
6275 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6276 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6277 2a06fe5f 2019-08-24 stsp {
6278 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6279 f2a9dc41 2019-12-13 tracey path++;
6280 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6281 2a06fe5f 2019-08-24 stsp return NULL;
6282 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6283 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6284 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6285 2a06fe5f 2019-08-24 stsp return NULL;
6286 2ec1f75b 2019-03-26 stsp }
6287 2ec1f75b 2019-03-26 stsp
6288 2ec1f75b 2019-03-26 stsp static const struct got_error *
6289 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6290 2ec1f75b 2019-03-26 stsp {
6291 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6292 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6293 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6294 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6295 17ed4618 2019-06-02 stsp char *cwd = NULL;
6296 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6297 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6298 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6299 2ec1f75b 2019-03-26 stsp
6300 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6301 17ed4618 2019-06-02 stsp
6302 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6303 2ec1f75b 2019-03-26 stsp switch (ch) {
6304 2ec1f75b 2019-03-26 stsp case 'f':
6305 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6306 2ec1f75b 2019-03-26 stsp break;
6307 70e3e7f5 2019-12-13 tracey case 'k':
6308 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6309 70e3e7f5 2019-12-13 tracey break;
6310 f2a9dc41 2019-12-13 tracey case 'R':
6311 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6312 f2a9dc41 2019-12-13 tracey break;
6313 766841c2 2020-08-13 stsp case 's':
6314 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6315 766841c2 2020-08-13 stsp switch (optarg[i]) {
6316 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6317 766841c2 2020-08-13 stsp delete_local_mods = 1;
6318 766841c2 2020-08-13 stsp break;
6319 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6320 766841c2 2020-08-13 stsp break;
6321 766841c2 2020-08-13 stsp default:
6322 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6323 766841c2 2020-08-13 stsp optarg[i]);
6324 766841c2 2020-08-13 stsp }
6325 766841c2 2020-08-13 stsp }
6326 766841c2 2020-08-13 stsp status_codes = optarg;
6327 766841c2 2020-08-13 stsp break;
6328 2ec1f75b 2019-03-26 stsp default:
6329 f2a9dc41 2019-12-13 tracey usage_remove();
6330 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6331 2ec1f75b 2019-03-26 stsp }
6332 2ec1f75b 2019-03-26 stsp }
6333 2ec1f75b 2019-03-26 stsp
6334 2ec1f75b 2019-03-26 stsp argc -= optind;
6335 2ec1f75b 2019-03-26 stsp argv += optind;
6336 2ec1f75b 2019-03-26 stsp
6337 43012d58 2019-07-14 stsp #ifndef PROFILE
6338 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6339 43012d58 2019-07-14 stsp NULL) == -1)
6340 43012d58 2019-07-14 stsp err(1, "pledge");
6341 43012d58 2019-07-14 stsp #endif
6342 17ed4618 2019-06-02 stsp if (argc < 1)
6343 648e4ef7 2019-07-09 stsp usage_remove();
6344 2ec1f75b 2019-03-26 stsp
6345 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6346 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6347 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6348 2ec1f75b 2019-03-26 stsp goto done;
6349 2ec1f75b 2019-03-26 stsp }
6350 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6351 fa51e947 2020-03-27 stsp if (error) {
6352 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6353 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6354 2ec1f75b 2019-03-26 stsp goto done;
6355 fa51e947 2020-03-27 stsp }
6356 2ec1f75b 2019-03-26 stsp
6357 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6358 c9956ddf 2019-09-08 stsp NULL);
6359 2af4a041 2019-05-11 jcs if (error)
6360 2ec1f75b 2019-03-26 stsp goto done;
6361 2ec1f75b 2019-03-26 stsp
6362 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6363 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6364 2ec1f75b 2019-03-26 stsp if (error)
6365 2ec1f75b 2019-03-26 stsp goto done;
6366 2ec1f75b 2019-03-26 stsp
6367 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6368 6d022e97 2019-08-04 stsp if (error)
6369 6d022e97 2019-08-04 stsp goto done;
6370 17ed4618 2019-06-02 stsp
6371 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6372 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6373 f2a9dc41 2019-12-13 tracey struct stat sb;
6374 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6375 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6376 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6377 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6378 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6379 f2a9dc41 2019-12-13 tracey goto done;
6380 f2a9dc41 2019-12-13 tracey }
6381 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6382 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6383 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6384 f2a9dc41 2019-12-13 tracey continue;
6385 f2a9dc41 2019-12-13 tracey }
6386 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6387 f2a9dc41 2019-12-13 tracey ondisk_path);
6388 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6389 f2a9dc41 2019-12-13 tracey goto done;
6390 f2a9dc41 2019-12-13 tracey }
6391 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6392 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6393 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6394 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6395 f2a9dc41 2019-12-13 tracey goto done;
6396 f2a9dc41 2019-12-13 tracey }
6397 f2a9dc41 2019-12-13 tracey }
6398 f2a9dc41 2019-12-13 tracey }
6399 f2a9dc41 2019-12-13 tracey
6400 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6401 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6402 766841c2 2020-08-13 stsp repo, keep_on_disk);
6403 a129376b 2019-03-28 stsp done:
6404 a129376b 2019-03-28 stsp if (repo)
6405 a129376b 2019-03-28 stsp got_repo_close(repo);
6406 a129376b 2019-03-28 stsp if (worktree)
6407 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6408 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6409 17ed4618 2019-06-02 stsp free((char *)pe->path);
6410 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6411 a129376b 2019-03-28 stsp free(cwd);
6412 a129376b 2019-03-28 stsp return error;
6413 a129376b 2019-03-28 stsp }
6414 a129376b 2019-03-28 stsp
6415 a129376b 2019-03-28 stsp __dead static void
6416 a129376b 2019-03-28 stsp usage_revert(void)
6417 a129376b 2019-03-28 stsp {
6418 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6419 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6420 a129376b 2019-03-28 stsp exit(1);
6421 a129376b 2019-03-28 stsp }
6422 a129376b 2019-03-28 stsp
6423 1ee397ad 2019-07-12 stsp static const struct got_error *
6424 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6425 a129376b 2019-03-28 stsp {
6426 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6427 fb9704af 2020-01-27 stsp return NULL;
6428 fb9704af 2020-01-27 stsp
6429 a129376b 2019-03-28 stsp while (path[0] == '/')
6430 a129376b 2019-03-28 stsp path++;
6431 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6432 33aa809d 2019-08-08 stsp return NULL;
6433 33aa809d 2019-08-08 stsp }
6434 33aa809d 2019-08-08 stsp
6435 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6436 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6437 33aa809d 2019-08-08 stsp const char *action;
6438 33aa809d 2019-08-08 stsp };
6439 33aa809d 2019-08-08 stsp
6440 33aa809d 2019-08-08 stsp static const struct got_error *
6441 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6442 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6443 33aa809d 2019-08-08 stsp {
6444 33aa809d 2019-08-08 stsp char *line = NULL;
6445 33aa809d 2019-08-08 stsp size_t linesize = 0;
6446 33aa809d 2019-08-08 stsp ssize_t linelen;
6447 33aa809d 2019-08-08 stsp
6448 33aa809d 2019-08-08 stsp switch (status) {
6449 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6450 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6451 33aa809d 2019-08-08 stsp break;
6452 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6453 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6454 33aa809d 2019-08-08 stsp break;
6455 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6456 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6457 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6458 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6459 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6460 33aa809d 2019-08-08 stsp printf("%s", line);
6461 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6462 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6463 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6464 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6465 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6466 33aa809d 2019-08-08 stsp break;
6467 33aa809d 2019-08-08 stsp default:
6468 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6469 33aa809d 2019-08-08 stsp }
6470 33aa809d 2019-08-08 stsp
6471 33aa809d 2019-08-08 stsp return NULL;
6472 33aa809d 2019-08-08 stsp }
6473 33aa809d 2019-08-08 stsp
6474 33aa809d 2019-08-08 stsp static const struct got_error *
6475 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6476 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6477 33aa809d 2019-08-08 stsp {
6478 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6479 33aa809d 2019-08-08 stsp char *line = NULL;
6480 33aa809d 2019-08-08 stsp size_t linesize = 0;
6481 33aa809d 2019-08-08 stsp ssize_t linelen;
6482 33aa809d 2019-08-08 stsp int resp = ' ';
6483 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6484 33aa809d 2019-08-08 stsp
6485 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6486 33aa809d 2019-08-08 stsp
6487 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6488 33aa809d 2019-08-08 stsp char *nl;
6489 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6490 33aa809d 2019-08-08 stsp a->action);
6491 33aa809d 2019-08-08 stsp if (err)
6492 33aa809d 2019-08-08 stsp return err;
6493 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6494 33aa809d 2019-08-08 stsp if (linelen == -1) {
6495 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6496 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6497 33aa809d 2019-08-08 stsp return NULL;
6498 33aa809d 2019-08-08 stsp }
6499 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6500 33aa809d 2019-08-08 stsp if (nl)
6501 33aa809d 2019-08-08 stsp *nl = '\0';
6502 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6503 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6504 33aa809d 2019-08-08 stsp printf("y\n");
6505 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6506 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6507 33aa809d 2019-08-08 stsp printf("n\n");
6508 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6509 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6510 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6511 33aa809d 2019-08-08 stsp printf("q\n");
6512 33aa809d 2019-08-08 stsp } else
6513 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6514 33aa809d 2019-08-08 stsp free(line);
6515 33aa809d 2019-08-08 stsp return NULL;
6516 33aa809d 2019-08-08 stsp }
6517 33aa809d 2019-08-08 stsp
6518 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6519 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6520 33aa809d 2019-08-08 stsp a->action);
6521 33aa809d 2019-08-08 stsp if (err)
6522 33aa809d 2019-08-08 stsp return err;
6523 33aa809d 2019-08-08 stsp resp = getchar();
6524 33aa809d 2019-08-08 stsp if (resp == '\n')
6525 33aa809d 2019-08-08 stsp resp = getchar();
6526 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6527 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6528 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6529 33aa809d 2019-08-08 stsp resp = ' ';
6530 33aa809d 2019-08-08 stsp }
6531 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6532 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6533 33aa809d 2019-08-08 stsp resp = ' ';
6534 33aa809d 2019-08-08 stsp }
6535 33aa809d 2019-08-08 stsp }
6536 33aa809d 2019-08-08 stsp
6537 33aa809d 2019-08-08 stsp if (resp == 'y')
6538 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6539 33aa809d 2019-08-08 stsp else if (resp == 'n')
6540 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6541 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6542 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6543 33aa809d 2019-08-08 stsp
6544 1ee397ad 2019-07-12 stsp return NULL;
6545 a129376b 2019-03-28 stsp }
6546 a129376b 2019-03-28 stsp
6547 33aa809d 2019-08-08 stsp
6548 a129376b 2019-03-28 stsp static const struct got_error *
6549 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6550 a129376b 2019-03-28 stsp {
6551 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6552 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6553 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6554 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6555 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6556 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6557 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6558 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6559 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6560 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6561 a129376b 2019-03-28 stsp
6562 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6563 e20a8b6f 2019-06-04 stsp
6564 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6565 a129376b 2019-03-28 stsp switch (ch) {
6566 33aa809d 2019-08-08 stsp case 'p':
6567 33aa809d 2019-08-08 stsp pflag = 1;
6568 33aa809d 2019-08-08 stsp break;
6569 33aa809d 2019-08-08 stsp case 'F':
6570 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6571 33aa809d 2019-08-08 stsp break;
6572 0f6d7415 2019-08-08 stsp case 'R':
6573 0f6d7415 2019-08-08 stsp can_recurse = 1;
6574 0f6d7415 2019-08-08 stsp break;
6575 a129376b 2019-03-28 stsp default:
6576 a129376b 2019-03-28 stsp usage_revert();
6577 a129376b 2019-03-28 stsp /* NOTREACHED */
6578 a129376b 2019-03-28 stsp }
6579 a129376b 2019-03-28 stsp }
6580 a129376b 2019-03-28 stsp
6581 a129376b 2019-03-28 stsp argc -= optind;
6582 a129376b 2019-03-28 stsp argv += optind;
6583 a129376b 2019-03-28 stsp
6584 43012d58 2019-07-14 stsp #ifndef PROFILE
6585 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6586 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6587 43012d58 2019-07-14 stsp err(1, "pledge");
6588 43012d58 2019-07-14 stsp #endif
6589 e20a8b6f 2019-06-04 stsp if (argc < 1)
6590 a129376b 2019-03-28 stsp usage_revert();
6591 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6592 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6593 a129376b 2019-03-28 stsp
6594 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6595 a129376b 2019-03-28 stsp if (cwd == NULL) {
6596 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6597 a129376b 2019-03-28 stsp goto done;
6598 a129376b 2019-03-28 stsp }
6599 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6600 fa51e947 2020-03-27 stsp if (error) {
6601 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6602 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6603 2ec1f75b 2019-03-26 stsp goto done;
6604 fa51e947 2020-03-27 stsp }
6605 a129376b 2019-03-28 stsp
6606 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6607 c9956ddf 2019-09-08 stsp NULL);
6608 a129376b 2019-03-28 stsp if (error != NULL)
6609 a129376b 2019-03-28 stsp goto done;
6610 a129376b 2019-03-28 stsp
6611 33aa809d 2019-08-08 stsp if (patch_script_path) {
6612 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6613 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6614 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6615 33aa809d 2019-08-08 stsp patch_script_path);
6616 33aa809d 2019-08-08 stsp goto done;
6617 33aa809d 2019-08-08 stsp }
6618 33aa809d 2019-08-08 stsp }
6619 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6620 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6621 a129376b 2019-03-28 stsp if (error)
6622 a129376b 2019-03-28 stsp goto done;
6623 a129376b 2019-03-28 stsp
6624 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6625 6d022e97 2019-08-04 stsp if (error)
6626 6d022e97 2019-08-04 stsp goto done;
6627 00db391e 2019-08-03 stsp
6628 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6629 0f6d7415 2019-08-08 stsp char *ondisk_path;
6630 0f6d7415 2019-08-08 stsp struct stat sb;
6631 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6632 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6633 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6634 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6635 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6636 0f6d7415 2019-08-08 stsp goto done;
6637 0f6d7415 2019-08-08 stsp }
6638 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6639 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6640 0f6d7415 2019-08-08 stsp free(ondisk_path);
6641 0f6d7415 2019-08-08 stsp continue;
6642 0f6d7415 2019-08-08 stsp }
6643 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6644 0f6d7415 2019-08-08 stsp ondisk_path);
6645 0f6d7415 2019-08-08 stsp free(ondisk_path);
6646 0f6d7415 2019-08-08 stsp goto done;
6647 0f6d7415 2019-08-08 stsp }
6648 0f6d7415 2019-08-08 stsp free(ondisk_path);
6649 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6650 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6651 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6652 0f6d7415 2019-08-08 stsp goto done;
6653 0f6d7415 2019-08-08 stsp }
6654 0f6d7415 2019-08-08 stsp }
6655 0f6d7415 2019-08-08 stsp }
6656 0f6d7415 2019-08-08 stsp
6657 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6658 33aa809d 2019-08-08 stsp cpa.action = "revert";
6659 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6660 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6661 2ec1f75b 2019-03-26 stsp done:
6662 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6663 33aa809d 2019-08-08 stsp error == NULL)
6664 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6665 2ec1f75b 2019-03-26 stsp if (repo)
6666 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6667 2ec1f75b 2019-03-26 stsp if (worktree)
6668 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6669 2ec1f75b 2019-03-26 stsp free(path);
6670 d00136be 2019-03-26 stsp free(cwd);
6671 6bad629b 2019-02-04 stsp return error;
6672 c4296144 2019-05-09 stsp }
6673 c4296144 2019-05-09 stsp
6674 c4296144 2019-05-09 stsp __dead static void
6675 c4296144 2019-05-09 stsp usage_commit(void)
6676 c4296144 2019-05-09 stsp {
6677 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6678 5c1e53bc 2019-07-28 stsp getprogname());
6679 c4296144 2019-05-09 stsp exit(1);
6680 33ad4cbe 2019-05-12 jcs }
6681 33ad4cbe 2019-05-12 jcs
6682 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6683 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6684 e2ba3d07 2019-05-13 stsp const char *editor;
6685 e0870e44 2019-05-13 stsp const char *worktree_path;
6686 76d98825 2019-06-03 stsp const char *branch_name;
6687 314a6357 2019-05-13 stsp const char *repo_path;
6688 e0870e44 2019-05-13 stsp char *logmsg_path;
6689 e2ba3d07 2019-05-13 stsp
6690 e2ba3d07 2019-05-13 stsp };
6691 e0870e44 2019-05-13 stsp
6692 33ad4cbe 2019-05-12 jcs static const struct got_error *
6693 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6694 33ad4cbe 2019-05-12 jcs void *arg)
6695 33ad4cbe 2019-05-12 jcs {
6696 76d98825 2019-06-03 stsp char *initial_content = NULL;
6697 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6698 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6699 e0870e44 2019-05-13 stsp char *template = NULL;
6700 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6701 1601cb9f 2020-09-11 naddy int initial_content_len;
6702 97972933 2020-09-11 stsp int fd = -1;
6703 33ad4cbe 2019-05-12 jcs size_t len;
6704 33ad4cbe 2019-05-12 jcs
6705 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6706 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6707 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6708 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6709 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6710 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6711 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6712 33ad4cbe 2019-05-12 jcs return NULL;
6713 33ad4cbe 2019-05-12 jcs }
6714 33ad4cbe 2019-05-12 jcs
6715 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6716 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6717 76d98825 2019-06-03 stsp
6718 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6719 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6720 1601cb9f 2020-09-11 naddy a->branch_name);
6721 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6722 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6723 e0870e44 2019-05-13 stsp
6724 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6725 793c30b5 2019-05-13 stsp if (err)
6726 e0870e44 2019-05-13 stsp goto done;
6727 33ad4cbe 2019-05-12 jcs
6728 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6729 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6730 97972933 2020-09-11 stsp goto done;
6731 97972933 2020-09-11 stsp }
6732 33ad4cbe 2019-05-12 jcs
6733 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6734 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6735 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6736 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6737 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6738 33ad4cbe 2019-05-12 jcs }
6739 33ad4cbe 2019-05-12 jcs
6740 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6741 33ad4cbe 2019-05-12 jcs done:
6742 76d98825 2019-06-03 stsp free(initial_content);
6743 e0870e44 2019-05-13 stsp free(template);
6744 314a6357 2019-05-13 stsp
6745 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6746 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6747 97972933 2020-09-11 stsp
6748 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6749 59f86c76 2020-09-11 stsp if (err == NULL)
6750 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6751 59f86c76 2020-09-11 stsp if (err) {
6752 59f86c76 2020-09-11 stsp free(*logmsg);
6753 59f86c76 2020-09-11 stsp *logmsg = NULL;
6754 3ce1b845 2019-07-15 stsp }
6755 33ad4cbe 2019-05-12 jcs return err;
6756 6bad629b 2019-02-04 stsp }
6757 c4296144 2019-05-09 stsp
6758 c4296144 2019-05-09 stsp static const struct got_error *
6759 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6760 c4296144 2019-05-09 stsp {
6761 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6762 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6763 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6764 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6765 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6766 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6767 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6768 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6769 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6770 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6771 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6772 5c1e53bc 2019-07-28 stsp
6773 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6774 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6775 c4296144 2019-05-09 stsp
6776 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6777 c4296144 2019-05-09 stsp switch (ch) {
6778 c4296144 2019-05-09 stsp case 'm':
6779 c4296144 2019-05-09 stsp logmsg = optarg;
6780 c4296144 2019-05-09 stsp break;
6781 35213c7c 2020-07-23 stsp case 'S':
6782 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6783 35213c7c 2020-07-23 stsp break;
6784 c4296144 2019-05-09 stsp default:
6785 c4296144 2019-05-09 stsp usage_commit();
6786 c4296144 2019-05-09 stsp /* NOTREACHED */
6787 c4296144 2019-05-09 stsp }
6788 c4296144 2019-05-09 stsp }
6789 c4296144 2019-05-09 stsp
6790 c4296144 2019-05-09 stsp argc -= optind;
6791 c4296144 2019-05-09 stsp argv += optind;
6792 c4296144 2019-05-09 stsp
6793 43012d58 2019-07-14 stsp #ifndef PROFILE
6794 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6795 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6796 43012d58 2019-07-14 stsp err(1, "pledge");
6797 43012d58 2019-07-14 stsp #endif
6798 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6799 c4296144 2019-05-09 stsp if (cwd == NULL) {
6800 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6801 c4296144 2019-05-09 stsp goto done;
6802 c4296144 2019-05-09 stsp }
6803 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6804 fa51e947 2020-03-27 stsp if (error) {
6805 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6806 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6807 7d5807f4 2019-07-11 stsp goto done;
6808 fa51e947 2020-03-27 stsp }
6809 7d5807f4 2019-07-11 stsp
6810 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6811 c4296144 2019-05-09 stsp if (error)
6812 a698f62e 2019-07-25 stsp goto done;
6813 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6814 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6815 c4296144 2019-05-09 stsp goto done;
6816 a698f62e 2019-07-25 stsp }
6817 5c1e53bc 2019-07-28 stsp
6818 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6819 916f288c 2019-07-30 stsp worktree);
6820 5c1e53bc 2019-07-28 stsp if (error)
6821 5c1e53bc 2019-07-28 stsp goto done;
6822 c4296144 2019-05-09 stsp
6823 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6824 c9956ddf 2019-09-08 stsp if (error)
6825 c9956ddf 2019-09-08 stsp goto done;
6826 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6827 c9956ddf 2019-09-08 stsp gitconfig_path);
6828 c4296144 2019-05-09 stsp if (error != NULL)
6829 c4296144 2019-05-09 stsp goto done;
6830 c4296144 2019-05-09 stsp
6831 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6832 aba9c984 2019-09-08 stsp if (error)
6833 aba9c984 2019-09-08 stsp return error;
6834 aba9c984 2019-09-08 stsp
6835 314a6357 2019-05-13 stsp /*
6836 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6837 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6838 314a6357 2019-05-13 stsp */
6839 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6840 314a6357 2019-05-13 stsp error = get_editor(&editor);
6841 314a6357 2019-05-13 stsp else
6842 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6843 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6844 c4296144 2019-05-09 stsp if (error)
6845 c4296144 2019-05-09 stsp goto done;
6846 c4296144 2019-05-09 stsp
6847 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6848 087fb88c 2019-08-04 stsp if (error)
6849 087fb88c 2019-08-04 stsp goto done;
6850 087fb88c 2019-08-04 stsp
6851 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6852 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6853 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6854 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6855 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6856 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6857 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6858 916f288c 2019-07-30 stsp goto done;
6859 916f288c 2019-07-30 stsp }
6860 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6861 916f288c 2019-07-30 stsp }
6862 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6863 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6864 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6865 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6866 e0870e44 2019-05-13 stsp if (error) {
6867 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6868 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6869 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6870 c4296144 2019-05-09 stsp goto done;
6871 e0870e44 2019-05-13 stsp }
6872 c4296144 2019-05-09 stsp
6873 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6874 c4296144 2019-05-09 stsp if (error)
6875 c4296144 2019-05-09 stsp goto done;
6876 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6877 c4296144 2019-05-09 stsp done:
6878 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6879 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6880 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6881 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6882 7266f21f 2019-10-21 stsp error == NULL)
6883 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6884 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6885 c4296144 2019-05-09 stsp if (repo)
6886 c4296144 2019-05-09 stsp got_repo_close(repo);
6887 c4296144 2019-05-09 stsp if (worktree)
6888 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6889 c4296144 2019-05-09 stsp free(cwd);
6890 c4296144 2019-05-09 stsp free(id_str);
6891 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6892 e2ba3d07 2019-05-13 stsp free(editor);
6893 aba9c984 2019-09-08 stsp free(author);
6894 234035bc 2019-06-01 stsp return error;
6895 234035bc 2019-06-01 stsp }
6896 234035bc 2019-06-01 stsp
6897 234035bc 2019-06-01 stsp __dead static void
6898 234035bc 2019-06-01 stsp usage_cherrypick(void)
6899 234035bc 2019-06-01 stsp {
6900 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6901 234035bc 2019-06-01 stsp exit(1);
6902 234035bc 2019-06-01 stsp }
6903 234035bc 2019-06-01 stsp
6904 234035bc 2019-06-01 stsp static const struct got_error *
6905 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6906 234035bc 2019-06-01 stsp {
6907 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6908 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6909 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6910 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6911 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6912 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6913 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6914 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6915 9627c110 2020-04-18 stsp int ch;
6916 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6917 234035bc 2019-06-01 stsp
6918 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6919 234035bc 2019-06-01 stsp switch (ch) {
6920 234035bc 2019-06-01 stsp default:
6921 234035bc 2019-06-01 stsp usage_cherrypick();
6922 234035bc 2019-06-01 stsp /* NOTREACHED */
6923 234035bc 2019-06-01 stsp }
6924 234035bc 2019-06-01 stsp }
6925 234035bc 2019-06-01 stsp
6926 234035bc 2019-06-01 stsp argc -= optind;
6927 234035bc 2019-06-01 stsp argv += optind;
6928 234035bc 2019-06-01 stsp
6929 43012d58 2019-07-14 stsp #ifndef PROFILE
6930 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6931 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6932 43012d58 2019-07-14 stsp err(1, "pledge");
6933 43012d58 2019-07-14 stsp #endif
6934 234035bc 2019-06-01 stsp if (argc != 1)
6935 234035bc 2019-06-01 stsp usage_cherrypick();
6936 234035bc 2019-06-01 stsp
6937 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6938 234035bc 2019-06-01 stsp if (cwd == NULL) {
6939 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6940 234035bc 2019-06-01 stsp goto done;
6941 234035bc 2019-06-01 stsp }
6942 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6943 fa51e947 2020-03-27 stsp if (error) {
6944 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6945 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6946 fa51e947 2020-03-27 stsp cwd);
6947 234035bc 2019-06-01 stsp goto done;
6948 fa51e947 2020-03-27 stsp }
6949 234035bc 2019-06-01 stsp
6950 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6951 c9956ddf 2019-09-08 stsp NULL);
6952 234035bc 2019-06-01 stsp if (error != NULL)
6953 234035bc 2019-06-01 stsp goto done;
6954 234035bc 2019-06-01 stsp
6955 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6956 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6957 234035bc 2019-06-01 stsp if (error)
6958 234035bc 2019-06-01 stsp goto done;
6959 234035bc 2019-06-01 stsp
6960 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6961 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6962 234035bc 2019-06-01 stsp if (error != NULL) {
6963 234035bc 2019-06-01 stsp struct got_reference *ref;
6964 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6965 234035bc 2019-06-01 stsp goto done;
6966 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6967 234035bc 2019-06-01 stsp if (error != NULL)
6968 234035bc 2019-06-01 stsp goto done;
6969 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6970 234035bc 2019-06-01 stsp got_ref_close(ref);
6971 234035bc 2019-06-01 stsp if (error != NULL)
6972 234035bc 2019-06-01 stsp goto done;
6973 234035bc 2019-06-01 stsp }
6974 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6975 234035bc 2019-06-01 stsp if (error)
6976 234035bc 2019-06-01 stsp goto done;
6977 234035bc 2019-06-01 stsp
6978 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6979 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6980 234035bc 2019-06-01 stsp if (error != NULL)
6981 234035bc 2019-06-01 stsp goto done;
6982 234035bc 2019-06-01 stsp
6983 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6984 234035bc 2019-06-01 stsp if (error) {
6985 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6986 234035bc 2019-06-01 stsp goto done;
6987 234035bc 2019-06-01 stsp error = NULL;
6988 234035bc 2019-06-01 stsp } else {
6989 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6990 234035bc 2019-06-01 stsp goto done;
6991 234035bc 2019-06-01 stsp }
6992 234035bc 2019-06-01 stsp
6993 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6994 234035bc 2019-06-01 stsp if (error)
6995 234035bc 2019-06-01 stsp goto done;
6996 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6997 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6998 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6999 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7000 03415a1a 2019-06-02 stsp NULL);
7001 234035bc 2019-06-01 stsp if (error != NULL)
7002 234035bc 2019-06-01 stsp goto done;
7003 234035bc 2019-06-01 stsp
7004 9627c110 2020-04-18 stsp if (upa.did_something)
7005 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7006 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7007 234035bc 2019-06-01 stsp done:
7008 234035bc 2019-06-01 stsp if (commit)
7009 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7010 234035bc 2019-06-01 stsp free(commit_id_str);
7011 234035bc 2019-06-01 stsp if (head_ref)
7012 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7013 234035bc 2019-06-01 stsp if (worktree)
7014 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7015 234035bc 2019-06-01 stsp if (repo)
7016 234035bc 2019-06-01 stsp got_repo_close(repo);
7017 c4296144 2019-05-09 stsp return error;
7018 c4296144 2019-05-09 stsp }
7019 5ef14e63 2019-06-02 stsp
7020 5ef14e63 2019-06-02 stsp __dead static void
7021 5ef14e63 2019-06-02 stsp usage_backout(void)
7022 5ef14e63 2019-06-02 stsp {
7023 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7024 5ef14e63 2019-06-02 stsp exit(1);
7025 5ef14e63 2019-06-02 stsp }
7026 5ef14e63 2019-06-02 stsp
7027 5ef14e63 2019-06-02 stsp static const struct got_error *
7028 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7029 5ef14e63 2019-06-02 stsp {
7030 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7031 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7032 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7033 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7034 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7035 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7036 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7037 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7038 9627c110 2020-04-18 stsp int ch;
7039 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7040 5ef14e63 2019-06-02 stsp
7041 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7042 5ef14e63 2019-06-02 stsp switch (ch) {
7043 5ef14e63 2019-06-02 stsp default:
7044 5ef14e63 2019-06-02 stsp usage_backout();
7045 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7046 5ef14e63 2019-06-02 stsp }
7047 5ef14e63 2019-06-02 stsp }
7048 5ef14e63 2019-06-02 stsp
7049 5ef14e63 2019-06-02 stsp argc -= optind;
7050 5ef14e63 2019-06-02 stsp argv += optind;
7051 5ef14e63 2019-06-02 stsp
7052 43012d58 2019-07-14 stsp #ifndef PROFILE
7053 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7054 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7055 43012d58 2019-07-14 stsp err(1, "pledge");
7056 43012d58 2019-07-14 stsp #endif
7057 5ef14e63 2019-06-02 stsp if (argc != 1)
7058 5ef14e63 2019-06-02 stsp usage_backout();
7059 5ef14e63 2019-06-02 stsp
7060 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7061 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7062 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7063 5ef14e63 2019-06-02 stsp goto done;
7064 5ef14e63 2019-06-02 stsp }
7065 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7066 fa51e947 2020-03-27 stsp if (error) {
7067 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7068 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7069 5ef14e63 2019-06-02 stsp goto done;
7070 fa51e947 2020-03-27 stsp }
7071 5ef14e63 2019-06-02 stsp
7072 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7073 c9956ddf 2019-09-08 stsp NULL);
7074 5ef14e63 2019-06-02 stsp if (error != NULL)
7075 5ef14e63 2019-06-02 stsp goto done;
7076 5ef14e63 2019-06-02 stsp
7077 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7078 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7079 5ef14e63 2019-06-02 stsp if (error)
7080 5ef14e63 2019-06-02 stsp goto done;
7081 5ef14e63 2019-06-02 stsp
7082 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7083 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7084 5ef14e63 2019-06-02 stsp if (error != NULL) {
7085 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7086 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7087 5ef14e63 2019-06-02 stsp goto done;
7088 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7089 5ef14e63 2019-06-02 stsp if (error != NULL)
7090 5ef14e63 2019-06-02 stsp goto done;
7091 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7092 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7093 5ef14e63 2019-06-02 stsp if (error != NULL)
7094 5ef14e63 2019-06-02 stsp goto done;
7095 5ef14e63 2019-06-02 stsp }
7096 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7097 5ef14e63 2019-06-02 stsp if (error)
7098 5ef14e63 2019-06-02 stsp goto done;
7099 5ef14e63 2019-06-02 stsp
7100 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7101 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7102 5ef14e63 2019-06-02 stsp if (error != NULL)
7103 5ef14e63 2019-06-02 stsp goto done;
7104 5ef14e63 2019-06-02 stsp
7105 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7106 5ef14e63 2019-06-02 stsp if (error)
7107 5ef14e63 2019-06-02 stsp goto done;
7108 5ef14e63 2019-06-02 stsp
7109 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7110 5ef14e63 2019-06-02 stsp if (error)
7111 5ef14e63 2019-06-02 stsp goto done;
7112 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7113 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7114 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7115 5ef14e63 2019-06-02 stsp goto done;
7116 5ef14e63 2019-06-02 stsp }
7117 5ef14e63 2019-06-02 stsp
7118 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7119 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7120 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7121 5ef14e63 2019-06-02 stsp if (error != NULL)
7122 5ef14e63 2019-06-02 stsp goto done;
7123 5ef14e63 2019-06-02 stsp
7124 9627c110 2020-04-18 stsp if (upa.did_something)
7125 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7126 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7127 5ef14e63 2019-06-02 stsp done:
7128 5ef14e63 2019-06-02 stsp if (commit)
7129 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7130 5ef14e63 2019-06-02 stsp free(commit_id_str);
7131 5ef14e63 2019-06-02 stsp if (head_ref)
7132 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7133 818c7501 2019-07-11 stsp if (worktree)
7134 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7135 818c7501 2019-07-11 stsp if (repo)
7136 818c7501 2019-07-11 stsp got_repo_close(repo);
7137 818c7501 2019-07-11 stsp return error;
7138 818c7501 2019-07-11 stsp }
7139 818c7501 2019-07-11 stsp
7140 818c7501 2019-07-11 stsp __dead static void
7141 818c7501 2019-07-11 stsp usage_rebase(void)
7142 818c7501 2019-07-11 stsp {
7143 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7144 af54c8f8 2019-07-11 stsp getprogname());
7145 818c7501 2019-07-11 stsp exit(1);
7146 0ebf8283 2019-07-24 stsp }
7147 0ebf8283 2019-07-24 stsp
7148 0ebf8283 2019-07-24 stsp void
7149 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7150 0ebf8283 2019-07-24 stsp {
7151 0ebf8283 2019-07-24 stsp char *nl;
7152 0ebf8283 2019-07-24 stsp size_t len;
7153 0ebf8283 2019-07-24 stsp
7154 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7155 0ebf8283 2019-07-24 stsp if (len > limit)
7156 0ebf8283 2019-07-24 stsp len = limit;
7157 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7158 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7159 0ebf8283 2019-07-24 stsp if (nl)
7160 0ebf8283 2019-07-24 stsp *nl = '\0';
7161 0ebf8283 2019-07-24 stsp }
7162 0ebf8283 2019-07-24 stsp
7163 0ebf8283 2019-07-24 stsp static const struct got_error *
7164 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7165 0ebf8283 2019-07-24 stsp {
7166 5943eee2 2019-08-13 stsp const struct got_error *err;
7167 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7168 5943eee2 2019-08-13 stsp const char *s;
7169 0ebf8283 2019-07-24 stsp
7170 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7171 5943eee2 2019-08-13 stsp if (err)
7172 5943eee2 2019-08-13 stsp return err;
7173 0ebf8283 2019-07-24 stsp
7174 5943eee2 2019-08-13 stsp s = logmsg0;
7175 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7176 5943eee2 2019-08-13 stsp s++;
7177 5943eee2 2019-08-13 stsp
7178 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7179 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7180 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7181 5943eee2 2019-08-13 stsp goto done;
7182 5943eee2 2019-08-13 stsp }
7183 0ebf8283 2019-07-24 stsp
7184 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7185 5943eee2 2019-08-13 stsp done:
7186 5943eee2 2019-08-13 stsp free(logmsg0);
7187 a0ea4fc0 2020-02-28 stsp return err;
7188 a0ea4fc0 2020-02-28 stsp }
7189 a0ea4fc0 2020-02-28 stsp
7190 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7191 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7192 a0ea4fc0 2020-02-28 stsp {
7193 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7194 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7195 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7196 a0ea4fc0 2020-02-28 stsp
7197 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7198 a0ea4fc0 2020-02-28 stsp if (err)
7199 a0ea4fc0 2020-02-28 stsp return err;
7200 a0ea4fc0 2020-02-28 stsp
7201 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7202 a0ea4fc0 2020-02-28 stsp if (err)
7203 a0ea4fc0 2020-02-28 stsp goto done;
7204 a0ea4fc0 2020-02-28 stsp
7205 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7206 a0ea4fc0 2020-02-28 stsp
7207 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7208 a0ea4fc0 2020-02-28 stsp if (err)
7209 a0ea4fc0 2020-02-28 stsp goto done;
7210 a0ea4fc0 2020-02-28 stsp
7211 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7212 a0ea4fc0 2020-02-28 stsp done:
7213 a0ea4fc0 2020-02-28 stsp free(id_str);
7214 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7215 a0ea4fc0 2020-02-28 stsp free(logmsg);
7216 5943eee2 2019-08-13 stsp return err;
7217 818c7501 2019-07-11 stsp }
7218 818c7501 2019-07-11 stsp
7219 818c7501 2019-07-11 stsp static const struct got_error *
7220 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7221 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7222 818c7501 2019-07-11 stsp {
7223 818c7501 2019-07-11 stsp const struct got_error *err;
7224 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7225 818c7501 2019-07-11 stsp
7226 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7227 818c7501 2019-07-11 stsp if (err)
7228 818c7501 2019-07-11 stsp goto done;
7229 818c7501 2019-07-11 stsp
7230 ff0d2220 2019-07-11 stsp if (new_id) {
7231 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7232 ff0d2220 2019-07-11 stsp if (err)
7233 ff0d2220 2019-07-11 stsp goto done;
7234 ff0d2220 2019-07-11 stsp }
7235 818c7501 2019-07-11 stsp
7236 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7237 ff0d2220 2019-07-11 stsp if (new_id_str)
7238 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7239 0ebf8283 2019-07-24 stsp
7240 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7241 0ebf8283 2019-07-24 stsp if (err)
7242 0ebf8283 2019-07-24 stsp goto done;
7243 0ebf8283 2019-07-24 stsp
7244 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7245 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7246 818c7501 2019-07-11 stsp done:
7247 818c7501 2019-07-11 stsp free(old_id_str);
7248 818c7501 2019-07-11 stsp free(new_id_str);
7249 272a1371 2020-02-28 stsp free(logmsg);
7250 818c7501 2019-07-11 stsp return err;
7251 818c7501 2019-07-11 stsp }
7252 818c7501 2019-07-11 stsp
7253 1ee397ad 2019-07-12 stsp static const struct got_error *
7254 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7255 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7256 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7257 818c7501 2019-07-11 stsp {
7258 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7259 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7260 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7261 818c7501 2019-07-11 stsp }
7262 818c7501 2019-07-11 stsp
7263 818c7501 2019-07-11 stsp static const struct got_error *
7264 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7265 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7266 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7267 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7268 ff0d2220 2019-07-11 stsp {
7269 ff0d2220 2019-07-11 stsp const struct got_error *error;
7270 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7271 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7272 ff0d2220 2019-07-11 stsp
7273 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7274 ff0d2220 2019-07-11 stsp if (error)
7275 ff0d2220 2019-07-11 stsp return error;
7276 ff0d2220 2019-07-11 stsp
7277 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7278 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7279 ff0d2220 2019-07-11 stsp if (error) {
7280 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7281 ff0d2220 2019-07-11 stsp goto done;
7282 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7283 ff0d2220 2019-07-11 stsp } else {
7284 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7285 ff0d2220 2019-07-11 stsp free(new_commit_id);
7286 ff0d2220 2019-07-11 stsp }
7287 ff0d2220 2019-07-11 stsp done:
7288 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7289 ff0d2220 2019-07-11 stsp return error;
7290 64c6d990 2019-07-11 stsp }
7291 64c6d990 2019-07-11 stsp
7292 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7293 64c6d990 2019-07-11 stsp const char *path_prefix;
7294 64c6d990 2019-07-11 stsp size_t len;
7295 8ca9bd68 2019-07-25 stsp int errcode;
7296 64c6d990 2019-07-11 stsp };
7297 64c6d990 2019-07-11 stsp
7298 64c6d990 2019-07-11 stsp static const struct got_error *
7299 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7300 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7301 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7302 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7303 64c6d990 2019-07-11 stsp {
7304 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7305 64c6d990 2019-07-11 stsp
7306 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7307 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7308 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7309 64c6d990 2019-07-11 stsp
7310 64c6d990 2019-07-11 stsp return NULL;
7311 64c6d990 2019-07-11 stsp }
7312 64c6d990 2019-07-11 stsp
7313 64c6d990 2019-07-11 stsp static const struct got_error *
7314 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7315 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7316 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7317 64c6d990 2019-07-11 stsp {
7318 64c6d990 2019-07-11 stsp const struct got_error *err;
7319 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7320 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7321 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7322 64c6d990 2019-07-11 stsp
7323 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7324 64c6d990 2019-07-11 stsp return NULL;
7325 64c6d990 2019-07-11 stsp
7326 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7327 64c6d990 2019-07-11 stsp if (err)
7328 64c6d990 2019-07-11 stsp goto done;
7329 64c6d990 2019-07-11 stsp
7330 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7331 64c6d990 2019-07-11 stsp if (err)
7332 64c6d990 2019-07-11 stsp goto done;
7333 64c6d990 2019-07-11 stsp
7334 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7335 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7336 64c6d990 2019-07-11 stsp if (err)
7337 64c6d990 2019-07-11 stsp goto done;
7338 64c6d990 2019-07-11 stsp
7339 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7340 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7341 64c6d990 2019-07-11 stsp if (err)
7342 64c6d990 2019-07-11 stsp goto done;
7343 64c6d990 2019-07-11 stsp
7344 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7345 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7346 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7347 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7348 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7349 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7350 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7351 64c6d990 2019-07-11 stsp done:
7352 64c6d990 2019-07-11 stsp if (tree1)
7353 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7354 64c6d990 2019-07-11 stsp if (tree2)
7355 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7356 64c6d990 2019-07-11 stsp if (commit)
7357 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7358 64c6d990 2019-07-11 stsp if (parent_commit)
7359 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7360 64c6d990 2019-07-11 stsp return err;
7361 ff0d2220 2019-07-11 stsp }
7362 ff0d2220 2019-07-11 stsp
7363 ff0d2220 2019-07-11 stsp static const struct got_error *
7364 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7365 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7366 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7367 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7368 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7369 af61c510 2019-07-19 stsp {
7370 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7371 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7372 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7373 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7374 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7375 af61c510 2019-07-19 stsp
7376 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7377 af61c510 2019-07-19 stsp if (err)
7378 af61c510 2019-07-19 stsp return err;
7379 af61c510 2019-07-19 stsp
7380 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7381 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7382 af61c510 2019-07-19 stsp if (err)
7383 af61c510 2019-07-19 stsp goto done;
7384 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7385 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7386 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7387 af61c510 2019-07-19 stsp if (err) {
7388 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7389 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7390 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7391 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7392 af61c510 2019-07-19 stsp "been reached?!?");
7393 ee780d5c 2020-01-04 stsp }
7394 ee780d5c 2020-01-04 stsp goto done;
7395 af61c510 2019-07-19 stsp } else {
7396 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7397 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7398 af61c510 2019-07-19 stsp if (err)
7399 af61c510 2019-07-19 stsp goto done;
7400 af61c510 2019-07-19 stsp
7401 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7402 af61c510 2019-07-19 stsp if (err)
7403 af61c510 2019-07-19 stsp goto done;
7404 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7405 af61c510 2019-07-19 stsp commit_id = parent_id;
7406 af61c510 2019-07-19 stsp }
7407 af61c510 2019-07-19 stsp }
7408 af61c510 2019-07-19 stsp done:
7409 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7410 af61c510 2019-07-19 stsp return err;
7411 af61c510 2019-07-19 stsp }
7412 af61c510 2019-07-19 stsp
7413 af61c510 2019-07-19 stsp static const struct got_error *
7414 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7415 818c7501 2019-07-11 stsp {
7416 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7417 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7418 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7419 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7420 818c7501 2019-07-11 stsp char *cwd = NULL;
7421 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7422 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7423 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7424 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7425 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7426 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7427 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7428 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7429 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7430 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7431 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7432 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7433 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7434 818c7501 2019-07-11 stsp
7435 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7436 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7437 818c7501 2019-07-11 stsp
7438 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7439 818c7501 2019-07-11 stsp switch (ch) {
7440 818c7501 2019-07-11 stsp case 'a':
7441 818c7501 2019-07-11 stsp abort_rebase = 1;
7442 818c7501 2019-07-11 stsp break;
7443 818c7501 2019-07-11 stsp case 'c':
7444 818c7501 2019-07-11 stsp continue_rebase = 1;
7445 818c7501 2019-07-11 stsp break;
7446 818c7501 2019-07-11 stsp default:
7447 818c7501 2019-07-11 stsp usage_rebase();
7448 818c7501 2019-07-11 stsp /* NOTREACHED */
7449 818c7501 2019-07-11 stsp }
7450 818c7501 2019-07-11 stsp }
7451 818c7501 2019-07-11 stsp
7452 818c7501 2019-07-11 stsp argc -= optind;
7453 818c7501 2019-07-11 stsp argv += optind;
7454 818c7501 2019-07-11 stsp
7455 43012d58 2019-07-14 stsp #ifndef PROFILE
7456 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7457 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7458 43012d58 2019-07-14 stsp err(1, "pledge");
7459 43012d58 2019-07-14 stsp #endif
7460 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7461 818c7501 2019-07-11 stsp usage_rebase();
7462 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7463 818c7501 2019-07-11 stsp if (argc != 0)
7464 818c7501 2019-07-11 stsp usage_rebase();
7465 818c7501 2019-07-11 stsp } else if (argc != 1)
7466 818c7501 2019-07-11 stsp usage_rebase();
7467 818c7501 2019-07-11 stsp
7468 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7469 818c7501 2019-07-11 stsp if (cwd == NULL) {
7470 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7471 818c7501 2019-07-11 stsp goto done;
7472 818c7501 2019-07-11 stsp }
7473 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7474 fa51e947 2020-03-27 stsp if (error) {
7475 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7476 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7477 818c7501 2019-07-11 stsp goto done;
7478 fa51e947 2020-03-27 stsp }
7479 818c7501 2019-07-11 stsp
7480 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7481 c9956ddf 2019-09-08 stsp NULL);
7482 818c7501 2019-07-11 stsp if (error != NULL)
7483 818c7501 2019-07-11 stsp goto done;
7484 818c7501 2019-07-11 stsp
7485 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7486 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7487 7ef62c4e 2020-02-24 stsp if (error)
7488 7ef62c4e 2020-02-24 stsp goto done;
7489 7ef62c4e 2020-02-24 stsp
7490 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7491 7ef62c4e 2020-02-24 stsp worktree);
7492 818c7501 2019-07-11 stsp if (error)
7493 7ef62c4e 2020-02-24 stsp goto done;
7494 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7495 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7496 818c7501 2019-07-11 stsp goto done;
7497 7ef62c4e 2020-02-24 stsp }
7498 818c7501 2019-07-11 stsp
7499 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7500 818c7501 2019-07-11 stsp if (error)
7501 818c7501 2019-07-11 stsp goto done;
7502 818c7501 2019-07-11 stsp
7503 f6794adc 2019-07-23 stsp if (abort_rebase) {
7504 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7505 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7506 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7507 f6794adc 2019-07-23 stsp goto done;
7508 f6794adc 2019-07-23 stsp }
7509 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7510 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7511 3e3a69f1 2019-07-25 stsp worktree, repo);
7512 818c7501 2019-07-11 stsp if (error)
7513 818c7501 2019-07-11 stsp goto done;
7514 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7515 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7516 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7517 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7518 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7519 818c7501 2019-07-11 stsp if (error)
7520 818c7501 2019-07-11 stsp goto done;
7521 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7522 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7523 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7524 818c7501 2019-07-11 stsp }
7525 818c7501 2019-07-11 stsp
7526 818c7501 2019-07-11 stsp if (continue_rebase) {
7527 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7528 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7529 f6794adc 2019-07-23 stsp goto done;
7530 f6794adc 2019-07-23 stsp }
7531 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7532 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7533 3e3a69f1 2019-07-25 stsp worktree, repo);
7534 818c7501 2019-07-11 stsp if (error)
7535 818c7501 2019-07-11 stsp goto done;
7536 818c7501 2019-07-11 stsp
7537 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7538 01757395 2019-07-12 stsp resume_commit_id, repo);
7539 818c7501 2019-07-11 stsp if (error)
7540 818c7501 2019-07-11 stsp goto done;
7541 818c7501 2019-07-11 stsp
7542 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7543 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7544 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7545 818c7501 2019-07-11 stsp goto done;
7546 818c7501 2019-07-11 stsp }
7547 818c7501 2019-07-11 stsp } else {
7548 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7549 818c7501 2019-07-11 stsp if (error != NULL)
7550 818c7501 2019-07-11 stsp goto done;
7551 ff0d2220 2019-07-11 stsp }
7552 818c7501 2019-07-11 stsp
7553 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7554 ff0d2220 2019-07-11 stsp if (error)
7555 ff0d2220 2019-07-11 stsp goto done;
7556 ff0d2220 2019-07-11 stsp
7557 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7558 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7559 a51a74b3 2019-07-27 stsp
7560 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7561 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7562 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7563 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7564 818c7501 2019-07-11 stsp if (error)
7565 818c7501 2019-07-11 stsp goto done;
7566 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7567 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7568 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7569 818c7501 2019-07-11 stsp "with work tree's branch");
7570 818c7501 2019-07-11 stsp goto done;
7571 818c7501 2019-07-11 stsp }
7572 818c7501 2019-07-11 stsp
7573 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7574 a51a74b3 2019-07-27 stsp if (error) {
7575 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7576 a51a74b3 2019-07-27 stsp goto done;
7577 a51a74b3 2019-07-27 stsp error = NULL;
7578 a51a74b3 2019-07-27 stsp } else {
7579 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7580 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7581 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7582 a51a74b3 2019-07-27 stsp goto done;
7583 a51a74b3 2019-07-27 stsp }
7584 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7585 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7586 818c7501 2019-07-11 stsp if (error)
7587 818c7501 2019-07-11 stsp goto done;
7588 818c7501 2019-07-11 stsp }
7589 818c7501 2019-07-11 stsp
7590 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7591 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7592 818c7501 2019-07-11 stsp if (error)
7593 818c7501 2019-07-11 stsp goto done;
7594 818c7501 2019-07-11 stsp
7595 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7596 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7597 fc66b545 2019-08-12 stsp if (pid == NULL) {
7598 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7599 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7600 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7601 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7602 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7603 fc66b545 2019-08-12 stsp if (error)
7604 fc66b545 2019-08-12 stsp goto done;
7605 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7606 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7607 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7608 9627c110 2020-04-18 stsp
7609 fc66b545 2019-08-12 stsp }
7610 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7611 fc66b545 2019-08-12 stsp goto done;
7612 fc66b545 2019-08-12 stsp }
7613 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7614 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7615 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7616 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7617 818c7501 2019-07-11 stsp commit = NULL;
7618 818c7501 2019-07-11 stsp if (error)
7619 818c7501 2019-07-11 stsp goto done;
7620 64c6d990 2019-07-11 stsp
7621 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7622 38b0338b 2019-11-29 stsp if (continue_rebase) {
7623 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7624 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7625 38b0338b 2019-11-29 stsp goto done;
7626 38b0338b 2019-11-29 stsp } else {
7627 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7628 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7629 38b0338b 2019-11-29 stsp char *id_str;
7630 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7631 38b0338b 2019-11-29 stsp new_base_branch);
7632 38b0338b 2019-11-29 stsp if (error)
7633 38b0338b 2019-11-29 stsp goto done;
7634 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7635 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7636 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7637 38b0338b 2019-11-29 stsp free(id_str);
7638 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7639 38b0338b 2019-11-29 stsp new_head_commit_id);
7640 38b0338b 2019-11-29 stsp if (error)
7641 38b0338b 2019-11-29 stsp goto done;
7642 38b0338b 2019-11-29 stsp }
7643 818c7501 2019-07-11 stsp }
7644 818c7501 2019-07-11 stsp
7645 818c7501 2019-07-11 stsp pid = NULL;
7646 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7647 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7648 9627c110 2020-04-18 stsp
7649 818c7501 2019-07-11 stsp commit_id = qid->id;
7650 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7651 818c7501 2019-07-11 stsp pid = qid;
7652 818c7501 2019-07-11 stsp
7653 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7654 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7655 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7656 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7657 818c7501 2019-07-11 stsp if (error)
7658 818c7501 2019-07-11 stsp goto done;
7659 9627c110 2020-04-18 stsp
7660 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7661 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7662 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7663 818c7501 2019-07-11 stsp
7664 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7665 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7666 a0ea4fc0 2020-02-28 stsp if (error)
7667 a0ea4fc0 2020-02-28 stsp goto done;
7668 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7669 818c7501 2019-07-11 stsp break;
7670 01757395 2019-07-12 stsp }
7671 818c7501 2019-07-11 stsp
7672 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7673 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7674 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7675 818c7501 2019-07-11 stsp if (error)
7676 818c7501 2019-07-11 stsp goto done;
7677 818c7501 2019-07-11 stsp }
7678 818c7501 2019-07-11 stsp
7679 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7680 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7681 818c7501 2019-07-11 stsp if (error)
7682 818c7501 2019-07-11 stsp goto done;
7683 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7684 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7685 818c7501 2019-07-11 stsp } else
7686 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7687 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7688 818c7501 2019-07-11 stsp done:
7689 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7690 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7691 818c7501 2019-07-11 stsp free(resume_commit_id);
7692 818c7501 2019-07-11 stsp free(yca_id);
7693 818c7501 2019-07-11 stsp if (commit)
7694 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7695 818c7501 2019-07-11 stsp if (branch)
7696 818c7501 2019-07-11 stsp got_ref_close(branch);
7697 818c7501 2019-07-11 stsp if (new_base_branch)
7698 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7699 818c7501 2019-07-11 stsp if (tmp_branch)
7700 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7701 5ef14e63 2019-06-02 stsp if (worktree)
7702 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7703 5ef14e63 2019-06-02 stsp if (repo)
7704 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7705 5ef14e63 2019-06-02 stsp return error;
7706 0ebf8283 2019-07-24 stsp }
7707 0ebf8283 2019-07-24 stsp
7708 0ebf8283 2019-07-24 stsp __dead static void
7709 0ebf8283 2019-07-24 stsp usage_histedit(void)
7710 0ebf8283 2019-07-24 stsp {
7711 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7712 0ebf8283 2019-07-24 stsp getprogname());
7713 0ebf8283 2019-07-24 stsp exit(1);
7714 0ebf8283 2019-07-24 stsp }
7715 0ebf8283 2019-07-24 stsp
7716 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7717 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7718 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7719 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7720 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7721 0ebf8283 2019-07-24 stsp
7722 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7723 0ebf8283 2019-07-24 stsp unsigned char code;
7724 0ebf8283 2019-07-24 stsp const char *name;
7725 0ebf8283 2019-07-24 stsp const char *desc;
7726 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7727 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7728 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7729 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7730 82997472 2020-01-29 stsp "be used" },
7731 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7732 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7733 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7734 0ebf8283 2019-07-24 stsp };
7735 0ebf8283 2019-07-24 stsp
7736 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7737 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7738 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7739 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7740 0ebf8283 2019-07-24 stsp char *logmsg;
7741 0ebf8283 2019-07-24 stsp };
7742 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7743 0ebf8283 2019-07-24 stsp
7744 0ebf8283 2019-07-24 stsp static const struct got_error *
7745 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7746 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7747 0ebf8283 2019-07-24 stsp {
7748 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7749 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7750 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7751 8138f3e1 2019-08-11 stsp int n;
7752 0ebf8283 2019-07-24 stsp
7753 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7754 0ebf8283 2019-07-24 stsp if (err)
7755 0ebf8283 2019-07-24 stsp goto done;
7756 0ebf8283 2019-07-24 stsp
7757 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7758 0ebf8283 2019-07-24 stsp if (err)
7759 0ebf8283 2019-07-24 stsp goto done;
7760 0ebf8283 2019-07-24 stsp
7761 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7762 0ebf8283 2019-07-24 stsp if (err)
7763 0ebf8283 2019-07-24 stsp goto done;
7764 0ebf8283 2019-07-24 stsp
7765 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7766 0ebf8283 2019-07-24 stsp if (n < 0)
7767 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7768 0ebf8283 2019-07-24 stsp done:
7769 0ebf8283 2019-07-24 stsp if (commit)
7770 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7771 0ebf8283 2019-07-24 stsp free(id_str);
7772 0ebf8283 2019-07-24 stsp free(logmsg);
7773 0ebf8283 2019-07-24 stsp return err;
7774 0ebf8283 2019-07-24 stsp }
7775 0ebf8283 2019-07-24 stsp
7776 0ebf8283 2019-07-24 stsp static const struct got_error *
7777 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7778 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7779 0ebf8283 2019-07-24 stsp {
7780 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7781 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7782 0ebf8283 2019-07-24 stsp
7783 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7784 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7785 0ebf8283 2019-07-24 stsp
7786 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7787 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7788 0ebf8283 2019-07-24 stsp f, repo);
7789 0ebf8283 2019-07-24 stsp if (err)
7790 0ebf8283 2019-07-24 stsp break;
7791 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7792 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7793 083957f4 2020-02-24 stsp if (n < 0) {
7794 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7795 083957f4 2020-02-24 stsp break;
7796 083957f4 2020-02-24 stsp }
7797 083957f4 2020-02-24 stsp }
7798 0ebf8283 2019-07-24 stsp }
7799 0ebf8283 2019-07-24 stsp
7800 0ebf8283 2019-07-24 stsp return err;
7801 0ebf8283 2019-07-24 stsp }
7802 0ebf8283 2019-07-24 stsp
7803 0ebf8283 2019-07-24 stsp static const struct got_error *
7804 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7805 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7806 0ebf8283 2019-07-24 stsp {
7807 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7808 0ebf8283 2019-07-24 stsp int n, i;
7809 514f2ffe 2020-01-29 stsp char *id_str;
7810 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7811 514f2ffe 2020-01-29 stsp
7812 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7813 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7814 514f2ffe 2020-01-29 stsp if (err)
7815 514f2ffe 2020-01-29 stsp return err;
7816 514f2ffe 2020-01-29 stsp
7817 514f2ffe 2020-01-29 stsp n = fprintf(f,
7818 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7819 514f2ffe 2020-01-29 stsp "# commit %s\n"
7820 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7821 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7822 514f2ffe 2020-01-29 stsp if (n < 0) {
7823 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7824 514f2ffe 2020-01-29 stsp goto done;
7825 514f2ffe 2020-01-29 stsp }
7826 0ebf8283 2019-07-24 stsp
7827 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7828 514f2ffe 2020-01-29 stsp if (n < 0) {
7829 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7830 514f2ffe 2020-01-29 stsp goto done;
7831 514f2ffe 2020-01-29 stsp }
7832 0ebf8283 2019-07-24 stsp
7833 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7834 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7835 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7836 0ebf8283 2019-07-24 stsp cmd->desc);
7837 0ebf8283 2019-07-24 stsp if (n < 0) {
7838 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7839 0ebf8283 2019-07-24 stsp break;
7840 0ebf8283 2019-07-24 stsp }
7841 0ebf8283 2019-07-24 stsp }
7842 514f2ffe 2020-01-29 stsp done:
7843 514f2ffe 2020-01-29 stsp free(id_str);
7844 0ebf8283 2019-07-24 stsp return err;
7845 0ebf8283 2019-07-24 stsp }
7846 0ebf8283 2019-07-24 stsp
7847 0ebf8283 2019-07-24 stsp static const struct got_error *
7848 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7849 0ebf8283 2019-07-24 stsp {
7850 0ebf8283 2019-07-24 stsp static char msg[42];
7851 0ebf8283 2019-07-24 stsp int ret;
7852 0ebf8283 2019-07-24 stsp
7853 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7854 0ebf8283 2019-07-24 stsp lineno);
7855 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7856 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7857 0ebf8283 2019-07-24 stsp
7858 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7859 0ebf8283 2019-07-24 stsp }
7860 0ebf8283 2019-07-24 stsp
7861 0ebf8283 2019-07-24 stsp static const struct got_error *
7862 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7863 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7864 0ebf8283 2019-07-24 stsp {
7865 0ebf8283 2019-07-24 stsp const struct got_error *err;
7866 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7867 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7868 0ebf8283 2019-07-24 stsp
7869 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7870 0ebf8283 2019-07-24 stsp if (err)
7871 0ebf8283 2019-07-24 stsp return err;
7872 0ebf8283 2019-07-24 stsp
7873 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7874 0ebf8283 2019-07-24 stsp if (err)
7875 0ebf8283 2019-07-24 stsp goto done;
7876 0ebf8283 2019-07-24 stsp
7877 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7878 5943eee2 2019-08-13 stsp if (err)
7879 5943eee2 2019-08-13 stsp goto done;
7880 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7881 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7882 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7883 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7884 0ebf8283 2019-07-24 stsp }
7885 0ebf8283 2019-07-24 stsp done:
7886 0ebf8283 2019-07-24 stsp if (folded_commit)
7887 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7888 0ebf8283 2019-07-24 stsp free(id_str);
7889 5943eee2 2019-08-13 stsp free(folded_logmsg);
7890 0ebf8283 2019-07-24 stsp return err;
7891 0ebf8283 2019-07-24 stsp }
7892 0ebf8283 2019-07-24 stsp
7893 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7894 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7895 0ebf8283 2019-07-24 stsp {
7896 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7897 0ebf8283 2019-07-24 stsp
7898 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7899 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7900 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7901 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7902 3f9de99f 2019-07-24 stsp folded = prev;
7903 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7904 0ebf8283 2019-07-24 stsp }
7905 0ebf8283 2019-07-24 stsp
7906 0ebf8283 2019-07-24 stsp return folded;
7907 0ebf8283 2019-07-24 stsp }
7908 0ebf8283 2019-07-24 stsp
7909 0ebf8283 2019-07-24 stsp static const struct got_error *
7910 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7911 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7912 0ebf8283 2019-07-24 stsp {
7913 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7914 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7915 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7916 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7917 1601cb9f 2020-09-11 naddy int logmsg_len;
7918 0ebf8283 2019-07-24 stsp int fd;
7919 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7920 0ebf8283 2019-07-24 stsp
7921 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7922 0ebf8283 2019-07-24 stsp if (err)
7923 0ebf8283 2019-07-24 stsp return err;
7924 0ebf8283 2019-07-24 stsp
7925 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7926 0ebf8283 2019-07-24 stsp if (folded) {
7927 0ebf8283 2019-07-24 stsp while (folded != hle) {
7928 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7929 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7930 3f9de99f 2019-07-24 stsp continue;
7931 3f9de99f 2019-07-24 stsp }
7932 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7933 0ebf8283 2019-07-24 stsp logmsg, repo);
7934 0ebf8283 2019-07-24 stsp if (err)
7935 0ebf8283 2019-07-24 stsp goto done;
7936 0ebf8283 2019-07-24 stsp free(logmsg);
7937 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7938 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7939 0ebf8283 2019-07-24 stsp }
7940 0ebf8283 2019-07-24 stsp }
7941 0ebf8283 2019-07-24 stsp
7942 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7943 0ebf8283 2019-07-24 stsp if (err)
7944 0ebf8283 2019-07-24 stsp goto done;
7945 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7946 5943eee2 2019-08-13 stsp if (err)
7947 5943eee2 2019-08-13 stsp goto done;
7948 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
7949 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7950 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
7951 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
7952 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7953 0ebf8283 2019-07-24 stsp goto done;
7954 0ebf8283 2019-07-24 stsp }
7955 0ebf8283 2019-07-24 stsp free(logmsg);
7956 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7957 0ebf8283 2019-07-24 stsp
7958 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7959 0ebf8283 2019-07-24 stsp if (err)
7960 0ebf8283 2019-07-24 stsp goto done;
7961 0ebf8283 2019-07-24 stsp
7962 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7963 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7964 0ebf8283 2019-07-24 stsp if (err)
7965 0ebf8283 2019-07-24 stsp goto done;
7966 0ebf8283 2019-07-24 stsp
7967 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
7968 0ebf8283 2019-07-24 stsp close(fd);
7969 0ebf8283 2019-07-24 stsp
7970 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7971 0ebf8283 2019-07-24 stsp if (err)
7972 0ebf8283 2019-07-24 stsp goto done;
7973 0ebf8283 2019-07-24 stsp
7974 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7975 0ebf8283 2019-07-24 stsp if (err) {
7976 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7977 0ebf8283 2019-07-24 stsp goto done;
7978 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7979 0ebf8283 2019-07-24 stsp }
7980 0ebf8283 2019-07-24 stsp done:
7981 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7982 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7983 0ebf8283 2019-07-24 stsp free(logmsg_path);
7984 0ebf8283 2019-07-24 stsp free(logmsg);
7985 5943eee2 2019-08-13 stsp free(orig_logmsg);
7986 0ebf8283 2019-07-24 stsp free(editor);
7987 0ebf8283 2019-07-24 stsp if (commit)
7988 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7989 0ebf8283 2019-07-24 stsp return err;
7990 5ef14e63 2019-06-02 stsp }
7991 0ebf8283 2019-07-24 stsp
7992 0ebf8283 2019-07-24 stsp static const struct got_error *
7993 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7994 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7995 0ebf8283 2019-07-24 stsp {
7996 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7997 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7998 0ebf8283 2019-07-24 stsp size_t size;
7999 0ebf8283 2019-07-24 stsp ssize_t len;
8000 0ebf8283 2019-07-24 stsp int lineno = 0, i;
8001 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8002 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8003 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8004 0ebf8283 2019-07-24 stsp
8005 0ebf8283 2019-07-24 stsp for (;;) {
8006 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8007 0ebf8283 2019-07-24 stsp if (len == -1) {
8008 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8009 0ebf8283 2019-07-24 stsp if (feof(f))
8010 0ebf8283 2019-07-24 stsp break;
8011 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8012 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8013 0ebf8283 2019-07-24 stsp break;
8014 0ebf8283 2019-07-24 stsp }
8015 0ebf8283 2019-07-24 stsp lineno++;
8016 0ebf8283 2019-07-24 stsp p = line;
8017 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8018 0ebf8283 2019-07-24 stsp p++;
8019 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8020 0ebf8283 2019-07-24 stsp free(line);
8021 0ebf8283 2019-07-24 stsp line = NULL;
8022 0ebf8283 2019-07-24 stsp continue;
8023 0ebf8283 2019-07-24 stsp }
8024 0ebf8283 2019-07-24 stsp cmd = NULL;
8025 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8026 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8027 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8028 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8029 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8030 0ebf8283 2019-07-24 stsp break;
8031 0ebf8283 2019-07-24 stsp }
8032 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8033 0ebf8283 2019-07-24 stsp p++;
8034 0ebf8283 2019-07-24 stsp break;
8035 0ebf8283 2019-07-24 stsp }
8036 0ebf8283 2019-07-24 stsp }
8037 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8038 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8039 0ebf8283 2019-07-24 stsp break;
8040 0ebf8283 2019-07-24 stsp }
8041 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8042 0ebf8283 2019-07-24 stsp p++;
8043 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8044 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8045 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8046 0ebf8283 2019-07-24 stsp break;
8047 0ebf8283 2019-07-24 stsp }
8048 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8049 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8050 0ebf8283 2019-07-24 stsp if (err)
8051 0ebf8283 2019-07-24 stsp break;
8052 0ebf8283 2019-07-24 stsp } else {
8053 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8054 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8055 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8056 0ebf8283 2019-07-24 stsp break;
8057 0ebf8283 2019-07-24 stsp }
8058 0ebf8283 2019-07-24 stsp }
8059 0ebf8283 2019-07-24 stsp free(line);
8060 0ebf8283 2019-07-24 stsp line = NULL;
8061 0ebf8283 2019-07-24 stsp continue;
8062 0ebf8283 2019-07-24 stsp } else {
8063 0ebf8283 2019-07-24 stsp end = p;
8064 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8065 0ebf8283 2019-07-24 stsp end++;
8066 0ebf8283 2019-07-24 stsp *end = '\0';
8067 0ebf8283 2019-07-24 stsp
8068 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8069 0ebf8283 2019-07-24 stsp if (err) {
8070 0ebf8283 2019-07-24 stsp /* override error code */
8071 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8072 0ebf8283 2019-07-24 stsp break;
8073 0ebf8283 2019-07-24 stsp }
8074 0ebf8283 2019-07-24 stsp }
8075 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8076 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8077 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8078 0ebf8283 2019-07-24 stsp break;
8079 0ebf8283 2019-07-24 stsp }
8080 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8081 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8082 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8083 0ebf8283 2019-07-24 stsp commit_id = NULL;
8084 0ebf8283 2019-07-24 stsp free(line);
8085 0ebf8283 2019-07-24 stsp line = NULL;
8086 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8087 0ebf8283 2019-07-24 stsp }
8088 0ebf8283 2019-07-24 stsp
8089 0ebf8283 2019-07-24 stsp free(line);
8090 0ebf8283 2019-07-24 stsp free(commit_id);
8091 0ebf8283 2019-07-24 stsp return err;
8092 0ebf8283 2019-07-24 stsp }
8093 0ebf8283 2019-07-24 stsp
8094 0ebf8283 2019-07-24 stsp static const struct got_error *
8095 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8096 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8097 bfce7f83 2019-07-27 stsp {
8098 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8099 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8100 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8101 5b87815e 2020-03-05 stsp static char msg[92];
8102 bfce7f83 2019-07-27 stsp char *id_str;
8103 bfce7f83 2019-07-27 stsp
8104 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8105 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8106 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8107 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8108 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8109 5b87815e 2020-03-05 stsp
8110 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8111 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8112 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8113 5b87815e 2020-03-05 stsp if (hle == hle2)
8114 5b87815e 2020-03-05 stsp continue;
8115 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8116 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8117 5b87815e 2020-03-05 stsp continue;
8118 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8119 5b87815e 2020-03-05 stsp if (err)
8120 5b87815e 2020-03-05 stsp return err;
8121 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8122 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8123 5b87815e 2020-03-05 stsp free(id_str);
8124 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8125 5b87815e 2020-03-05 stsp }
8126 5b87815e 2020-03-05 stsp }
8127 bfce7f83 2019-07-27 stsp
8128 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8129 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8130 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8131 bfce7f83 2019-07-27 stsp break;
8132 bfce7f83 2019-07-27 stsp }
8133 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8134 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8135 bfce7f83 2019-07-27 stsp if (err)
8136 bfce7f83 2019-07-27 stsp return err;
8137 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8138 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8139 bfce7f83 2019-07-27 stsp free(id_str);
8140 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8141 bfce7f83 2019-07-27 stsp }
8142 bfce7f83 2019-07-27 stsp }
8143 bfce7f83 2019-07-27 stsp
8144 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8145 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8146 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8147 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8148 bfce7f83 2019-07-27 stsp
8149 bfce7f83 2019-07-27 stsp return NULL;
8150 bfce7f83 2019-07-27 stsp }
8151 bfce7f83 2019-07-27 stsp
8152 bfce7f83 2019-07-27 stsp static const struct got_error *
8153 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8154 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8155 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8156 0ebf8283 2019-07-24 stsp {
8157 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8158 0ebf8283 2019-07-24 stsp char *editor;
8159 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8160 0ebf8283 2019-07-24 stsp
8161 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8162 0ebf8283 2019-07-24 stsp if (err)
8163 0ebf8283 2019-07-24 stsp return err;
8164 0ebf8283 2019-07-24 stsp
8165 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8166 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8167 0ebf8283 2019-07-24 stsp goto done;
8168 0ebf8283 2019-07-24 stsp }
8169 0ebf8283 2019-07-24 stsp
8170 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8171 0ebf8283 2019-07-24 stsp if (f == NULL) {
8172 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8173 0ebf8283 2019-07-24 stsp goto done;
8174 0ebf8283 2019-07-24 stsp }
8175 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8176 bfce7f83 2019-07-27 stsp if (err)
8177 bfce7f83 2019-07-27 stsp goto done;
8178 bfce7f83 2019-07-27 stsp
8179 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8180 0ebf8283 2019-07-24 stsp done:
8181 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8182 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8183 0ebf8283 2019-07-24 stsp free(editor);
8184 0ebf8283 2019-07-24 stsp return err;
8185 0ebf8283 2019-07-24 stsp }
8186 0ebf8283 2019-07-24 stsp
8187 0ebf8283 2019-07-24 stsp static const struct got_error *
8188 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8189 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8190 514f2ffe 2020-01-29 stsp struct got_repository *);
8191 0ebf8283 2019-07-24 stsp
8192 0ebf8283 2019-07-24 stsp static const struct got_error *
8193 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8194 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8195 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8196 0ebf8283 2019-07-24 stsp {
8197 0ebf8283 2019-07-24 stsp const struct got_error *err;
8198 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8199 0ebf8283 2019-07-24 stsp char *path = NULL;
8200 0ebf8283 2019-07-24 stsp
8201 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8202 0ebf8283 2019-07-24 stsp if (err)
8203 0ebf8283 2019-07-24 stsp return err;
8204 0ebf8283 2019-07-24 stsp
8205 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8206 0ebf8283 2019-07-24 stsp if (err)
8207 0ebf8283 2019-07-24 stsp goto done;
8208 0ebf8283 2019-07-24 stsp
8209 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8210 0ebf8283 2019-07-24 stsp if (err)
8211 0ebf8283 2019-07-24 stsp goto done;
8212 0ebf8283 2019-07-24 stsp
8213 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8214 083957f4 2020-02-24 stsp rewind(f);
8215 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8216 083957f4 2020-02-24 stsp } else {
8217 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8218 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8219 0ebf8283 2019-07-24 stsp goto done;
8220 083957f4 2020-02-24 stsp }
8221 083957f4 2020-02-24 stsp f = NULL;
8222 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8223 083957f4 2020-02-24 stsp if (err) {
8224 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8225 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8226 083957f4 2020-02-24 stsp goto done;
8227 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8228 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8229 083957f4 2020-02-24 stsp }
8230 0ebf8283 2019-07-24 stsp }
8231 0ebf8283 2019-07-24 stsp done:
8232 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8233 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8234 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8235 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8236 0ebf8283 2019-07-24 stsp free(path);
8237 0ebf8283 2019-07-24 stsp return err;
8238 0ebf8283 2019-07-24 stsp }
8239 0ebf8283 2019-07-24 stsp
8240 0ebf8283 2019-07-24 stsp static const struct got_error *
8241 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8242 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8243 0ebf8283 2019-07-24 stsp {
8244 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8245 0ebf8283 2019-07-24 stsp char *path = NULL;
8246 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8247 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8248 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8249 0ebf8283 2019-07-24 stsp
8250 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8251 0ebf8283 2019-07-24 stsp if (err)
8252 0ebf8283 2019-07-24 stsp return err;
8253 0ebf8283 2019-07-24 stsp
8254 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8255 0ebf8283 2019-07-24 stsp if (f == NULL) {
8256 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8257 0ebf8283 2019-07-24 stsp goto done;
8258 0ebf8283 2019-07-24 stsp }
8259 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8260 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8261 0ebf8283 2019-07-24 stsp repo);
8262 0ebf8283 2019-07-24 stsp if (err)
8263 0ebf8283 2019-07-24 stsp break;
8264 0ebf8283 2019-07-24 stsp
8265 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8266 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8267 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8268 0ebf8283 2019-07-24 stsp if (n < 0) {
8269 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8270 0ebf8283 2019-07-24 stsp break;
8271 0ebf8283 2019-07-24 stsp }
8272 0ebf8283 2019-07-24 stsp }
8273 0ebf8283 2019-07-24 stsp }
8274 0ebf8283 2019-07-24 stsp done:
8275 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8276 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8277 0ebf8283 2019-07-24 stsp free(path);
8278 0ebf8283 2019-07-24 stsp if (commit)
8279 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8280 0ebf8283 2019-07-24 stsp return err;
8281 0ebf8283 2019-07-24 stsp }
8282 0ebf8283 2019-07-24 stsp
8283 bfce7f83 2019-07-27 stsp void
8284 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8285 bfce7f83 2019-07-27 stsp {
8286 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8287 bfce7f83 2019-07-27 stsp
8288 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8289 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8290 bfce7f83 2019-07-27 stsp free(hle);
8291 bfce7f83 2019-07-27 stsp }
8292 bfce7f83 2019-07-27 stsp }
8293 bfce7f83 2019-07-27 stsp
8294 0ebf8283 2019-07-24 stsp static const struct got_error *
8295 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8296 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8297 0ebf8283 2019-07-24 stsp {
8298 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8299 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8300 0ebf8283 2019-07-24 stsp
8301 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8302 0ebf8283 2019-07-24 stsp if (f == NULL) {
8303 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8304 0ebf8283 2019-07-24 stsp goto done;
8305 0ebf8283 2019-07-24 stsp }
8306 0ebf8283 2019-07-24 stsp
8307 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8308 0ebf8283 2019-07-24 stsp done:
8309 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8310 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8311 0ebf8283 2019-07-24 stsp return err;
8312 0ebf8283 2019-07-24 stsp }
8313 0ebf8283 2019-07-24 stsp
8314 0ebf8283 2019-07-24 stsp static const struct got_error *
8315 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8316 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8317 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8318 0ebf8283 2019-07-24 stsp {
8319 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8320 0ebf8283 2019-07-24 stsp int resp = ' ';
8321 0ebf8283 2019-07-24 stsp
8322 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8323 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8324 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8325 0ebf8283 2019-07-24 stsp resp = getchar();
8326 a61a4414 2019-08-07 stsp if (resp == '\n')
8327 a61a4414 2019-08-07 stsp resp = getchar();
8328 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8329 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8330 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8331 bfce7f83 2019-07-27 stsp repo);
8332 426ebf2e 2019-07-25 stsp if (err) {
8333 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8334 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8335 426ebf2e 2019-07-25 stsp break;
8336 bfce7f83 2019-07-27 stsp prev_err = err;
8337 426ebf2e 2019-07-25 stsp resp = ' ';
8338 426ebf2e 2019-07-25 stsp continue;
8339 426ebf2e 2019-07-25 stsp }
8340 bfce7f83 2019-07-27 stsp break;
8341 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8342 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8343 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8344 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8345 426ebf2e 2019-07-25 stsp if (err) {
8346 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8347 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8348 426ebf2e 2019-07-25 stsp break;
8349 bfce7f83 2019-07-27 stsp prev_err = err;
8350 426ebf2e 2019-07-25 stsp resp = ' ';
8351 426ebf2e 2019-07-25 stsp continue;
8352 426ebf2e 2019-07-25 stsp }
8353 bfce7f83 2019-07-27 stsp break;
8354 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8355 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8356 0ebf8283 2019-07-24 stsp break;
8357 426ebf2e 2019-07-25 stsp } else
8358 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8359 0ebf8283 2019-07-24 stsp }
8360 0ebf8283 2019-07-24 stsp
8361 0ebf8283 2019-07-24 stsp return err;
8362 0ebf8283 2019-07-24 stsp }
8363 0ebf8283 2019-07-24 stsp
8364 0ebf8283 2019-07-24 stsp static const struct got_error *
8365 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8366 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8367 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8368 0ebf8283 2019-07-24 stsp {
8369 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8370 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8371 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8372 3e3a69f1 2019-07-25 stsp branch, repo);
8373 0ebf8283 2019-07-24 stsp }
8374 0ebf8283 2019-07-24 stsp
8375 0ebf8283 2019-07-24 stsp static const struct got_error *
8376 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8377 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8378 0ebf8283 2019-07-24 stsp {
8379 0ebf8283 2019-07-24 stsp const struct got_error *err;
8380 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8381 0ebf8283 2019-07-24 stsp
8382 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8383 0ebf8283 2019-07-24 stsp if (err)
8384 0ebf8283 2019-07-24 stsp goto done;
8385 0ebf8283 2019-07-24 stsp
8386 0ebf8283 2019-07-24 stsp if (new_id) {
8387 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8388 0ebf8283 2019-07-24 stsp if (err)
8389 0ebf8283 2019-07-24 stsp goto done;
8390 0ebf8283 2019-07-24 stsp }
8391 0ebf8283 2019-07-24 stsp
8392 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8393 0ebf8283 2019-07-24 stsp if (new_id_str)
8394 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8395 0ebf8283 2019-07-24 stsp
8396 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8397 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8398 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8399 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8400 0ebf8283 2019-07-24 stsp goto done;
8401 0ebf8283 2019-07-24 stsp }
8402 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8403 0ebf8283 2019-07-24 stsp } else {
8404 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8405 0ebf8283 2019-07-24 stsp if (err)
8406 0ebf8283 2019-07-24 stsp goto done;
8407 0ebf8283 2019-07-24 stsp }
8408 0ebf8283 2019-07-24 stsp
8409 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8410 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8411 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8412 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8413 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8414 0ebf8283 2019-07-24 stsp break;
8415 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8416 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8417 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8418 0ebf8283 2019-07-24 stsp logmsg);
8419 0ebf8283 2019-07-24 stsp break;
8420 0ebf8283 2019-07-24 stsp default:
8421 0ebf8283 2019-07-24 stsp break;
8422 0ebf8283 2019-07-24 stsp }
8423 0ebf8283 2019-07-24 stsp done:
8424 0ebf8283 2019-07-24 stsp free(old_id_str);
8425 0ebf8283 2019-07-24 stsp free(new_id_str);
8426 0ebf8283 2019-07-24 stsp return err;
8427 0ebf8283 2019-07-24 stsp }
8428 0ebf8283 2019-07-24 stsp
8429 0ebf8283 2019-07-24 stsp static const struct got_error *
8430 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8431 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8432 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8433 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8434 0ebf8283 2019-07-24 stsp {
8435 0ebf8283 2019-07-24 stsp const struct got_error *err;
8436 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8437 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8438 0ebf8283 2019-07-24 stsp
8439 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8440 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8441 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8442 0ebf8283 2019-07-24 stsp if (err)
8443 0ebf8283 2019-07-24 stsp return err;
8444 0ebf8283 2019-07-24 stsp }
8445 0ebf8283 2019-07-24 stsp
8446 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8447 0ebf8283 2019-07-24 stsp if (err)
8448 0ebf8283 2019-07-24 stsp return err;
8449 0ebf8283 2019-07-24 stsp
8450 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8451 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8452 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8453 0ebf8283 2019-07-24 stsp if (err) {
8454 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8455 0ebf8283 2019-07-24 stsp goto done;
8456 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8457 0ebf8283 2019-07-24 stsp } else {
8458 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8459 0ebf8283 2019-07-24 stsp free(new_commit_id);
8460 0ebf8283 2019-07-24 stsp }
8461 0ebf8283 2019-07-24 stsp done:
8462 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8463 0ebf8283 2019-07-24 stsp return err;
8464 0ebf8283 2019-07-24 stsp }
8465 0ebf8283 2019-07-24 stsp
8466 0ebf8283 2019-07-24 stsp static const struct got_error *
8467 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8468 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8469 0ebf8283 2019-07-24 stsp {
8470 0ebf8283 2019-07-24 stsp const struct got_error *error;
8471 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8472 0ebf8283 2019-07-24 stsp
8473 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8474 0ebf8283 2019-07-24 stsp repo);
8475 0ebf8283 2019-07-24 stsp if (error)
8476 0ebf8283 2019-07-24 stsp return error;
8477 0ebf8283 2019-07-24 stsp
8478 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8479 0ebf8283 2019-07-24 stsp if (error)
8480 0ebf8283 2019-07-24 stsp return error;
8481 0ebf8283 2019-07-24 stsp
8482 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8483 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8484 0ebf8283 2019-07-24 stsp return error;
8485 ab20a43a 2020-01-29 stsp }
8486 ab20a43a 2020-01-29 stsp
8487 ab20a43a 2020-01-29 stsp static const struct got_error *
8488 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8489 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8490 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8491 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8492 ab20a43a 2020-01-29 stsp {
8493 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8494 ab20a43a 2020-01-29 stsp
8495 ab20a43a 2020-01-29 stsp switch (status) {
8496 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8497 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8498 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8499 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8500 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8501 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8502 ab20a43a 2020-01-29 stsp default:
8503 ab20a43a 2020-01-29 stsp break;
8504 ab20a43a 2020-01-29 stsp }
8505 ab20a43a 2020-01-29 stsp
8506 ab20a43a 2020-01-29 stsp switch (staged_status) {
8507 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8508 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8509 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8510 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8511 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8512 ab20a43a 2020-01-29 stsp default:
8513 ab20a43a 2020-01-29 stsp break;
8514 ab20a43a 2020-01-29 stsp }
8515 ab20a43a 2020-01-29 stsp
8516 ab20a43a 2020-01-29 stsp return NULL;
8517 0ebf8283 2019-07-24 stsp }
8518 0ebf8283 2019-07-24 stsp
8519 0ebf8283 2019-07-24 stsp static const struct got_error *
8520 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8521 0ebf8283 2019-07-24 stsp {
8522 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8523 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8524 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8525 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8526 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8527 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8528 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8529 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8530 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8531 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8532 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8533 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8534 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8535 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8536 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8537 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8538 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8539 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8540 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8541 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8542 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8543 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8544 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8545 0ebf8283 2019-07-24 stsp
8546 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8547 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8548 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8549 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8550 0ebf8283 2019-07-24 stsp
8551 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8552 0ebf8283 2019-07-24 stsp switch (ch) {
8553 0ebf8283 2019-07-24 stsp case 'a':
8554 0ebf8283 2019-07-24 stsp abort_edit = 1;
8555 0ebf8283 2019-07-24 stsp break;
8556 0ebf8283 2019-07-24 stsp case 'c':
8557 0ebf8283 2019-07-24 stsp continue_edit = 1;
8558 0ebf8283 2019-07-24 stsp break;
8559 0ebf8283 2019-07-24 stsp case 'F':
8560 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8561 0ebf8283 2019-07-24 stsp break;
8562 083957f4 2020-02-24 stsp case 'm':
8563 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8564 083957f4 2020-02-24 stsp break;
8565 0ebf8283 2019-07-24 stsp default:
8566 0ebf8283 2019-07-24 stsp usage_histedit();
8567 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8568 0ebf8283 2019-07-24 stsp }
8569 0ebf8283 2019-07-24 stsp }
8570 0ebf8283 2019-07-24 stsp
8571 0ebf8283 2019-07-24 stsp argc -= optind;
8572 0ebf8283 2019-07-24 stsp argv += optind;
8573 0ebf8283 2019-07-24 stsp
8574 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8575 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8576 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8577 0ebf8283 2019-07-24 stsp err(1, "pledge");
8578 0ebf8283 2019-07-24 stsp #endif
8579 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8580 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8581 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8582 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8583 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8584 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8585 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8586 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8587 0ebf8283 2019-07-24 stsp if (argc != 0)
8588 0ebf8283 2019-07-24 stsp usage_histedit();
8589 0ebf8283 2019-07-24 stsp
8590 0ebf8283 2019-07-24 stsp /*
8591 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8592 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8593 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8594 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8595 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8596 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8597 0ebf8283 2019-07-24 stsp */
8598 0ebf8283 2019-07-24 stsp
8599 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8600 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8601 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8602 0ebf8283 2019-07-24 stsp goto done;
8603 0ebf8283 2019-07-24 stsp }
8604 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8605 fa51e947 2020-03-27 stsp if (error) {
8606 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8607 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8608 0ebf8283 2019-07-24 stsp goto done;
8609 fa51e947 2020-03-27 stsp }
8610 0ebf8283 2019-07-24 stsp
8611 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8612 c9956ddf 2019-09-08 stsp NULL);
8613 0ebf8283 2019-07-24 stsp if (error != NULL)
8614 0ebf8283 2019-07-24 stsp goto done;
8615 0ebf8283 2019-07-24 stsp
8616 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8617 0ebf8283 2019-07-24 stsp if (error)
8618 0ebf8283 2019-07-24 stsp goto done;
8619 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8620 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8621 0ebf8283 2019-07-24 stsp goto done;
8622 0ebf8283 2019-07-24 stsp }
8623 0ebf8283 2019-07-24 stsp
8624 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8625 0ebf8283 2019-07-24 stsp if (error)
8626 0ebf8283 2019-07-24 stsp goto done;
8627 0ebf8283 2019-07-24 stsp
8628 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8629 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8630 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8631 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8632 083957f4 2020-02-24 stsp "before the -m option can be used");
8633 083957f4 2020-02-24 stsp goto done;
8634 083957f4 2020-02-24 stsp }
8635 083957f4 2020-02-24 stsp
8636 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8637 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8638 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8639 3e3a69f1 2019-07-25 stsp worktree, repo);
8640 0ebf8283 2019-07-24 stsp if (error)
8641 0ebf8283 2019-07-24 stsp goto done;
8642 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8643 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8644 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8645 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8646 0ebf8283 2019-07-24 stsp if (error)
8647 0ebf8283 2019-07-24 stsp goto done;
8648 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8649 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8650 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8651 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8652 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8653 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8654 0ebf8283 2019-07-24 stsp goto done;
8655 0ebf8283 2019-07-24 stsp }
8656 0ebf8283 2019-07-24 stsp
8657 0ebf8283 2019-07-24 stsp if (continue_edit) {
8658 0ebf8283 2019-07-24 stsp char *path;
8659 0ebf8283 2019-07-24 stsp
8660 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8661 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8662 0ebf8283 2019-07-24 stsp goto done;
8663 0ebf8283 2019-07-24 stsp }
8664 0ebf8283 2019-07-24 stsp
8665 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8666 0ebf8283 2019-07-24 stsp if (error)
8667 0ebf8283 2019-07-24 stsp goto done;
8668 0ebf8283 2019-07-24 stsp
8669 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8670 0ebf8283 2019-07-24 stsp free(path);
8671 0ebf8283 2019-07-24 stsp if (error)
8672 0ebf8283 2019-07-24 stsp goto done;
8673 0ebf8283 2019-07-24 stsp
8674 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8675 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8676 3e3a69f1 2019-07-25 stsp worktree, repo);
8677 0ebf8283 2019-07-24 stsp if (error)
8678 0ebf8283 2019-07-24 stsp goto done;
8679 0ebf8283 2019-07-24 stsp
8680 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8681 0ebf8283 2019-07-24 stsp if (error)
8682 0ebf8283 2019-07-24 stsp goto done;
8683 0ebf8283 2019-07-24 stsp
8684 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8685 0ebf8283 2019-07-24 stsp head_commit_id);
8686 0ebf8283 2019-07-24 stsp if (error)
8687 0ebf8283 2019-07-24 stsp goto done;
8688 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8689 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8690 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8691 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8692 3aac7cf7 2019-07-25 stsp goto done;
8693 3aac7cf7 2019-07-25 stsp }
8694 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8695 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8696 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8697 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8698 0ebf8283 2019-07-24 stsp commit = NULL;
8699 0ebf8283 2019-07-24 stsp if (error)
8700 0ebf8283 2019-07-24 stsp goto done;
8701 0ebf8283 2019-07-24 stsp } else {
8702 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8703 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8704 0ebf8283 2019-07-24 stsp goto done;
8705 0ebf8283 2019-07-24 stsp }
8706 0ebf8283 2019-07-24 stsp
8707 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8708 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8709 0ebf8283 2019-07-24 stsp if (error != NULL)
8710 0ebf8283 2019-07-24 stsp goto done;
8711 0ebf8283 2019-07-24 stsp
8712 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8713 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8714 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8715 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8716 c7d20a3f 2019-07-30 stsp goto done;
8717 c7d20a3f 2019-07-30 stsp }
8718 c7d20a3f 2019-07-30 stsp
8719 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8720 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8721 bfce7f83 2019-07-27 stsp branch = NULL;
8722 0ebf8283 2019-07-24 stsp if (error)
8723 0ebf8283 2019-07-24 stsp goto done;
8724 0ebf8283 2019-07-24 stsp
8725 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8726 0ebf8283 2019-07-24 stsp head_commit_id);
8727 0ebf8283 2019-07-24 stsp if (error)
8728 0ebf8283 2019-07-24 stsp goto done;
8729 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8730 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8731 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8732 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8733 3aac7cf7 2019-07-25 stsp goto done;
8734 3aac7cf7 2019-07-25 stsp }
8735 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8736 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8737 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8738 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8739 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8740 0ebf8283 2019-07-24 stsp commit = NULL;
8741 0ebf8283 2019-07-24 stsp if (error)
8742 0ebf8283 2019-07-24 stsp goto done;
8743 0ebf8283 2019-07-24 stsp
8744 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8745 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8746 c5996fff 2020-01-29 stsp goto done;
8747 c5996fff 2020-01-29 stsp }
8748 c5996fff 2020-01-29 stsp
8749 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8750 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8751 bfce7f83 2019-07-27 stsp if (error)
8752 bfce7f83 2019-07-27 stsp goto done;
8753 bfce7f83 2019-07-27 stsp
8754 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8755 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8756 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8757 6ba2bea2 2019-07-27 stsp if (error) {
8758 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8759 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8760 9627c110 2020-04-18 stsp update_progress, &upa);
8761 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8762 0ebf8283 2019-07-24 stsp goto done;
8763 6ba2bea2 2019-07-27 stsp }
8764 0ebf8283 2019-07-24 stsp } else {
8765 514f2ffe 2020-01-29 stsp const char *branch_name;
8766 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8767 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8768 514f2ffe 2020-01-29 stsp branch_name += 11;
8769 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8770 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8771 6ba2bea2 2019-07-27 stsp if (error) {
8772 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8773 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8774 9627c110 2020-04-18 stsp update_progress, &upa);
8775 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8776 0ebf8283 2019-07-24 stsp goto done;
8777 6ba2bea2 2019-07-27 stsp }
8778 0ebf8283 2019-07-24 stsp
8779 0ebf8283 2019-07-24 stsp }
8780 0ebf8283 2019-07-24 stsp
8781 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8782 0ebf8283 2019-07-24 stsp repo);
8783 6ba2bea2 2019-07-27 stsp if (error) {
8784 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8785 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8786 9627c110 2020-04-18 stsp update_progress, &upa);
8787 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8788 0ebf8283 2019-07-24 stsp goto done;
8789 6ba2bea2 2019-07-27 stsp }
8790 0ebf8283 2019-07-24 stsp
8791 0ebf8283 2019-07-24 stsp }
8792 0ebf8283 2019-07-24 stsp
8793 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8794 4ec14e60 2019-08-28 hiltjo if (error)
8795 0ebf8283 2019-07-24 stsp goto done;
8796 0ebf8283 2019-07-24 stsp
8797 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8798 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8799 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8800 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8801 0ebf8283 2019-07-24 stsp continue;
8802 0ebf8283 2019-07-24 stsp
8803 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8804 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8805 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8806 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8807 0ebf8283 2019-07-24 stsp repo);
8808 ab20a43a 2020-01-29 stsp if (error)
8809 ab20a43a 2020-01-29 stsp goto done;
8810 0ebf8283 2019-07-24 stsp } else {
8811 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8812 ab20a43a 2020-01-29 stsp int have_changes = 0;
8813 ab20a43a 2020-01-29 stsp
8814 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8815 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8816 ab20a43a 2020-01-29 stsp if (error)
8817 ab20a43a 2020-01-29 stsp goto done;
8818 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8819 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8820 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8821 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8822 ab20a43a 2020-01-29 stsp if (error) {
8823 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8824 ab20a43a 2020-01-29 stsp goto done;
8825 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8826 ab20a43a 2020-01-29 stsp goto done;
8827 ab20a43a 2020-01-29 stsp }
8828 ab20a43a 2020-01-29 stsp if (have_changes) {
8829 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8830 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8831 ab20a43a 2020-01-29 stsp if (error)
8832 ab20a43a 2020-01-29 stsp goto done;
8833 ab20a43a 2020-01-29 stsp } else {
8834 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8835 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8836 ab20a43a 2020-01-29 stsp if (error)
8837 ab20a43a 2020-01-29 stsp goto done;
8838 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8839 ab20a43a 2020-01-29 stsp hle, NULL);
8840 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8841 ab20a43a 2020-01-29 stsp commit = NULL;
8842 ab20a43a 2020-01-29 stsp if (error)
8843 ab20a43a 2020-01-29 stsp goto done;
8844 ab20a43a 2020-01-29 stsp }
8845 0ebf8283 2019-07-24 stsp }
8846 0ebf8283 2019-07-24 stsp continue;
8847 0ebf8283 2019-07-24 stsp }
8848 0ebf8283 2019-07-24 stsp
8849 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8850 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8851 0ebf8283 2019-07-24 stsp if (error)
8852 0ebf8283 2019-07-24 stsp goto done;
8853 0ebf8283 2019-07-24 stsp continue;
8854 0ebf8283 2019-07-24 stsp }
8855 0ebf8283 2019-07-24 stsp
8856 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8857 0ebf8283 2019-07-24 stsp hle->commit_id);
8858 0ebf8283 2019-07-24 stsp if (error)
8859 0ebf8283 2019-07-24 stsp goto done;
8860 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8861 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8862 0ebf8283 2019-07-24 stsp
8863 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8864 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8865 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8866 0ebf8283 2019-07-24 stsp if (error)
8867 0ebf8283 2019-07-24 stsp goto done;
8868 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8869 0ebf8283 2019-07-24 stsp commit = NULL;
8870 0ebf8283 2019-07-24 stsp
8871 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8872 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8873 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8874 9627c110 2020-04-18 stsp
8875 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8876 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8877 a0ea4fc0 2020-02-28 stsp repo);
8878 a0ea4fc0 2020-02-28 stsp if (error)
8879 a0ea4fc0 2020-02-28 stsp goto done;
8880 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8881 0ebf8283 2019-07-24 stsp break;
8882 0ebf8283 2019-07-24 stsp }
8883 0ebf8283 2019-07-24 stsp
8884 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8885 0ebf8283 2019-07-24 stsp char *id_str;
8886 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8887 0ebf8283 2019-07-24 stsp if (error)
8888 0ebf8283 2019-07-24 stsp goto done;
8889 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8890 0ebf8283 2019-07-24 stsp id_str);
8891 0ebf8283 2019-07-24 stsp free(id_str);
8892 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8893 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8894 3e3a69f1 2019-07-25 stsp fileindex);
8895 0ebf8283 2019-07-24 stsp goto done;
8896 0ebf8283 2019-07-24 stsp }
8897 0ebf8283 2019-07-24 stsp
8898 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8899 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8900 0ebf8283 2019-07-24 stsp if (error)
8901 0ebf8283 2019-07-24 stsp goto done;
8902 0ebf8283 2019-07-24 stsp continue;
8903 0ebf8283 2019-07-24 stsp }
8904 0ebf8283 2019-07-24 stsp
8905 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8906 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8907 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8908 0ebf8283 2019-07-24 stsp if (error)
8909 0ebf8283 2019-07-24 stsp goto done;
8910 0ebf8283 2019-07-24 stsp }
8911 0ebf8283 2019-07-24 stsp
8912 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8913 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8914 0ebf8283 2019-07-24 stsp if (error)
8915 0ebf8283 2019-07-24 stsp goto done;
8916 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8917 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8918 0ebf8283 2019-07-24 stsp } else
8919 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8920 3e3a69f1 2019-07-25 stsp branch, repo);
8921 0ebf8283 2019-07-24 stsp done:
8922 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8923 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8924 0ebf8283 2019-07-24 stsp free(head_commit_id);
8925 0ebf8283 2019-07-24 stsp free(base_commit_id);
8926 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8927 0ebf8283 2019-07-24 stsp if (commit)
8928 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8929 0ebf8283 2019-07-24 stsp if (branch)
8930 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8931 0ebf8283 2019-07-24 stsp if (tmp_branch)
8932 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8933 0ebf8283 2019-07-24 stsp if (worktree)
8934 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8935 2822a352 2019-10-15 stsp if (repo)
8936 2822a352 2019-10-15 stsp got_repo_close(repo);
8937 2822a352 2019-10-15 stsp return error;
8938 2822a352 2019-10-15 stsp }
8939 2822a352 2019-10-15 stsp
8940 2822a352 2019-10-15 stsp __dead static void
8941 2822a352 2019-10-15 stsp usage_integrate(void)
8942 2822a352 2019-10-15 stsp {
8943 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8944 2822a352 2019-10-15 stsp exit(1);
8945 2822a352 2019-10-15 stsp }
8946 2822a352 2019-10-15 stsp
8947 2822a352 2019-10-15 stsp static const struct got_error *
8948 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8949 2822a352 2019-10-15 stsp {
8950 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8951 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8952 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8953 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8954 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8955 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8956 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8957 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8958 9627c110 2020-04-18 stsp int ch;
8959 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8960 2822a352 2019-10-15 stsp
8961 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8962 2822a352 2019-10-15 stsp switch (ch) {
8963 2822a352 2019-10-15 stsp default:
8964 2822a352 2019-10-15 stsp usage_integrate();
8965 2822a352 2019-10-15 stsp /* NOTREACHED */
8966 2822a352 2019-10-15 stsp }
8967 2822a352 2019-10-15 stsp }
8968 2822a352 2019-10-15 stsp
8969 2822a352 2019-10-15 stsp argc -= optind;
8970 2822a352 2019-10-15 stsp argv += optind;
8971 2822a352 2019-10-15 stsp
8972 2822a352 2019-10-15 stsp if (argc != 1)
8973 2822a352 2019-10-15 stsp usage_integrate();
8974 2822a352 2019-10-15 stsp branch_arg = argv[0];
8975 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
8976 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8977 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8978 2822a352 2019-10-15 stsp err(1, "pledge");
8979 b08a0ccd 2020-09-24 stsp #endif
8980 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8981 2822a352 2019-10-15 stsp if (cwd == NULL) {
8982 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8983 2822a352 2019-10-15 stsp goto done;
8984 2822a352 2019-10-15 stsp }
8985 2822a352 2019-10-15 stsp
8986 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8987 fa51e947 2020-03-27 stsp if (error) {
8988 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8989 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8990 fa51e947 2020-03-27 stsp cwd);
8991 2822a352 2019-10-15 stsp goto done;
8992 fa51e947 2020-03-27 stsp }
8993 2822a352 2019-10-15 stsp
8994 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8995 2822a352 2019-10-15 stsp if (error)
8996 2822a352 2019-10-15 stsp goto done;
8997 2822a352 2019-10-15 stsp
8998 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8999 2822a352 2019-10-15 stsp NULL);
9000 2822a352 2019-10-15 stsp if (error != NULL)
9001 2822a352 2019-10-15 stsp goto done;
9002 2822a352 2019-10-15 stsp
9003 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9004 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9005 2822a352 2019-10-15 stsp if (error)
9006 2822a352 2019-10-15 stsp goto done;
9007 2822a352 2019-10-15 stsp
9008 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9009 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9010 2822a352 2019-10-15 stsp goto done;
9011 2822a352 2019-10-15 stsp }
9012 2822a352 2019-10-15 stsp
9013 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9014 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9015 2822a352 2019-10-15 stsp if (error)
9016 2822a352 2019-10-15 stsp goto done;
9017 2822a352 2019-10-15 stsp
9018 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9019 2822a352 2019-10-15 stsp if (refname == NULL) {
9020 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9021 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9022 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9023 2822a352 2019-10-15 stsp goto done;
9024 2822a352 2019-10-15 stsp }
9025 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9026 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9027 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9028 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9029 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9030 2822a352 2019-10-15 stsp goto done;
9031 2822a352 2019-10-15 stsp }
9032 2822a352 2019-10-15 stsp
9033 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9034 2822a352 2019-10-15 stsp if (error)
9035 2822a352 2019-10-15 stsp goto done;
9036 2822a352 2019-10-15 stsp
9037 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9038 2822a352 2019-10-15 stsp if (error)
9039 2822a352 2019-10-15 stsp goto done;
9040 2822a352 2019-10-15 stsp
9041 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9042 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9043 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9044 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9045 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9046 2822a352 2019-10-15 stsp goto done;
9047 2822a352 2019-10-15 stsp }
9048 2822a352 2019-10-15 stsp
9049 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9050 2822a352 2019-10-15 stsp if (error) {
9051 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9052 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9053 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9054 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9055 2822a352 2019-10-15 stsp goto done;
9056 2822a352 2019-10-15 stsp }
9057 2822a352 2019-10-15 stsp
9058 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9059 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9060 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9061 2822a352 2019-10-15 stsp check_cancelled, NULL);
9062 2822a352 2019-10-15 stsp if (error)
9063 2822a352 2019-10-15 stsp goto done;
9064 2822a352 2019-10-15 stsp
9065 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9066 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9067 2822a352 2019-10-15 stsp done:
9068 715dc77e 2019-08-03 stsp if (repo)
9069 715dc77e 2019-08-03 stsp got_repo_close(repo);
9070 2822a352 2019-10-15 stsp if (worktree)
9071 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9072 2822a352 2019-10-15 stsp free(cwd);
9073 2822a352 2019-10-15 stsp free(base_commit_id);
9074 2822a352 2019-10-15 stsp free(commit_id);
9075 2822a352 2019-10-15 stsp free(refname);
9076 2822a352 2019-10-15 stsp free(base_refname);
9077 715dc77e 2019-08-03 stsp return error;
9078 715dc77e 2019-08-03 stsp }
9079 715dc77e 2019-08-03 stsp
9080 715dc77e 2019-08-03 stsp __dead static void
9081 715dc77e 2019-08-03 stsp usage_stage(void)
9082 715dc77e 2019-08-03 stsp {
9083 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9084 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9085 715dc77e 2019-08-03 stsp getprogname());
9086 715dc77e 2019-08-03 stsp exit(1);
9087 715dc77e 2019-08-03 stsp }
9088 715dc77e 2019-08-03 stsp
9089 715dc77e 2019-08-03 stsp static const struct got_error *
9090 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9091 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9092 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9093 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9094 408b4ebc 2019-08-03 stsp {
9095 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9096 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9097 408b4ebc 2019-08-03 stsp
9098 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9099 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9100 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9101 408b4ebc 2019-08-03 stsp return NULL;
9102 408b4ebc 2019-08-03 stsp
9103 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9104 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9105 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9106 408b4ebc 2019-08-03 stsp else
9107 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9108 408b4ebc 2019-08-03 stsp if (err)
9109 408b4ebc 2019-08-03 stsp return err;
9110 408b4ebc 2019-08-03 stsp
9111 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9112 408b4ebc 2019-08-03 stsp free(id_str);
9113 dc424a06 2019-08-07 stsp return NULL;
9114 dc424a06 2019-08-07 stsp }
9115 2e1f37b0 2019-08-08 stsp
9116 dc424a06 2019-08-07 stsp static const struct got_error *
9117 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9118 715dc77e 2019-08-03 stsp {
9119 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9120 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9121 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9122 715dc77e 2019-08-03 stsp char *cwd = NULL;
9123 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9124 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9125 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9126 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9127 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9128 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9129 715dc77e 2019-08-03 stsp
9130 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9131 715dc77e 2019-08-03 stsp
9132 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9133 715dc77e 2019-08-03 stsp switch (ch) {
9134 408b4ebc 2019-08-03 stsp case 'l':
9135 408b4ebc 2019-08-03 stsp list_stage = 1;
9136 408b4ebc 2019-08-03 stsp break;
9137 dc424a06 2019-08-07 stsp case 'p':
9138 dc424a06 2019-08-07 stsp pflag = 1;
9139 dc424a06 2019-08-07 stsp break;
9140 dc424a06 2019-08-07 stsp case 'F':
9141 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9142 dc424a06 2019-08-07 stsp break;
9143 35213c7c 2020-07-23 stsp case 'S':
9144 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9145 35213c7c 2020-07-23 stsp break;
9146 715dc77e 2019-08-03 stsp default:
9147 715dc77e 2019-08-03 stsp usage_stage();
9148 715dc77e 2019-08-03 stsp /* NOTREACHED */
9149 715dc77e 2019-08-03 stsp }
9150 715dc77e 2019-08-03 stsp }
9151 715dc77e 2019-08-03 stsp
9152 715dc77e 2019-08-03 stsp argc -= optind;
9153 715dc77e 2019-08-03 stsp argv += optind;
9154 715dc77e 2019-08-03 stsp
9155 715dc77e 2019-08-03 stsp #ifndef PROFILE
9156 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9157 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9158 715dc77e 2019-08-03 stsp err(1, "pledge");
9159 715dc77e 2019-08-03 stsp #endif
9160 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9161 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9162 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9163 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9164 715dc77e 2019-08-03 stsp
9165 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9166 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9167 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9168 715dc77e 2019-08-03 stsp goto done;
9169 715dc77e 2019-08-03 stsp }
9170 715dc77e 2019-08-03 stsp
9171 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9172 fa51e947 2020-03-27 stsp if (error) {
9173 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9174 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9175 715dc77e 2019-08-03 stsp goto done;
9176 fa51e947 2020-03-27 stsp }
9177 715dc77e 2019-08-03 stsp
9178 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9179 c9956ddf 2019-09-08 stsp NULL);
9180 715dc77e 2019-08-03 stsp if (error != NULL)
9181 715dc77e 2019-08-03 stsp goto done;
9182 715dc77e 2019-08-03 stsp
9183 dc424a06 2019-08-07 stsp if (patch_script_path) {
9184 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9185 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9186 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9187 dc424a06 2019-08-07 stsp patch_script_path);
9188 dc424a06 2019-08-07 stsp goto done;
9189 dc424a06 2019-08-07 stsp }
9190 dc424a06 2019-08-07 stsp }
9191 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9192 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9193 715dc77e 2019-08-03 stsp if (error)
9194 715dc77e 2019-08-03 stsp goto done;
9195 715dc77e 2019-08-03 stsp
9196 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9197 6d022e97 2019-08-04 stsp if (error)
9198 6d022e97 2019-08-04 stsp goto done;
9199 715dc77e 2019-08-03 stsp
9200 6d022e97 2019-08-04 stsp if (list_stage)
9201 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9202 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9203 2e1f37b0 2019-08-08 stsp else {
9204 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9205 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9206 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9207 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9208 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9209 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9210 2e1f37b0 2019-08-08 stsp }
9211 ad493afc 2019-08-03 stsp done:
9212 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9213 2e1f37b0 2019-08-08 stsp error == NULL)
9214 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9215 ad493afc 2019-08-03 stsp if (repo)
9216 ad493afc 2019-08-03 stsp got_repo_close(repo);
9217 ad493afc 2019-08-03 stsp if (worktree)
9218 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9219 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9220 ad493afc 2019-08-03 stsp free((char *)pe->path);
9221 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9222 ad493afc 2019-08-03 stsp free(cwd);
9223 ad493afc 2019-08-03 stsp return error;
9224 ad493afc 2019-08-03 stsp }
9225 ad493afc 2019-08-03 stsp
9226 ad493afc 2019-08-03 stsp __dead static void
9227 ad493afc 2019-08-03 stsp usage_unstage(void)
9228 ad493afc 2019-08-03 stsp {
9229 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9230 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9231 ad493afc 2019-08-03 stsp getprogname());
9232 ad493afc 2019-08-03 stsp exit(1);
9233 ad493afc 2019-08-03 stsp }
9234 ad493afc 2019-08-03 stsp
9235 ad493afc 2019-08-03 stsp
9236 ad493afc 2019-08-03 stsp static const struct got_error *
9237 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9238 ad493afc 2019-08-03 stsp {
9239 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9240 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9241 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9242 ad493afc 2019-08-03 stsp char *cwd = NULL;
9243 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9244 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9245 9627c110 2020-04-18 stsp int ch, pflag = 0;
9246 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9247 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9248 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9249 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9250 ad493afc 2019-08-03 stsp
9251 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9252 ad493afc 2019-08-03 stsp
9253 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9254 ad493afc 2019-08-03 stsp switch (ch) {
9255 2e1f37b0 2019-08-08 stsp case 'p':
9256 2e1f37b0 2019-08-08 stsp pflag = 1;
9257 2e1f37b0 2019-08-08 stsp break;
9258 2e1f37b0 2019-08-08 stsp case 'F':
9259 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9260 2e1f37b0 2019-08-08 stsp break;
9261 ad493afc 2019-08-03 stsp default:
9262 ad493afc 2019-08-03 stsp usage_unstage();
9263 ad493afc 2019-08-03 stsp /* NOTREACHED */
9264 ad493afc 2019-08-03 stsp }
9265 ad493afc 2019-08-03 stsp }
9266 ad493afc 2019-08-03 stsp
9267 ad493afc 2019-08-03 stsp argc -= optind;
9268 ad493afc 2019-08-03 stsp argv += optind;
9269 ad493afc 2019-08-03 stsp
9270 ad493afc 2019-08-03 stsp #ifndef PROFILE
9271 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9272 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9273 ad493afc 2019-08-03 stsp err(1, "pledge");
9274 ad493afc 2019-08-03 stsp #endif
9275 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9276 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9277 2e1f37b0 2019-08-08 stsp
9278 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9279 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9280 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9281 ad493afc 2019-08-03 stsp goto done;
9282 ad493afc 2019-08-03 stsp }
9283 ad493afc 2019-08-03 stsp
9284 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9285 fa51e947 2020-03-27 stsp if (error) {
9286 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9287 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9288 ad493afc 2019-08-03 stsp goto done;
9289 fa51e947 2020-03-27 stsp }
9290 ad493afc 2019-08-03 stsp
9291 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9292 c9956ddf 2019-09-08 stsp NULL);
9293 ad493afc 2019-08-03 stsp if (error != NULL)
9294 ad493afc 2019-08-03 stsp goto done;
9295 ad493afc 2019-08-03 stsp
9296 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9297 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9298 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9299 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9300 2e1f37b0 2019-08-08 stsp patch_script_path);
9301 2e1f37b0 2019-08-08 stsp goto done;
9302 2e1f37b0 2019-08-08 stsp }
9303 2e1f37b0 2019-08-08 stsp }
9304 2e1f37b0 2019-08-08 stsp
9305 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9306 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9307 ad493afc 2019-08-03 stsp if (error)
9308 ad493afc 2019-08-03 stsp goto done;
9309 ad493afc 2019-08-03 stsp
9310 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9311 ad493afc 2019-08-03 stsp if (error)
9312 ad493afc 2019-08-03 stsp goto done;
9313 ad493afc 2019-08-03 stsp
9314 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9315 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9316 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9317 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9318 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9319 9627c110 2020-04-18 stsp if (!error)
9320 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9321 715dc77e 2019-08-03 stsp done:
9322 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9323 2e1f37b0 2019-08-08 stsp error == NULL)
9324 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9325 0ebf8283 2019-07-24 stsp if (repo)
9326 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9327 715dc77e 2019-08-03 stsp if (worktree)
9328 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9329 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9330 715dc77e 2019-08-03 stsp free((char *)pe->path);
9331 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9332 715dc77e 2019-08-03 stsp free(cwd);
9333 01073a5d 2019-08-22 stsp return error;
9334 01073a5d 2019-08-22 stsp }
9335 01073a5d 2019-08-22 stsp
9336 01073a5d 2019-08-22 stsp __dead static void
9337 01073a5d 2019-08-22 stsp usage_cat(void)
9338 01073a5d 2019-08-22 stsp {
9339 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9340 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9341 01073a5d 2019-08-22 stsp exit(1);
9342 01073a5d 2019-08-22 stsp }
9343 01073a5d 2019-08-22 stsp
9344 01073a5d 2019-08-22 stsp static const struct got_error *
9345 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9346 01073a5d 2019-08-22 stsp {
9347 01073a5d 2019-08-22 stsp const struct got_error *err;
9348 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9349 01073a5d 2019-08-22 stsp
9350 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9351 01073a5d 2019-08-22 stsp if (err)
9352 01073a5d 2019-08-22 stsp return err;
9353 01073a5d 2019-08-22 stsp
9354 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9355 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9356 01073a5d 2019-08-22 stsp return err;
9357 01073a5d 2019-08-22 stsp }
9358 01073a5d 2019-08-22 stsp
9359 01073a5d 2019-08-22 stsp static const struct got_error *
9360 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9361 01073a5d 2019-08-22 stsp {
9362 01073a5d 2019-08-22 stsp const struct got_error *err;
9363 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9364 56e0773d 2019-11-28 stsp int nentries, i;
9365 01073a5d 2019-08-22 stsp
9366 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9367 01073a5d 2019-08-22 stsp if (err)
9368 01073a5d 2019-08-22 stsp return err;
9369 01073a5d 2019-08-22 stsp
9370 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9371 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9372 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9373 01073a5d 2019-08-22 stsp char *id_str;
9374 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9375 01073a5d 2019-08-22 stsp break;
9376 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9377 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9378 01073a5d 2019-08-22 stsp if (err)
9379 01073a5d 2019-08-22 stsp break;
9380 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9381 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9382 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9383 01073a5d 2019-08-22 stsp free(id_str);
9384 01073a5d 2019-08-22 stsp }
9385 01073a5d 2019-08-22 stsp
9386 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9387 01073a5d 2019-08-22 stsp return err;
9388 01073a5d 2019-08-22 stsp }
9389 01073a5d 2019-08-22 stsp
9390 01073a5d 2019-08-22 stsp static const struct got_error *
9391 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9392 01073a5d 2019-08-22 stsp {
9393 01073a5d 2019-08-22 stsp const struct got_error *err;
9394 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9395 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9396 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9397 01073a5d 2019-08-22 stsp char *id_str = NULL;
9398 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9399 01073a5d 2019-08-22 stsp
9400 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9401 01073a5d 2019-08-22 stsp if (err)
9402 01073a5d 2019-08-22 stsp return err;
9403 01073a5d 2019-08-22 stsp
9404 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9405 01073a5d 2019-08-22 stsp if (err)
9406 01073a5d 2019-08-22 stsp goto done;
9407 01073a5d 2019-08-22 stsp
9408 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9409 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9410 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9411 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9412 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9413 01073a5d 2019-08-22 stsp char *pid_str;
9414 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9415 01073a5d 2019-08-22 stsp if (err)
9416 01073a5d 2019-08-22 stsp goto done;
9417 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9418 01073a5d 2019-08-22 stsp free(pid_str);
9419 01073a5d 2019-08-22 stsp }
9420 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9421 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9422 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
9423 01073a5d 2019-08-22 stsp
9424 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9425 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9426 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
9427 01073a5d 2019-08-22 stsp
9428 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9429 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9430 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9431 01073a5d 2019-08-22 stsp done:
9432 01073a5d 2019-08-22 stsp free(id_str);
9433 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9434 01073a5d 2019-08-22 stsp return err;
9435 01073a5d 2019-08-22 stsp }
9436 01073a5d 2019-08-22 stsp
9437 01073a5d 2019-08-22 stsp static const struct got_error *
9438 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9439 01073a5d 2019-08-22 stsp {
9440 01073a5d 2019-08-22 stsp const struct got_error *err;
9441 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9442 01073a5d 2019-08-22 stsp char *id_str = NULL;
9443 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9444 01073a5d 2019-08-22 stsp
9445 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9446 01073a5d 2019-08-22 stsp if (err)
9447 01073a5d 2019-08-22 stsp return err;
9448 01073a5d 2019-08-22 stsp
9449 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9450 01073a5d 2019-08-22 stsp if (err)
9451 01073a5d 2019-08-22 stsp goto done;
9452 01073a5d 2019-08-22 stsp
9453 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9454 e15d5241 2019-08-22 stsp
9455 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9456 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9457 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9458 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9459 01073a5d 2019-08-22 stsp break;
9460 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9461 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9462 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9463 01073a5d 2019-08-22 stsp break;
9464 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9465 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9466 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9467 01073a5d 2019-08-22 stsp break;
9468 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9469 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9470 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9471 01073a5d 2019-08-22 stsp break;
9472 01073a5d 2019-08-22 stsp default:
9473 01073a5d 2019-08-22 stsp break;
9474 01073a5d 2019-08-22 stsp }
9475 01073a5d 2019-08-22 stsp
9476 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9477 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9478 e15d5241 2019-08-22 stsp
9479 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9480 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9481 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
9482 01073a5d 2019-08-22 stsp
9483 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9484 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9485 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9486 01073a5d 2019-08-22 stsp done:
9487 01073a5d 2019-08-22 stsp free(id_str);
9488 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9489 01073a5d 2019-08-22 stsp return err;
9490 01073a5d 2019-08-22 stsp }
9491 01073a5d 2019-08-22 stsp
9492 01073a5d 2019-08-22 stsp static const struct got_error *
9493 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9494 01073a5d 2019-08-22 stsp {
9495 01073a5d 2019-08-22 stsp const struct got_error *error;
9496 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9497 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9498 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9499 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9500 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9501 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9502 01073a5d 2019-08-22 stsp
9503 01073a5d 2019-08-22 stsp #ifndef PROFILE
9504 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9505 01073a5d 2019-08-22 stsp NULL) == -1)
9506 01073a5d 2019-08-22 stsp err(1, "pledge");
9507 01073a5d 2019-08-22 stsp #endif
9508 01073a5d 2019-08-22 stsp
9509 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9510 01073a5d 2019-08-22 stsp switch (ch) {
9511 896e9b6f 2019-08-26 stsp case 'c':
9512 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9513 896e9b6f 2019-08-26 stsp break;
9514 01073a5d 2019-08-22 stsp case 'r':
9515 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9516 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9517 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9518 9ba1d308 2019-10-21 stsp optarg);
9519 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9520 01073a5d 2019-08-22 stsp break;
9521 896e9b6f 2019-08-26 stsp case 'P':
9522 896e9b6f 2019-08-26 stsp force_path = 1;
9523 896e9b6f 2019-08-26 stsp break;
9524 01073a5d 2019-08-22 stsp default:
9525 01073a5d 2019-08-22 stsp usage_cat();
9526 01073a5d 2019-08-22 stsp /* NOTREACHED */
9527 01073a5d 2019-08-22 stsp }
9528 01073a5d 2019-08-22 stsp }
9529 01073a5d 2019-08-22 stsp
9530 01073a5d 2019-08-22 stsp argc -= optind;
9531 01073a5d 2019-08-22 stsp argv += optind;
9532 01073a5d 2019-08-22 stsp
9533 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9534 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9535 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9536 01073a5d 2019-08-22 stsp goto done;
9537 01073a5d 2019-08-22 stsp }
9538 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9539 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9540 01073a5d 2019-08-22 stsp goto done;
9541 01073a5d 2019-08-22 stsp if (worktree) {
9542 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9543 01073a5d 2019-08-22 stsp repo_path = strdup(
9544 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9545 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9546 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9547 01073a5d 2019-08-22 stsp goto done;
9548 01073a5d 2019-08-22 stsp }
9549 01073a5d 2019-08-22 stsp }
9550 01073a5d 2019-08-22 stsp }
9551 01073a5d 2019-08-22 stsp
9552 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9553 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9554 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9555 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9556 01073a5d 2019-08-22 stsp }
9557 01073a5d 2019-08-22 stsp
9558 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9559 01073a5d 2019-08-22 stsp free(repo_path);
9560 01073a5d 2019-08-22 stsp if (error != NULL)
9561 01073a5d 2019-08-22 stsp goto done;
9562 01073a5d 2019-08-22 stsp
9563 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9564 896e9b6f 2019-08-26 stsp if (error)
9565 896e9b6f 2019-08-26 stsp goto done;
9566 896e9b6f 2019-08-26 stsp
9567 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9568 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9569 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9570 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9571 01073a5d 2019-08-22 stsp if (error)
9572 01073a5d 2019-08-22 stsp goto done;
9573 01073a5d 2019-08-22 stsp
9574 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9575 896e9b6f 2019-08-26 stsp if (force_path) {
9576 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9577 896e9b6f 2019-08-26 stsp argv[i]);
9578 896e9b6f 2019-08-26 stsp if (error)
9579 896e9b6f 2019-08-26 stsp break;
9580 896e9b6f 2019-08-26 stsp } else {
9581 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9582 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9583 896e9b6f 2019-08-26 stsp if (error) {
9584 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9585 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9586 896e9b6f 2019-08-26 stsp break;
9587 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9588 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9589 896e9b6f 2019-08-26 stsp if (error)
9590 896e9b6f 2019-08-26 stsp break;
9591 896e9b6f 2019-08-26 stsp }
9592 896e9b6f 2019-08-26 stsp }
9593 01073a5d 2019-08-22 stsp
9594 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9595 01073a5d 2019-08-22 stsp if (error)
9596 01073a5d 2019-08-22 stsp break;
9597 01073a5d 2019-08-22 stsp
9598 01073a5d 2019-08-22 stsp switch (obj_type) {
9599 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9600 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9601 01073a5d 2019-08-22 stsp break;
9602 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9603 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9604 01073a5d 2019-08-22 stsp break;
9605 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9606 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9607 01073a5d 2019-08-22 stsp break;
9608 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9609 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9610 01073a5d 2019-08-22 stsp break;
9611 01073a5d 2019-08-22 stsp default:
9612 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9613 01073a5d 2019-08-22 stsp break;
9614 01073a5d 2019-08-22 stsp }
9615 01073a5d 2019-08-22 stsp if (error)
9616 01073a5d 2019-08-22 stsp break;
9617 01073a5d 2019-08-22 stsp free(label);
9618 01073a5d 2019-08-22 stsp label = NULL;
9619 01073a5d 2019-08-22 stsp free(id);
9620 01073a5d 2019-08-22 stsp id = NULL;
9621 01073a5d 2019-08-22 stsp }
9622 01073a5d 2019-08-22 stsp done:
9623 01073a5d 2019-08-22 stsp free(label);
9624 01073a5d 2019-08-22 stsp free(id);
9625 896e9b6f 2019-08-26 stsp free(commit_id);
9626 01073a5d 2019-08-22 stsp if (worktree)
9627 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9628 01073a5d 2019-08-22 stsp if (repo) {
9629 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9630 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9631 01073a5d 2019-08-22 stsp if (error == NULL)
9632 01073a5d 2019-08-22 stsp error = repo_error;
9633 b2118c49 2020-07-28 stsp }
9634 b2118c49 2020-07-28 stsp return error;
9635 b2118c49 2020-07-28 stsp }
9636 b2118c49 2020-07-28 stsp
9637 b2118c49 2020-07-28 stsp __dead static void
9638 b2118c49 2020-07-28 stsp usage_info(void)
9639 b2118c49 2020-07-28 stsp {
9640 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9641 b2118c49 2020-07-28 stsp getprogname());
9642 b2118c49 2020-07-28 stsp exit(1);
9643 b2118c49 2020-07-28 stsp }
9644 b2118c49 2020-07-28 stsp
9645 b2118c49 2020-07-28 stsp static const struct got_error *
9646 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9647 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9648 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9649 b2118c49 2020-07-28 stsp {
9650 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9651 b2118c49 2020-07-28 stsp char *id_str = NULL;
9652 b2118c49 2020-07-28 stsp char datebuf[128];
9653 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9654 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9655 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9656 b2118c49 2020-07-28 stsp
9657 b2118c49 2020-07-28 stsp /*
9658 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9659 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9660 b2118c49 2020-07-28 stsp */
9661 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9662 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9663 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9664 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9665 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9666 b2118c49 2020-07-28 stsp }
9667 b2118c49 2020-07-28 stsp
9668 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9669 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9670 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9671 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9672 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9673 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9674 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9675 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9676 b2118c49 2020-07-28 stsp else
9677 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9678 b2118c49 2020-07-28 stsp
9679 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9680 b2118c49 2020-07-28 stsp if (tm == NULL)
9681 b2118c49 2020-07-28 stsp return NULL;
9682 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9683 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9684 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9685 b2118c49 2020-07-28 stsp
9686 b2118c49 2020-07-28 stsp if (blob_id) {
9687 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9688 b2118c49 2020-07-28 stsp if (err)
9689 b2118c49 2020-07-28 stsp return err;
9690 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9691 b2118c49 2020-07-28 stsp free(id_str);
9692 b2118c49 2020-07-28 stsp }
9693 b2118c49 2020-07-28 stsp
9694 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9695 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9696 b2118c49 2020-07-28 stsp if (err)
9697 b2118c49 2020-07-28 stsp return err;
9698 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9699 b2118c49 2020-07-28 stsp free(id_str);
9700 b2118c49 2020-07-28 stsp }
9701 b2118c49 2020-07-28 stsp
9702 b2118c49 2020-07-28 stsp if (commit_id) {
9703 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9704 b2118c49 2020-07-28 stsp if (err)
9705 b2118c49 2020-07-28 stsp return err;
9706 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9707 b2118c49 2020-07-28 stsp free(id_str);
9708 b2118c49 2020-07-28 stsp }
9709 b2118c49 2020-07-28 stsp
9710 b2118c49 2020-07-28 stsp return NULL;
9711 b2118c49 2020-07-28 stsp }
9712 b2118c49 2020-07-28 stsp
9713 b2118c49 2020-07-28 stsp static const struct got_error *
9714 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9715 b2118c49 2020-07-28 stsp {
9716 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9717 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9718 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9719 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9720 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9721 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9722 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9723 b2118c49 2020-07-28 stsp
9724 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9725 b2118c49 2020-07-28 stsp
9726 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9727 b2118c49 2020-07-28 stsp switch (ch) {
9728 b2118c49 2020-07-28 stsp default:
9729 b2118c49 2020-07-28 stsp usage_info();
9730 b2118c49 2020-07-28 stsp /* NOTREACHED */
9731 b2118c49 2020-07-28 stsp }
9732 01073a5d 2019-08-22 stsp }
9733 b2118c49 2020-07-28 stsp
9734 b2118c49 2020-07-28 stsp argc -= optind;
9735 b2118c49 2020-07-28 stsp argv += optind;
9736 b2118c49 2020-07-28 stsp
9737 b2118c49 2020-07-28 stsp #ifndef PROFILE
9738 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9739 b2118c49 2020-07-28 stsp NULL) == -1)
9740 b2118c49 2020-07-28 stsp err(1, "pledge");
9741 b2118c49 2020-07-28 stsp #endif
9742 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9743 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9744 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9745 b2118c49 2020-07-28 stsp goto done;
9746 b2118c49 2020-07-28 stsp }
9747 b2118c49 2020-07-28 stsp
9748 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9749 b2118c49 2020-07-28 stsp if (error) {
9750 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9751 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9752 b2118c49 2020-07-28 stsp goto done;
9753 b2118c49 2020-07-28 stsp }
9754 b2118c49 2020-07-28 stsp
9755 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9756 b2118c49 2020-07-28 stsp if (error)
9757 b2118c49 2020-07-28 stsp goto done;
9758 b2118c49 2020-07-28 stsp
9759 b2118c49 2020-07-28 stsp if (argc >= 1) {
9760 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9761 b2118c49 2020-07-28 stsp worktree);
9762 b2118c49 2020-07-28 stsp if (error)
9763 b2118c49 2020-07-28 stsp goto done;
9764 b2118c49 2020-07-28 stsp show_files = 1;
9765 b2118c49 2020-07-28 stsp }
9766 b2118c49 2020-07-28 stsp
9767 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9768 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9769 b2118c49 2020-07-28 stsp if (error)
9770 b2118c49 2020-07-28 stsp goto done;
9771 b2118c49 2020-07-28 stsp
9772 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9773 b2118c49 2020-07-28 stsp if (error)
9774 b2118c49 2020-07-28 stsp goto done;
9775 b2118c49 2020-07-28 stsp
9776 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9777 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9778 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9779 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9780 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9781 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9782 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9783 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9784 b2118c49 2020-07-28 stsp
9785 b2118c49 2020-07-28 stsp if (show_files) {
9786 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9787 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9788 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9789 b2118c49 2020-07-28 stsp continue;
9790 b2118c49 2020-07-28 stsp /*
9791 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9792 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9793 b2118c49 2020-07-28 stsp */
9794 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9795 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9796 b2118c49 2020-07-28 stsp }
9797 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9798 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9799 b2118c49 2020-07-28 stsp if (error)
9800 b2118c49 2020-07-28 stsp goto done;
9801 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9802 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9803 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9804 b2118c49 2020-07-28 stsp break;
9805 b2118c49 2020-07-28 stsp }
9806 b2118c49 2020-07-28 stsp }
9807 b2118c49 2020-07-28 stsp }
9808 b2118c49 2020-07-28 stsp done:
9809 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9810 b2118c49 2020-07-28 stsp free((char *)pe->path);
9811 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9812 b2118c49 2020-07-28 stsp free(cwd);
9813 b2118c49 2020-07-28 stsp free(id_str);
9814 b2118c49 2020-07-28 stsp free(uuidstr);
9815 0ebf8283 2019-07-24 stsp return error;
9816 0ebf8283 2019-07-24 stsp }