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 892ac3b6 2020-03-18 stsp };
863 93658fb9 2020-03-18 stsp
864 93658fb9 2020-03-18 stsp static const struct got_error *
865 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
866 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
867 531c3985 2020-03-18 stsp {
868 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
869 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
870 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
871 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
872 b2409d58 2020-03-18 stsp
873 68999b92 2020-03-18 stsp if (a->verbosity < 0)
874 68999b92 2020-03-18 stsp return NULL;
875 68999b92 2020-03-18 stsp
876 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
877 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
878 892ac3b6 2020-03-18 stsp fflush(stdout);
879 12d1281e 2020-03-19 stsp return NULL;
880 b2409d58 2020-03-18 stsp }
881 b2409d58 2020-03-18 stsp
882 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
883 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
884 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
885 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
886 892ac3b6 2020-03-18 stsp print_size = 1;
887 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
888 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
889 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
890 892ac3b6 2020-03-18 stsp }
891 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
892 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
893 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
894 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
895 892ac3b6 2020-03-18 stsp print_indexed = 1;
896 892ac3b6 2020-03-18 stsp print_size = 1;
897 892ac3b6 2020-03-18 stsp }
898 61cc1a7a 2020-03-18 stsp }
899 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
900 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
901 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
902 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
903 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
904 892ac3b6 2020-03-18 stsp print_resolved = 1;
905 892ac3b6 2020-03-18 stsp print_indexed = 1;
906 892ac3b6 2020-03-18 stsp print_size = 1;
907 892ac3b6 2020-03-18 stsp }
908 61cc1a7a 2020-03-18 stsp }
909 3168e5da 2020-09-10 stsp
910 d2cdc636 2020-03-18 stsp }
911 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
912 892ac3b6 2020-03-18 stsp printf("\r");
913 892ac3b6 2020-03-18 stsp if (print_size)
914 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
915 d715f13e 2020-03-19 stsp if (print_indexed)
916 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
917 d715f13e 2020-03-19 stsp if (print_resolved)
918 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
919 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
920 892ac3b6 2020-03-18 stsp fflush(stdout);
921 892ac3b6 2020-03-18 stsp
922 531c3985 2020-03-18 stsp return NULL;
923 531c3985 2020-03-18 stsp }
924 531c3985 2020-03-18 stsp
925 531c3985 2020-03-18 stsp static const struct got_error *
926 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
927 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
928 4ba14133 2020-03-20 stsp {
929 4ba14133 2020-03-20 stsp const struct got_error *err;
930 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
931 4ba14133 2020-03-20 stsp
932 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
933 4ba14133 2020-03-20 stsp if (err)
934 4ba14133 2020-03-20 stsp return err;
935 4ba14133 2020-03-20 stsp
936 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
937 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
938 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
939 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
940 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
941 6338a6a1 2020-03-21 stsp }
942 4ba14133 2020-03-20 stsp return err;
943 4ba14133 2020-03-20 stsp }
944 4ba14133 2020-03-20 stsp
945 4ba14133 2020-03-20 stsp static const struct got_error *
946 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
947 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
948 41b0de12 2020-03-21 stsp {
949 41b0de12 2020-03-21 stsp const struct got_error *err;
950 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
951 41b0de12 2020-03-21 stsp
952 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
953 41b0de12 2020-03-21 stsp const char *refname = pe->path;
954 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
955 41b0de12 2020-03-21 stsp
956 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
957 41b0de12 2020-03-21 stsp }
958 41b0de12 2020-03-21 stsp
959 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
960 41b0de12 2020-03-21 stsp const char *refname = pe->path;
961 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
962 41b0de12 2020-03-21 stsp char *id_str;
963 41b0de12 2020-03-21 stsp
964 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
965 41b0de12 2020-03-21 stsp if (err)
966 41b0de12 2020-03-21 stsp return err;
967 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
968 41b0de12 2020-03-21 stsp free(id_str);
969 41b0de12 2020-03-21 stsp }
970 41b0de12 2020-03-21 stsp
971 41b0de12 2020-03-21 stsp return NULL;
972 6338a6a1 2020-03-21 stsp }
973 6338a6a1 2020-03-21 stsp
974 6338a6a1 2020-03-21 stsp static const struct got_error *
975 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
976 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
977 6338a6a1 2020-03-21 stsp {
978 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
979 6338a6a1 2020-03-21 stsp struct got_reference *ref;
980 6338a6a1 2020-03-21 stsp char *id_str;
981 6338a6a1 2020-03-21 stsp
982 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
983 6338a6a1 2020-03-21 stsp if (err)
984 6338a6a1 2020-03-21 stsp return err;
985 6338a6a1 2020-03-21 stsp
986 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
987 6338a6a1 2020-03-21 stsp if (err)
988 6338a6a1 2020-03-21 stsp goto done;
989 6338a6a1 2020-03-21 stsp
990 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
991 6338a6a1 2020-03-21 stsp got_ref_close(ref);
992 6338a6a1 2020-03-21 stsp
993 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
994 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
995 6338a6a1 2020-03-21 stsp done:
996 6338a6a1 2020-03-21 stsp free(id_str);
997 6338a6a1 2020-03-21 stsp return err;
998 0e4002ca 2020-03-21 stsp }
999 0e4002ca 2020-03-21 stsp
1000 0e4002ca 2020-03-21 stsp static int
1001 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1002 0e4002ca 2020-03-21 stsp {
1003 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1004 0e4002ca 2020-03-21 stsp return 0;
1005 0e4002ca 2020-03-21 stsp refname += 5;
1006 0e4002ca 2020-03-21 stsp
1007 0e4002ca 2020-03-21 stsp /*
1008 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1009 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1010 0e4002ca 2020-03-21 stsp */
1011 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1012 0e4002ca 2020-03-21 stsp return 0;
1013 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1014 0e4002ca 2020-03-21 stsp return 0;
1015 0e4002ca 2020-03-21 stsp
1016 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1017 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1018 0e4002ca 2020-03-21 stsp
1019 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1020 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1021 0e4002ca 2020-03-21 stsp return 1;
1022 0e4002ca 2020-03-21 stsp
1023 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1024 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1025 41b0de12 2020-03-21 stsp }
1026 41b0de12 2020-03-21 stsp
1027 0e4002ca 2020-03-21 stsp static int
1028 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1029 0e4002ca 2020-03-21 stsp {
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1031 0e4002ca 2020-03-21 stsp
1032 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1033 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1034 0e4002ca 2020-03-21 stsp return 1;
1035 0e4002ca 2020-03-21 stsp }
1036 0e4002ca 2020-03-21 stsp
1037 0e4002ca 2020-03-21 stsp return 0;
1038 0e4002ca 2020-03-21 stsp }
1039 0e4002ca 2020-03-21 stsp
1040 41b0de12 2020-03-21 stsp static const struct got_error *
1041 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1042 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1043 0e4002ca 2020-03-21 stsp {
1044 0e4002ca 2020-03-21 stsp const struct got_error *err;
1045 0e4002ca 2020-03-21 stsp char *remote_refname;
1046 0e4002ca 2020-03-21 stsp
1047 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1048 0e4002ca 2020-03-21 stsp refname += 5;
1049 0e4002ca 2020-03-21 stsp
1050 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1051 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1052 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1053 0e4002ca 2020-03-21 stsp
1054 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1055 0e4002ca 2020-03-21 stsp free(remote_refname);
1056 0e4002ca 2020-03-21 stsp return err;
1057 0e4002ca 2020-03-21 stsp }
1058 0e4002ca 2020-03-21 stsp
1059 0e4002ca 2020-03-21 stsp static const struct got_error *
1060 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1061 93658fb9 2020-03-18 stsp {
1062 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1063 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1064 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1065 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1066 bb64b798 2020-03-18 stsp const char *repo_path;
1067 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1068 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1069 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1070 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1071 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1072 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1073 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1074 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1075 257add31 2020-09-09 stsp char *gitconfig_path = NULL, *gotconfig_path = NULL;
1076 257add31 2020-09-09 stsp char *gitconfig = NULL, *gotconfig = NULL;
1077 257add31 2020-09-09 stsp FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
1078 b46f3e71 2020-03-18 stsp ssize_t n;
1079 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1080 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1081 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1082 93658fb9 2020-03-18 stsp
1083 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1084 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1085 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1086 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1087 d9b4d0c0 2020-03-18 stsp
1088 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1089 93658fb9 2020-03-18 stsp switch (ch) {
1090 659e7fbd 2020-03-20 stsp case 'a':
1091 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1092 4ba14133 2020-03-20 stsp break;
1093 4ba14133 2020-03-20 stsp case 'b':
1094 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1095 4ba14133 2020-03-20 stsp optarg, NULL);
1096 4ba14133 2020-03-20 stsp if (error)
1097 4ba14133 2020-03-20 stsp return error;
1098 659e7fbd 2020-03-20 stsp break;
1099 41b0de12 2020-03-21 stsp case 'l':
1100 41b0de12 2020-03-21 stsp list_refs_only = 1;
1101 41b0de12 2020-03-21 stsp break;
1102 469dd726 2020-03-20 stsp case 'm':
1103 469dd726 2020-03-20 stsp mirror_references = 1;
1104 469dd726 2020-03-20 stsp break;
1105 68999b92 2020-03-18 stsp case 'v':
1106 68999b92 2020-03-18 stsp if (verbosity < 0)
1107 68999b92 2020-03-18 stsp verbosity = 0;
1108 68999b92 2020-03-18 stsp else if (verbosity < 3)
1109 68999b92 2020-03-18 stsp verbosity++;
1110 68999b92 2020-03-18 stsp break;
1111 68999b92 2020-03-18 stsp case 'q':
1112 68999b92 2020-03-18 stsp verbosity = -1;
1113 68999b92 2020-03-18 stsp break;
1114 0e4002ca 2020-03-21 stsp case 'R':
1115 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1116 0e4002ca 2020-03-21 stsp optarg, NULL);
1117 0e4002ca 2020-03-21 stsp if (error)
1118 0e4002ca 2020-03-21 stsp return error;
1119 0e4002ca 2020-03-21 stsp break;
1120 93658fb9 2020-03-18 stsp default:
1121 93658fb9 2020-03-18 stsp usage_clone();
1122 93658fb9 2020-03-18 stsp break;
1123 93658fb9 2020-03-18 stsp }
1124 93658fb9 2020-03-18 stsp }
1125 93658fb9 2020-03-18 stsp argc -= optind;
1126 93658fb9 2020-03-18 stsp argv += optind;
1127 39c64a6a 2020-03-18 stsp
1128 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1129 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1130 41b0de12 2020-03-21 stsp if (list_refs_only) {
1131 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1132 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1133 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1134 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1135 41b0de12 2020-03-21 stsp if (mirror_references)
1136 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1137 41b0de12 2020-03-21 stsp if (verbosity == -1)
1138 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1139 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1140 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1141 41b0de12 2020-03-21 stsp }
1142 4ba14133 2020-03-20 stsp
1143 93658fb9 2020-03-18 stsp uri = argv[0];
1144 9df6f38b 2020-03-18 stsp
1145 9df6f38b 2020-03-18 stsp if (argc == 1)
1146 93658fb9 2020-03-18 stsp dirname = NULL;
1147 9df6f38b 2020-03-18 stsp else if (argc == 2)
1148 93658fb9 2020-03-18 stsp dirname = argv[1];
1149 93658fb9 2020-03-18 stsp else
1150 93658fb9 2020-03-18 stsp usage_clone();
1151 09838ffc 2020-03-18 stsp
1152 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1153 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1154 39c64a6a 2020-03-18 stsp if (error)
1155 09f63084 2020-03-20 stsp goto done;
1156 09f63084 2020-03-20 stsp
1157 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1158 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1159 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1160 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1161 09838ffc 2020-03-18 stsp goto done;
1162 09f63084 2020-03-20 stsp }
1163 09838ffc 2020-03-18 stsp
1164 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1165 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1166 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1167 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1168 39c64a6a 2020-03-18 stsp err(1, "pledge");
1169 b46f3e71 2020-03-18 stsp #endif
1170 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1171 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1172 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1173 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1174 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1175 39c64a6a 2020-03-18 stsp err(1, "pledge");
1176 b46f3e71 2020-03-18 stsp #endif
1177 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1178 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1179 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1180 39c64a6a 2020-03-18 stsp goto done;
1181 39c64a6a 2020-03-18 stsp } else {
1182 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1183 39c64a6a 2020-03-18 stsp goto done;
1184 39c64a6a 2020-03-18 stsp }
1185 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1186 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1187 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1188 bb64b798 2020-03-18 stsp goto done;
1189 bb64b798 2020-03-18 stsp }
1190 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1191 bb64b798 2020-03-18 stsp } else
1192 bb64b798 2020-03-18 stsp repo_path = dirname;
1193 bb64b798 2020-03-18 stsp
1194 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1195 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1196 41b0de12 2020-03-21 stsp if (error)
1197 41b0de12 2020-03-21 stsp goto done;
1198 bb64b798 2020-03-18 stsp
1199 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1200 41b0de12 2020-03-21 stsp if (error)
1201 41b0de12 2020-03-21 stsp goto done;
1202 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1203 41b0de12 2020-03-21 stsp if (error)
1204 41b0de12 2020-03-21 stsp goto done;
1205 41b0de12 2020-03-21 stsp }
1206 bb64b798 2020-03-18 stsp
1207 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1208 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1209 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1210 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1211 ee448f5f 2020-03-18 stsp goto done;
1212 ee448f5f 2020-03-18 stsp }
1213 ee448f5f 2020-03-18 stsp }
1214 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1215 ee448f5f 2020-03-18 stsp if (error)
1216 ee448f5f 2020-03-18 stsp goto done;
1217 f79e6490 2020-04-19 stsp
1218 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1219 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1220 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1221 ee448f5f 2020-03-18 stsp
1222 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1223 9c52365f 2020-03-21 stsp server_path, verbosity);
1224 39c64a6a 2020-03-18 stsp if (error)
1225 bb64b798 2020-03-18 stsp goto done;
1226 bb64b798 2020-03-18 stsp
1227 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1228 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1229 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1230 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1231 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1232 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1233 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1234 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1235 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1236 39c64a6a 2020-03-18 stsp if (error)
1237 d9b4d0c0 2020-03-18 stsp goto done;
1238 d9b4d0c0 2020-03-18 stsp
1239 41b0de12 2020-03-21 stsp if (list_refs_only) {
1240 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1241 41b0de12 2020-03-21 stsp goto done;
1242 41b0de12 2020-03-21 stsp }
1243 41b0de12 2020-03-21 stsp
1244 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1245 39c64a6a 2020-03-18 stsp if (error)
1246 d9b4d0c0 2020-03-18 stsp goto done;
1247 68999b92 2020-03-18 stsp if (verbosity >= 0)
1248 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1249 d9b4d0c0 2020-03-18 stsp free(id_str);
1250 d9b4d0c0 2020-03-18 stsp
1251 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1253 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1254 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1255 7ebc0570 2020-03-18 stsp char *remote_refname;
1256 0e4002ca 2020-03-21 stsp
1257 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1258 0e4002ca 2020-03-21 stsp !mirror_references) {
1259 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1260 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1261 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1262 0e4002ca 2020-03-21 stsp if (error)
1263 0e4002ca 2020-03-21 stsp goto done;
1264 0e4002ca 2020-03-21 stsp continue;
1265 0e4002ca 2020-03-21 stsp }
1266 668a20f6 2020-03-18 stsp
1267 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1268 39c64a6a 2020-03-18 stsp if (error)
1269 d9b4d0c0 2020-03-18 stsp goto done;
1270 d9b4d0c0 2020-03-18 stsp
1271 469dd726 2020-03-20 stsp if (mirror_references)
1272 469dd726 2020-03-20 stsp continue;
1273 469dd726 2020-03-20 stsp
1274 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1275 7ebc0570 2020-03-18 stsp continue;
1276 7ebc0570 2020-03-18 stsp
1277 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1278 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1279 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1280 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1281 7ebc0570 2020-03-18 stsp goto done;
1282 7ebc0570 2020-03-18 stsp }
1283 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1284 f298ae0f 2020-03-25 stsp free(remote_refname);
1285 39c64a6a 2020-03-18 stsp if (error)
1286 d9b4d0c0 2020-03-18 stsp goto done;
1287 d9b4d0c0 2020-03-18 stsp }
1288 d9b4d0c0 2020-03-18 stsp
1289 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1290 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1291 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1292 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1293 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1294 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1295 d9b4d0c0 2020-03-18 stsp
1296 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1297 d9b4d0c0 2020-03-18 stsp continue;
1298 d9b4d0c0 2020-03-18 stsp
1299 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1300 39c64a6a 2020-03-18 stsp if (error) {
1301 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1302 55330abe 2020-03-20 stsp error = NULL;
1303 d9b4d0c0 2020-03-18 stsp continue;
1304 55330abe 2020-03-20 stsp }
1305 d9b4d0c0 2020-03-18 stsp goto done;
1306 d9b4d0c0 2020-03-18 stsp }
1307 d9b4d0c0 2020-03-18 stsp
1308 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1309 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1310 f298ae0f 2020-03-25 stsp if (error)
1311 f298ae0f 2020-03-25 stsp goto done;
1312 f298ae0f 2020-03-25 stsp
1313 f298ae0f 2020-03-25 stsp if (mirror_references)
1314 f298ae0f 2020-03-25 stsp continue;
1315 f298ae0f 2020-03-25 stsp
1316 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1317 f298ae0f 2020-03-25 stsp continue;
1318 f298ae0f 2020-03-25 stsp
1319 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1320 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1321 f298ae0f 2020-03-25 stsp refname) == -1) {
1322 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1323 f298ae0f 2020-03-25 stsp goto done;
1324 f298ae0f 2020-03-25 stsp }
1325 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1326 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1327 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1328 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1329 f298ae0f 2020-03-25 stsp free(remote_refname);
1330 f298ae0f 2020-03-25 stsp goto done;
1331 f298ae0f 2020-03-25 stsp }
1332 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1333 f298ae0f 2020-03-25 stsp if (error) {
1334 f298ae0f 2020-03-25 stsp free(remote_refname);
1335 f298ae0f 2020-03-25 stsp free(remote_target);
1336 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1337 f298ae0f 2020-03-25 stsp error = NULL;
1338 f298ae0f 2020-03-25 stsp continue;
1339 f298ae0f 2020-03-25 stsp }
1340 f298ae0f 2020-03-25 stsp goto done;
1341 f298ae0f 2020-03-25 stsp }
1342 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1343 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1344 f298ae0f 2020-03-25 stsp free(remote_refname);
1345 f298ae0f 2020-03-25 stsp free(remote_target);
1346 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1347 39c64a6a 2020-03-18 stsp if (error)
1348 d9b4d0c0 2020-03-18 stsp goto done;
1349 4ba14133 2020-03-20 stsp }
1350 4ba14133 2020-03-20 stsp if (pe == NULL) {
1351 4ba14133 2020-03-20 stsp /*
1352 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1353 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1354 4ba14133 2020-03-20 stsp * which could be fetched instead.
1355 4ba14133 2020-03-20 stsp */
1356 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1357 4ba14133 2020-03-20 stsp const char *target = pe->path;
1358 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1359 d9b4d0c0 2020-03-18 stsp
1360 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1361 4ba14133 2020-03-20 stsp if (error) {
1362 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1363 4ba14133 2020-03-20 stsp error = NULL;
1364 4ba14133 2020-03-20 stsp continue;
1365 4ba14133 2020-03-20 stsp }
1366 4ba14133 2020-03-20 stsp goto done;
1367 4ba14133 2020-03-20 stsp }
1368 4ba14133 2020-03-20 stsp
1369 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1370 f298ae0f 2020-03-25 stsp verbosity, repo);
1371 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1372 4ba14133 2020-03-20 stsp if (error)
1373 4ba14133 2020-03-20 stsp goto done;
1374 4ba14133 2020-03-20 stsp break;
1375 4ba14133 2020-03-20 stsp }
1376 659e7fbd 2020-03-20 stsp }
1377 659e7fbd 2020-03-20 stsp
1378 257add31 2020-09-09 stsp /* Create got.conf(5). */
1379 257add31 2020-09-09 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1380 257add31 2020-09-09 stsp if (gotconfig_path == NULL) {
1381 257add31 2020-09-09 stsp error = got_error_from_errno("got_repo_get_path_gotconfig");
1382 257add31 2020-09-09 stsp goto done;
1383 257add31 2020-09-09 stsp }
1384 257add31 2020-09-09 stsp gotconfig_file = fopen(gotconfig_path, "a");
1385 257add31 2020-09-09 stsp if (gotconfig_file == NULL) {
1386 257add31 2020-09-09 stsp error = got_error_from_errno2("fopen", gotconfig_path);
1387 257add31 2020-09-09 stsp goto done;
1388 257add31 2020-09-09 stsp }
1389 257add31 2020-09-09 stsp got_path_strip_trailing_slashes(server_path);
1390 257add31 2020-09-09 stsp if (asprintf(&gotconfig,
1391 257add31 2020-09-09 stsp "remote \"%s\" {\n"
1392 257add31 2020-09-09 stsp "\tserver %s\n"
1393 257add31 2020-09-09 stsp "\tprotocol %s\n"
1394 257add31 2020-09-09 stsp "%s%s%s"
1395 257add31 2020-09-09 stsp "\trepository \"%s\"\n"
1396 257add31 2020-09-09 stsp "%s"
1397 257add31 2020-09-09 stsp "}\n",
1398 257add31 2020-09-09 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1399 257add31 2020-09-09 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1400 257add31 2020-09-09 stsp server_path,
1401 257add31 2020-09-09 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1402 257add31 2020-09-09 stsp error = got_error_from_errno("asprintf");
1403 257add31 2020-09-09 stsp goto done;
1404 257add31 2020-09-09 stsp }
1405 257add31 2020-09-09 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1406 257add31 2020-09-09 stsp if (n != strlen(gotconfig)) {
1407 257add31 2020-09-09 stsp error = got_ferror(gotconfig_file, GOT_ERR_IO);
1408 257add31 2020-09-09 stsp goto done;
1409 257add31 2020-09-09 stsp }
1410 257add31 2020-09-09 stsp
1411 257add31 2020-09-09 stsp /* Create a config file Git can understand. */
1412 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1413 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1414 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1415 659e7fbd 2020-03-20 stsp goto done;
1416 659e7fbd 2020-03-20 stsp }
1417 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1418 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1419 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1420 659e7fbd 2020-03-20 stsp goto done;
1421 b46f3e71 2020-03-18 stsp }
1422 659e7fbd 2020-03-20 stsp if (mirror_references) {
1423 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1424 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1425 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1426 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1427 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1428 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1429 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1430 659e7fbd 2020-03-20 stsp goto done;
1431 659e7fbd 2020-03-20 stsp }
1432 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1433 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1434 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1435 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1436 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1437 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1438 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1439 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1440 659e7fbd 2020-03-20 stsp goto done;
1441 659e7fbd 2020-03-20 stsp }
1442 659e7fbd 2020-03-20 stsp } else {
1443 659e7fbd 2020-03-20 stsp const char *branchname;
1444 d715f13e 2020-03-19 stsp
1445 659e7fbd 2020-03-20 stsp /*
1446 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1447 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1448 659e7fbd 2020-03-20 stsp */
1449 659e7fbd 2020-03-20 stsp if (head_symref) {
1450 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1451 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1452 659e7fbd 2020-03-20 stsp branchname += 11;
1453 659e7fbd 2020-03-20 stsp } else
1454 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1455 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1456 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1457 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1458 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1459 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1460 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1461 659e7fbd 2020-03-20 stsp branchname) == -1) {
1462 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1463 659e7fbd 2020-03-20 stsp goto done;
1464 659e7fbd 2020-03-20 stsp }
1465 659e7fbd 2020-03-20 stsp }
1466 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1467 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1468 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1469 659e7fbd 2020-03-20 stsp goto done;
1470 659e7fbd 2020-03-20 stsp }
1471 659e7fbd 2020-03-20 stsp
1472 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1473 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1474 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1475 09838ffc 2020-03-18 stsp done:
1476 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1477 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1478 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1479 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1480 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1481 9c52365f 2020-03-21 stsp }
1482 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1483 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1484 257add31 2020-09-09 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1485 257add31 2020-09-09 stsp error = got_error_from_errno("fclose");
1486 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1487 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1488 bb64b798 2020-03-18 stsp if (repo)
1489 bb64b798 2020-03-18 stsp got_repo_close(repo);
1490 659e7fbd 2020-03-20 stsp if (head_symref)
1491 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1492 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1493 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1494 d9b4d0c0 2020-03-18 stsp free(pe->data);
1495 d9b4d0c0 2020-03-18 stsp }
1496 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1497 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1498 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1499 d9b4d0c0 2020-03-18 stsp free(pe->data);
1500 d9b4d0c0 2020-03-18 stsp }
1501 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1502 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1503 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1504 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1505 09838ffc 2020-03-18 stsp free(proto);
1506 09838ffc 2020-03-18 stsp free(host);
1507 09838ffc 2020-03-18 stsp free(port);
1508 09838ffc 2020-03-18 stsp free(server_path);
1509 09838ffc 2020-03-18 stsp free(repo_name);
1510 bb64b798 2020-03-18 stsp free(default_destdir);
1511 257add31 2020-09-09 stsp free(gotconfig);
1512 257add31 2020-09-09 stsp free(gitconfig);
1513 257add31 2020-09-09 stsp free(gotconfig_path);
1514 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1515 b46f3e71 2020-03-18 stsp free(git_url);
1516 39c64a6a 2020-03-18 stsp return error;
1517 7848a0e1 2020-03-19 stsp }
1518 7848a0e1 2020-03-19 stsp
1519 7848a0e1 2020-03-19 stsp static const struct got_error *
1520 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1521 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1522 7848a0e1 2020-03-19 stsp {
1523 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1524 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1525 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1526 7848a0e1 2020-03-19 stsp
1527 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1528 7848a0e1 2020-03-19 stsp if (err)
1529 7848a0e1 2020-03-19 stsp goto done;
1530 7848a0e1 2020-03-19 stsp
1531 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1532 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1533 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1534 88609724 2020-03-21 stsp if (err)
1535 88609724 2020-03-21 stsp goto done;
1536 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1537 88609724 2020-03-21 stsp goto done;
1538 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1539 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1540 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1541 db6d8ad8 2020-03-21 stsp }
1542 db6d8ad8 2020-03-21 stsp goto done;
1543 db6d8ad8 2020-03-21 stsp }
1544 db6d8ad8 2020-03-21 stsp
1545 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1546 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1547 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1548 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1549 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1550 688f11b3 2020-03-21 stsp }
1551 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1552 7848a0e1 2020-03-19 stsp if (err)
1553 7848a0e1 2020-03-19 stsp goto done;
1554 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1555 e8a967e0 2020-03-21 stsp if (err)
1556 e8a967e0 2020-03-21 stsp goto done;
1557 7848a0e1 2020-03-19 stsp } else {
1558 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1559 7848a0e1 2020-03-19 stsp if (err)
1560 7848a0e1 2020-03-19 stsp goto done;
1561 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1562 6338a6a1 2020-03-21 stsp goto done;
1563 6338a6a1 2020-03-21 stsp
1564 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1565 6338a6a1 2020-03-21 stsp if (err)
1566 6338a6a1 2020-03-21 stsp goto done;
1567 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1568 6338a6a1 2020-03-21 stsp if (err)
1569 6338a6a1 2020-03-21 stsp goto done;
1570 7848a0e1 2020-03-19 stsp }
1571 6338a6a1 2020-03-21 stsp
1572 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1573 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1574 6338a6a1 2020-03-21 stsp new_id_str);
1575 7848a0e1 2020-03-19 stsp done:
1576 7848a0e1 2020-03-19 stsp free(old_id);
1577 7848a0e1 2020-03-19 stsp free(new_id_str);
1578 7848a0e1 2020-03-19 stsp return err;
1579 2ab43947 2020-03-18 stsp }
1580 f1bcca34 2020-03-25 stsp
1581 f1bcca34 2020-03-25 stsp static const struct got_error *
1582 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1583 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1584 f1bcca34 2020-03-25 stsp {
1585 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1586 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1587 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1588 f1bcca34 2020-03-25 stsp
1589 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1590 bcf34b0e 2020-03-26 stsp if (err) {
1591 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1592 bcf34b0e 2020-03-26 stsp return err;
1593 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1594 bcf34b0e 2020-03-26 stsp if (err)
1595 bcf34b0e 2020-03-26 stsp goto done;
1596 2ab43947 2020-03-18 stsp
1597 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1598 bcf34b0e 2020-03-26 stsp if (err)
1599 bcf34b0e 2020-03-26 stsp goto done;
1600 f1bcca34 2020-03-25 stsp
1601 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1602 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1603 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1604 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1605 bcf34b0e 2020-03-26 stsp } else {
1606 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1607 f1bcca34 2020-03-25 stsp
1608 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1609 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1610 bcf34b0e 2020-03-26 stsp goto done;
1611 bcf34b0e 2020-03-26 stsp
1612 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1613 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1614 bcf34b0e 2020-03-26 stsp if (err)
1615 bcf34b0e 2020-03-26 stsp goto done;
1616 bcf34b0e 2020-03-26 stsp
1617 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1618 bcf34b0e 2020-03-26 stsp if (err)
1619 bcf34b0e 2020-03-26 stsp goto done;
1620 bcf34b0e 2020-03-26 stsp
1621 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1622 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1623 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1624 bcf34b0e 2020-03-26 stsp
1625 bcf34b0e 2020-03-26 stsp }
1626 f1bcca34 2020-03-25 stsp done:
1627 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1628 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1629 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1630 bcf34b0e 2020-03-26 stsp err = unlock_err;
1631 bcf34b0e 2020-03-26 stsp }
1632 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1633 f1bcca34 2020-03-25 stsp return err;
1634 f1bcca34 2020-03-25 stsp }
1635 f1bcca34 2020-03-25 stsp
1636 2ab43947 2020-03-18 stsp __dead static void
1637 7848a0e1 2020-03-19 stsp usage_fetch(void)
1638 7848a0e1 2020-03-19 stsp {
1639 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1640 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1641 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1642 13f12b09 2020-03-21 stsp getprogname());
1643 7848a0e1 2020-03-19 stsp exit(1);
1644 7848a0e1 2020-03-19 stsp }
1645 7848a0e1 2020-03-19 stsp
1646 7848a0e1 2020-03-19 stsp static const struct got_error *
1647 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1648 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1649 f21ec2f0 2020-03-21 stsp {
1650 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1651 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1652 3789fd73 2020-03-26 stsp char *id_str = NULL;
1653 3789fd73 2020-03-26 stsp
1654 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1655 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1656 3789fd73 2020-03-26 stsp if (err)
1657 3789fd73 2020-03-26 stsp return err;
1658 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1659 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1660 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1661 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1662 3789fd73 2020-03-26 stsp }
1663 3789fd73 2020-03-26 stsp } else {
1664 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1665 3789fd73 2020-03-26 stsp if (err)
1666 3789fd73 2020-03-26 stsp return err;
1667 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1668 3789fd73 2020-03-26 stsp if (err)
1669 3789fd73 2020-03-26 stsp goto done;
1670 3168e5da 2020-09-10 stsp
1671 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1672 3789fd73 2020-03-26 stsp if (err)
1673 3789fd73 2020-03-26 stsp goto done;
1674 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1675 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1676 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1677 3789fd73 2020-03-26 stsp }
1678 3789fd73 2020-03-26 stsp }
1679 3789fd73 2020-03-26 stsp done:
1680 3789fd73 2020-03-26 stsp free(id);
1681 3789fd73 2020-03-26 stsp free(id_str);
1682 3789fd73 2020-03-26 stsp return NULL;
1683 3789fd73 2020-03-26 stsp }
1684 3789fd73 2020-03-26 stsp
1685 3789fd73 2020-03-26 stsp static const struct got_error *
1686 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1687 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1688 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1689 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1690 3789fd73 2020-03-26 stsp {
1691 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1692 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1693 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1694 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1695 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1696 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1697 f21ec2f0 2020-03-21 stsp
1698 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1699 f21ec2f0 2020-03-21 stsp
1700 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1701 3789fd73 2020-03-26 stsp == -1)
1702 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1703 3789fd73 2020-03-26 stsp
1704 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1705 f21ec2f0 2020-03-21 stsp if (err)
1706 3789fd73 2020-03-26 stsp goto done;
1707 f21ec2f0 2020-03-21 stsp
1708 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1709 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1710 f21ec2f0 2020-03-21 stsp
1711 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1712 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1713 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1714 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1715 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1716 3789fd73 2020-03-26 stsp continue;
1717 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1718 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1719 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1720 3789fd73 2020-03-26 stsp goto done;
1721 3789fd73 2020-03-26 stsp }
1722 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1723 3789fd73 2020-03-26 stsp continue;
1724 3789fd73 2020-03-26 stsp }
1725 f21ec2f0 2020-03-21 stsp
1726 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1727 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1728 f21ec2f0 2020-03-21 stsp break;
1729 f21ec2f0 2020-03-21 stsp }
1730 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1731 f21ec2f0 2020-03-21 stsp continue;
1732 f21ec2f0 2020-03-21 stsp
1733 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1734 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1735 3789fd73 2020-03-26 stsp break;
1736 3789fd73 2020-03-26 stsp }
1737 3789fd73 2020-03-26 stsp if (pe != NULL)
1738 3789fd73 2020-03-26 stsp continue;
1739 f21ec2f0 2020-03-21 stsp
1740 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1741 f21ec2f0 2020-03-21 stsp if (err)
1742 f21ec2f0 2020-03-21 stsp break;
1743 3789fd73 2020-03-26 stsp
1744 3789fd73 2020-03-26 stsp if (local_refname) {
1745 3789fd73 2020-03-26 stsp struct got_reference *ref;
1746 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1747 3789fd73 2020-03-26 stsp if (err) {
1748 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1749 3789fd73 2020-03-26 stsp break;
1750 3789fd73 2020-03-26 stsp free(local_refname);
1751 3789fd73 2020-03-26 stsp local_refname = NULL;
1752 3789fd73 2020-03-26 stsp continue;
1753 3789fd73 2020-03-26 stsp }
1754 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1755 3789fd73 2020-03-26 stsp if (err)
1756 3789fd73 2020-03-26 stsp break;
1757 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1758 3789fd73 2020-03-26 stsp got_ref_close(ref);
1759 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1760 3789fd73 2020-03-26 stsp err = unlock_err;
1761 3789fd73 2020-03-26 stsp break;
1762 3789fd73 2020-03-26 stsp }
1763 3789fd73 2020-03-26 stsp
1764 3789fd73 2020-03-26 stsp free(local_refname);
1765 3789fd73 2020-03-26 stsp local_refname = NULL;
1766 6338a6a1 2020-03-21 stsp }
1767 f21ec2f0 2020-03-21 stsp }
1768 3789fd73 2020-03-26 stsp done:
1769 3789fd73 2020-03-26 stsp free(remote_namespace);
1770 3789fd73 2020-03-26 stsp free(local_refname);
1771 0e4002ca 2020-03-21 stsp return err;
1772 0e4002ca 2020-03-21 stsp }
1773 0e4002ca 2020-03-21 stsp
1774 0e4002ca 2020-03-21 stsp static const struct got_error *
1775 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1776 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1777 0e4002ca 2020-03-21 stsp {
1778 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1779 0e4002ca 2020-03-21 stsp char *remote_refname;
1780 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1781 0e4002ca 2020-03-21 stsp
1782 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1783 0e4002ca 2020-03-21 stsp refname += 5;
1784 0e4002ca 2020-03-21 stsp
1785 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1786 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1787 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1788 f21ec2f0 2020-03-21 stsp
1789 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1790 0e4002ca 2020-03-21 stsp if (err) {
1791 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1792 0e4002ca 2020-03-21 stsp goto done;
1793 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1794 0e4002ca 2020-03-21 stsp } else {
1795 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1796 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1797 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1798 9f142382 2020-03-21 stsp err = unlock_err;
1799 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1800 0e4002ca 2020-03-21 stsp }
1801 0e4002ca 2020-03-21 stsp done:
1802 0e4002ca 2020-03-21 stsp free(remote_refname);
1803 f21ec2f0 2020-03-21 stsp return err;
1804 f21ec2f0 2020-03-21 stsp }
1805 f21ec2f0 2020-03-21 stsp
1806 f21ec2f0 2020-03-21 stsp static const struct got_error *
1807 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1808 7848a0e1 2020-03-19 stsp {
1809 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1810 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1811 7848a0e1 2020-03-19 stsp const char *remote_name;
1812 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1813 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1814 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1815 7848a0e1 2020-03-19 stsp int nremotes;
1816 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1817 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1818 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1819 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1820 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1821 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1822 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1823 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1824 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1825 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1826 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1827 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1828 7848a0e1 2020-03-19 stsp
1829 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1830 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1831 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1832 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1833 7848a0e1 2020-03-19 stsp
1834 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1835 7848a0e1 2020-03-19 stsp switch (ch) {
1836 659e7fbd 2020-03-20 stsp case 'a':
1837 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1838 4ba14133 2020-03-20 stsp break;
1839 4ba14133 2020-03-20 stsp case 'b':
1840 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1841 4ba14133 2020-03-20 stsp optarg, NULL);
1842 4ba14133 2020-03-20 stsp if (error)
1843 4ba14133 2020-03-20 stsp return error;
1844 41b0de12 2020-03-21 stsp break;
1845 f21ec2f0 2020-03-21 stsp case 'd':
1846 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1847 f21ec2f0 2020-03-21 stsp break;
1848 41b0de12 2020-03-21 stsp case 'l':
1849 41b0de12 2020-03-21 stsp list_refs_only = 1;
1850 659e7fbd 2020-03-20 stsp break;
1851 7848a0e1 2020-03-19 stsp case 'r':
1852 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1853 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1854 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1855 7848a0e1 2020-03-19 stsp optarg);
1856 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1857 7848a0e1 2020-03-19 stsp break;
1858 db6d8ad8 2020-03-21 stsp case 't':
1859 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1860 db6d8ad8 2020-03-21 stsp break;
1861 7848a0e1 2020-03-19 stsp case 'v':
1862 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1863 7848a0e1 2020-03-19 stsp verbosity = 0;
1864 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1865 7848a0e1 2020-03-19 stsp verbosity++;
1866 7848a0e1 2020-03-19 stsp break;
1867 7848a0e1 2020-03-19 stsp case 'q':
1868 7848a0e1 2020-03-19 stsp verbosity = -1;
1869 7848a0e1 2020-03-19 stsp break;
1870 0e4002ca 2020-03-21 stsp case 'R':
1871 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1872 0e4002ca 2020-03-21 stsp optarg, NULL);
1873 0e4002ca 2020-03-21 stsp if (error)
1874 0e4002ca 2020-03-21 stsp return error;
1875 0e4002ca 2020-03-21 stsp break;
1876 7848a0e1 2020-03-19 stsp default:
1877 7848a0e1 2020-03-19 stsp usage_fetch();
1878 7848a0e1 2020-03-19 stsp break;
1879 7848a0e1 2020-03-19 stsp }
1880 7848a0e1 2020-03-19 stsp }
1881 7848a0e1 2020-03-19 stsp argc -= optind;
1882 7848a0e1 2020-03-19 stsp argv += optind;
1883 7848a0e1 2020-03-19 stsp
1884 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1885 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1886 41b0de12 2020-03-21 stsp if (list_refs_only) {
1887 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1888 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1889 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1890 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1891 f21ec2f0 2020-03-21 stsp if (delete_refs)
1892 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1893 41b0de12 2020-03-21 stsp if (verbosity == -1)
1894 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1895 41b0de12 2020-03-21 stsp }
1896 4ba14133 2020-03-20 stsp
1897 7848a0e1 2020-03-19 stsp if (argc == 0)
1898 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1899 7848a0e1 2020-03-19 stsp else if (argc == 1)
1900 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1901 7848a0e1 2020-03-19 stsp else
1902 7848a0e1 2020-03-19 stsp usage_fetch();
1903 7848a0e1 2020-03-19 stsp
1904 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1905 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1906 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1907 7848a0e1 2020-03-19 stsp goto done;
1908 7848a0e1 2020-03-19 stsp }
1909 7848a0e1 2020-03-19 stsp
1910 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1911 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1912 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1913 7848a0e1 2020-03-19 stsp goto done;
1914 7848a0e1 2020-03-19 stsp else
1915 7848a0e1 2020-03-19 stsp error = NULL;
1916 7848a0e1 2020-03-19 stsp if (worktree) {
1917 7848a0e1 2020-03-19 stsp repo_path =
1918 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1919 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1920 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1921 7848a0e1 2020-03-19 stsp if (error)
1922 7848a0e1 2020-03-19 stsp goto done;
1923 7848a0e1 2020-03-19 stsp } else {
1924 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1925 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1926 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1927 7848a0e1 2020-03-19 stsp goto done;
1928 7848a0e1 2020-03-19 stsp }
1929 7848a0e1 2020-03-19 stsp }
1930 7848a0e1 2020-03-19 stsp }
1931 7848a0e1 2020-03-19 stsp
1932 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1933 7848a0e1 2020-03-19 stsp if (error)
1934 7848a0e1 2020-03-19 stsp goto done;
1935 7848a0e1 2020-03-19 stsp
1936 50b0790e 2020-09-11 stsp if (worktree) {
1937 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
1938 50b0790e 2020-09-11 stsp if (worktree_conf) {
1939 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1940 50b0790e 2020-09-11 stsp worktree_conf);
1941 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1942 50b0790e 2020-09-11 stsp remote = &remotes[i];
1943 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1944 50b0790e 2020-09-11 stsp break;
1945 50b0790e 2020-09-11 stsp }
1946 50b0790e 2020-09-11 stsp }
1947 7848a0e1 2020-03-19 stsp }
1948 50b0790e 2020-09-11 stsp if (remote == NULL) {
1949 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
1950 50b0790e 2020-09-11 stsp if (repo_conf) {
1951 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1952 50b0790e 2020-09-11 stsp repo_conf);
1953 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1954 50b0790e 2020-09-11 stsp remote = &remotes[i];
1955 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1956 50b0790e 2020-09-11 stsp break;
1957 50b0790e 2020-09-11 stsp }
1958 50b0790e 2020-09-11 stsp }
1959 50b0790e 2020-09-11 stsp }
1960 50b0790e 2020-09-11 stsp if (remote == NULL) {
1961 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1962 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
1963 257add31 2020-09-09 stsp remote = &remotes[i];
1964 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
1965 257add31 2020-09-09 stsp break;
1966 257add31 2020-09-09 stsp }
1967 7848a0e1 2020-03-19 stsp }
1968 50b0790e 2020-09-11 stsp if (remote == NULL) {
1969 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1970 50b0790e 2020-09-11 stsp goto done;
1971 50b0790e 2020-09-11 stsp }
1972 7848a0e1 2020-03-19 stsp
1973 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1974 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1975 7848a0e1 2020-03-19 stsp if (error)
1976 7848a0e1 2020-03-19 stsp goto done;
1977 7848a0e1 2020-03-19 stsp
1978 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1979 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1980 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1981 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1982 7848a0e1 2020-03-19 stsp err(1, "pledge");
1983 7848a0e1 2020-03-19 stsp #endif
1984 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1985 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1986 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1987 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1988 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1989 7848a0e1 2020-03-19 stsp err(1, "pledge");
1990 7848a0e1 2020-03-19 stsp #endif
1991 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1992 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1993 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1994 7848a0e1 2020-03-19 stsp goto done;
1995 7848a0e1 2020-03-19 stsp } else {
1996 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1997 7848a0e1 2020-03-19 stsp goto done;
1998 7848a0e1 2020-03-19 stsp }
1999 7848a0e1 2020-03-19 stsp
2000 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2001 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2002 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2003 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2004 7848a0e1 2020-03-19 stsp goto done;
2005 7848a0e1 2020-03-19 stsp }
2006 7848a0e1 2020-03-19 stsp }
2007 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2008 7848a0e1 2020-03-19 stsp if (error)
2009 7848a0e1 2020-03-19 stsp goto done;
2010 f79e6490 2020-04-19 stsp
2011 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2012 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2013 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2014 7848a0e1 2020-03-19 stsp
2015 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2016 9c52365f 2020-03-21 stsp server_path, verbosity);
2017 7848a0e1 2020-03-19 stsp if (error)
2018 7848a0e1 2020-03-19 stsp goto done;
2019 7848a0e1 2020-03-19 stsp
2020 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2021 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2022 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2023 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2024 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2025 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2026 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2027 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2028 7848a0e1 2020-03-19 stsp if (error)
2029 7848a0e1 2020-03-19 stsp goto done;
2030 7848a0e1 2020-03-19 stsp
2031 41b0de12 2020-03-21 stsp if (list_refs_only) {
2032 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2033 41b0de12 2020-03-21 stsp goto done;
2034 41b0de12 2020-03-21 stsp }
2035 41b0de12 2020-03-21 stsp
2036 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2037 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2038 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2039 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2040 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2041 984065c8 2020-03-19 stsp if (error)
2042 984065c8 2020-03-19 stsp goto done;
2043 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2044 984065c8 2020-03-19 stsp free(id_str);
2045 984065c8 2020-03-19 stsp id_str = NULL;
2046 984065c8 2020-03-19 stsp }
2047 7848a0e1 2020-03-19 stsp
2048 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2049 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2050 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2051 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2052 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2053 7848a0e1 2020-03-19 stsp char *remote_refname;
2054 7848a0e1 2020-03-19 stsp
2055 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2056 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2057 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2058 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2059 0e4002ca 2020-03-21 stsp if (error)
2060 0e4002ca 2020-03-21 stsp goto done;
2061 0e4002ca 2020-03-21 stsp continue;
2062 0e4002ca 2020-03-21 stsp }
2063 0e4002ca 2020-03-21 stsp
2064 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2065 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2066 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2067 7848a0e1 2020-03-19 stsp if (error) {
2068 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2069 7848a0e1 2020-03-19 stsp goto done;
2070 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2071 6338a6a1 2020-03-21 stsp repo);
2072 7848a0e1 2020-03-19 stsp if (error)
2073 7848a0e1 2020-03-19 stsp goto done;
2074 7848a0e1 2020-03-19 stsp } else {
2075 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2076 db6d8ad8 2020-03-21 stsp verbosity, repo);
2077 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2078 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2079 9f142382 2020-03-21 stsp error = unlock_err;
2080 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2081 7848a0e1 2020-03-19 stsp if (error)
2082 7848a0e1 2020-03-19 stsp goto done;
2083 7848a0e1 2020-03-19 stsp }
2084 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2085 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2086 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2087 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2088 7848a0e1 2020-03-19 stsp goto done;
2089 7848a0e1 2020-03-19 stsp }
2090 7848a0e1 2020-03-19 stsp
2091 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2092 7848a0e1 2020-03-19 stsp if (error) {
2093 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2094 7848a0e1 2020-03-19 stsp goto done;
2095 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2096 688f11b3 2020-03-21 stsp verbosity, repo);
2097 7848a0e1 2020-03-19 stsp if (error)
2098 7848a0e1 2020-03-19 stsp goto done;
2099 7848a0e1 2020-03-19 stsp } else {
2100 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2101 db6d8ad8 2020-03-21 stsp verbosity, repo);
2102 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2103 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2104 9f142382 2020-03-21 stsp error = unlock_err;
2105 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2106 7848a0e1 2020-03-19 stsp if (error)
2107 7848a0e1 2020-03-19 stsp goto done;
2108 7848a0e1 2020-03-19 stsp }
2109 2ec30c80 2020-03-20 stsp
2110 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2111 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2112 2ec30c80 2020-03-20 stsp if (error) {
2113 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2114 2ec30c80 2020-03-20 stsp goto done;
2115 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2116 6338a6a1 2020-03-21 stsp repo);
2117 2ec30c80 2020-03-20 stsp if (error)
2118 2ec30c80 2020-03-20 stsp goto done;
2119 9f142382 2020-03-21 stsp } else {
2120 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2121 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2122 9f142382 2020-03-21 stsp error = unlock_err;
2123 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2124 9f142382 2020-03-21 stsp }
2125 7848a0e1 2020-03-19 stsp }
2126 7848a0e1 2020-03-19 stsp }
2127 f1bcca34 2020-03-25 stsp if (delete_refs) {
2128 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2129 3789fd73 2020-03-26 stsp verbosity, repo);
2130 f1bcca34 2020-03-25 stsp if (error)
2131 f1bcca34 2020-03-25 stsp goto done;
2132 f1bcca34 2020-03-25 stsp }
2133 f1bcca34 2020-03-25 stsp
2134 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2135 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2136 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2137 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2138 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2139 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2140 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2141 f1bcca34 2020-03-25 stsp
2142 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2143 f1bcca34 2020-03-25 stsp continue;
2144 f1bcca34 2020-03-25 stsp
2145 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2146 f1bcca34 2020-03-25 stsp continue;
2147 f1bcca34 2020-03-25 stsp
2148 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2149 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2150 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2151 f1bcca34 2020-03-25 stsp goto done;
2152 f1bcca34 2020-03-25 stsp }
2153 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2154 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2155 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2156 f1bcca34 2020-03-25 stsp free(remote_refname);
2157 f1bcca34 2020-03-25 stsp goto done;
2158 f1bcca34 2020-03-25 stsp }
2159 f1bcca34 2020-03-25 stsp
2160 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2161 f1bcca34 2020-03-25 stsp 0);
2162 f1bcca34 2020-03-25 stsp if (error) {
2163 f1bcca34 2020-03-25 stsp free(remote_refname);
2164 f1bcca34 2020-03-25 stsp free(remote_target);
2165 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2166 f1bcca34 2020-03-25 stsp error = NULL;
2167 f1bcca34 2020-03-25 stsp continue;
2168 f1bcca34 2020-03-25 stsp }
2169 f1bcca34 2020-03-25 stsp goto done;
2170 f1bcca34 2020-03-25 stsp }
2171 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2172 f1bcca34 2020-03-25 stsp verbosity, repo);
2173 f1bcca34 2020-03-25 stsp free(remote_refname);
2174 f1bcca34 2020-03-25 stsp free(remote_target);
2175 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2176 f1bcca34 2020-03-25 stsp if (error)
2177 f1bcca34 2020-03-25 stsp goto done;
2178 f1bcca34 2020-03-25 stsp }
2179 f1bcca34 2020-03-25 stsp }
2180 7848a0e1 2020-03-19 stsp done:
2181 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2182 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2183 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2184 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2185 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2186 9c52365f 2020-03-21 stsp }
2187 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2188 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2189 7848a0e1 2020-03-19 stsp if (repo)
2190 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2191 7848a0e1 2020-03-19 stsp if (worktree)
2192 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2193 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2194 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2195 7848a0e1 2020-03-19 stsp free(pe->data);
2196 7848a0e1 2020-03-19 stsp }
2197 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2198 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2199 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2200 7848a0e1 2020-03-19 stsp free(pe->data);
2201 7848a0e1 2020-03-19 stsp }
2202 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2203 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2204 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2205 7848a0e1 2020-03-19 stsp free(id_str);
2206 7848a0e1 2020-03-19 stsp free(cwd);
2207 7848a0e1 2020-03-19 stsp free(repo_path);
2208 7848a0e1 2020-03-19 stsp free(pack_hash);
2209 7848a0e1 2020-03-19 stsp free(proto);
2210 7848a0e1 2020-03-19 stsp free(host);
2211 7848a0e1 2020-03-19 stsp free(port);
2212 7848a0e1 2020-03-19 stsp free(server_path);
2213 7848a0e1 2020-03-19 stsp free(repo_name);
2214 7848a0e1 2020-03-19 stsp return error;
2215 7848a0e1 2020-03-19 stsp }
2216 7848a0e1 2020-03-19 stsp
2217 7848a0e1 2020-03-19 stsp
2218 7848a0e1 2020-03-19 stsp __dead static void
2219 2ab43947 2020-03-18 stsp usage_checkout(void)
2220 2ab43947 2020-03-18 stsp {
2221 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2222 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2223 2ab43947 2020-03-18 stsp exit(1);
2224 2ab43947 2020-03-18 stsp }
2225 2ab43947 2020-03-18 stsp
2226 2ab43947 2020-03-18 stsp static void
2227 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2228 2ab43947 2020-03-18 stsp {
2229 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2230 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2231 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2232 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2233 2ab43947 2020-03-18 stsp getprogname());
2234 2ab43947 2020-03-18 stsp }
2235 2ab43947 2020-03-18 stsp
2236 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2237 2ab43947 2020-03-18 stsp const char *worktree_path;
2238 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2239 2ab43947 2020-03-18 stsp };
2240 2ab43947 2020-03-18 stsp
2241 2ab43947 2020-03-18 stsp static const struct got_error *
2242 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2243 2ab43947 2020-03-18 stsp {
2244 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2245 2ab43947 2020-03-18 stsp
2246 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2247 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2248 2ab43947 2020-03-18 stsp return NULL;
2249 2ab43947 2020-03-18 stsp
2250 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2251 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2252 2ab43947 2020-03-18 stsp return NULL;
2253 2ab43947 2020-03-18 stsp }
2254 2ab43947 2020-03-18 stsp
2255 2ab43947 2020-03-18 stsp while (path[0] == '/')
2256 2ab43947 2020-03-18 stsp path++;
2257 2ab43947 2020-03-18 stsp
2258 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2259 2ab43947 2020-03-18 stsp return NULL;
2260 2ab43947 2020-03-18 stsp }
2261 2ab43947 2020-03-18 stsp
2262 2ab43947 2020-03-18 stsp static const struct got_error *
2263 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2264 2ab43947 2020-03-18 stsp {
2265 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2266 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2267 2ab43947 2020-03-18 stsp return NULL;
2268 2ab43947 2020-03-18 stsp }
2269 2ab43947 2020-03-18 stsp
2270 2ab43947 2020-03-18 stsp static const struct got_error *
2271 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2272 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2273 2ab43947 2020-03-18 stsp struct got_repository *repo)
2274 2ab43947 2020-03-18 stsp {
2275 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2276 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2277 2ab43947 2020-03-18 stsp
2278 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2279 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2280 2ab43947 2020-03-18 stsp if (err)
2281 2ab43947 2020-03-18 stsp return err;
2282 2ab43947 2020-03-18 stsp
2283 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2284 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2285 2ab43947 2020-03-18 stsp
2286 2ab43947 2020-03-18 stsp /*
2287 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2288 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2289 2ab43947 2020-03-18 stsp *
2290 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2291 2ab43947 2020-03-18 stsp *
2292 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2293 2ab43947 2020-03-18 stsp * \ /
2294 2ab43947 2020-03-18 stsp * C E
2295 2ab43947 2020-03-18 stsp * \ /
2296 2ab43947 2020-03-18 stsp * B (yca)
2297 2ab43947 2020-03-18 stsp * |
2298 2ab43947 2020-03-18 stsp * A
2299 2ab43947 2020-03-18 stsp *
2300 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2301 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2302 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2303 2ab43947 2020-03-18 stsp */
2304 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2305 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2306 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2307 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2308 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2309 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2310 2ab43947 2020-03-18 stsp
2311 2ab43947 2020-03-18 stsp free(yca_id);
2312 2ab43947 2020-03-18 stsp return NULL;
2313 2ab43947 2020-03-18 stsp }
2314 2ab43947 2020-03-18 stsp
2315 2ab43947 2020-03-18 stsp static const struct got_error *
2316 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2317 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2318 2ab43947 2020-03-18 stsp struct got_repository *repo)
2319 2ab43947 2020-03-18 stsp {
2320 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2321 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2322 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2323 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2324 2ab43947 2020-03-18 stsp
2325 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2326 2ab43947 2020-03-18 stsp if (err)
2327 2ab43947 2020-03-18 stsp goto done;
2328 2ab43947 2020-03-18 stsp
2329 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2330 2ab43947 2020-03-18 stsp is_same_branch = 1;
2331 2ab43947 2020-03-18 stsp goto done;
2332 2ab43947 2020-03-18 stsp }
2333 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2334 2ab43947 2020-03-18 stsp is_same_branch = 1;
2335 2ab43947 2020-03-18 stsp goto done;
2336 2ab43947 2020-03-18 stsp }
2337 2ab43947 2020-03-18 stsp
2338 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2339 2ab43947 2020-03-18 stsp if (err)
2340 2ab43947 2020-03-18 stsp goto done;
2341 2ab43947 2020-03-18 stsp
2342 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2343 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2344 2ab43947 2020-03-18 stsp if (err)
2345 2ab43947 2020-03-18 stsp goto done;
2346 2ab43947 2020-03-18 stsp
2347 2ab43947 2020-03-18 stsp for (;;) {
2348 2ab43947 2020-03-18 stsp struct got_object_id *id;
2349 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2350 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2351 2ab43947 2020-03-18 stsp if (err) {
2352 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2353 2ab43947 2020-03-18 stsp err = NULL;
2354 2ab43947 2020-03-18 stsp break;
2355 2ab43947 2020-03-18 stsp }
2356 2ab43947 2020-03-18 stsp
2357 2ab43947 2020-03-18 stsp if (id) {
2358 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2359 2ab43947 2020-03-18 stsp break;
2360 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2361 2ab43947 2020-03-18 stsp is_same_branch = 1;
2362 2ab43947 2020-03-18 stsp break;
2363 2ab43947 2020-03-18 stsp }
2364 2ab43947 2020-03-18 stsp }
2365 2ab43947 2020-03-18 stsp }
2366 2ab43947 2020-03-18 stsp done:
2367 2ab43947 2020-03-18 stsp if (graph)
2368 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2369 2ab43947 2020-03-18 stsp free(head_commit_id);
2370 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2371 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2372 2ab43947 2020-03-18 stsp return err;
2373 a367ff0f 2019-05-14 stsp }
2374 8069f636 2019-01-12 stsp
2375 4ed7e80c 2018-05-20 stsp static const struct got_error *
2376 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2377 4b6c9460 2020-03-05 stsp {
2378 4b6c9460 2020-03-05 stsp static char msg[512];
2379 4b6c9460 2020-03-05 stsp const char *branch_name;
2380 4b6c9460 2020-03-05 stsp
2381 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2382 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2383 4b6c9460 2020-03-05 stsp else
2384 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2385 4b6c9460 2020-03-05 stsp
2386 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2387 4b6c9460 2020-03-05 stsp branch_name += 11;
2388 4b6c9460 2020-03-05 stsp
2389 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2390 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2391 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2392 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2393 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2394 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2395 4b6c9460 2020-03-05 stsp
2396 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2397 4b6c9460 2020-03-05 stsp }
2398 4b6c9460 2020-03-05 stsp
2399 4b6c9460 2020-03-05 stsp static const struct got_error *
2400 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2401 c09a553d 2018-03-12 stsp {
2402 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2403 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2404 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2405 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2406 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2407 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2408 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2409 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2410 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2411 c47340da 2020-09-20 stsp char *cwd = NULL, *path = NULL;
2412 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2413 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2414 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2415 f2ea84fa 2019-07-27 stsp
2416 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2417 c09a553d 2018-03-12 stsp
2418 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2419 0bb8a95e 2018-03-12 stsp switch (ch) {
2420 08573d5b 2019-05-14 stsp case 'b':
2421 08573d5b 2019-05-14 stsp branch_name = optarg;
2422 08573d5b 2019-05-14 stsp break;
2423 8069f636 2019-01-12 stsp case 'c':
2424 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2425 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2426 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2427 bb51a5b4 2020-01-13 stsp break;
2428 bb51a5b4 2020-01-13 stsp case 'E':
2429 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2430 8069f636 2019-01-12 stsp break;
2431 0bb8a95e 2018-03-12 stsp case 'p':
2432 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2433 0bb8a95e 2018-03-12 stsp break;
2434 0bb8a95e 2018-03-12 stsp default:
2435 2deda0b9 2019-03-07 stsp usage_checkout();
2436 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2437 0bb8a95e 2018-03-12 stsp }
2438 0bb8a95e 2018-03-12 stsp }
2439 0bb8a95e 2018-03-12 stsp
2440 0bb8a95e 2018-03-12 stsp argc -= optind;
2441 0bb8a95e 2018-03-12 stsp argv += optind;
2442 0bb8a95e 2018-03-12 stsp
2443 6715a751 2018-03-16 stsp #ifndef PROFILE
2444 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2445 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2446 c09a553d 2018-03-12 stsp err(1, "pledge");
2447 6715a751 2018-03-16 stsp #endif
2448 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2449 c47340da 2020-09-20 stsp char *base, *dotgit;
2450 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2451 76089277 2018-04-01 stsp if (repo_path == NULL)
2452 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2453 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2454 76089277 2018-04-01 stsp if (cwd == NULL) {
2455 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2456 76089277 2018-04-01 stsp goto done;
2457 76089277 2018-04-01 stsp }
2458 c47340da 2020-09-20 stsp if (path_prefix[0])
2459 c47340da 2020-09-20 stsp path = strdup(path_prefix);
2460 c47340da 2020-09-20 stsp else
2461 c47340da 2020-09-20 stsp path = strdup(repo_path);
2462 c47340da 2020-09-20 stsp if (path == NULL) {
2463 c47340da 2020-09-20 stsp error = got_error_from_errno("strdup");
2464 c47340da 2020-09-20 stsp goto done;
2465 c47340da 2020-09-20 stsp }
2466 c47340da 2020-09-20 stsp base = basename(path);
2467 c47340da 2020-09-20 stsp if (base == NULL) {
2468 c47340da 2020-09-20 stsp error = got_error_from_errno2("basename", path);
2469 c47340da 2020-09-20 stsp goto done;
2470 76089277 2018-04-01 stsp }
2471 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2472 c09a553d 2018-03-12 stsp if (dotgit)
2473 c09a553d 2018-03-12 stsp *dotgit = '\0';
2474 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2475 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2476 76089277 2018-04-01 stsp goto done;
2477 c09a553d 2018-03-12 stsp }
2478 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2479 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2480 76089277 2018-04-01 stsp if (repo_path == NULL) {
2481 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2482 76089277 2018-04-01 stsp goto done;
2483 76089277 2018-04-01 stsp }
2484 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2485 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2486 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2487 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2488 b4b3a7dd 2019-07-22 stsp argv[1]);
2489 b4b3a7dd 2019-07-22 stsp goto done;
2490 b4b3a7dd 2019-07-22 stsp }
2491 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2492 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2493 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2494 b4b3a7dd 2019-07-22 stsp goto done;
2495 b4b3a7dd 2019-07-22 stsp }
2496 76089277 2018-04-01 stsp }
2497 c09a553d 2018-03-12 stsp } else
2498 c09a553d 2018-03-12 stsp usage_checkout();
2499 c09a553d 2018-03-12 stsp
2500 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2501 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2502 13bfb272 2019-05-10 jcs
2503 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2504 c09a553d 2018-03-12 stsp if (error != NULL)
2505 c02c541e 2019-03-29 stsp goto done;
2506 c02c541e 2019-03-29 stsp
2507 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2508 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2509 c530dc23 2019-07-23 stsp if (error) {
2510 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2511 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2512 c530dc23 2019-07-23 stsp goto done;
2513 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2514 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2515 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2516 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2517 c530dc23 2019-07-23 stsp goto done;
2518 c530dc23 2019-07-23 stsp }
2519 c530dc23 2019-07-23 stsp }
2520 c530dc23 2019-07-23 stsp
2521 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2522 c02c541e 2019-03-29 stsp if (error)
2523 c09a553d 2018-03-12 stsp goto done;
2524 8069f636 2019-01-12 stsp
2525 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2526 c09a553d 2018-03-12 stsp if (error != NULL)
2527 c09a553d 2018-03-12 stsp goto done;
2528 c09a553d 2018-03-12 stsp
2529 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2530 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2531 c09a553d 2018-03-12 stsp goto done;
2532 c09a553d 2018-03-12 stsp
2533 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2534 c09a553d 2018-03-12 stsp if (error != NULL)
2535 c09a553d 2018-03-12 stsp goto done;
2536 c09a553d 2018-03-12 stsp
2537 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2538 e5dc7198 2018-12-29 stsp path_prefix);
2539 e5dc7198 2018-12-29 stsp if (error != NULL)
2540 e5dc7198 2018-12-29 stsp goto done;
2541 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2542 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2543 49520a32 2018-12-29 stsp goto done;
2544 49520a32 2018-12-29 stsp }
2545 49520a32 2018-12-29 stsp
2546 8069f636 2019-01-12 stsp if (commit_id_str) {
2547 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2548 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2549 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2550 30837e32 2019-07-25 stsp if (error)
2551 8069f636 2019-01-12 stsp goto done;
2552 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2553 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2554 8069f636 2019-01-12 stsp if (error != NULL) {
2555 8069f636 2019-01-12 stsp free(commit_id);
2556 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2557 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2558 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2559 4b6c9460 2020-03-05 stsp }
2560 8069f636 2019-01-12 stsp goto done;
2561 8069f636 2019-01-12 stsp }
2562 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2563 4b6c9460 2020-03-05 stsp if (error) {
2564 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2565 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2566 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2567 4b6c9460 2020-03-05 stsp }
2568 45d344f6 2019-05-14 stsp goto done;
2569 4b6c9460 2020-03-05 stsp }
2570 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2571 8069f636 2019-01-12 stsp commit_id);
2572 8069f636 2019-01-12 stsp free(commit_id);
2573 8069f636 2019-01-12 stsp if (error)
2574 8069f636 2019-01-12 stsp goto done;
2575 8069f636 2019-01-12 stsp }
2576 8069f636 2019-01-12 stsp
2577 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2578 f2ea84fa 2019-07-27 stsp if (error)
2579 f2ea84fa 2019-07-27 stsp goto done;
2580 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2581 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2582 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2583 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2584 c09a553d 2018-03-12 stsp if (error != NULL)
2585 c09a553d 2018-03-12 stsp goto done;
2586 c09a553d 2018-03-12 stsp
2587 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2588 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2589 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2590 c09a553d 2018-03-12 stsp done:
2591 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2592 8069f636 2019-01-12 stsp free(commit_id_str);
2593 76089277 2018-04-01 stsp free(repo_path);
2594 507dc3bb 2018-12-29 stsp free(worktree_path);
2595 c47340da 2020-09-20 stsp free(cwd);
2596 c47340da 2020-09-20 stsp free(path);
2597 507dc3bb 2018-12-29 stsp return error;
2598 9627c110 2020-04-18 stsp }
2599 9627c110 2020-04-18 stsp
2600 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2601 9627c110 2020-04-18 stsp int did_something;
2602 9627c110 2020-04-18 stsp int conflicts;
2603 9627c110 2020-04-18 stsp int obstructed;
2604 9627c110 2020-04-18 stsp int not_updated;
2605 9627c110 2020-04-18 stsp };
2606 9627c110 2020-04-18 stsp
2607 9627c110 2020-04-18 stsp void
2608 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2609 9627c110 2020-04-18 stsp {
2610 9627c110 2020-04-18 stsp if (!upa->did_something)
2611 9627c110 2020-04-18 stsp return;
2612 9627c110 2020-04-18 stsp
2613 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2614 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2615 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2616 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2617 9627c110 2020-04-18 stsp upa->obstructed);
2618 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2619 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2620 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2621 507dc3bb 2018-12-29 stsp }
2622 507dc3bb 2018-12-29 stsp
2623 507dc3bb 2018-12-29 stsp __dead static void
2624 507dc3bb 2018-12-29 stsp usage_update(void)
2625 507dc3bb 2018-12-29 stsp {
2626 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2627 507dc3bb 2018-12-29 stsp getprogname());
2628 507dc3bb 2018-12-29 stsp exit(1);
2629 507dc3bb 2018-12-29 stsp }
2630 507dc3bb 2018-12-29 stsp
2631 1ee397ad 2019-07-12 stsp static const struct got_error *
2632 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2633 507dc3bb 2018-12-29 stsp {
2634 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2635 784955db 2019-01-12 stsp
2636 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2637 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2638 1ee397ad 2019-07-12 stsp return NULL;
2639 507dc3bb 2018-12-29 stsp
2640 9627c110 2020-04-18 stsp upa->did_something = 1;
2641 a484d721 2019-06-10 stsp
2642 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2643 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2644 1ee397ad 2019-07-12 stsp return NULL;
2645 a484d721 2019-06-10 stsp
2646 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2647 9627c110 2020-04-18 stsp upa->conflicts++;
2648 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2649 9627c110 2020-04-18 stsp upa->obstructed++;
2650 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2651 9627c110 2020-04-18 stsp upa->not_updated++;
2652 9627c110 2020-04-18 stsp
2653 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2654 507dc3bb 2018-12-29 stsp path++;
2655 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2656 1ee397ad 2019-07-12 stsp return NULL;
2657 be7061eb 2018-12-30 stsp }
2658 be7061eb 2018-12-30 stsp
2659 be7061eb 2018-12-30 stsp static const struct got_error *
2660 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2661 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2662 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2663 a1fb16d8 2019-05-24 stsp {
2664 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2665 a1fb16d8 2019-05-24 stsp char *base_id_str;
2666 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2667 a1fb16d8 2019-05-24 stsp
2668 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2669 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2670 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2671 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2672 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2673 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2674 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2675 a1fb16d8 2019-05-24 stsp }
2676 a1fb16d8 2019-05-24 stsp
2677 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2678 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2679 a1fb16d8 2019-05-24 stsp if (err) {
2680 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2681 a1fb16d8 2019-05-24 stsp return err;
2682 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2683 a1fb16d8 2019-05-24 stsp }
2684 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2685 a1fb16d8 2019-05-24 stsp return NULL;
2686 a1fb16d8 2019-05-24 stsp
2687 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2688 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2689 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2690 a1fb16d8 2019-05-24 stsp if (err)
2691 a1fb16d8 2019-05-24 stsp return err;
2692 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2693 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2694 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2695 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2696 0ebf8283 2019-07-24 stsp return NULL;
2697 0ebf8283 2019-07-24 stsp }
2698 0ebf8283 2019-07-24 stsp
2699 0ebf8283 2019-07-24 stsp static const struct got_error *
2700 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2701 0ebf8283 2019-07-24 stsp {
2702 0ebf8283 2019-07-24 stsp const struct got_error *err;
2703 0ebf8283 2019-07-24 stsp int in_progress;
2704 0ebf8283 2019-07-24 stsp
2705 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2706 0ebf8283 2019-07-24 stsp if (err)
2707 0ebf8283 2019-07-24 stsp return err;
2708 0ebf8283 2019-07-24 stsp if (in_progress)
2709 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2710 0ebf8283 2019-07-24 stsp
2711 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2712 0ebf8283 2019-07-24 stsp if (err)
2713 0ebf8283 2019-07-24 stsp return err;
2714 0ebf8283 2019-07-24 stsp if (in_progress)
2715 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2716 0ebf8283 2019-07-24 stsp
2717 a1fb16d8 2019-05-24 stsp return NULL;
2718 a5edda0a 2019-07-27 stsp }
2719 a5edda0a 2019-07-27 stsp
2720 a5edda0a 2019-07-27 stsp static const struct got_error *
2721 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2722 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2723 a5edda0a 2019-07-27 stsp {
2724 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2725 a5edda0a 2019-07-27 stsp char *path;
2726 a5edda0a 2019-07-27 stsp int i;
2727 a5edda0a 2019-07-27 stsp
2728 a5edda0a 2019-07-27 stsp if (argc == 0) {
2729 a5edda0a 2019-07-27 stsp path = strdup("");
2730 a5edda0a 2019-07-27 stsp if (path == NULL)
2731 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2732 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2733 a5edda0a 2019-07-27 stsp }
2734 a5edda0a 2019-07-27 stsp
2735 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2736 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2737 a5edda0a 2019-07-27 stsp if (err)
2738 a5edda0a 2019-07-27 stsp break;
2739 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2740 a5edda0a 2019-07-27 stsp if (err) {
2741 a5edda0a 2019-07-27 stsp free(path);
2742 a5edda0a 2019-07-27 stsp break;
2743 a5edda0a 2019-07-27 stsp }
2744 a5edda0a 2019-07-27 stsp }
2745 a5edda0a 2019-07-27 stsp
2746 a5edda0a 2019-07-27 stsp return err;
2747 a1fb16d8 2019-05-24 stsp }
2748 a1fb16d8 2019-05-24 stsp
2749 a1fb16d8 2019-05-24 stsp static const struct got_error *
2750 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2751 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2752 fa51e947 2020-03-27 stsp {
2753 fa51e947 2020-03-27 stsp const struct got_error *err;
2754 fa51e947 2020-03-27 stsp struct got_repository *repo;
2755 fa51e947 2020-03-27 stsp static char msg[512];
2756 fa51e947 2020-03-27 stsp
2757 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2758 fa51e947 2020-03-27 stsp if (err)
2759 fa51e947 2020-03-27 stsp return orig_err;
2760 fa51e947 2020-03-27 stsp
2761 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2762 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2763 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2764 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2765 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2766 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2767 fa51e947 2020-03-27 stsp got_repo_close(repo);
2768 fa51e947 2020-03-27 stsp return err;
2769 fa51e947 2020-03-27 stsp }
2770 fa51e947 2020-03-27 stsp
2771 fa51e947 2020-03-27 stsp static const struct got_error *
2772 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2773 507dc3bb 2018-12-29 stsp {
2774 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2775 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2776 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2777 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2778 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2779 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2780 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2781 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2782 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2783 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2784 9627c110 2020-04-18 stsp int ch;
2785 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2786 507dc3bb 2018-12-29 stsp
2787 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2788 f2ea84fa 2019-07-27 stsp
2789 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2790 507dc3bb 2018-12-29 stsp switch (ch) {
2791 024e9686 2019-05-14 stsp case 'b':
2792 024e9686 2019-05-14 stsp branch_name = optarg;
2793 024e9686 2019-05-14 stsp break;
2794 507dc3bb 2018-12-29 stsp case 'c':
2795 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2796 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2797 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2798 507dc3bb 2018-12-29 stsp break;
2799 507dc3bb 2018-12-29 stsp default:
2800 2deda0b9 2019-03-07 stsp usage_update();
2801 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2802 507dc3bb 2018-12-29 stsp }
2803 507dc3bb 2018-12-29 stsp }
2804 507dc3bb 2018-12-29 stsp
2805 507dc3bb 2018-12-29 stsp argc -= optind;
2806 507dc3bb 2018-12-29 stsp argv += optind;
2807 507dc3bb 2018-12-29 stsp
2808 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2809 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2810 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2811 507dc3bb 2018-12-29 stsp err(1, "pledge");
2812 507dc3bb 2018-12-29 stsp #endif
2813 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2814 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2815 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2816 c4cdcb68 2019-04-03 stsp goto done;
2817 c4cdcb68 2019-04-03 stsp }
2818 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2819 fa51e947 2020-03-27 stsp if (error) {
2820 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2821 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2822 fa51e947 2020-03-27 stsp worktree_path);
2823 7d5807f4 2019-07-11 stsp goto done;
2824 fa51e947 2020-03-27 stsp }
2825 7d5807f4 2019-07-11 stsp
2826 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2827 f2ea84fa 2019-07-27 stsp if (error)
2828 f2ea84fa 2019-07-27 stsp goto done;
2829 507dc3bb 2018-12-29 stsp
2830 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2831 c9956ddf 2019-09-08 stsp NULL);
2832 507dc3bb 2018-12-29 stsp if (error != NULL)
2833 507dc3bb 2018-12-29 stsp goto done;
2834 507dc3bb 2018-12-29 stsp
2835 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2836 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2837 087fb88c 2019-08-04 stsp if (error)
2838 087fb88c 2019-08-04 stsp goto done;
2839 087fb88c 2019-08-04 stsp
2840 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2841 0266afb7 2019-01-04 stsp if (error)
2842 0266afb7 2019-01-04 stsp goto done;
2843 0266afb7 2019-01-04 stsp
2844 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2845 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2846 024e9686 2019-05-14 stsp if (error != NULL)
2847 024e9686 2019-05-14 stsp goto done;
2848 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2849 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2850 9c4b8182 2019-01-02 stsp if (error != NULL)
2851 9c4b8182 2019-01-02 stsp goto done;
2852 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2853 507dc3bb 2018-12-29 stsp if (error != NULL)
2854 507dc3bb 2018-12-29 stsp goto done;
2855 507dc3bb 2018-12-29 stsp } else {
2856 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2857 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2858 04f57cb3 2019-07-25 stsp free(commit_id_str);
2859 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2860 30837e32 2019-07-25 stsp if (error)
2861 a297e751 2019-07-11 stsp goto done;
2862 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2863 a297e751 2019-07-11 stsp if (error)
2864 507dc3bb 2018-12-29 stsp goto done;
2865 507dc3bb 2018-12-29 stsp }
2866 35c965b2 2018-12-29 stsp
2867 a1fb16d8 2019-05-24 stsp if (branch_name) {
2868 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2869 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2870 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2871 f2ea84fa 2019-07-27 stsp continue;
2872 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2873 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2874 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2875 024e9686 2019-05-14 stsp goto done;
2876 024e9686 2019-05-14 stsp }
2877 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2878 024e9686 2019-05-14 stsp if (error)
2879 024e9686 2019-05-14 stsp goto done;
2880 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2881 3aef623b 2019-10-15 stsp repo);
2882 a367ff0f 2019-05-14 stsp free(head_commit_id);
2883 024e9686 2019-05-14 stsp if (error != NULL)
2884 024e9686 2019-05-14 stsp goto done;
2885 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2886 a367ff0f 2019-05-14 stsp if (error)
2887 a367ff0f 2019-05-14 stsp goto done;
2888 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2889 024e9686 2019-05-14 stsp if (error)
2890 024e9686 2019-05-14 stsp goto done;
2891 024e9686 2019-05-14 stsp } else {
2892 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2893 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2894 a367ff0f 2019-05-14 stsp if (error != NULL) {
2895 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2896 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2897 024e9686 2019-05-14 stsp goto done;
2898 a367ff0f 2019-05-14 stsp }
2899 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2900 a367ff0f 2019-05-14 stsp if (error)
2901 a367ff0f 2019-05-14 stsp goto done;
2902 024e9686 2019-05-14 stsp }
2903 507dc3bb 2018-12-29 stsp
2904 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2905 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2906 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2907 507dc3bb 2018-12-29 stsp commit_id);
2908 507dc3bb 2018-12-29 stsp if (error)
2909 507dc3bb 2018-12-29 stsp goto done;
2910 507dc3bb 2018-12-29 stsp }
2911 507dc3bb 2018-12-29 stsp
2912 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
2913 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2914 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
2915 507dc3bb 2018-12-29 stsp if (error != NULL)
2916 507dc3bb 2018-12-29 stsp goto done;
2917 9c4b8182 2019-01-02 stsp
2918 9627c110 2020-04-18 stsp if (upa.did_something)
2919 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2920 784955db 2019-01-12 stsp else
2921 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2922 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
2923 507dc3bb 2018-12-29 stsp done:
2924 c09a553d 2018-03-12 stsp free(worktree_path);
2925 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2926 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2927 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2928 507dc3bb 2018-12-29 stsp free(commit_id);
2929 9c4b8182 2019-01-02 stsp free(commit_id_str);
2930 c09a553d 2018-03-12 stsp return error;
2931 c09a553d 2018-03-12 stsp }
2932 c09a553d 2018-03-12 stsp
2933 f42b1b34 2018-03-12 stsp static const struct got_error *
2934 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2935 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2936 63035f9f 2019-10-06 stsp struct got_repository *repo)
2937 5c860e29 2018-03-12 stsp {
2938 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2939 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2940 79109fed 2018-03-27 stsp
2941 44392932 2019-08-25 stsp if (blob_id1) {
2942 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2943 44392932 2019-08-25 stsp if (err)
2944 44392932 2019-08-25 stsp goto done;
2945 44392932 2019-08-25 stsp }
2946 79109fed 2018-03-27 stsp
2947 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2948 44392932 2019-08-25 stsp if (err)
2949 44392932 2019-08-25 stsp goto done;
2950 79109fed 2018-03-27 stsp
2951 44392932 2019-08-25 stsp while (path[0] == '/')
2952 44392932 2019-08-25 stsp path++;
2953 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2954 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2955 44392932 2019-08-25 stsp done:
2956 44392932 2019-08-25 stsp if (blob1)
2957 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2958 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2959 44392932 2019-08-25 stsp return err;
2960 44392932 2019-08-25 stsp }
2961 44392932 2019-08-25 stsp
2962 44392932 2019-08-25 stsp static const struct got_error *
2963 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2964 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2965 63035f9f 2019-10-06 stsp struct got_repository *repo)
2966 44392932 2019-08-25 stsp {
2967 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2968 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2969 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2970 44392932 2019-08-25 stsp
2971 44392932 2019-08-25 stsp if (tree_id1) {
2972 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2973 79109fed 2018-03-27 stsp if (err)
2974 44392932 2019-08-25 stsp goto done;
2975 79109fed 2018-03-27 stsp }
2976 79109fed 2018-03-27 stsp
2977 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2978 0f2b3dca 2018-12-22 stsp if (err)
2979 0f2b3dca 2018-12-22 stsp goto done;
2980 0f2b3dca 2018-12-22 stsp
2981 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2982 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2983 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2984 44392932 2019-08-25 stsp while (path[0] == '/')
2985 44392932 2019-08-25 stsp path++;
2986 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2987 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2988 0f2b3dca 2018-12-22 stsp done:
2989 79109fed 2018-03-27 stsp if (tree1)
2990 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2991 366e0a5f 2019-10-10 stsp if (tree2)
2992 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2993 44392932 2019-08-25 stsp return err;
2994 44392932 2019-08-25 stsp }
2995 44392932 2019-08-25 stsp
2996 44392932 2019-08-25 stsp static const struct got_error *
2997 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
2998 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
2999 0208f208 2020-05-05 stsp {
3000 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3001 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3002 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3003 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3004 0208f208 2020-05-05 stsp
3005 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3006 0208f208 2020-05-05 stsp if (qid != NULL) {
3007 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3008 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3009 0208f208 2020-05-05 stsp qid->id);
3010 0208f208 2020-05-05 stsp if (err)
3011 0208f208 2020-05-05 stsp return err;
3012 0208f208 2020-05-05 stsp
3013 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3014 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3015 0208f208 2020-05-05 stsp
3016 0208f208 2020-05-05 stsp }
3017 0208f208 2020-05-05 stsp
3018 0208f208 2020-05-05 stsp if (tree_id1) {
3019 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3020 0208f208 2020-05-05 stsp if (err)
3021 0208f208 2020-05-05 stsp goto done;
3022 0208f208 2020-05-05 stsp }
3023 0208f208 2020-05-05 stsp
3024 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3025 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3026 0208f208 2020-05-05 stsp if (err)
3027 0208f208 2020-05-05 stsp goto done;
3028 0208f208 2020-05-05 stsp
3029 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3030 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3031 0208f208 2020-05-05 stsp done:
3032 0208f208 2020-05-05 stsp if (tree1)
3033 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3034 0208f208 2020-05-05 stsp if (tree2)
3035 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3036 0208f208 2020-05-05 stsp return err;
3037 0208f208 2020-05-05 stsp }
3038 0208f208 2020-05-05 stsp
3039 0208f208 2020-05-05 stsp static const struct got_error *
3040 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3041 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3042 44392932 2019-08-25 stsp {
3043 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3044 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3045 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3046 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3047 44392932 2019-08-25 stsp struct got_object_qid *qid;
3048 44392932 2019-08-25 stsp
3049 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3050 44392932 2019-08-25 stsp if (qid != NULL) {
3051 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3052 44392932 2019-08-25 stsp qid->id);
3053 44392932 2019-08-25 stsp if (err)
3054 44392932 2019-08-25 stsp return err;
3055 44392932 2019-08-25 stsp }
3056 44392932 2019-08-25 stsp
3057 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3058 44392932 2019-08-25 stsp int obj_type;
3059 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3060 44392932 2019-08-25 stsp if (err)
3061 44392932 2019-08-25 stsp goto done;
3062 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3063 44392932 2019-08-25 stsp if (err) {
3064 44392932 2019-08-25 stsp free(obj_id2);
3065 44392932 2019-08-25 stsp goto done;
3066 44392932 2019-08-25 stsp }
3067 44392932 2019-08-25 stsp if (pcommit) {
3068 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3069 44392932 2019-08-25 stsp qid->id, path);
3070 44392932 2019-08-25 stsp if (err) {
3071 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3072 2e8c69d1 2020-05-04 stsp free(obj_id2);
3073 2e8c69d1 2020-05-04 stsp goto done;
3074 2e8c69d1 2020-05-04 stsp }
3075 2e8c69d1 2020-05-04 stsp } else {
3076 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3077 2e8c69d1 2020-05-04 stsp if (err) {
3078 2e8c69d1 2020-05-04 stsp free(obj_id2);
3079 2e8c69d1 2020-05-04 stsp goto done;
3080 2e8c69d1 2020-05-04 stsp }
3081 44392932 2019-08-25 stsp }
3082 44392932 2019-08-25 stsp }
3083 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3084 44392932 2019-08-25 stsp if (err) {
3085 44392932 2019-08-25 stsp free(obj_id2);
3086 44392932 2019-08-25 stsp goto done;
3087 44392932 2019-08-25 stsp }
3088 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3089 44392932 2019-08-25 stsp switch (obj_type) {
3090 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3091 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3092 63035f9f 2019-10-06 stsp 0, repo);
3093 44392932 2019-08-25 stsp break;
3094 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3095 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3096 63035f9f 2019-10-06 stsp 0, repo);
3097 44392932 2019-08-25 stsp break;
3098 44392932 2019-08-25 stsp default:
3099 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3100 44392932 2019-08-25 stsp break;
3101 44392932 2019-08-25 stsp }
3102 44392932 2019-08-25 stsp free(obj_id1);
3103 44392932 2019-08-25 stsp free(obj_id2);
3104 44392932 2019-08-25 stsp } else {
3105 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3106 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3107 44392932 2019-08-25 stsp if (err)
3108 44392932 2019-08-25 stsp goto done;
3109 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3110 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3111 44392932 2019-08-25 stsp if (err)
3112 44392932 2019-08-25 stsp goto done;
3113 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3114 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3115 44392932 2019-08-25 stsp }
3116 44392932 2019-08-25 stsp done:
3117 0f2b3dca 2018-12-22 stsp free(id_str1);
3118 0f2b3dca 2018-12-22 stsp free(id_str2);
3119 44392932 2019-08-25 stsp if (pcommit)
3120 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3121 79109fed 2018-03-27 stsp return err;
3122 79109fed 2018-03-27 stsp }
3123 79109fed 2018-03-27 stsp
3124 4bb494d5 2018-06-16 stsp static char *
3125 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3126 6c281f94 2018-06-11 stsp {
3127 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3128 09867e48 2019-08-13 stsp char *p, *s;
3129 09867e48 2019-08-13 stsp
3130 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3131 09867e48 2019-08-13 stsp if (tm == NULL)
3132 09867e48 2019-08-13 stsp return NULL;
3133 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3134 09867e48 2019-08-13 stsp if (s == NULL)
3135 09867e48 2019-08-13 stsp return NULL;
3136 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3137 6c281f94 2018-06-11 stsp if (p)
3138 6c281f94 2018-06-11 stsp *p = '\0';
3139 6c281f94 2018-06-11 stsp return s;
3140 6c281f94 2018-06-11 stsp }
3141 dc424a06 2019-08-07 stsp
3142 6841bf13 2019-11-29 kn static const struct got_error *
3143 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3144 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3145 6841bf13 2019-11-29 kn {
3146 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3147 6841bf13 2019-11-29 kn regmatch_t regmatch;
3148 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3149 6841bf13 2019-11-29 kn
3150 6841bf13 2019-11-29 kn *have_match = 0;
3151 6841bf13 2019-11-29 kn
3152 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3153 6841bf13 2019-11-29 kn if (err)
3154 6841bf13 2019-11-29 kn return err;
3155 6841bf13 2019-11-29 kn
3156 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3157 6841bf13 2019-11-29 kn if (err)
3158 6841bf13 2019-11-29 kn goto done;
3159 6841bf13 2019-11-29 kn
3160 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3161 6841bf13 2019-11-29 kn *have_match = 1;
3162 6841bf13 2019-11-29 kn done:
3163 6841bf13 2019-11-29 kn free(id_str);
3164 6841bf13 2019-11-29 kn free(logmsg);
3165 6841bf13 2019-11-29 kn return err;
3166 6841bf13 2019-11-29 kn }
3167 6841bf13 2019-11-29 kn
3168 0208f208 2020-05-05 stsp static void
3169 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3170 0208f208 2020-05-05 stsp regex_t *regex)
3171 0208f208 2020-05-05 stsp {
3172 0208f208 2020-05-05 stsp regmatch_t regmatch;
3173 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3174 0208f208 2020-05-05 stsp
3175 0208f208 2020-05-05 stsp *have_match = 0;
3176 0208f208 2020-05-05 stsp
3177 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3178 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3179 0208f208 2020-05-05 stsp *have_match = 1;
3180 0208f208 2020-05-05 stsp break;
3181 0208f208 2020-05-05 stsp }
3182 0208f208 2020-05-05 stsp }
3183 0208f208 2020-05-05 stsp }
3184 0208f208 2020-05-05 stsp
3185 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3186 6c281f94 2018-06-11 stsp
3187 79109fed 2018-03-27 stsp static const struct got_error *
3188 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3189 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3190 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3191 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3192 79109fed 2018-03-27 stsp {
3193 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3194 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3195 6c281f94 2018-06-11 stsp char datebuf[26];
3196 45d799e2 2018-12-23 stsp time_t committer_time;
3197 45d799e2 2018-12-23 stsp const char *author, *committer;
3198 199a4027 2019-02-02 stsp char *refs_str = NULL;
3199 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3200 5c860e29 2018-03-12 stsp
3201 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3202 199a4027 2019-02-02 stsp char *s;
3203 199a4027 2019-02-02 stsp const char *name;
3204 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3205 a436ad14 2019-08-13 stsp int cmp;
3206 a436ad14 2019-08-13 stsp
3207 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3208 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3209 d9498b20 2019-02-05 stsp continue;
3210 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3211 199a4027 2019-02-02 stsp name += 5;
3212 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3213 7143d404 2019-03-12 stsp continue;
3214 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3215 e34f9ed6 2019-02-02 stsp name += 6;
3216 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3217 141c2bff 2019-02-04 stsp name += 8;
3218 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3219 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3220 4343a07f 2020-04-24 stsp continue;
3221 4343a07f 2020-04-24 stsp }
3222 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3223 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
3224 5d844a1e 2019-08-13 stsp if (err) {
3225 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3226 5d844a1e 2019-08-13 stsp return err;
3227 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3228 5d844a1e 2019-08-13 stsp err = NULL;
3229 5d844a1e 2019-08-13 stsp tag = NULL;
3230 5d844a1e 2019-08-13 stsp }
3231 a436ad14 2019-08-13 stsp }
3232 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3233 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
3234 a436ad14 2019-08-13 stsp if (tag)
3235 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3236 a436ad14 2019-08-13 stsp if (cmp != 0)
3237 a436ad14 2019-08-13 stsp continue;
3238 199a4027 2019-02-02 stsp s = refs_str;
3239 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3240 199a4027 2019-02-02 stsp name) == -1) {
3241 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3242 199a4027 2019-02-02 stsp free(s);
3243 34ca4898 2019-08-13 stsp return err;
3244 199a4027 2019-02-02 stsp }
3245 199a4027 2019-02-02 stsp free(s);
3246 199a4027 2019-02-02 stsp }
3247 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3248 8bf5b3c9 2018-03-17 stsp if (err)
3249 8bf5b3c9 2018-03-17 stsp return err;
3250 788c352e 2018-06-16 stsp
3251 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3252 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3253 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3254 832c249c 2018-06-10 stsp free(id_str);
3255 d3d493d7 2019-02-21 stsp id_str = NULL;
3256 d3d493d7 2019-02-21 stsp free(refs_str);
3257 d3d493d7 2019-02-21 stsp refs_str = NULL;
3258 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3259 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3260 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3261 09867e48 2019-08-13 stsp if (datestr)
3262 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3263 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3264 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3265 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3266 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3267 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3268 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3269 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3270 3fe1abad 2018-06-10 stsp int n = 1;
3271 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3272 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3273 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3274 a0603db2 2018-06-10 stsp if (err)
3275 a0603db2 2018-06-10 stsp return err;
3276 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3277 a0603db2 2018-06-10 stsp free(id_str);
3278 a0603db2 2018-06-10 stsp }
3279 a0603db2 2018-06-10 stsp }
3280 832c249c 2018-06-10 stsp
3281 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3282 5943eee2 2019-08-13 stsp if (err)
3283 5943eee2 2019-08-13 stsp return err;
3284 8bf5b3c9 2018-03-17 stsp
3285 621015ac 2018-07-23 stsp logmsg = logmsg0;
3286 832c249c 2018-06-10 stsp do {
3287 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3288 832c249c 2018-06-10 stsp if (line)
3289 832c249c 2018-06-10 stsp printf(" %s\n", line);
3290 832c249c 2018-06-10 stsp } while (line);
3291 621015ac 2018-07-23 stsp free(logmsg0);
3292 832c249c 2018-06-10 stsp
3293 0208f208 2020-05-05 stsp if (changed_paths) {
3294 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3295 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3296 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3297 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3298 0208f208 2020-05-05 stsp }
3299 0208f208 2020-05-05 stsp printf("\n");
3300 0208f208 2020-05-05 stsp }
3301 971751ac 2018-03-27 stsp if (show_patch) {
3302 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3303 971751ac 2018-03-27 stsp if (err == 0)
3304 971751ac 2018-03-27 stsp printf("\n");
3305 971751ac 2018-03-27 stsp }
3306 07862c20 2018-09-15 stsp
3307 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3308 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3309 07862c20 2018-09-15 stsp return err;
3310 f42b1b34 2018-03-12 stsp }
3311 5c860e29 2018-03-12 stsp
3312 f42b1b34 2018-03-12 stsp static const struct got_error *
3313 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3314 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3315 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3316 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3317 f42b1b34 2018-03-12 stsp {
3318 f42b1b34 2018-03-12 stsp const struct got_error *err;
3319 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3320 6841bf13 2019-11-29 kn regex_t regex;
3321 6841bf13 2019-11-29 kn int have_match;
3322 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3323 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3324 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3325 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3326 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3327 dbec59df 2020-04-18 stsp
3328 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3329 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3330 372ccdbb 2018-06-10 stsp
3331 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3332 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3333 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3334 6841bf13 2019-11-29 kn
3335 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3336 f42b1b34 2018-03-12 stsp if (err)
3337 f42b1b34 2018-03-12 stsp return err;
3338 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3339 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3340 372ccdbb 2018-06-10 stsp if (err)
3341 fcc85cad 2018-07-22 stsp goto done;
3342 656b1f76 2019-05-11 jcs for (;;) {
3343 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3344 8bf5b3c9 2018-03-17 stsp
3345 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3346 84453469 2018-11-11 stsp break;
3347 84453469 2018-11-11 stsp
3348 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3349 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3350 372ccdbb 2018-06-10 stsp if (err) {
3351 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3352 9ba79e04 2018-06-11 stsp err = NULL;
3353 ee780d5c 2020-01-04 stsp break;
3354 7e665116 2018-04-02 stsp }
3355 b43fbaa0 2018-06-11 stsp if (id == NULL)
3356 7e665116 2018-04-02 stsp break;
3357 b43fbaa0 2018-06-11 stsp
3358 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3359 b43fbaa0 2018-06-11 stsp if (err)
3360 fcc85cad 2018-07-22 stsp break;
3361 6841bf13 2019-11-29 kn
3362 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3363 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3364 0208f208 2020-05-05 stsp if (err)
3365 0208f208 2020-05-05 stsp break;
3366 0208f208 2020-05-05 stsp }
3367 0208f208 2020-05-05 stsp
3368 6841bf13 2019-11-29 kn if (search_pattern) {
3369 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3370 6841bf13 2019-11-29 kn if (err) {
3371 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3372 6841bf13 2019-11-29 kn break;
3373 6841bf13 2019-11-29 kn }
3374 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3375 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3376 0208f208 2020-05-05 stsp &changed_paths, &regex);
3377 6841bf13 2019-11-29 kn if (have_match == 0) {
3378 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3379 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3380 0208f208 2020-05-05 stsp free((char *)pe->path);
3381 0208f208 2020-05-05 stsp free(pe->data);
3382 0208f208 2020-05-05 stsp }
3383 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3384 6841bf13 2019-11-29 kn continue;
3385 6841bf13 2019-11-29 kn }
3386 6841bf13 2019-11-29 kn }
3387 6841bf13 2019-11-29 kn
3388 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3389 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3390 dbec59df 2020-04-18 stsp if (err)
3391 dbec59df 2020-04-18 stsp break;
3392 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3393 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3394 dbec59df 2020-04-18 stsp } else {
3395 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3396 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3397 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3398 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3399 dbec59df 2020-04-18 stsp if (err)
3400 dbec59df 2020-04-18 stsp break;
3401 dbec59df 2020-04-18 stsp }
3402 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3403 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3404 7e665116 2018-04-02 stsp break;
3405 0208f208 2020-05-05 stsp
3406 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3407 0208f208 2020-05-05 stsp free((char *)pe->path);
3408 0208f208 2020-05-05 stsp free(pe->data);
3409 0208f208 2020-05-05 stsp }
3410 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3411 31cedeaf 2018-09-15 stsp }
3412 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3413 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3414 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3415 dbec59df 2020-04-18 stsp if (err)
3416 dbec59df 2020-04-18 stsp break;
3417 502b9684 2020-07-31 stsp if (show_changed_paths) {
3418 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3419 502b9684 2020-07-31 stsp commit, repo);
3420 502b9684 2020-07-31 stsp if (err)
3421 502b9684 2020-07-31 stsp break;
3422 502b9684 2020-07-31 stsp }
3423 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3424 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3425 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3426 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3427 dbec59df 2020-04-18 stsp if (err)
3428 dbec59df 2020-04-18 stsp break;
3429 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3430 502b9684 2020-07-31 stsp free((char *)pe->path);
3431 502b9684 2020-07-31 stsp free(pe->data);
3432 502b9684 2020-07-31 stsp }
3433 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3434 dbec59df 2020-04-18 stsp }
3435 dbec59df 2020-04-18 stsp }
3436 fcc85cad 2018-07-22 stsp done:
3437 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3438 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3439 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3440 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3441 dbec59df 2020-04-18 stsp }
3442 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3443 0208f208 2020-05-05 stsp free((char *)pe->path);
3444 0208f208 2020-05-05 stsp free(pe->data);
3445 0208f208 2020-05-05 stsp }
3446 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3447 6841bf13 2019-11-29 kn if (search_pattern)
3448 6841bf13 2019-11-29 kn regfree(&regex);
3449 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3450 f42b1b34 2018-03-12 stsp return err;
3451 f42b1b34 2018-03-12 stsp }
3452 5c860e29 2018-03-12 stsp
3453 4ed7e80c 2018-05-20 stsp __dead static void
3454 6f3d1eb0 2018-03-12 stsp usage_log(void)
3455 6f3d1eb0 2018-03-12 stsp {
3456 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3457 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3458 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3459 6f3d1eb0 2018-03-12 stsp exit(1);
3460 b1ebc001 2019-08-13 stsp }
3461 b1ebc001 2019-08-13 stsp
3462 b1ebc001 2019-08-13 stsp static int
3463 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3464 b1ebc001 2019-08-13 stsp {
3465 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3466 b1ebc001 2019-08-13 stsp long long n;
3467 b1ebc001 2019-08-13 stsp const char *errstr;
3468 b1ebc001 2019-08-13 stsp
3469 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3470 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3471 b1ebc001 2019-08-13 stsp return 0;
3472 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3473 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3474 b1ebc001 2019-08-13 stsp return 0;
3475 b1ebc001 2019-08-13 stsp return n;
3476 d1fe46f9 2020-04-18 stsp }
3477 d1fe46f9 2020-04-18 stsp
3478 d1fe46f9 2020-04-18 stsp static const struct got_error *
3479 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3480 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3481 d1fe46f9 2020-04-18 stsp {
3482 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3483 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3484 d1fe46f9 2020-04-18 stsp
3485 d1fe46f9 2020-04-18 stsp *id = NULL;
3486 d1fe46f9 2020-04-18 stsp
3487 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3488 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3489 d1fe46f9 2020-04-18 stsp int obj_type;
3490 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3491 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3492 d1fe46f9 2020-04-18 stsp if (err)
3493 d1fe46f9 2020-04-18 stsp return err;
3494 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3495 d1fe46f9 2020-04-18 stsp if (err)
3496 d1fe46f9 2020-04-18 stsp return err;
3497 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3498 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3499 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3500 d1fe46f9 2020-04-18 stsp if (err)
3501 d1fe46f9 2020-04-18 stsp return err;
3502 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3503 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3504 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3505 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3506 d1fe46f9 2020-04-18 stsp }
3507 d1fe46f9 2020-04-18 stsp free(*id);
3508 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3509 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3510 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3511 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3512 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3513 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3514 d1fe46f9 2020-04-18 stsp if (err)
3515 d1fe46f9 2020-04-18 stsp return err;
3516 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3517 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3518 d1fe46f9 2020-04-18 stsp } else {
3519 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3520 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3521 d1fe46f9 2020-04-18 stsp }
3522 d1fe46f9 2020-04-18 stsp
3523 d1fe46f9 2020-04-18 stsp return err;
3524 6f3d1eb0 2018-03-12 stsp }
3525 6f3d1eb0 2018-03-12 stsp
3526 4ed7e80c 2018-05-20 stsp static const struct got_error *
3527 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3528 f42b1b34 2018-03-12 stsp {
3529 f42b1b34 2018-03-12 stsp const struct got_error *error;
3530 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3531 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3532 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3533 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3534 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3535 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3536 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3537 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3538 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3539 64a96a6d 2018-04-01 stsp const char *errstr;
3540 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3541 1b3893a2 2019-03-18 stsp
3542 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3543 5c860e29 2018-03-12 stsp
3544 6715a751 2018-03-16 stsp #ifndef PROFILE
3545 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3546 6098196c 2019-01-04 stsp NULL)
3547 ad242220 2018-09-08 stsp == -1)
3548 f42b1b34 2018-03-12 stsp err(1, "pledge");
3549 6715a751 2018-03-16 stsp #endif
3550 79109fed 2018-03-27 stsp
3551 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3552 b1ebc001 2019-08-13 stsp
3553 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3554 79109fed 2018-03-27 stsp switch (ch) {
3555 79109fed 2018-03-27 stsp case 'p':
3556 79109fed 2018-03-27 stsp show_patch = 1;
3557 d142fc45 2018-04-01 stsp break;
3558 0208f208 2020-05-05 stsp case 'P':
3559 0208f208 2020-05-05 stsp show_changed_paths = 1;
3560 0208f208 2020-05-05 stsp break;
3561 d142fc45 2018-04-01 stsp case 'c':
3562 d142fc45 2018-04-01 stsp start_commit = optarg;
3563 64a96a6d 2018-04-01 stsp break;
3564 c0cc5c62 2018-10-18 stsp case 'C':
3565 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3566 4a8520aa 2018-10-18 stsp &errstr);
3567 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3568 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3569 c0cc5c62 2018-10-18 stsp break;
3570 64a96a6d 2018-04-01 stsp case 'l':
3571 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3572 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3573 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3574 cc54c501 2019-07-15 stsp break;
3575 48c8c60d 2020-01-27 stsp case 'b':
3576 48c8c60d 2020-01-27 stsp log_branches = 1;
3577 a0603db2 2018-06-10 stsp break;
3578 04ca23f4 2018-07-16 stsp case 'r':
3579 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3580 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3581 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3582 9ba1d308 2019-10-21 stsp optarg);
3583 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3584 04ca23f4 2018-07-16 stsp break;
3585 dbec59df 2020-04-18 stsp case 'R':
3586 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3587 dbec59df 2020-04-18 stsp break;
3588 6841bf13 2019-11-29 kn case 's':
3589 6841bf13 2019-11-29 kn search_pattern = optarg;
3590 6841bf13 2019-11-29 kn break;
3591 d1fe46f9 2020-04-18 stsp case 'x':
3592 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3593 d1fe46f9 2020-04-18 stsp break;
3594 79109fed 2018-03-27 stsp default:
3595 2deda0b9 2019-03-07 stsp usage_log();
3596 79109fed 2018-03-27 stsp /* NOTREACHED */
3597 79109fed 2018-03-27 stsp }
3598 79109fed 2018-03-27 stsp }
3599 79109fed 2018-03-27 stsp
3600 79109fed 2018-03-27 stsp argc -= optind;
3601 79109fed 2018-03-27 stsp argv += optind;
3602 f42b1b34 2018-03-12 stsp
3603 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3604 dc1edbfa 2019-11-29 kn diff_context = 3;
3605 dc1edbfa 2019-11-29 kn else if (!show_patch)
3606 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3607 dc1edbfa 2019-11-29 kn
3608 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3609 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3610 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3611 04ca23f4 2018-07-16 stsp goto done;
3612 04ca23f4 2018-07-16 stsp }
3613 cffc0aa4 2019-02-05 stsp
3614 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3615 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3616 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3617 50f2fada 2020-04-24 stsp goto done;
3618 50f2fada 2020-04-24 stsp error = NULL;
3619 50f2fada 2020-04-24 stsp }
3620 cffc0aa4 2019-02-05 stsp
3621 e7301579 2019-03-18 stsp if (argc == 0) {
3622 cbd1af7a 2019-03-18 stsp path = strdup("");
3623 e7301579 2019-03-18 stsp if (path == NULL) {
3624 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3625 cbd1af7a 2019-03-18 stsp goto done;
3626 e7301579 2019-03-18 stsp }
3627 e7301579 2019-03-18 stsp } else if (argc == 1) {
3628 e7301579 2019-03-18 stsp if (worktree) {
3629 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3630 e7301579 2019-03-18 stsp argv[0]);
3631 e7301579 2019-03-18 stsp if (error)
3632 e7301579 2019-03-18 stsp goto done;
3633 e7301579 2019-03-18 stsp } else {
3634 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3635 e7301579 2019-03-18 stsp if (path == NULL) {
3636 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3637 e7301579 2019-03-18 stsp goto done;
3638 e7301579 2019-03-18 stsp }
3639 e7301579 2019-03-18 stsp }
3640 cbd1af7a 2019-03-18 stsp } else
3641 cbd1af7a 2019-03-18 stsp usage_log();
3642 cbd1af7a 2019-03-18 stsp
3643 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3644 5486daa2 2019-05-11 stsp repo_path = worktree ?
3645 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3646 5486daa2 2019-05-11 stsp }
3647 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3648 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3649 cffc0aa4 2019-02-05 stsp goto done;
3650 04ca23f4 2018-07-16 stsp }
3651 6098196c 2019-01-04 stsp
3652 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3653 d7d4f210 2018-03-12 stsp if (error != NULL)
3654 04ca23f4 2018-07-16 stsp goto done;
3655 f42b1b34 2018-03-12 stsp
3656 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3657 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3658 c02c541e 2019-03-29 stsp if (error)
3659 c02c541e 2019-03-29 stsp goto done;
3660 c02c541e 2019-03-29 stsp
3661 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3662 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3663 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3664 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3665 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3666 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3667 3235492e 2018-04-01 stsp if (error != NULL)
3668 d1fe46f9 2020-04-18 stsp goto done;
3669 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3670 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3671 3235492e 2018-04-01 stsp if (error != NULL)
3672 d1fe46f9 2020-04-18 stsp goto done;
3673 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3674 d1fe46f9 2020-04-18 stsp start_id);
3675 d1fe46f9 2020-04-18 stsp if (error != NULL)
3676 d1fe46f9 2020-04-18 stsp goto done;
3677 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3678 3235492e 2018-04-01 stsp } else {
3679 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3680 d1fe46f9 2020-04-18 stsp if (error != NULL)
3681 d1fe46f9 2020-04-18 stsp goto done;
3682 3235492e 2018-04-01 stsp }
3683 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3684 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3685 d1fe46f9 2020-04-18 stsp if (error != NULL)
3686 d1fe46f9 2020-04-18 stsp goto done;
3687 d1fe46f9 2020-04-18 stsp }
3688 04ca23f4 2018-07-16 stsp
3689 deeabeae 2019-08-27 stsp if (worktree) {
3690 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3691 deeabeae 2019-08-27 stsp char *p;
3692 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3693 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3694 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3695 deeabeae 2019-08-27 stsp goto done;
3696 deeabeae 2019-08-27 stsp }
3697 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3698 deeabeae 2019-08-27 stsp free(p);
3699 deeabeae 2019-08-27 stsp } else
3700 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3701 04ca23f4 2018-07-16 stsp if (error != NULL)
3702 04ca23f4 2018-07-16 stsp goto done;
3703 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3704 04ca23f4 2018-07-16 stsp free(path);
3705 04ca23f4 2018-07-16 stsp path = in_repo_path;
3706 04ca23f4 2018-07-16 stsp }
3707 199a4027 2019-02-02 stsp
3708 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3709 199a4027 2019-02-02 stsp if (error)
3710 199a4027 2019-02-02 stsp goto done;
3711 04ca23f4 2018-07-16 stsp
3712 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3713 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3714 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3715 04ca23f4 2018-07-16 stsp done:
3716 04ca23f4 2018-07-16 stsp free(path);
3717 04ca23f4 2018-07-16 stsp free(repo_path);
3718 04ca23f4 2018-07-16 stsp free(cwd);
3719 cffc0aa4 2019-02-05 stsp if (worktree)
3720 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3721 ad242220 2018-09-08 stsp if (repo) {
3722 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3723 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3724 ad242220 2018-09-08 stsp if (error == NULL)
3725 ad242220 2018-09-08 stsp error = repo_error;
3726 ad242220 2018-09-08 stsp }
3727 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3728 8bf5b3c9 2018-03-17 stsp return error;
3729 5c860e29 2018-03-12 stsp }
3730 5c860e29 2018-03-12 stsp
3731 4ed7e80c 2018-05-20 stsp __dead static void
3732 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3733 3f8b7d6a 2018-04-01 stsp {
3734 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3735 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3736 3f8b7d6a 2018-04-01 stsp exit(1);
3737 b00d56cd 2018-04-01 stsp }
3738 b00d56cd 2018-04-01 stsp
3739 b72f483a 2019-02-05 stsp struct print_diff_arg {
3740 b72f483a 2019-02-05 stsp struct got_repository *repo;
3741 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3742 b72f483a 2019-02-05 stsp int diff_context;
3743 3fc0c068 2019-02-10 stsp const char *id_str;
3744 3fc0c068 2019-02-10 stsp int header_shown;
3745 98eaaa12 2019-08-03 stsp int diff_staged;
3746 63035f9f 2019-10-06 stsp int ignore_whitespace;
3747 b72f483a 2019-02-05 stsp };
3748 39449a05 2020-07-23 stsp
3749 39449a05 2020-07-23 stsp /*
3750 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3751 39449a05 2020-07-23 stsp * it as content to the diff engine.
3752 39449a05 2020-07-23 stsp */
3753 39449a05 2020-07-23 stsp static const struct got_error *
3754 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3755 39449a05 2020-07-23 stsp const char *abspath)
3756 39449a05 2020-07-23 stsp {
3757 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3758 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3759 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3760 39449a05 2020-07-23 stsp
3761 39449a05 2020-07-23 stsp *fd = -1;
3762 39449a05 2020-07-23 stsp
3763 39449a05 2020-07-23 stsp if (dirfd != -1) {
3764 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3765 39449a05 2020-07-23 stsp if (target_len == -1)
3766 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3767 39449a05 2020-07-23 stsp } else {
3768 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3769 39449a05 2020-07-23 stsp if (target_len == -1)
3770 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3771 39449a05 2020-07-23 stsp }
3772 39449a05 2020-07-23 stsp
3773 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3774 39449a05 2020-07-23 stsp if (*fd == -1)
3775 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3776 39449a05 2020-07-23 stsp
3777 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3778 39449a05 2020-07-23 stsp if (outlen == -1) {
3779 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3780 39449a05 2020-07-23 stsp goto done;
3781 39449a05 2020-07-23 stsp }
3782 39449a05 2020-07-23 stsp
3783 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3784 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3785 39449a05 2020-07-23 stsp goto done;
3786 39449a05 2020-07-23 stsp }
3787 39449a05 2020-07-23 stsp done:
3788 39449a05 2020-07-23 stsp if (err) {
3789 39449a05 2020-07-23 stsp close(*fd);
3790 39449a05 2020-07-23 stsp *fd = -1;
3791 39449a05 2020-07-23 stsp }
3792 39449a05 2020-07-23 stsp return err;
3793 39449a05 2020-07-23 stsp }
3794 b72f483a 2019-02-05 stsp
3795 4ed7e80c 2018-05-20 stsp static const struct got_error *
3796 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3797 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3798 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3799 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3800 b72f483a 2019-02-05 stsp {
3801 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3802 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3803 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3804 12463d8b 2019-12-13 stsp int fd = -1;
3805 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3806 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3807 b72f483a 2019-02-05 stsp struct stat sb;
3808 b72f483a 2019-02-05 stsp
3809 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3810 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3811 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3812 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3813 98eaaa12 2019-08-03 stsp return NULL;
3814 98eaaa12 2019-08-03 stsp } else {
3815 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3816 98eaaa12 2019-08-03 stsp return NULL;
3817 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3818 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3819 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3820 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3821 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3822 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3823 98eaaa12 2019-08-03 stsp return NULL;
3824 98eaaa12 2019-08-03 stsp }
3825 3fc0c068 2019-02-10 stsp
3826 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3827 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3828 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3829 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3830 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3831 3fc0c068 2019-02-10 stsp }
3832 b72f483a 2019-02-05 stsp
3833 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3834 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3835 98eaaa12 2019-08-03 stsp switch (staged_status) {
3836 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3837 98eaaa12 2019-08-03 stsp label1 = path;
3838 98eaaa12 2019-08-03 stsp label2 = path;
3839 98eaaa12 2019-08-03 stsp break;
3840 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3841 98eaaa12 2019-08-03 stsp label2 = path;
3842 98eaaa12 2019-08-03 stsp break;
3843 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3844 98eaaa12 2019-08-03 stsp label1 = path;
3845 98eaaa12 2019-08-03 stsp break;
3846 98eaaa12 2019-08-03 stsp default:
3847 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3848 98eaaa12 2019-08-03 stsp }
3849 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3850 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3851 63035f9f 2019-10-06 stsp a->repo, stdout);
3852 98eaaa12 2019-08-03 stsp }
3853 98eaaa12 2019-08-03 stsp
3854 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3855 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3856 4ce46740 2019-08-08 stsp char *id_str;
3857 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3858 408b4ebc 2019-08-03 stsp 8192);
3859 4ce46740 2019-08-08 stsp if (err)
3860 4ce46740 2019-08-08 stsp goto done;
3861 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3862 4ce46740 2019-08-08 stsp if (err)
3863 4ce46740 2019-08-08 stsp goto done;
3864 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3865 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3866 4ce46740 2019-08-08 stsp free(id_str);
3867 4ce46740 2019-08-08 stsp goto done;
3868 4ce46740 2019-08-08 stsp }
3869 4ce46740 2019-08-08 stsp free(id_str);
3870 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3871 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3872 4ce46740 2019-08-08 stsp if (err)
3873 4ce46740 2019-08-08 stsp goto done;
3874 4ce46740 2019-08-08 stsp }
3875 d00136be 2019-03-26 stsp
3876 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3877 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3878 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3879 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3880 2ec1f75b 2019-03-26 stsp goto done;
3881 2ec1f75b 2019-03-26 stsp }
3882 b72f483a 2019-02-05 stsp
3883 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3884 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3885 12463d8b 2019-12-13 stsp if (fd == -1) {
3886 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3887 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
3888 39449a05 2020-07-23 stsp abspath);
3889 39449a05 2020-07-23 stsp goto done;
3890 39449a05 2020-07-23 stsp }
3891 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3892 39449a05 2020-07-23 stsp de_name, abspath);
3893 39449a05 2020-07-23 stsp if (err)
3894 39449a05 2020-07-23 stsp goto done;
3895 12463d8b 2019-12-13 stsp }
3896 12463d8b 2019-12-13 stsp } else {
3897 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3898 12463d8b 2019-12-13 stsp if (fd == -1) {
3899 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3900 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
3901 39449a05 2020-07-23 stsp abspath);
3902 39449a05 2020-07-23 stsp goto done;
3903 39449a05 2020-07-23 stsp }
3904 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3905 39449a05 2020-07-23 stsp de_name, abspath);
3906 39449a05 2020-07-23 stsp if (err)
3907 39449a05 2020-07-23 stsp goto done;
3908 12463d8b 2019-12-13 stsp }
3909 12463d8b 2019-12-13 stsp }
3910 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3911 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3912 2ec1f75b 2019-03-26 stsp goto done;
3913 2ec1f75b 2019-03-26 stsp }
3914 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3915 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3916 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3917 2ec1f75b 2019-03-26 stsp goto done;
3918 2ec1f75b 2019-03-26 stsp }
3919 12463d8b 2019-12-13 stsp fd = -1;
3920 2ec1f75b 2019-03-26 stsp } else
3921 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3922 b72f483a 2019-02-05 stsp
3923 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3924 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3925 b72f483a 2019-02-05 stsp done:
3926 b72f483a 2019-02-05 stsp if (blob1)
3927 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3928 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3929 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3930 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3931 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3932 b72f483a 2019-02-05 stsp free(abspath);
3933 927df6b7 2019-02-10 stsp return err;
3934 927df6b7 2019-02-10 stsp }
3935 927df6b7 2019-02-10 stsp
3936 927df6b7 2019-02-10 stsp static const struct got_error *
3937 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3938 b00d56cd 2018-04-01 stsp {
3939 b00d56cd 2018-04-01 stsp const struct got_error *error;
3940 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3941 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3942 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3943 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3944 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3945 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3946 15a94983 2018-12-23 stsp int type1, type2;
3947 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3948 c0cc5c62 2018-10-18 stsp const char *errstr;
3949 927df6b7 2019-02-10 stsp char *path = NULL;
3950 b00d56cd 2018-04-01 stsp
3951 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3952 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3953 25eccc22 2019-01-04 stsp NULL) == -1)
3954 b00d56cd 2018-04-01 stsp err(1, "pledge");
3955 b00d56cd 2018-04-01 stsp #endif
3956 b00d56cd 2018-04-01 stsp
3957 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3958 b00d56cd 2018-04-01 stsp switch (ch) {
3959 c0cc5c62 2018-10-18 stsp case 'C':
3960 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3961 dfcab68b 2019-11-29 kn &errstr);
3962 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3963 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3964 c0cc5c62 2018-10-18 stsp break;
3965 b72f483a 2019-02-05 stsp case 'r':
3966 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3967 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3968 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3969 9ba1d308 2019-10-21 stsp optarg);
3970 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3971 b72f483a 2019-02-05 stsp break;
3972 98eaaa12 2019-08-03 stsp case 's':
3973 98eaaa12 2019-08-03 stsp diff_staged = 1;
3974 63035f9f 2019-10-06 stsp break;
3975 63035f9f 2019-10-06 stsp case 'w':
3976 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3977 98eaaa12 2019-08-03 stsp break;
3978 b00d56cd 2018-04-01 stsp default:
3979 2deda0b9 2019-03-07 stsp usage_diff();
3980 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3981 b00d56cd 2018-04-01 stsp }
3982 b00d56cd 2018-04-01 stsp }
3983 b00d56cd 2018-04-01 stsp
3984 b00d56cd 2018-04-01 stsp argc -= optind;
3985 b00d56cd 2018-04-01 stsp argv += optind;
3986 b00d56cd 2018-04-01 stsp
3987 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3988 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3989 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3990 b72f483a 2019-02-05 stsp goto done;
3991 b72f483a 2019-02-05 stsp }
3992 927df6b7 2019-02-10 stsp if (argc <= 1) {
3993 b72f483a 2019-02-05 stsp if (repo_path)
3994 b72f483a 2019-02-05 stsp errx(1,
3995 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3996 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3997 fa51e947 2020-03-27 stsp if (error) {
3998 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3999 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4000 fa51e947 2020-03-27 stsp cwd);
4001 1f03b8da 2020-03-20 stsp goto done;
4002 fa51e947 2020-03-27 stsp }
4003 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4004 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4005 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4006 927df6b7 2019-02-10 stsp goto done;
4007 927df6b7 2019-02-10 stsp }
4008 927df6b7 2019-02-10 stsp if (argc == 1) {
4009 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4010 cbd1af7a 2019-03-18 stsp argv[0]);
4011 927df6b7 2019-02-10 stsp if (error)
4012 927df6b7 2019-02-10 stsp goto done;
4013 927df6b7 2019-02-10 stsp } else {
4014 927df6b7 2019-02-10 stsp path = strdup("");
4015 927df6b7 2019-02-10 stsp if (path == NULL) {
4016 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4017 927df6b7 2019-02-10 stsp goto done;
4018 927df6b7 2019-02-10 stsp }
4019 927df6b7 2019-02-10 stsp }
4020 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4021 98eaaa12 2019-08-03 stsp if (diff_staged)
4022 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4023 98eaaa12 2019-08-03 stsp "objects in repository");
4024 15a94983 2018-12-23 stsp id_str1 = argv[0];
4025 15a94983 2018-12-23 stsp id_str2 = argv[1];
4026 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4027 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4028 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4029 30db809c 2019-06-05 stsp goto done;
4030 1f03b8da 2020-03-20 stsp if (worktree) {
4031 1f03b8da 2020-03-20 stsp repo_path = strdup(
4032 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4033 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4034 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4035 1f03b8da 2020-03-20 stsp goto done;
4036 1f03b8da 2020-03-20 stsp }
4037 1f03b8da 2020-03-20 stsp } else {
4038 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4039 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4040 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4041 1f03b8da 2020-03-20 stsp goto done;
4042 1f03b8da 2020-03-20 stsp }
4043 30db809c 2019-06-05 stsp }
4044 30db809c 2019-06-05 stsp }
4045 b00d56cd 2018-04-01 stsp } else
4046 b00d56cd 2018-04-01 stsp usage_diff();
4047 b00d56cd 2018-04-01 stsp
4048 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4049 76089277 2018-04-01 stsp free(repo_path);
4050 b00d56cd 2018-04-01 stsp if (error != NULL)
4051 b00d56cd 2018-04-01 stsp goto done;
4052 b00d56cd 2018-04-01 stsp
4053 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4054 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4055 c02c541e 2019-03-29 stsp if (error)
4056 c02c541e 2019-03-29 stsp goto done;
4057 c02c541e 2019-03-29 stsp
4058 30db809c 2019-06-05 stsp if (argc <= 1) {
4059 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4060 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4061 b72f483a 2019-02-05 stsp char *id_str;
4062 72ea6654 2019-07-27 stsp
4063 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4064 72ea6654 2019-07-27 stsp
4065 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4066 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4067 b72f483a 2019-02-05 stsp if (error)
4068 b72f483a 2019-02-05 stsp goto done;
4069 b72f483a 2019-02-05 stsp arg.repo = repo;
4070 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4071 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4072 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4073 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4074 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4075 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4076 b72f483a 2019-02-05 stsp
4077 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4078 72ea6654 2019-07-27 stsp if (error)
4079 72ea6654 2019-07-27 stsp goto done;
4080 72ea6654 2019-07-27 stsp
4081 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4082 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4083 b72f483a 2019-02-05 stsp free(id_str);
4084 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4085 b72f483a 2019-02-05 stsp goto done;
4086 b72f483a 2019-02-05 stsp }
4087 b72f483a 2019-02-05 stsp
4088 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4089 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4090 0adb7196 2019-08-11 stsp if (error)
4091 0adb7196 2019-08-11 stsp goto done;
4092 b00d56cd 2018-04-01 stsp
4093 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4094 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4095 0adb7196 2019-08-11 stsp if (error)
4096 0adb7196 2019-08-11 stsp goto done;
4097 b00d56cd 2018-04-01 stsp
4098 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4099 15a94983 2018-12-23 stsp if (error)
4100 15a94983 2018-12-23 stsp goto done;
4101 15a94983 2018-12-23 stsp
4102 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4103 15a94983 2018-12-23 stsp if (error)
4104 15a94983 2018-12-23 stsp goto done;
4105 15a94983 2018-12-23 stsp
4106 15a94983 2018-12-23 stsp if (type1 != type2) {
4107 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4108 b00d56cd 2018-04-01 stsp goto done;
4109 b00d56cd 2018-04-01 stsp }
4110 b00d56cd 2018-04-01 stsp
4111 15a94983 2018-12-23 stsp switch (type1) {
4112 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4113 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4114 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4115 b00d56cd 2018-04-01 stsp break;
4116 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4117 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4118 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4119 b00d56cd 2018-04-01 stsp break;
4120 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4121 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4122 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4123 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4124 b00d56cd 2018-04-01 stsp break;
4125 b00d56cd 2018-04-01 stsp default:
4126 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4127 b00d56cd 2018-04-01 stsp }
4128 b00d56cd 2018-04-01 stsp done:
4129 5e70831e 2019-05-28 stsp free(label1);
4130 5e70831e 2019-05-28 stsp free(label2);
4131 15a94983 2018-12-23 stsp free(id1);
4132 15a94983 2018-12-23 stsp free(id2);
4133 927df6b7 2019-02-10 stsp free(path);
4134 b72f483a 2019-02-05 stsp if (worktree)
4135 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4136 ad242220 2018-09-08 stsp if (repo) {
4137 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4138 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4139 ad242220 2018-09-08 stsp if (error == NULL)
4140 ad242220 2018-09-08 stsp error = repo_error;
4141 ad242220 2018-09-08 stsp }
4142 b00d56cd 2018-04-01 stsp return error;
4143 404c43c4 2018-06-21 stsp }
4144 404c43c4 2018-06-21 stsp
4145 404c43c4 2018-06-21 stsp __dead static void
4146 404c43c4 2018-06-21 stsp usage_blame(void)
4147 404c43c4 2018-06-21 stsp {
4148 9270e621 2019-02-05 stsp fprintf(stderr,
4149 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4150 404c43c4 2018-06-21 stsp getprogname());
4151 404c43c4 2018-06-21 stsp exit(1);
4152 b00d56cd 2018-04-01 stsp }
4153 b00d56cd 2018-04-01 stsp
4154 28315671 2019-08-14 stsp struct blame_line {
4155 28315671 2019-08-14 stsp int annotated;
4156 28315671 2019-08-14 stsp char *id_str;
4157 82f6abb8 2019-08-14 stsp char *committer;
4158 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4159 28315671 2019-08-14 stsp };
4160 28315671 2019-08-14 stsp
4161 28315671 2019-08-14 stsp struct blame_cb_args {
4162 28315671 2019-08-14 stsp struct blame_line *lines;
4163 28315671 2019-08-14 stsp int nlines;
4164 7ef28ff8 2019-08-14 stsp int nlines_prec;
4165 28315671 2019-08-14 stsp int lineno_cur;
4166 28315671 2019-08-14 stsp off_t *line_offsets;
4167 28315671 2019-08-14 stsp FILE *f;
4168 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4169 28315671 2019-08-14 stsp };
4170 28315671 2019-08-14 stsp
4171 404c43c4 2018-06-21 stsp static const struct got_error *
4172 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4173 28315671 2019-08-14 stsp {
4174 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4175 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4176 28315671 2019-08-14 stsp struct blame_line *bline;
4177 82f6abb8 2019-08-14 stsp char *line = NULL;
4178 28315671 2019-08-14 stsp size_t linesize = 0;
4179 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4180 28315671 2019-08-14 stsp off_t offset;
4181 bcb49d15 2019-08-14 stsp struct tm tm;
4182 bcb49d15 2019-08-14 stsp time_t committer_time;
4183 28315671 2019-08-14 stsp
4184 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4185 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4186 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4187 28315671 2019-08-14 stsp
4188 28315671 2019-08-14 stsp if (sigint_received)
4189 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4190 28315671 2019-08-14 stsp
4191 2839f8b9 2019-08-18 stsp if (lineno == -1)
4192 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4193 2839f8b9 2019-08-18 stsp
4194 28315671 2019-08-14 stsp /* Annotate this line. */
4195 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4196 28315671 2019-08-14 stsp if (bline->annotated)
4197 28315671 2019-08-14 stsp return NULL;
4198 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4199 28315671 2019-08-14 stsp if (err)
4200 28315671 2019-08-14 stsp return err;
4201 82f6abb8 2019-08-14 stsp
4202 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4203 82f6abb8 2019-08-14 stsp if (err)
4204 82f6abb8 2019-08-14 stsp goto done;
4205 82f6abb8 2019-08-14 stsp
4206 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4207 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4208 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4209 bcb49d15 2019-08-14 stsp goto done;
4210 bcb49d15 2019-08-14 stsp }
4211 bcb49d15 2019-08-14 stsp
4212 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4213 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4214 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4215 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4216 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4217 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4218 82f6abb8 2019-08-14 stsp goto done;
4219 82f6abb8 2019-08-14 stsp }
4220 28315671 2019-08-14 stsp bline->annotated = 1;
4221 28315671 2019-08-14 stsp
4222 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4223 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4224 28315671 2019-08-14 stsp if (!bline->annotated)
4225 82f6abb8 2019-08-14 stsp goto done;
4226 28315671 2019-08-14 stsp
4227 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4228 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4229 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4230 82f6abb8 2019-08-14 stsp goto done;
4231 82f6abb8 2019-08-14 stsp }
4232 28315671 2019-08-14 stsp
4233 28315671 2019-08-14 stsp while (bline->annotated) {
4234 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4235 82f6abb8 2019-08-14 stsp size_t len;
4236 82f6abb8 2019-08-14 stsp
4237 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4238 28315671 2019-08-14 stsp if (ferror(a->f))
4239 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4240 28315671 2019-08-14 stsp break;
4241 28315671 2019-08-14 stsp }
4242 82f6abb8 2019-08-14 stsp
4243 82f6abb8 2019-08-14 stsp committer = bline->committer;
4244 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4245 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4246 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4247 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4248 82f6abb8 2019-08-14 stsp if (at)
4249 82f6abb8 2019-08-14 stsp *at = '\0';
4250 82f6abb8 2019-08-14 stsp len = strlen(committer);
4251 82f6abb8 2019-08-14 stsp if (len >= 9)
4252 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4253 28315671 2019-08-14 stsp
4254 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4255 28315671 2019-08-14 stsp if (nl)
4256 28315671 2019-08-14 stsp *nl = '\0';
4257 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4258 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4259 28315671 2019-08-14 stsp
4260 28315671 2019-08-14 stsp a->lineno_cur++;
4261 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4262 28315671 2019-08-14 stsp }
4263 82f6abb8 2019-08-14 stsp done:
4264 82f6abb8 2019-08-14 stsp if (commit)
4265 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4266 28315671 2019-08-14 stsp free(line);
4267 28315671 2019-08-14 stsp return err;
4268 28315671 2019-08-14 stsp }
4269 28315671 2019-08-14 stsp
4270 28315671 2019-08-14 stsp static const struct got_error *
4271 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4272 404c43c4 2018-06-21 stsp {
4273 404c43c4 2018-06-21 stsp const struct got_error *error;
4274 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4275 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4276 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4277 0587e10c 2020-07-23 stsp char *link_target = NULL;
4278 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4279 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4280 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4281 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4282 28315671 2019-08-14 stsp struct blame_cb_args bca;
4283 28315671 2019-08-14 stsp int ch, obj_type, i;
4284 b02560ec 2019-08-19 stsp size_t filesize;
4285 90f3c347 2019-08-19 stsp
4286 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4287 404c43c4 2018-06-21 stsp
4288 404c43c4 2018-06-21 stsp #ifndef PROFILE
4289 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4290 36e2fb66 2019-01-04 stsp NULL) == -1)
4291 404c43c4 2018-06-21 stsp err(1, "pledge");
4292 404c43c4 2018-06-21 stsp #endif
4293 404c43c4 2018-06-21 stsp
4294 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4295 404c43c4 2018-06-21 stsp switch (ch) {
4296 404c43c4 2018-06-21 stsp case 'c':
4297 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4298 404c43c4 2018-06-21 stsp break;
4299 66bea077 2018-08-02 stsp case 'r':
4300 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4301 66bea077 2018-08-02 stsp if (repo_path == NULL)
4302 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4303 9ba1d308 2019-10-21 stsp optarg);
4304 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4305 66bea077 2018-08-02 stsp break;
4306 404c43c4 2018-06-21 stsp default:
4307 2deda0b9 2019-03-07 stsp usage_blame();
4308 404c43c4 2018-06-21 stsp /* NOTREACHED */
4309 404c43c4 2018-06-21 stsp }
4310 404c43c4 2018-06-21 stsp }
4311 404c43c4 2018-06-21 stsp
4312 404c43c4 2018-06-21 stsp argc -= optind;
4313 404c43c4 2018-06-21 stsp argv += optind;
4314 404c43c4 2018-06-21 stsp
4315 a39318fd 2018-08-02 stsp if (argc == 1)
4316 404c43c4 2018-06-21 stsp path = argv[0];
4317 a39318fd 2018-08-02 stsp else
4318 404c43c4 2018-06-21 stsp usage_blame();
4319 404c43c4 2018-06-21 stsp
4320 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4321 66bea077 2018-08-02 stsp if (cwd == NULL) {
4322 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4323 66bea077 2018-08-02 stsp goto done;
4324 66bea077 2018-08-02 stsp }
4325 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4326 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4327 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4328 66bea077 2018-08-02 stsp goto done;
4329 0c06baac 2019-02-05 stsp else
4330 0c06baac 2019-02-05 stsp error = NULL;
4331 0c06baac 2019-02-05 stsp if (worktree) {
4332 0c06baac 2019-02-05 stsp repo_path =
4333 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4334 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4335 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4336 1f03b8da 2020-03-20 stsp if (error)
4337 1f03b8da 2020-03-20 stsp goto done;
4338 1f03b8da 2020-03-20 stsp }
4339 0c06baac 2019-02-05 stsp } else {
4340 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4341 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4342 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4343 0c06baac 2019-02-05 stsp goto done;
4344 0c06baac 2019-02-05 stsp }
4345 66bea077 2018-08-02 stsp }
4346 66bea077 2018-08-02 stsp }
4347 36e2fb66 2019-01-04 stsp
4348 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4349 c02c541e 2019-03-29 stsp if (error != NULL)
4350 36e2fb66 2019-01-04 stsp goto done;
4351 66bea077 2018-08-02 stsp
4352 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4353 c02c541e 2019-03-29 stsp if (error)
4354 404c43c4 2018-06-21 stsp goto done;
4355 404c43c4 2018-06-21 stsp
4356 0c06baac 2019-02-05 stsp if (worktree) {
4357 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4358 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4359 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4360 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4361 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4362 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4363 6efaaa2d 2019-02-05 stsp path) == -1) {
4364 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4365 0c06baac 2019-02-05 stsp goto done;
4366 0c06baac 2019-02-05 stsp }
4367 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4368 0c06baac 2019-02-05 stsp free(p);
4369 0c06baac 2019-02-05 stsp } else {
4370 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4371 0c06baac 2019-02-05 stsp }
4372 0c06baac 2019-02-05 stsp if (error)
4373 66bea077 2018-08-02 stsp goto done;
4374 66bea077 2018-08-02 stsp
4375 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4376 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4377 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4378 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4379 404c43c4 2018-06-21 stsp if (error != NULL)
4380 66bea077 2018-08-02 stsp goto done;
4381 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4382 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4383 404c43c4 2018-06-21 stsp if (error != NULL)
4384 66bea077 2018-08-02 stsp goto done;
4385 404c43c4 2018-06-21 stsp } else {
4386 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4387 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4388 30837e32 2019-07-25 stsp if (error)
4389 66bea077 2018-08-02 stsp goto done;
4390 404c43c4 2018-06-21 stsp }
4391 404c43c4 2018-06-21 stsp
4392 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4393 0587e10c 2020-07-23 stsp commit_id, repo);
4394 0587e10c 2020-07-23 stsp if (error)
4395 0587e10c 2020-07-23 stsp goto done;
4396 0587e10c 2020-07-23 stsp
4397 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4398 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4399 28315671 2019-08-14 stsp if (error)
4400 28315671 2019-08-14 stsp goto done;
4401 28315671 2019-08-14 stsp
4402 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4403 28315671 2019-08-14 stsp if (error)
4404 28315671 2019-08-14 stsp goto done;
4405 28315671 2019-08-14 stsp
4406 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4407 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4408 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4409 28315671 2019-08-14 stsp goto done;
4410 28315671 2019-08-14 stsp }
4411 28315671 2019-08-14 stsp
4412 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4413 28315671 2019-08-14 stsp if (error)
4414 28315671 2019-08-14 stsp goto done;
4415 28315671 2019-08-14 stsp bca.f = got_opentemp();
4416 28315671 2019-08-14 stsp if (bca.f == NULL) {
4417 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4418 28315671 2019-08-14 stsp goto done;
4419 28315671 2019-08-14 stsp }
4420 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4421 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4422 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4423 28315671 2019-08-14 stsp goto done;
4424 28315671 2019-08-14 stsp
4425 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4426 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4427 b02560ec 2019-08-19 stsp bca.nlines--;
4428 b02560ec 2019-08-19 stsp
4429 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4430 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4431 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4432 28315671 2019-08-14 stsp goto done;
4433 28315671 2019-08-14 stsp }
4434 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4435 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4436 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4437 7ef28ff8 2019-08-14 stsp while (i > 0) {
4438 7ef28ff8 2019-08-14 stsp i /= 10;
4439 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4440 7ef28ff8 2019-08-14 stsp }
4441 82f6abb8 2019-08-14 stsp bca.repo = repo;
4442 7ef28ff8 2019-08-14 stsp
4443 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4444 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4445 404c43c4 2018-06-21 stsp done:
4446 66bea077 2018-08-02 stsp free(in_repo_path);
4447 0587e10c 2020-07-23 stsp free(link_target);
4448 66bea077 2018-08-02 stsp free(repo_path);
4449 66bea077 2018-08-02 stsp free(cwd);
4450 404c43c4 2018-06-21 stsp free(commit_id);
4451 28315671 2019-08-14 stsp free(obj_id);
4452 28315671 2019-08-14 stsp if (blob)
4453 28315671 2019-08-14 stsp got_object_blob_close(blob);
4454 0c06baac 2019-02-05 stsp if (worktree)
4455 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4456 ad242220 2018-09-08 stsp if (repo) {
4457 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4458 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4459 ad242220 2018-09-08 stsp if (error == NULL)
4460 ad242220 2018-09-08 stsp error = repo_error;
4461 ad242220 2018-09-08 stsp }
4462 3affba96 2019-09-22 stsp if (bca.lines) {
4463 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4464 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4465 3affba96 2019-09-22 stsp free(bline->id_str);
4466 3affba96 2019-09-22 stsp free(bline->committer);
4467 3affba96 2019-09-22 stsp }
4468 3affba96 2019-09-22 stsp free(bca.lines);
4469 28315671 2019-08-14 stsp }
4470 28315671 2019-08-14 stsp free(bca.line_offsets);
4471 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4472 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4473 404c43c4 2018-06-21 stsp return error;
4474 5de5890b 2018-10-18 stsp }
4475 5de5890b 2018-10-18 stsp
4476 5de5890b 2018-10-18 stsp __dead static void
4477 5de5890b 2018-10-18 stsp usage_tree(void)
4478 5de5890b 2018-10-18 stsp {
4479 c1669e2e 2019-01-09 stsp fprintf(stderr,
4480 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4481 5de5890b 2018-10-18 stsp getprogname());
4482 5de5890b 2018-10-18 stsp exit(1);
4483 5de5890b 2018-10-18 stsp }
4484 5de5890b 2018-10-18 stsp
4485 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4486 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4487 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4488 c1669e2e 2019-01-09 stsp {
4489 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4490 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4491 848d6979 2019-08-12 stsp const char *modestr = "";
4492 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4493 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4494 5de5890b 2018-10-18 stsp
4495 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4496 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4497 c1669e2e 2019-01-09 stsp path++;
4498 c1669e2e 2019-01-09 stsp
4499 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4500 63c5ca5d 2019-08-24 stsp modestr = "$";
4501 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4502 0d6c6ee3 2020-05-20 stsp int i;
4503 0d6c6ee3 2020-05-20 stsp
4504 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4505 0d6c6ee3 2020-05-20 stsp if (err)
4506 0d6c6ee3 2020-05-20 stsp return err;
4507 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4508 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4509 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4510 0d6c6ee3 2020-05-20 stsp }
4511 0d6c6ee3 2020-05-20 stsp
4512 848d6979 2019-08-12 stsp modestr = "@";
4513 0d6c6ee3 2020-05-20 stsp }
4514 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4515 848d6979 2019-08-12 stsp modestr = "/";
4516 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4517 848d6979 2019-08-12 stsp modestr = "*";
4518 848d6979 2019-08-12 stsp
4519 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4520 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4521 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4522 0d6c6ee3 2020-05-20 stsp
4523 0d6c6ee3 2020-05-20 stsp free(link_target);
4524 0d6c6ee3 2020-05-20 stsp return NULL;
4525 c1669e2e 2019-01-09 stsp }
4526 c1669e2e 2019-01-09 stsp
4527 5de5890b 2018-10-18 stsp static const struct got_error *
4528 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4529 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4530 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4531 5de5890b 2018-10-18 stsp {
4532 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4533 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4534 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4535 56e0773d 2019-11-28 stsp int nentries, i;
4536 5de5890b 2018-10-18 stsp
4537 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4538 5de5890b 2018-10-18 stsp if (err)
4539 5de5890b 2018-10-18 stsp goto done;
4540 5de5890b 2018-10-18 stsp
4541 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4542 5de5890b 2018-10-18 stsp if (err)
4543 5de5890b 2018-10-18 stsp goto done;
4544 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4545 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4546 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4547 5de5890b 2018-10-18 stsp char *id = NULL;
4548 84453469 2018-11-11 stsp
4549 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4550 84453469 2018-11-11 stsp break;
4551 84453469 2018-11-11 stsp
4552 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4553 5de5890b 2018-10-18 stsp if (show_ids) {
4554 5de5890b 2018-10-18 stsp char *id_str;
4555 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4556 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4557 5de5890b 2018-10-18 stsp if (err)
4558 5de5890b 2018-10-18 stsp goto done;
4559 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4560 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4561 5de5890b 2018-10-18 stsp free(id_str);
4562 5de5890b 2018-10-18 stsp goto done;
4563 5de5890b 2018-10-18 stsp }
4564 5de5890b 2018-10-18 stsp free(id_str);
4565 5de5890b 2018-10-18 stsp }
4566 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4567 5de5890b 2018-10-18 stsp free(id);
4568 0d6c6ee3 2020-05-20 stsp if (err)
4569 0d6c6ee3 2020-05-20 stsp goto done;
4570 c1669e2e 2019-01-09 stsp
4571 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4572 c1669e2e 2019-01-09 stsp char *child_path;
4573 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4574 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4575 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4576 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4577 c1669e2e 2019-01-09 stsp goto done;
4578 c1669e2e 2019-01-09 stsp }
4579 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4580 c1669e2e 2019-01-09 stsp root_path, repo);
4581 c1669e2e 2019-01-09 stsp free(child_path);
4582 c1669e2e 2019-01-09 stsp if (err)
4583 c1669e2e 2019-01-09 stsp goto done;
4584 c1669e2e 2019-01-09 stsp }
4585 5de5890b 2018-10-18 stsp }
4586 5de5890b 2018-10-18 stsp done:
4587 5de5890b 2018-10-18 stsp if (tree)
4588 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4589 5de5890b 2018-10-18 stsp free(tree_id);
4590 5de5890b 2018-10-18 stsp return err;
4591 404c43c4 2018-06-21 stsp }
4592 404c43c4 2018-06-21 stsp
4593 5de5890b 2018-10-18 stsp static const struct got_error *
4594 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4595 5de5890b 2018-10-18 stsp {
4596 5de5890b 2018-10-18 stsp const struct got_error *error;
4597 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4598 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4599 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4600 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4601 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4602 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4603 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4604 5de5890b 2018-10-18 stsp int ch;
4605 5de5890b 2018-10-18 stsp
4606 5de5890b 2018-10-18 stsp #ifndef PROFILE
4607 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4608 0f8d269b 2019-01-04 stsp NULL) == -1)
4609 5de5890b 2018-10-18 stsp err(1, "pledge");
4610 5de5890b 2018-10-18 stsp #endif
4611 5de5890b 2018-10-18 stsp
4612 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4613 5de5890b 2018-10-18 stsp switch (ch) {
4614 5de5890b 2018-10-18 stsp case 'c':
4615 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4616 5de5890b 2018-10-18 stsp break;
4617 5de5890b 2018-10-18 stsp case 'r':
4618 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4619 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4620 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4621 9ba1d308 2019-10-21 stsp optarg);
4622 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4623 5de5890b 2018-10-18 stsp break;
4624 5de5890b 2018-10-18 stsp case 'i':
4625 5de5890b 2018-10-18 stsp show_ids = 1;
4626 5de5890b 2018-10-18 stsp break;
4627 c1669e2e 2019-01-09 stsp case 'R':
4628 c1669e2e 2019-01-09 stsp recurse = 1;
4629 c1669e2e 2019-01-09 stsp break;
4630 5de5890b 2018-10-18 stsp default:
4631 2deda0b9 2019-03-07 stsp usage_tree();
4632 5de5890b 2018-10-18 stsp /* NOTREACHED */
4633 5de5890b 2018-10-18 stsp }
4634 5de5890b 2018-10-18 stsp }
4635 5de5890b 2018-10-18 stsp
4636 5de5890b 2018-10-18 stsp argc -= optind;
4637 5de5890b 2018-10-18 stsp argv += optind;
4638 5de5890b 2018-10-18 stsp
4639 5de5890b 2018-10-18 stsp if (argc == 1)
4640 5de5890b 2018-10-18 stsp path = argv[0];
4641 5de5890b 2018-10-18 stsp else if (argc > 1)
4642 5de5890b 2018-10-18 stsp usage_tree();
4643 5de5890b 2018-10-18 stsp else
4644 7a2c19d6 2019-02-05 stsp path = NULL;
4645 7a2c19d6 2019-02-05 stsp
4646 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4647 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4648 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4649 9bf7a39b 2019-02-05 stsp goto done;
4650 9bf7a39b 2019-02-05 stsp }
4651 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4652 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4653 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4654 7a2c19d6 2019-02-05 stsp goto done;
4655 7a2c19d6 2019-02-05 stsp else
4656 7a2c19d6 2019-02-05 stsp error = NULL;
4657 7a2c19d6 2019-02-05 stsp if (worktree) {
4658 7a2c19d6 2019-02-05 stsp repo_path =
4659 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4660 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4661 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4662 7a2c19d6 2019-02-05 stsp if (error)
4663 7a2c19d6 2019-02-05 stsp goto done;
4664 7a2c19d6 2019-02-05 stsp } else {
4665 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4666 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4667 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4668 7a2c19d6 2019-02-05 stsp goto done;
4669 7a2c19d6 2019-02-05 stsp }
4670 5de5890b 2018-10-18 stsp }
4671 5de5890b 2018-10-18 stsp }
4672 5de5890b 2018-10-18 stsp
4673 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4674 5de5890b 2018-10-18 stsp if (error != NULL)
4675 c02c541e 2019-03-29 stsp goto done;
4676 c02c541e 2019-03-29 stsp
4677 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4678 c02c541e 2019-03-29 stsp if (error)
4679 5de5890b 2018-10-18 stsp goto done;
4680 5de5890b 2018-10-18 stsp
4681 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4682 9bf7a39b 2019-02-05 stsp if (worktree) {
4683 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4684 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4685 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4686 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4687 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4688 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4689 9bf7a39b 2019-02-05 stsp goto done;
4690 9bf7a39b 2019-02-05 stsp }
4691 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4692 9bf7a39b 2019-02-05 stsp free(p);
4693 9bf7a39b 2019-02-05 stsp if (error)
4694 9bf7a39b 2019-02-05 stsp goto done;
4695 9bf7a39b 2019-02-05 stsp } else
4696 9bf7a39b 2019-02-05 stsp path = "/";
4697 9bf7a39b 2019-02-05 stsp }
4698 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4699 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4700 9bf7a39b 2019-02-05 stsp if (error != NULL)
4701 9bf7a39b 2019-02-05 stsp goto done;
4702 9bf7a39b 2019-02-05 stsp }
4703 5de5890b 2018-10-18 stsp
4704 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4705 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4706 4e0a20a4 2020-03-23 tracey if (worktree)
4707 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4708 4e0a20a4 2020-03-23 tracey else
4709 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4710 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4711 5de5890b 2018-10-18 stsp if (error != NULL)
4712 5de5890b 2018-10-18 stsp goto done;
4713 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4714 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4715 5de5890b 2018-10-18 stsp if (error != NULL)
4716 5de5890b 2018-10-18 stsp goto done;
4717 5de5890b 2018-10-18 stsp } else {
4718 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4719 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4720 30837e32 2019-07-25 stsp if (error)
4721 5de5890b 2018-10-18 stsp goto done;
4722 5de5890b 2018-10-18 stsp }
4723 5de5890b 2018-10-18 stsp
4724 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4725 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4726 5de5890b 2018-10-18 stsp done:
4727 5de5890b 2018-10-18 stsp free(in_repo_path);
4728 5de5890b 2018-10-18 stsp free(repo_path);
4729 5de5890b 2018-10-18 stsp free(cwd);
4730 5de5890b 2018-10-18 stsp free(commit_id);
4731 7a2c19d6 2019-02-05 stsp if (worktree)
4732 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4733 5de5890b 2018-10-18 stsp if (repo) {
4734 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4735 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4736 5de5890b 2018-10-18 stsp if (error == NULL)
4737 5de5890b 2018-10-18 stsp error = repo_error;
4738 5de5890b 2018-10-18 stsp }
4739 5de5890b 2018-10-18 stsp return error;
4740 5de5890b 2018-10-18 stsp }
4741 5de5890b 2018-10-18 stsp
4742 6bad629b 2019-02-04 stsp __dead static void
4743 6bad629b 2019-02-04 stsp usage_status(void)
4744 6bad629b 2019-02-04 stsp {
4745 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4746 081470ac 2020-08-13 stsp getprogname());
4747 6bad629b 2019-02-04 stsp exit(1);
4748 6bad629b 2019-02-04 stsp }
4749 5c860e29 2018-03-12 stsp
4750 b72f483a 2019-02-05 stsp static const struct got_error *
4751 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4752 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4753 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4754 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4755 6bad629b 2019-02-04 stsp {
4756 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4757 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4758 081470ac 2020-08-13 stsp if (arg) {
4759 081470ac 2020-08-13 stsp char *status_codes = arg;
4760 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4761 081470ac 2020-08-13 stsp int i;
4762 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4763 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4764 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4765 081470ac 2020-08-13 stsp break;
4766 081470ac 2020-08-13 stsp }
4767 081470ac 2020-08-13 stsp if (i == ncodes)
4768 081470ac 2020-08-13 stsp return NULL;
4769 081470ac 2020-08-13 stsp }
4770 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4771 b72f483a 2019-02-05 stsp return NULL;
4772 6bad629b 2019-02-04 stsp }
4773 5c860e29 2018-03-12 stsp
4774 6bad629b 2019-02-04 stsp static const struct got_error *
4775 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4776 6bad629b 2019-02-04 stsp {
4777 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4778 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4779 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4780 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4781 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4782 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4783 081470ac 2020-08-13 stsp int ch, i;
4784 5c860e29 2018-03-12 stsp
4785 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4786 72ea6654 2019-07-27 stsp
4787 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4788 6bad629b 2019-02-04 stsp switch (ch) {
4789 081470ac 2020-08-13 stsp case 's':
4790 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4791 081470ac 2020-08-13 stsp switch (optarg[i]) {
4792 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4793 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4794 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4795 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4796 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4797 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4798 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4799 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4800 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4801 081470ac 2020-08-13 stsp break;
4802 081470ac 2020-08-13 stsp default:
4803 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
4804 081470ac 2020-08-13 stsp optarg[i]);
4805 081470ac 2020-08-13 stsp }
4806 081470ac 2020-08-13 stsp }
4807 081470ac 2020-08-13 stsp status_codes = optarg;
4808 081470ac 2020-08-13 stsp break;
4809 5c860e29 2018-03-12 stsp default:
4810 2deda0b9 2019-03-07 stsp usage_status();
4811 6bad629b 2019-02-04 stsp /* NOTREACHED */
4812 5c860e29 2018-03-12 stsp }
4813 5c860e29 2018-03-12 stsp }
4814 5c860e29 2018-03-12 stsp
4815 6bad629b 2019-02-04 stsp argc -= optind;
4816 6bad629b 2019-02-04 stsp argv += optind;
4817 5c860e29 2018-03-12 stsp
4818 6bad629b 2019-02-04 stsp #ifndef PROFILE
4819 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4820 6bad629b 2019-02-04 stsp NULL) == -1)
4821 6bad629b 2019-02-04 stsp err(1, "pledge");
4822 f42b1b34 2018-03-12 stsp #endif
4823 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4824 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4825 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4826 927df6b7 2019-02-10 stsp goto done;
4827 927df6b7 2019-02-10 stsp }
4828 927df6b7 2019-02-10 stsp
4829 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4830 fa51e947 2020-03-27 stsp if (error) {
4831 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4832 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4833 a5edda0a 2019-07-27 stsp goto done;
4834 fa51e947 2020-03-27 stsp }
4835 6bad629b 2019-02-04 stsp
4836 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4837 c9956ddf 2019-09-08 stsp NULL);
4838 6bad629b 2019-02-04 stsp if (error != NULL)
4839 6bad629b 2019-02-04 stsp goto done;
4840 6bad629b 2019-02-04 stsp
4841 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4842 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4843 087fb88c 2019-08-04 stsp if (error)
4844 087fb88c 2019-08-04 stsp goto done;
4845 087fb88c 2019-08-04 stsp
4846 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4847 6bad629b 2019-02-04 stsp if (error)
4848 6bad629b 2019-02-04 stsp goto done;
4849 6bad629b 2019-02-04 stsp
4850 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4851 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
4852 6bad629b 2019-02-04 stsp done:
4853 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4854 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4855 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4856 927df6b7 2019-02-10 stsp free(cwd);
4857 d0eebce4 2019-03-11 stsp return error;
4858 d0eebce4 2019-03-11 stsp }
4859 d0eebce4 2019-03-11 stsp
4860 d0eebce4 2019-03-11 stsp __dead static void
4861 d0eebce4 2019-03-11 stsp usage_ref(void)
4862 d0eebce4 2019-03-11 stsp {
4863 d0eebce4 2019-03-11 stsp fprintf(stderr,
4864 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4865 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4866 d0eebce4 2019-03-11 stsp getprogname());
4867 d0eebce4 2019-03-11 stsp exit(1);
4868 d0eebce4 2019-03-11 stsp }
4869 d0eebce4 2019-03-11 stsp
4870 d0eebce4 2019-03-11 stsp static const struct got_error *
4871 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4872 d0eebce4 2019-03-11 stsp {
4873 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4874 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4875 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4876 d0eebce4 2019-03-11 stsp
4877 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4878 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4879 d0eebce4 2019-03-11 stsp if (err)
4880 d0eebce4 2019-03-11 stsp return err;
4881 d0eebce4 2019-03-11 stsp
4882 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4883 d0eebce4 2019-03-11 stsp char *refstr;
4884 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4885 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4886 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4887 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4888 d0eebce4 2019-03-11 stsp free(refstr);
4889 d0eebce4 2019-03-11 stsp }
4890 d0eebce4 2019-03-11 stsp
4891 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4892 d0eebce4 2019-03-11 stsp return NULL;
4893 d0eebce4 2019-03-11 stsp }
4894 d0eebce4 2019-03-11 stsp
4895 d0eebce4 2019-03-11 stsp static const struct got_error *
4896 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4897 d0eebce4 2019-03-11 stsp {
4898 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4899 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4900 d0eebce4 2019-03-11 stsp
4901 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4902 d0eebce4 2019-03-11 stsp if (err)
4903 d0eebce4 2019-03-11 stsp return err;
4904 d0eebce4 2019-03-11 stsp
4905 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4906 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4907 d0eebce4 2019-03-11 stsp return err;
4908 d0eebce4 2019-03-11 stsp }
4909 d0eebce4 2019-03-11 stsp
4910 d0eebce4 2019-03-11 stsp static const struct got_error *
4911 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4912 d0eebce4 2019-03-11 stsp {
4913 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4914 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4915 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4916 d1644381 2019-07-14 stsp
4917 d1644381 2019-07-14 stsp /*
4918 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4919 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4920 d1644381 2019-07-14 stsp * an unintended typo.
4921 d1644381 2019-07-14 stsp */
4922 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4923 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4924 d0eebce4 2019-03-11 stsp
4925 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4926 dd88155e 2019-06-29 stsp repo);
4927 d83d9d5c 2019-05-13 stsp if (err) {
4928 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4929 d0eebce4 2019-03-11 stsp
4930 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4931 d83d9d5c 2019-05-13 stsp return err;
4932 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4933 d83d9d5c 2019-05-13 stsp if (err)
4934 d83d9d5c 2019-05-13 stsp return err;
4935 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4936 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4937 d83d9d5c 2019-05-13 stsp if (err)
4938 d83d9d5c 2019-05-13 stsp return err;
4939 d83d9d5c 2019-05-13 stsp }
4940 d83d9d5c 2019-05-13 stsp
4941 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4942 d0eebce4 2019-03-11 stsp if (err)
4943 d0eebce4 2019-03-11 stsp goto done;
4944 d0eebce4 2019-03-11 stsp
4945 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4946 d0eebce4 2019-03-11 stsp done:
4947 d0eebce4 2019-03-11 stsp if (ref)
4948 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4949 d0eebce4 2019-03-11 stsp free(id);
4950 d1c1ae5f 2019-08-12 stsp return err;
4951 d1c1ae5f 2019-08-12 stsp }
4952 d1c1ae5f 2019-08-12 stsp
4953 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4954 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4955 d1c1ae5f 2019-08-12 stsp {
4956 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4957 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4958 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4959 d1c1ae5f 2019-08-12 stsp
4960 d1c1ae5f 2019-08-12 stsp /*
4961 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4962 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4963 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4964 d1c1ae5f 2019-08-12 stsp */
4965 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4966 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4967 d1c1ae5f 2019-08-12 stsp
4968 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4969 d1c1ae5f 2019-08-12 stsp if (err)
4970 d1c1ae5f 2019-08-12 stsp return err;
4971 d1c1ae5f 2019-08-12 stsp
4972 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4973 d1c1ae5f 2019-08-12 stsp if (err)
4974 d1c1ae5f 2019-08-12 stsp goto done;
4975 d1c1ae5f 2019-08-12 stsp
4976 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4977 d1c1ae5f 2019-08-12 stsp done:
4978 d1c1ae5f 2019-08-12 stsp if (target_ref)
4979 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4980 d1c1ae5f 2019-08-12 stsp if (ref)
4981 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4982 d0eebce4 2019-03-11 stsp return err;
4983 d0eebce4 2019-03-11 stsp }
4984 d0eebce4 2019-03-11 stsp
4985 d0eebce4 2019-03-11 stsp static const struct got_error *
4986 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4987 d0eebce4 2019-03-11 stsp {
4988 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4989 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4990 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4991 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4992 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4993 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4994 b2070a3f 2020-03-22 stsp char *refname = NULL;
4995 d0eebce4 2019-03-11 stsp
4996 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4997 d0eebce4 2019-03-11 stsp switch (ch) {
4998 e31abbf2 2020-03-22 stsp case 'c':
4999 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5000 e31abbf2 2020-03-22 stsp break;
5001 d0eebce4 2019-03-11 stsp case 'd':
5002 e31abbf2 2020-03-22 stsp do_delete = 1;
5003 d0eebce4 2019-03-11 stsp break;
5004 d0eebce4 2019-03-11 stsp case 'r':
5005 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5006 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5007 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5008 9ba1d308 2019-10-21 stsp optarg);
5009 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5010 d0eebce4 2019-03-11 stsp break;
5011 d0eebce4 2019-03-11 stsp case 'l':
5012 d0eebce4 2019-03-11 stsp do_list = 1;
5013 d1c1ae5f 2019-08-12 stsp break;
5014 d1c1ae5f 2019-08-12 stsp case 's':
5015 e31abbf2 2020-03-22 stsp symref_target = optarg;
5016 d0eebce4 2019-03-11 stsp break;
5017 d0eebce4 2019-03-11 stsp default:
5018 d0eebce4 2019-03-11 stsp usage_ref();
5019 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5020 d0eebce4 2019-03-11 stsp }
5021 d0eebce4 2019-03-11 stsp }
5022 d0eebce4 2019-03-11 stsp
5023 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5024 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5025 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5026 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5027 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5028 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5029 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5030 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5031 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5032 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5033 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5034 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5035 d0eebce4 2019-03-11 stsp
5036 d0eebce4 2019-03-11 stsp argc -= optind;
5037 d0eebce4 2019-03-11 stsp argv += optind;
5038 d0eebce4 2019-03-11 stsp
5039 e31abbf2 2020-03-22 stsp if (do_list) {
5040 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5041 d0eebce4 2019-03-11 stsp usage_ref();
5042 b2070a3f 2020-03-22 stsp if (argc == 1) {
5043 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5044 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5045 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5046 b2070a3f 2020-03-22 stsp goto done;
5047 b2070a3f 2020-03-22 stsp }
5048 b2070a3f 2020-03-22 stsp }
5049 e31abbf2 2020-03-22 stsp } else {
5050 e31abbf2 2020-03-22 stsp if (argc != 1)
5051 e31abbf2 2020-03-22 stsp usage_ref();
5052 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5053 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5054 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5055 b2070a3f 2020-03-22 stsp goto done;
5056 b2070a3f 2020-03-22 stsp }
5057 e31abbf2 2020-03-22 stsp }
5058 b2070a3f 2020-03-22 stsp
5059 b2070a3f 2020-03-22 stsp if (refname)
5060 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5061 d0eebce4 2019-03-11 stsp
5062 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5063 e0b57350 2019-03-12 stsp if (do_list) {
5064 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5065 e0b57350 2019-03-12 stsp NULL) == -1)
5066 e0b57350 2019-03-12 stsp err(1, "pledge");
5067 e0b57350 2019-03-12 stsp } else {
5068 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5069 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5070 e0b57350 2019-03-12 stsp err(1, "pledge");
5071 e0b57350 2019-03-12 stsp }
5072 d0eebce4 2019-03-11 stsp #endif
5073 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5074 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5075 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5076 d0eebce4 2019-03-11 stsp goto done;
5077 d0eebce4 2019-03-11 stsp }
5078 d0eebce4 2019-03-11 stsp
5079 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5080 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5081 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5082 d0eebce4 2019-03-11 stsp goto done;
5083 d0eebce4 2019-03-11 stsp else
5084 d0eebce4 2019-03-11 stsp error = NULL;
5085 d0eebce4 2019-03-11 stsp if (worktree) {
5086 d0eebce4 2019-03-11 stsp repo_path =
5087 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5088 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5089 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5090 d0eebce4 2019-03-11 stsp if (error)
5091 d0eebce4 2019-03-11 stsp goto done;
5092 d0eebce4 2019-03-11 stsp } else {
5093 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5094 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5095 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5096 d0eebce4 2019-03-11 stsp goto done;
5097 d0eebce4 2019-03-11 stsp }
5098 d0eebce4 2019-03-11 stsp }
5099 d0eebce4 2019-03-11 stsp }
5100 d0eebce4 2019-03-11 stsp
5101 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5102 d0eebce4 2019-03-11 stsp if (error != NULL)
5103 d0eebce4 2019-03-11 stsp goto done;
5104 d0eebce4 2019-03-11 stsp
5105 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5106 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5107 c02c541e 2019-03-29 stsp if (error)
5108 c02c541e 2019-03-29 stsp goto done;
5109 c02c541e 2019-03-29 stsp
5110 d0eebce4 2019-03-11 stsp if (do_list)
5111 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5112 e31abbf2 2020-03-22 stsp else if (do_delete)
5113 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5114 e31abbf2 2020-03-22 stsp else if (symref_target)
5115 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5116 e31abbf2 2020-03-22 stsp else {
5117 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5118 e31abbf2 2020-03-22 stsp usage_ref();
5119 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5120 e31abbf2 2020-03-22 stsp }
5121 4e759de4 2019-06-26 stsp done:
5122 b2070a3f 2020-03-22 stsp free(refname);
5123 4e759de4 2019-06-26 stsp if (repo)
5124 4e759de4 2019-06-26 stsp got_repo_close(repo);
5125 4e759de4 2019-06-26 stsp if (worktree)
5126 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5127 4e759de4 2019-06-26 stsp free(cwd);
5128 4e759de4 2019-06-26 stsp free(repo_path);
5129 4e759de4 2019-06-26 stsp return error;
5130 4e759de4 2019-06-26 stsp }
5131 4e759de4 2019-06-26 stsp
5132 4e759de4 2019-06-26 stsp __dead static void
5133 4e759de4 2019-06-26 stsp usage_branch(void)
5134 4e759de4 2019-06-26 stsp {
5135 4e759de4 2019-06-26 stsp fprintf(stderr,
5136 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5137 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5138 4e759de4 2019-06-26 stsp exit(1);
5139 4e759de4 2019-06-26 stsp }
5140 4e759de4 2019-06-26 stsp
5141 4e759de4 2019-06-26 stsp static const struct got_error *
5142 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5143 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5144 4e99b47e 2019-10-04 stsp {
5145 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5146 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5147 4e99b47e 2019-10-04 stsp char *refstr;
5148 4e99b47e 2019-10-04 stsp
5149 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5150 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5151 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5152 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5153 4e99b47e 2019-10-04 stsp
5154 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5155 4e99b47e 2019-10-04 stsp if (err)
5156 4e99b47e 2019-10-04 stsp return err;
5157 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5158 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5159 4e99b47e 2019-10-04 stsp marker = "* ";
5160 4e99b47e 2019-10-04 stsp else
5161 4e99b47e 2019-10-04 stsp marker = "~ ";
5162 4e99b47e 2019-10-04 stsp free(id);
5163 4e99b47e 2019-10-04 stsp }
5164 4e99b47e 2019-10-04 stsp
5165 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5166 4e99b47e 2019-10-04 stsp refname += 11;
5167 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5168 4e99b47e 2019-10-04 stsp refname += 18;
5169 4e99b47e 2019-10-04 stsp
5170 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5171 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5172 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5173 4e99b47e 2019-10-04 stsp
5174 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5175 4e99b47e 2019-10-04 stsp free(refstr);
5176 4e99b47e 2019-10-04 stsp return NULL;
5177 4e99b47e 2019-10-04 stsp }
5178 4e99b47e 2019-10-04 stsp
5179 4e99b47e 2019-10-04 stsp static const struct got_error *
5180 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5181 ad89fa31 2019-10-04 stsp {
5182 ad89fa31 2019-10-04 stsp const char *refname;
5183 ad89fa31 2019-10-04 stsp
5184 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5185 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5186 ad89fa31 2019-10-04 stsp
5187 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5188 ad89fa31 2019-10-04 stsp
5189 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5190 ad89fa31 2019-10-04 stsp refname += 11;
5191 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5192 ad89fa31 2019-10-04 stsp refname += 18;
5193 ad89fa31 2019-10-04 stsp
5194 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5195 ad89fa31 2019-10-04 stsp
5196 ad89fa31 2019-10-04 stsp return NULL;
5197 ad89fa31 2019-10-04 stsp }
5198 ad89fa31 2019-10-04 stsp
5199 ad89fa31 2019-10-04 stsp static const struct got_error *
5200 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5201 4e759de4 2019-06-26 stsp {
5202 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5203 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5204 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5205 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5206 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5207 4e759de4 2019-06-26 stsp
5208 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5209 ba882ee3 2019-07-11 stsp
5210 4e99b47e 2019-10-04 stsp if (worktree) {
5211 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5212 4e99b47e 2019-10-04 stsp worktree);
5213 4e99b47e 2019-10-04 stsp if (err)
5214 4e99b47e 2019-10-04 stsp return err;
5215 4e99b47e 2019-10-04 stsp
5216 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5217 4e99b47e 2019-10-04 stsp worktree);
5218 4e99b47e 2019-10-04 stsp if (err)
5219 4e99b47e 2019-10-04 stsp return err;
5220 4e99b47e 2019-10-04 stsp
5221 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5222 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5223 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5224 4e99b47e 2019-10-04 stsp if (err)
5225 4e99b47e 2019-10-04 stsp return err;
5226 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5227 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5228 4e99b47e 2019-10-04 stsp }
5229 4e99b47e 2019-10-04 stsp }
5230 4e99b47e 2019-10-04 stsp
5231 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5232 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5233 4e759de4 2019-06-26 stsp if (err)
5234 4e759de4 2019-06-26 stsp return err;
5235 4e759de4 2019-06-26 stsp
5236 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5237 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5238 4e759de4 2019-06-26 stsp
5239 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5240 4e759de4 2019-06-26 stsp return NULL;
5241 4e759de4 2019-06-26 stsp }
5242 4e759de4 2019-06-26 stsp
5243 4e759de4 2019-06-26 stsp static const struct got_error *
5244 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5245 45cd4e47 2019-08-25 stsp const char *branch_name)
5246 4e759de4 2019-06-26 stsp {
5247 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5248 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5249 4e759de4 2019-06-26 stsp char *refname;
5250 4e759de4 2019-06-26 stsp
5251 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5252 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5253 4e759de4 2019-06-26 stsp
5254 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5255 4e759de4 2019-06-26 stsp if (err)
5256 4e759de4 2019-06-26 stsp goto done;
5257 4e759de4 2019-06-26 stsp
5258 45cd4e47 2019-08-25 stsp if (worktree &&
5259 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5260 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5261 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5262 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5263 45cd4e47 2019-08-25 stsp goto done;
5264 45cd4e47 2019-08-25 stsp }
5265 45cd4e47 2019-08-25 stsp
5266 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5267 4e759de4 2019-06-26 stsp done:
5268 45cd4e47 2019-08-25 stsp if (ref)
5269 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5270 4e759de4 2019-06-26 stsp free(refname);
5271 4e759de4 2019-06-26 stsp return err;
5272 4e759de4 2019-06-26 stsp }
5273 4e759de4 2019-06-26 stsp
5274 4e759de4 2019-06-26 stsp static const struct got_error *
5275 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5276 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5277 4e759de4 2019-06-26 stsp {
5278 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5279 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5280 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5281 d3f84d51 2019-07-11 stsp
5282 d3f84d51 2019-07-11 stsp /*
5283 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5284 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5285 d3f84d51 2019-07-11 stsp * an unintended typo.
5286 d3f84d51 2019-07-11 stsp */
5287 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5288 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5289 4e759de4 2019-06-26 stsp
5290 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5291 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5292 4e759de4 2019-06-26 stsp goto done;
5293 4e759de4 2019-06-26 stsp }
5294 4e759de4 2019-06-26 stsp
5295 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5296 4e759de4 2019-06-26 stsp if (err == NULL) {
5297 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5298 4e759de4 2019-06-26 stsp goto done;
5299 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5300 4e759de4 2019-06-26 stsp goto done;
5301 4e759de4 2019-06-26 stsp
5302 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5303 4e759de4 2019-06-26 stsp if (err)
5304 4e759de4 2019-06-26 stsp goto done;
5305 4e759de4 2019-06-26 stsp
5306 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5307 d0eebce4 2019-03-11 stsp done:
5308 4e759de4 2019-06-26 stsp if (ref)
5309 4e759de4 2019-06-26 stsp got_ref_close(ref);
5310 4e759de4 2019-06-26 stsp free(base_refname);
5311 4e759de4 2019-06-26 stsp free(refname);
5312 4e759de4 2019-06-26 stsp return err;
5313 4e759de4 2019-06-26 stsp }
5314 4e759de4 2019-06-26 stsp
5315 4e759de4 2019-06-26 stsp static const struct got_error *
5316 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5317 4e759de4 2019-06-26 stsp {
5318 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5319 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5320 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5321 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5322 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5323 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5324 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5325 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5326 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5327 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5328 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5329 4e759de4 2019-06-26 stsp
5330 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5331 da76fce2 2020-02-24 stsp
5332 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5333 4e759de4 2019-06-26 stsp switch (ch) {
5334 a74f7e83 2019-11-10 stsp case 'c':
5335 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5336 a74f7e83 2019-11-10 stsp break;
5337 4e759de4 2019-06-26 stsp case 'd':
5338 4e759de4 2019-06-26 stsp delref = optarg;
5339 4e759de4 2019-06-26 stsp break;
5340 4e759de4 2019-06-26 stsp case 'r':
5341 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5342 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5343 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5344 9ba1d308 2019-10-21 stsp optarg);
5345 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5346 4e759de4 2019-06-26 stsp break;
5347 4e759de4 2019-06-26 stsp case 'l':
5348 4e759de4 2019-06-26 stsp do_list = 1;
5349 da76fce2 2020-02-24 stsp break;
5350 da76fce2 2020-02-24 stsp case 'n':
5351 da76fce2 2020-02-24 stsp do_update = 0;
5352 4e759de4 2019-06-26 stsp break;
5353 4e759de4 2019-06-26 stsp default:
5354 4e759de4 2019-06-26 stsp usage_branch();
5355 4e759de4 2019-06-26 stsp /* NOTREACHED */
5356 4e759de4 2019-06-26 stsp }
5357 4e759de4 2019-06-26 stsp }
5358 4e759de4 2019-06-26 stsp
5359 4e759de4 2019-06-26 stsp if (do_list && delref)
5360 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5361 4e759de4 2019-06-26 stsp
5362 4e759de4 2019-06-26 stsp argc -= optind;
5363 4e759de4 2019-06-26 stsp argv += optind;
5364 ad89fa31 2019-10-04 stsp
5365 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5366 ad89fa31 2019-10-04 stsp do_show = 1;
5367 4e759de4 2019-06-26 stsp
5368 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5369 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5370 a74f7e83 2019-11-10 stsp
5371 4e759de4 2019-06-26 stsp if (do_list || delref) {
5372 4e759de4 2019-06-26 stsp if (argc > 0)
5373 4e759de4 2019-06-26 stsp usage_branch();
5374 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5375 4e759de4 2019-06-26 stsp usage_branch();
5376 4e759de4 2019-06-26 stsp
5377 4e759de4 2019-06-26 stsp #ifndef PROFILE
5378 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5379 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5380 4e759de4 2019-06-26 stsp NULL) == -1)
5381 4e759de4 2019-06-26 stsp err(1, "pledge");
5382 4e759de4 2019-06-26 stsp } else {
5383 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5384 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5385 4e759de4 2019-06-26 stsp err(1, "pledge");
5386 4e759de4 2019-06-26 stsp }
5387 4e759de4 2019-06-26 stsp #endif
5388 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5389 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5390 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5391 4e759de4 2019-06-26 stsp goto done;
5392 4e759de4 2019-06-26 stsp }
5393 4e759de4 2019-06-26 stsp
5394 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5395 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5396 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5397 4e759de4 2019-06-26 stsp goto done;
5398 4e759de4 2019-06-26 stsp else
5399 4e759de4 2019-06-26 stsp error = NULL;
5400 4e759de4 2019-06-26 stsp if (worktree) {
5401 4e759de4 2019-06-26 stsp repo_path =
5402 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5403 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5404 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5405 4e759de4 2019-06-26 stsp if (error)
5406 4e759de4 2019-06-26 stsp goto done;
5407 4e759de4 2019-06-26 stsp } else {
5408 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5409 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5410 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5411 4e759de4 2019-06-26 stsp goto done;
5412 4e759de4 2019-06-26 stsp }
5413 4e759de4 2019-06-26 stsp }
5414 4e759de4 2019-06-26 stsp }
5415 4e759de4 2019-06-26 stsp
5416 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5417 4e759de4 2019-06-26 stsp if (error != NULL)
5418 4e759de4 2019-06-26 stsp goto done;
5419 4e759de4 2019-06-26 stsp
5420 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5421 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5422 4e759de4 2019-06-26 stsp if (error)
5423 4e759de4 2019-06-26 stsp goto done;
5424 4e759de4 2019-06-26 stsp
5425 ad89fa31 2019-10-04 stsp if (do_show)
5426 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5427 ad89fa31 2019-10-04 stsp else if (do_list)
5428 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5429 4e759de4 2019-06-26 stsp else if (delref)
5430 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5431 4e759de4 2019-06-26 stsp else {
5432 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5433 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5434 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5435 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5436 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5437 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5438 a74f7e83 2019-11-10 stsp if (error)
5439 a74f7e83 2019-11-10 stsp goto done;
5440 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5441 da76fce2 2020-02-24 stsp if (error)
5442 da76fce2 2020-02-24 stsp goto done;
5443 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5444 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5445 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5446 da76fce2 2020-02-24 stsp
5447 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5448 da76fce2 2020-02-24 stsp if (error)
5449 da76fce2 2020-02-24 stsp goto done;
5450 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5451 da76fce2 2020-02-24 stsp worktree);
5452 da76fce2 2020-02-24 stsp if (error)
5453 da76fce2 2020-02-24 stsp goto done;
5454 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5455 da76fce2 2020-02-24 stsp == -1) {
5456 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5457 da76fce2 2020-02-24 stsp goto done;
5458 da76fce2 2020-02-24 stsp }
5459 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5460 da76fce2 2020-02-24 stsp free(branch_refname);
5461 da76fce2 2020-02-24 stsp if (error)
5462 da76fce2 2020-02-24 stsp goto done;
5463 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5464 da76fce2 2020-02-24 stsp repo);
5465 da76fce2 2020-02-24 stsp if (error)
5466 da76fce2 2020-02-24 stsp goto done;
5467 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5468 da76fce2 2020-02-24 stsp commit_id);
5469 da76fce2 2020-02-24 stsp if (error)
5470 da76fce2 2020-02-24 stsp goto done;
5471 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5472 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5473 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5474 9627c110 2020-04-18 stsp NULL);
5475 da76fce2 2020-02-24 stsp if (error)
5476 da76fce2 2020-02-24 stsp goto done;
5477 9627c110 2020-04-18 stsp if (upa.did_something)
5478 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5479 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5480 da76fce2 2020-02-24 stsp }
5481 4e759de4 2019-06-26 stsp }
5482 4e759de4 2019-06-26 stsp done:
5483 da76fce2 2020-02-24 stsp if (ref)
5484 da76fce2 2020-02-24 stsp got_ref_close(ref);
5485 d0eebce4 2019-03-11 stsp if (repo)
5486 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5487 d0eebce4 2019-03-11 stsp if (worktree)
5488 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5489 d0eebce4 2019-03-11 stsp free(cwd);
5490 d0eebce4 2019-03-11 stsp free(repo_path);
5491 da76fce2 2020-02-24 stsp free(commit_id);
5492 da76fce2 2020-02-24 stsp free(commit_id_str);
5493 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5494 da76fce2 2020-02-24 stsp free((char *)pe->path);
5495 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5496 d00136be 2019-03-26 stsp return error;
5497 d00136be 2019-03-26 stsp }
5498 d00136be 2019-03-26 stsp
5499 8e7bd50a 2019-08-22 stsp
5500 d00136be 2019-03-26 stsp __dead static void
5501 8e7bd50a 2019-08-22 stsp usage_tag(void)
5502 8e7bd50a 2019-08-22 stsp {
5503 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5504 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5505 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5506 8e7bd50a 2019-08-22 stsp exit(1);
5507 b8bad2ba 2019-08-23 stsp }
5508 b8bad2ba 2019-08-23 stsp
5509 b8bad2ba 2019-08-23 stsp #if 0
5510 b8bad2ba 2019-08-23 stsp static const struct got_error *
5511 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5512 b8bad2ba 2019-08-23 stsp {
5513 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5514 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5515 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5516 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5517 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5518 b8bad2ba 2019-08-23 stsp
5519 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5520 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5521 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5522 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5523 b8bad2ba 2019-08-23 stsp if (err)
5524 b8bad2ba 2019-08-23 stsp return err;
5525 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5526 b8bad2ba 2019-08-23 stsp continue;
5527 b8bad2ba 2019-08-23 stsp } else {
5528 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5529 b8bad2ba 2019-08-23 stsp if (err)
5530 b8bad2ba 2019-08-23 stsp break;
5531 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5532 b8bad2ba 2019-08-23 stsp free(re_id);
5533 b8bad2ba 2019-08-23 stsp if (err)
5534 b8bad2ba 2019-08-23 stsp break;
5535 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5536 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5537 b8bad2ba 2019-08-23 stsp }
5538 b8bad2ba 2019-08-23 stsp
5539 b8bad2ba 2019-08-23 stsp while (se) {
5540 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5541 b8bad2ba 2019-08-23 stsp if (err)
5542 b8bad2ba 2019-08-23 stsp break;
5543 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5544 b8bad2ba 2019-08-23 stsp free(se_id);
5545 b8bad2ba 2019-08-23 stsp if (err)
5546 b8bad2ba 2019-08-23 stsp break;
5547 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5548 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5549 b8bad2ba 2019-08-23 stsp
5550 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5551 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5552 b8bad2ba 2019-08-23 stsp if (err)
5553 b8bad2ba 2019-08-23 stsp return err;
5554 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5555 b8bad2ba 2019-08-23 stsp break;
5556 b8bad2ba 2019-08-23 stsp }
5557 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5558 b8bad2ba 2019-08-23 stsp continue;
5559 b8bad2ba 2019-08-23 stsp }
5560 b8bad2ba 2019-08-23 stsp }
5561 b8bad2ba 2019-08-23 stsp done:
5562 b8bad2ba 2019-08-23 stsp return err;
5563 8e7bd50a 2019-08-22 stsp }
5564 b8bad2ba 2019-08-23 stsp #endif
5565 b8bad2ba 2019-08-23 stsp
5566 b8bad2ba 2019-08-23 stsp static const struct got_error *
5567 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5568 8e7bd50a 2019-08-22 stsp {
5569 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5570 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5571 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5572 8e7bd50a 2019-08-22 stsp
5573 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5574 8e7bd50a 2019-08-22 stsp
5575 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5576 8e7bd50a 2019-08-22 stsp if (err)
5577 8e7bd50a 2019-08-22 stsp return err;
5578 8e7bd50a 2019-08-22 stsp
5579 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5580 8e7bd50a 2019-08-22 stsp const char *refname;
5581 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5582 8e7bd50a 2019-08-22 stsp char datebuf[26];
5583 d4efa91b 2020-01-14 stsp const char *tagger;
5584 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5585 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5586 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5587 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5588 8e7bd50a 2019-08-22 stsp
5589 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5590 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5591 8e7bd50a 2019-08-22 stsp continue;
5592 8e7bd50a 2019-08-22 stsp refname += 10;
5593 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5594 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5595 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5596 8e7bd50a 2019-08-22 stsp break;
5597 8e7bd50a 2019-08-22 stsp }
5598 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5599 8e7bd50a 2019-08-22 stsp free(refstr);
5600 8e7bd50a 2019-08-22 stsp
5601 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5602 8e7bd50a 2019-08-22 stsp if (err)
5603 8e7bd50a 2019-08-22 stsp break;
5604 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5605 d4efa91b 2020-01-14 stsp if (err) {
5606 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5607 d4efa91b 2020-01-14 stsp free(id);
5608 d4efa91b 2020-01-14 stsp break;
5609 d4efa91b 2020-01-14 stsp }
5610 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5611 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5612 d4efa91b 2020-01-14 stsp if (err) {
5613 d4efa91b 2020-01-14 stsp free(id);
5614 d4efa91b 2020-01-14 stsp break;
5615 d4efa91b 2020-01-14 stsp }
5616 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5617 d4efa91b 2020-01-14 stsp tagger_time =
5618 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5619 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5620 d4efa91b 2020-01-14 stsp free(id);
5621 d4efa91b 2020-01-14 stsp if (err)
5622 d4efa91b 2020-01-14 stsp break;
5623 d4efa91b 2020-01-14 stsp } else {
5624 d4efa91b 2020-01-14 stsp free(id);
5625 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5626 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5627 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5628 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5629 d4efa91b 2020-01-14 stsp if (err)
5630 d4efa91b 2020-01-14 stsp break;
5631 d4efa91b 2020-01-14 stsp }
5632 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5633 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5634 2417344c 2019-08-23 stsp if (datestr)
5635 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5636 d4efa91b 2020-01-14 stsp if (commit)
5637 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5638 d4efa91b 2020-01-14 stsp else {
5639 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5640 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5641 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5642 d4efa91b 2020-01-14 stsp id_str);
5643 d4efa91b 2020-01-14 stsp break;
5644 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5645 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5646 d4efa91b 2020-01-14 stsp id_str);
5647 d4efa91b 2020-01-14 stsp break;
5648 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5649 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5650 d4efa91b 2020-01-14 stsp id_str);
5651 d4efa91b 2020-01-14 stsp break;
5652 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5653 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5654 d4efa91b 2020-01-14 stsp id_str);
5655 d4efa91b 2020-01-14 stsp break;
5656 d4efa91b 2020-01-14 stsp default:
5657 d4efa91b 2020-01-14 stsp break;
5658 d4efa91b 2020-01-14 stsp }
5659 8e7bd50a 2019-08-22 stsp }
5660 8e7bd50a 2019-08-22 stsp free(id_str);
5661 d4efa91b 2020-01-14 stsp if (commit) {
5662 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5663 d4efa91b 2020-01-14 stsp if (err)
5664 d4efa91b 2020-01-14 stsp break;
5665 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5666 d4efa91b 2020-01-14 stsp } else {
5667 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5668 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5669 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5670 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5671 d4efa91b 2020-01-14 stsp break;
5672 d4efa91b 2020-01-14 stsp }
5673 8e7bd50a 2019-08-22 stsp }
5674 8e7bd50a 2019-08-22 stsp
5675 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5676 8e7bd50a 2019-08-22 stsp do {
5677 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5678 8e7bd50a 2019-08-22 stsp if (line)
5679 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5680 8e7bd50a 2019-08-22 stsp } while (line);
5681 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5682 8e7bd50a 2019-08-22 stsp }
5683 8e7bd50a 2019-08-22 stsp
5684 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5685 8e7bd50a 2019-08-22 stsp return NULL;
5686 8e7bd50a 2019-08-22 stsp }
5687 8e7bd50a 2019-08-22 stsp
5688 8e7bd50a 2019-08-22 stsp static const struct got_error *
5689 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5690 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5691 8e7bd50a 2019-08-22 stsp {
5692 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5693 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5694 f372d5cd 2019-10-21 stsp char *editor = NULL;
5695 1601cb9f 2020-09-11 naddy int initial_content_len;
5696 8e7bd50a 2019-08-22 stsp int fd = -1;
5697 8e7bd50a 2019-08-22 stsp
5698 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5699 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5700 8e7bd50a 2019-08-22 stsp goto done;
5701 8e7bd50a 2019-08-22 stsp }
5702 8e7bd50a 2019-08-22 stsp
5703 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5704 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5705 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5706 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5707 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5708 8e7bd50a 2019-08-22 stsp goto done;
5709 8e7bd50a 2019-08-22 stsp }
5710 8e7bd50a 2019-08-22 stsp
5711 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5712 8e7bd50a 2019-08-22 stsp if (err)
5713 8e7bd50a 2019-08-22 stsp goto done;
5714 8e7bd50a 2019-08-22 stsp
5715 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5716 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5717 97972933 2020-09-11 stsp goto done;
5718 97972933 2020-09-11 stsp }
5719 8e7bd50a 2019-08-22 stsp
5720 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5721 8e7bd50a 2019-08-22 stsp if (err)
5722 8e7bd50a 2019-08-22 stsp goto done;
5723 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5724 8e7bd50a 2019-08-22 stsp done:
5725 8e7bd50a 2019-08-22 stsp free(initial_content);
5726 8e7bd50a 2019-08-22 stsp free(template);
5727 8e7bd50a 2019-08-22 stsp free(editor);
5728 8e7bd50a 2019-08-22 stsp
5729 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5730 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5731 97972933 2020-09-11 stsp
5732 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5733 59f86c76 2020-09-11 stsp if (err == NULL)
5734 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5735 59f86c76 2020-09-11 stsp if (err) {
5736 59f86c76 2020-09-11 stsp free(*tagmsg);
5737 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5738 8e7bd50a 2019-08-22 stsp }
5739 8e7bd50a 2019-08-22 stsp return err;
5740 8e7bd50a 2019-08-22 stsp }
5741 8e7bd50a 2019-08-22 stsp
5742 8e7bd50a 2019-08-22 stsp static const struct got_error *
5743 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5744 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5745 8e7bd50a 2019-08-22 stsp {
5746 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5747 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5748 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5749 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5750 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5751 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5752 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5753 8e7bd50a 2019-08-22 stsp
5754 8e7bd50a 2019-08-22 stsp /*
5755 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5756 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5757 8e7bd50a 2019-08-22 stsp * an unintended typo.
5758 8e7bd50a 2019-08-22 stsp */
5759 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5760 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5761 8e7bd50a 2019-08-22 stsp
5762 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5763 8e7bd50a 2019-08-22 stsp if (err)
5764 8e7bd50a 2019-08-22 stsp return err;
5765 8e7bd50a 2019-08-22 stsp
5766 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5767 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5768 8e7bd50a 2019-08-22 stsp if (err)
5769 8e7bd50a 2019-08-22 stsp goto done;
5770 8e7bd50a 2019-08-22 stsp
5771 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5772 8e7bd50a 2019-08-22 stsp if (err)
5773 8e7bd50a 2019-08-22 stsp goto done;
5774 8e7bd50a 2019-08-22 stsp
5775 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5776 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5777 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5778 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5779 8e7bd50a 2019-08-22 stsp goto done;
5780 8e7bd50a 2019-08-22 stsp }
5781 8e7bd50a 2019-08-22 stsp tag_name += 10;
5782 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5783 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5784 8e7bd50a 2019-08-22 stsp goto done;
5785 8e7bd50a 2019-08-22 stsp }
5786 8e7bd50a 2019-08-22 stsp
5787 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5788 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5789 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5790 8e7bd50a 2019-08-22 stsp goto done;
5791 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5792 8e7bd50a 2019-08-22 stsp goto done;
5793 8e7bd50a 2019-08-22 stsp
5794 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5795 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5796 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5797 f372d5cd 2019-10-21 stsp if (err) {
5798 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5799 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5800 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5801 8e7bd50a 2019-08-22 stsp goto done;
5802 f372d5cd 2019-10-21 stsp }
5803 8e7bd50a 2019-08-22 stsp }
5804 8e7bd50a 2019-08-22 stsp
5805 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5806 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5807 f372d5cd 2019-10-21 stsp if (err) {
5808 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5809 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5810 8e7bd50a 2019-08-22 stsp goto done;
5811 f372d5cd 2019-10-21 stsp }
5812 8e7bd50a 2019-08-22 stsp
5813 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5814 f372d5cd 2019-10-21 stsp if (err) {
5815 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5816 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5817 8e7bd50a 2019-08-22 stsp goto done;
5818 f372d5cd 2019-10-21 stsp }
5819 8e7bd50a 2019-08-22 stsp
5820 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5821 f372d5cd 2019-10-21 stsp if (err) {
5822 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5823 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5824 f372d5cd 2019-10-21 stsp goto done;
5825 f372d5cd 2019-10-21 stsp }
5826 8e7bd50a 2019-08-22 stsp
5827 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5828 f372d5cd 2019-10-21 stsp if (err) {
5829 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5830 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5831 f372d5cd 2019-10-21 stsp goto done;
5832 8e7bd50a 2019-08-22 stsp }
5833 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5834 8e7bd50a 2019-08-22 stsp done:
5835 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5836 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5837 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5838 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5839 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5840 f372d5cd 2019-10-21 stsp free(tag_id_str);
5841 8e7bd50a 2019-08-22 stsp if (ref)
5842 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5843 8e7bd50a 2019-08-22 stsp free(commit_id);
5844 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5845 8e7bd50a 2019-08-22 stsp free(refname);
5846 8e7bd50a 2019-08-22 stsp free(tagmsg);
5847 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5848 aba9c984 2019-09-08 stsp free(tagger);
5849 8e7bd50a 2019-08-22 stsp return err;
5850 8e7bd50a 2019-08-22 stsp }
5851 8e7bd50a 2019-08-22 stsp
5852 8e7bd50a 2019-08-22 stsp static const struct got_error *
5853 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5854 8e7bd50a 2019-08-22 stsp {
5855 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5856 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5857 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5858 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5859 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5860 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5861 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5862 8e7bd50a 2019-08-22 stsp
5863 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5864 8e7bd50a 2019-08-22 stsp switch (ch) {
5865 80106605 2020-02-24 stsp case 'c':
5866 80106605 2020-02-24 stsp commit_id_arg = optarg;
5867 80106605 2020-02-24 stsp break;
5868 8e7bd50a 2019-08-22 stsp case 'm':
5869 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5870 8e7bd50a 2019-08-22 stsp break;
5871 8e7bd50a 2019-08-22 stsp case 'r':
5872 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5873 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5874 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5875 9ba1d308 2019-10-21 stsp optarg);
5876 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5877 8e7bd50a 2019-08-22 stsp break;
5878 8e7bd50a 2019-08-22 stsp case 'l':
5879 8e7bd50a 2019-08-22 stsp do_list = 1;
5880 8e7bd50a 2019-08-22 stsp break;
5881 8e7bd50a 2019-08-22 stsp default:
5882 8e7bd50a 2019-08-22 stsp usage_tag();
5883 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5884 8e7bd50a 2019-08-22 stsp }
5885 8e7bd50a 2019-08-22 stsp }
5886 8e7bd50a 2019-08-22 stsp
5887 8e7bd50a 2019-08-22 stsp argc -= optind;
5888 8e7bd50a 2019-08-22 stsp argv += optind;
5889 8e7bd50a 2019-08-22 stsp
5890 8e7bd50a 2019-08-22 stsp if (do_list) {
5891 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5892 775ce909 2020-03-22 stsp errx(1,
5893 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5894 8e7bd50a 2019-08-22 stsp if (tagmsg)
5895 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5896 8e7bd50a 2019-08-22 stsp if (argc > 0)
5897 8e7bd50a 2019-08-22 stsp usage_tag();
5898 80106605 2020-02-24 stsp } else if (argc != 1)
5899 8e7bd50a 2019-08-22 stsp usage_tag();
5900 80106605 2020-02-24 stsp
5901 a2887370 2019-08-23 stsp tag_name = argv[0];
5902 8e7bd50a 2019-08-22 stsp
5903 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5904 8e7bd50a 2019-08-22 stsp if (do_list) {
5905 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5906 8e7bd50a 2019-08-22 stsp NULL) == -1)
5907 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5908 8e7bd50a 2019-08-22 stsp } else {
5909 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5910 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5911 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5912 8e7bd50a 2019-08-22 stsp }
5913 8e7bd50a 2019-08-22 stsp #endif
5914 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5915 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5916 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5917 8e7bd50a 2019-08-22 stsp goto done;
5918 8e7bd50a 2019-08-22 stsp }
5919 8e7bd50a 2019-08-22 stsp
5920 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5921 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5922 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5923 8e7bd50a 2019-08-22 stsp goto done;
5924 8e7bd50a 2019-08-22 stsp else
5925 8e7bd50a 2019-08-22 stsp error = NULL;
5926 8e7bd50a 2019-08-22 stsp if (worktree) {
5927 8e7bd50a 2019-08-22 stsp repo_path =
5928 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5929 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5930 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5931 8e7bd50a 2019-08-22 stsp if (error)
5932 8e7bd50a 2019-08-22 stsp goto done;
5933 8e7bd50a 2019-08-22 stsp } else {
5934 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5935 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5936 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5937 8e7bd50a 2019-08-22 stsp goto done;
5938 8e7bd50a 2019-08-22 stsp }
5939 8e7bd50a 2019-08-22 stsp }
5940 8e7bd50a 2019-08-22 stsp }
5941 8e7bd50a 2019-08-22 stsp
5942 8e7bd50a 2019-08-22 stsp if (do_list) {
5943 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5944 c9956ddf 2019-09-08 stsp if (error != NULL)
5945 c9956ddf 2019-09-08 stsp goto done;
5946 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5947 8e7bd50a 2019-08-22 stsp if (error)
5948 8e7bd50a 2019-08-22 stsp goto done;
5949 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5950 8e7bd50a 2019-08-22 stsp } else {
5951 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5952 c9956ddf 2019-09-08 stsp if (error)
5953 c9956ddf 2019-09-08 stsp goto done;
5954 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5955 c9956ddf 2019-09-08 stsp if (error != NULL)
5956 c9956ddf 2019-09-08 stsp goto done;
5957 c9956ddf 2019-09-08 stsp
5958 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5959 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5960 8e7bd50a 2019-08-22 stsp if (error)
5961 8e7bd50a 2019-08-22 stsp goto done;
5962 8e7bd50a 2019-08-22 stsp }
5963 8e7bd50a 2019-08-22 stsp
5964 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5965 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5966 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5967 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5968 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5969 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5970 8e7bd50a 2019-08-22 stsp if (error)
5971 8e7bd50a 2019-08-22 stsp goto done;
5972 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5973 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5974 8e7bd50a 2019-08-22 stsp if (error)
5975 8e7bd50a 2019-08-22 stsp goto done;
5976 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5977 8e7bd50a 2019-08-22 stsp free(commit_id);
5978 8e7bd50a 2019-08-22 stsp if (error)
5979 8e7bd50a 2019-08-22 stsp goto done;
5980 8e7bd50a 2019-08-22 stsp }
5981 8e7bd50a 2019-08-22 stsp
5982 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
5983 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5984 8e7bd50a 2019-08-22 stsp }
5985 8e7bd50a 2019-08-22 stsp done:
5986 8e7bd50a 2019-08-22 stsp if (repo)
5987 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5988 8e7bd50a 2019-08-22 stsp if (worktree)
5989 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5990 8e7bd50a 2019-08-22 stsp free(cwd);
5991 8e7bd50a 2019-08-22 stsp free(repo_path);
5992 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5993 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5994 8e7bd50a 2019-08-22 stsp return error;
5995 8e7bd50a 2019-08-22 stsp }
5996 8e7bd50a 2019-08-22 stsp
5997 8e7bd50a 2019-08-22 stsp __dead static void
5998 d00136be 2019-03-26 stsp usage_add(void)
5999 d00136be 2019-03-26 stsp {
6000 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6001 022fae89 2019-12-06 tracey getprogname());
6002 d00136be 2019-03-26 stsp exit(1);
6003 d00136be 2019-03-26 stsp }
6004 d00136be 2019-03-26 stsp
6005 d00136be 2019-03-26 stsp static const struct got_error *
6006 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6007 4e68cba3 2019-11-23 stsp {
6008 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6009 4e68cba3 2019-11-23 stsp path++;
6010 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6011 4e68cba3 2019-11-23 stsp return NULL;
6012 4e68cba3 2019-11-23 stsp }
6013 4e68cba3 2019-11-23 stsp
6014 4e68cba3 2019-11-23 stsp static const struct got_error *
6015 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6016 d00136be 2019-03-26 stsp {
6017 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6018 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6019 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6020 1dd54920 2019-05-11 stsp char *cwd = NULL;
6021 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6022 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6023 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6024 1dd54920 2019-05-11 stsp
6025 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6026 d00136be 2019-03-26 stsp
6027 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6028 d00136be 2019-03-26 stsp switch (ch) {
6029 022fae89 2019-12-06 tracey case 'I':
6030 022fae89 2019-12-06 tracey no_ignores = 1;
6031 022fae89 2019-12-06 tracey break;
6032 4e68cba3 2019-11-23 stsp case 'R':
6033 4e68cba3 2019-11-23 stsp can_recurse = 1;
6034 4e68cba3 2019-11-23 stsp break;
6035 d00136be 2019-03-26 stsp default:
6036 d00136be 2019-03-26 stsp usage_add();
6037 d00136be 2019-03-26 stsp /* NOTREACHED */
6038 d00136be 2019-03-26 stsp }
6039 d00136be 2019-03-26 stsp }
6040 d00136be 2019-03-26 stsp
6041 d00136be 2019-03-26 stsp argc -= optind;
6042 d00136be 2019-03-26 stsp argv += optind;
6043 d00136be 2019-03-26 stsp
6044 43012d58 2019-07-14 stsp #ifndef PROFILE
6045 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6046 43012d58 2019-07-14 stsp NULL) == -1)
6047 43012d58 2019-07-14 stsp err(1, "pledge");
6048 43012d58 2019-07-14 stsp #endif
6049 723c305c 2019-05-11 jcs if (argc < 1)
6050 d00136be 2019-03-26 stsp usage_add();
6051 d00136be 2019-03-26 stsp
6052 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6053 d00136be 2019-03-26 stsp if (cwd == NULL) {
6054 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6055 d00136be 2019-03-26 stsp goto done;
6056 d00136be 2019-03-26 stsp }
6057 723c305c 2019-05-11 jcs
6058 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6059 fa51e947 2020-03-27 stsp if (error) {
6060 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6061 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6062 d00136be 2019-03-26 stsp goto done;
6063 fa51e947 2020-03-27 stsp }
6064 d00136be 2019-03-26 stsp
6065 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6066 c9956ddf 2019-09-08 stsp NULL);
6067 031a5338 2019-03-26 stsp if (error != NULL)
6068 031a5338 2019-03-26 stsp goto done;
6069 031a5338 2019-03-26 stsp
6070 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6071 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6072 d00136be 2019-03-26 stsp if (error)
6073 d00136be 2019-03-26 stsp goto done;
6074 d00136be 2019-03-26 stsp
6075 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6076 6d022e97 2019-08-04 stsp if (error)
6077 022fae89 2019-12-06 tracey goto done;
6078 022fae89 2019-12-06 tracey
6079 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6080 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6081 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6082 6d022e97 2019-08-04 stsp goto done;
6083 723c305c 2019-05-11 jcs
6084 022fae89 2019-12-06 tracey }
6085 022fae89 2019-12-06 tracey
6086 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6087 4e68cba3 2019-11-23 stsp char *ondisk_path;
6088 4e68cba3 2019-11-23 stsp struct stat sb;
6089 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6090 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6091 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6092 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6093 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6094 4e68cba3 2019-11-23 stsp goto done;
6095 4e68cba3 2019-11-23 stsp }
6096 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6097 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6098 4e68cba3 2019-11-23 stsp free(ondisk_path);
6099 4e68cba3 2019-11-23 stsp continue;
6100 4e68cba3 2019-11-23 stsp }
6101 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6102 4e68cba3 2019-11-23 stsp ondisk_path);
6103 4e68cba3 2019-11-23 stsp free(ondisk_path);
6104 4e68cba3 2019-11-23 stsp goto done;
6105 4e68cba3 2019-11-23 stsp }
6106 4e68cba3 2019-11-23 stsp free(ondisk_path);
6107 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6108 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6109 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6110 4e68cba3 2019-11-23 stsp goto done;
6111 4e68cba3 2019-11-23 stsp }
6112 4e68cba3 2019-11-23 stsp }
6113 4e68cba3 2019-11-23 stsp }
6114 022fae89 2019-12-06 tracey
6115 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6116 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6117 d00136be 2019-03-26 stsp done:
6118 031a5338 2019-03-26 stsp if (repo)
6119 031a5338 2019-03-26 stsp got_repo_close(repo);
6120 d00136be 2019-03-26 stsp if (worktree)
6121 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6122 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6123 1dd54920 2019-05-11 stsp free((char *)pe->path);
6124 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6125 2ec1f75b 2019-03-26 stsp free(cwd);
6126 2ec1f75b 2019-03-26 stsp return error;
6127 2ec1f75b 2019-03-26 stsp }
6128 2ec1f75b 2019-03-26 stsp
6129 2ec1f75b 2019-03-26 stsp __dead static void
6130 648e4ef7 2019-07-09 stsp usage_remove(void)
6131 2ec1f75b 2019-03-26 stsp {
6132 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6133 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6134 2ec1f75b 2019-03-26 stsp exit(1);
6135 2a06fe5f 2019-08-24 stsp }
6136 2a06fe5f 2019-08-24 stsp
6137 2a06fe5f 2019-08-24 stsp static const struct got_error *
6138 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6139 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6140 2a06fe5f 2019-08-24 stsp {
6141 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6142 f2a9dc41 2019-12-13 tracey path++;
6143 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6144 2a06fe5f 2019-08-24 stsp return NULL;
6145 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6146 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6147 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6148 2a06fe5f 2019-08-24 stsp return NULL;
6149 2ec1f75b 2019-03-26 stsp }
6150 2ec1f75b 2019-03-26 stsp
6151 2ec1f75b 2019-03-26 stsp static const struct got_error *
6152 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6153 2ec1f75b 2019-03-26 stsp {
6154 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6155 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6156 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6157 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6158 17ed4618 2019-06-02 stsp char *cwd = NULL;
6159 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6160 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6161 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6162 2ec1f75b 2019-03-26 stsp
6163 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6164 17ed4618 2019-06-02 stsp
6165 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6166 2ec1f75b 2019-03-26 stsp switch (ch) {
6167 2ec1f75b 2019-03-26 stsp case 'f':
6168 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6169 2ec1f75b 2019-03-26 stsp break;
6170 70e3e7f5 2019-12-13 tracey case 'k':
6171 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6172 70e3e7f5 2019-12-13 tracey break;
6173 f2a9dc41 2019-12-13 tracey case 'R':
6174 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6175 f2a9dc41 2019-12-13 tracey break;
6176 766841c2 2020-08-13 stsp case 's':
6177 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6178 766841c2 2020-08-13 stsp switch (optarg[i]) {
6179 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6180 766841c2 2020-08-13 stsp delete_local_mods = 1;
6181 766841c2 2020-08-13 stsp break;
6182 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6183 766841c2 2020-08-13 stsp break;
6184 766841c2 2020-08-13 stsp default:
6185 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6186 766841c2 2020-08-13 stsp optarg[i]);
6187 766841c2 2020-08-13 stsp }
6188 766841c2 2020-08-13 stsp }
6189 766841c2 2020-08-13 stsp status_codes = optarg;
6190 766841c2 2020-08-13 stsp break;
6191 2ec1f75b 2019-03-26 stsp default:
6192 f2a9dc41 2019-12-13 tracey usage_remove();
6193 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6194 2ec1f75b 2019-03-26 stsp }
6195 2ec1f75b 2019-03-26 stsp }
6196 2ec1f75b 2019-03-26 stsp
6197 2ec1f75b 2019-03-26 stsp argc -= optind;
6198 2ec1f75b 2019-03-26 stsp argv += optind;
6199 2ec1f75b 2019-03-26 stsp
6200 43012d58 2019-07-14 stsp #ifndef PROFILE
6201 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6202 43012d58 2019-07-14 stsp NULL) == -1)
6203 43012d58 2019-07-14 stsp err(1, "pledge");
6204 43012d58 2019-07-14 stsp #endif
6205 17ed4618 2019-06-02 stsp if (argc < 1)
6206 648e4ef7 2019-07-09 stsp usage_remove();
6207 2ec1f75b 2019-03-26 stsp
6208 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6209 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6210 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6211 2ec1f75b 2019-03-26 stsp goto done;
6212 2ec1f75b 2019-03-26 stsp }
6213 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6214 fa51e947 2020-03-27 stsp if (error) {
6215 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6216 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6217 2ec1f75b 2019-03-26 stsp goto done;
6218 fa51e947 2020-03-27 stsp }
6219 2ec1f75b 2019-03-26 stsp
6220 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6221 c9956ddf 2019-09-08 stsp NULL);
6222 2af4a041 2019-05-11 jcs if (error)
6223 2ec1f75b 2019-03-26 stsp goto done;
6224 2ec1f75b 2019-03-26 stsp
6225 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6226 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6227 2ec1f75b 2019-03-26 stsp if (error)
6228 2ec1f75b 2019-03-26 stsp goto done;
6229 2ec1f75b 2019-03-26 stsp
6230 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6231 6d022e97 2019-08-04 stsp if (error)
6232 6d022e97 2019-08-04 stsp goto done;
6233 17ed4618 2019-06-02 stsp
6234 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6235 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6236 f2a9dc41 2019-12-13 tracey struct stat sb;
6237 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6238 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6239 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6240 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6241 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6242 f2a9dc41 2019-12-13 tracey goto done;
6243 f2a9dc41 2019-12-13 tracey }
6244 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6245 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6246 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6247 f2a9dc41 2019-12-13 tracey continue;
6248 f2a9dc41 2019-12-13 tracey }
6249 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6250 f2a9dc41 2019-12-13 tracey ondisk_path);
6251 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6252 f2a9dc41 2019-12-13 tracey goto done;
6253 f2a9dc41 2019-12-13 tracey }
6254 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6255 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6256 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6257 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6258 f2a9dc41 2019-12-13 tracey goto done;
6259 f2a9dc41 2019-12-13 tracey }
6260 f2a9dc41 2019-12-13 tracey }
6261 f2a9dc41 2019-12-13 tracey }
6262 f2a9dc41 2019-12-13 tracey
6263 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6264 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6265 766841c2 2020-08-13 stsp repo, keep_on_disk);
6266 a129376b 2019-03-28 stsp done:
6267 a129376b 2019-03-28 stsp if (repo)
6268 a129376b 2019-03-28 stsp got_repo_close(repo);
6269 a129376b 2019-03-28 stsp if (worktree)
6270 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6271 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6272 17ed4618 2019-06-02 stsp free((char *)pe->path);
6273 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6274 a129376b 2019-03-28 stsp free(cwd);
6275 a129376b 2019-03-28 stsp return error;
6276 a129376b 2019-03-28 stsp }
6277 a129376b 2019-03-28 stsp
6278 a129376b 2019-03-28 stsp __dead static void
6279 a129376b 2019-03-28 stsp usage_revert(void)
6280 a129376b 2019-03-28 stsp {
6281 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6282 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6283 a129376b 2019-03-28 stsp exit(1);
6284 a129376b 2019-03-28 stsp }
6285 a129376b 2019-03-28 stsp
6286 1ee397ad 2019-07-12 stsp static const struct got_error *
6287 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6288 a129376b 2019-03-28 stsp {
6289 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6290 fb9704af 2020-01-27 stsp return NULL;
6291 fb9704af 2020-01-27 stsp
6292 a129376b 2019-03-28 stsp while (path[0] == '/')
6293 a129376b 2019-03-28 stsp path++;
6294 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6295 33aa809d 2019-08-08 stsp return NULL;
6296 33aa809d 2019-08-08 stsp }
6297 33aa809d 2019-08-08 stsp
6298 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6299 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6300 33aa809d 2019-08-08 stsp const char *action;
6301 33aa809d 2019-08-08 stsp };
6302 33aa809d 2019-08-08 stsp
6303 33aa809d 2019-08-08 stsp static const struct got_error *
6304 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6305 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6306 33aa809d 2019-08-08 stsp {
6307 33aa809d 2019-08-08 stsp char *line = NULL;
6308 33aa809d 2019-08-08 stsp size_t linesize = 0;
6309 33aa809d 2019-08-08 stsp ssize_t linelen;
6310 33aa809d 2019-08-08 stsp
6311 33aa809d 2019-08-08 stsp switch (status) {
6312 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6313 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6314 33aa809d 2019-08-08 stsp break;
6315 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6316 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6317 33aa809d 2019-08-08 stsp break;
6318 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6319 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6320 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6321 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6322 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6323 33aa809d 2019-08-08 stsp printf("%s", line);
6324 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6325 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6326 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6327 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6328 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6329 33aa809d 2019-08-08 stsp break;
6330 33aa809d 2019-08-08 stsp default:
6331 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6332 33aa809d 2019-08-08 stsp }
6333 33aa809d 2019-08-08 stsp
6334 33aa809d 2019-08-08 stsp return NULL;
6335 33aa809d 2019-08-08 stsp }
6336 33aa809d 2019-08-08 stsp
6337 33aa809d 2019-08-08 stsp static const struct got_error *
6338 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6339 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6340 33aa809d 2019-08-08 stsp {
6341 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6342 33aa809d 2019-08-08 stsp char *line = NULL;
6343 33aa809d 2019-08-08 stsp size_t linesize = 0;
6344 33aa809d 2019-08-08 stsp ssize_t linelen;
6345 33aa809d 2019-08-08 stsp int resp = ' ';
6346 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6347 33aa809d 2019-08-08 stsp
6348 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6349 33aa809d 2019-08-08 stsp
6350 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6351 33aa809d 2019-08-08 stsp char *nl;
6352 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6353 33aa809d 2019-08-08 stsp a->action);
6354 33aa809d 2019-08-08 stsp if (err)
6355 33aa809d 2019-08-08 stsp return err;
6356 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6357 33aa809d 2019-08-08 stsp if (linelen == -1) {
6358 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6359 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6360 33aa809d 2019-08-08 stsp return NULL;
6361 33aa809d 2019-08-08 stsp }
6362 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6363 33aa809d 2019-08-08 stsp if (nl)
6364 33aa809d 2019-08-08 stsp *nl = '\0';
6365 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6366 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6367 33aa809d 2019-08-08 stsp printf("y\n");
6368 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6369 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6370 33aa809d 2019-08-08 stsp printf("n\n");
6371 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6372 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6373 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6374 33aa809d 2019-08-08 stsp printf("q\n");
6375 33aa809d 2019-08-08 stsp } else
6376 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6377 33aa809d 2019-08-08 stsp free(line);
6378 33aa809d 2019-08-08 stsp return NULL;
6379 33aa809d 2019-08-08 stsp }
6380 33aa809d 2019-08-08 stsp
6381 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6382 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6383 33aa809d 2019-08-08 stsp a->action);
6384 33aa809d 2019-08-08 stsp if (err)
6385 33aa809d 2019-08-08 stsp return err;
6386 33aa809d 2019-08-08 stsp resp = getchar();
6387 33aa809d 2019-08-08 stsp if (resp == '\n')
6388 33aa809d 2019-08-08 stsp resp = getchar();
6389 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6390 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6391 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6392 33aa809d 2019-08-08 stsp resp = ' ';
6393 33aa809d 2019-08-08 stsp }
6394 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6395 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6396 33aa809d 2019-08-08 stsp resp = ' ';
6397 33aa809d 2019-08-08 stsp }
6398 33aa809d 2019-08-08 stsp }
6399 33aa809d 2019-08-08 stsp
6400 33aa809d 2019-08-08 stsp if (resp == 'y')
6401 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6402 33aa809d 2019-08-08 stsp else if (resp == 'n')
6403 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6404 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6405 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6406 33aa809d 2019-08-08 stsp
6407 1ee397ad 2019-07-12 stsp return NULL;
6408 a129376b 2019-03-28 stsp }
6409 a129376b 2019-03-28 stsp
6410 33aa809d 2019-08-08 stsp
6411 a129376b 2019-03-28 stsp static const struct got_error *
6412 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6413 a129376b 2019-03-28 stsp {
6414 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6415 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6416 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6417 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6418 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6419 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6420 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6421 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6422 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6423 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6424 a129376b 2019-03-28 stsp
6425 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6426 e20a8b6f 2019-06-04 stsp
6427 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6428 a129376b 2019-03-28 stsp switch (ch) {
6429 33aa809d 2019-08-08 stsp case 'p':
6430 33aa809d 2019-08-08 stsp pflag = 1;
6431 33aa809d 2019-08-08 stsp break;
6432 33aa809d 2019-08-08 stsp case 'F':
6433 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6434 33aa809d 2019-08-08 stsp break;
6435 0f6d7415 2019-08-08 stsp case 'R':
6436 0f6d7415 2019-08-08 stsp can_recurse = 1;
6437 0f6d7415 2019-08-08 stsp break;
6438 a129376b 2019-03-28 stsp default:
6439 a129376b 2019-03-28 stsp usage_revert();
6440 a129376b 2019-03-28 stsp /* NOTREACHED */
6441 a129376b 2019-03-28 stsp }
6442 a129376b 2019-03-28 stsp }
6443 a129376b 2019-03-28 stsp
6444 a129376b 2019-03-28 stsp argc -= optind;
6445 a129376b 2019-03-28 stsp argv += optind;
6446 a129376b 2019-03-28 stsp
6447 43012d58 2019-07-14 stsp #ifndef PROFILE
6448 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6449 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6450 43012d58 2019-07-14 stsp err(1, "pledge");
6451 43012d58 2019-07-14 stsp #endif
6452 e20a8b6f 2019-06-04 stsp if (argc < 1)
6453 a129376b 2019-03-28 stsp usage_revert();
6454 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6455 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6456 a129376b 2019-03-28 stsp
6457 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6458 a129376b 2019-03-28 stsp if (cwd == NULL) {
6459 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6460 a129376b 2019-03-28 stsp goto done;
6461 a129376b 2019-03-28 stsp }
6462 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6463 fa51e947 2020-03-27 stsp if (error) {
6464 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6465 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6466 2ec1f75b 2019-03-26 stsp goto done;
6467 fa51e947 2020-03-27 stsp }
6468 a129376b 2019-03-28 stsp
6469 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6470 c9956ddf 2019-09-08 stsp NULL);
6471 a129376b 2019-03-28 stsp if (error != NULL)
6472 a129376b 2019-03-28 stsp goto done;
6473 a129376b 2019-03-28 stsp
6474 33aa809d 2019-08-08 stsp if (patch_script_path) {
6475 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6476 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6477 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6478 33aa809d 2019-08-08 stsp patch_script_path);
6479 33aa809d 2019-08-08 stsp goto done;
6480 33aa809d 2019-08-08 stsp }
6481 33aa809d 2019-08-08 stsp }
6482 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6483 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6484 a129376b 2019-03-28 stsp if (error)
6485 a129376b 2019-03-28 stsp goto done;
6486 a129376b 2019-03-28 stsp
6487 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6488 6d022e97 2019-08-04 stsp if (error)
6489 6d022e97 2019-08-04 stsp goto done;
6490 00db391e 2019-08-03 stsp
6491 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6492 0f6d7415 2019-08-08 stsp char *ondisk_path;
6493 0f6d7415 2019-08-08 stsp struct stat sb;
6494 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6495 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6496 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6497 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6498 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6499 0f6d7415 2019-08-08 stsp goto done;
6500 0f6d7415 2019-08-08 stsp }
6501 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6502 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6503 0f6d7415 2019-08-08 stsp free(ondisk_path);
6504 0f6d7415 2019-08-08 stsp continue;
6505 0f6d7415 2019-08-08 stsp }
6506 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6507 0f6d7415 2019-08-08 stsp ondisk_path);
6508 0f6d7415 2019-08-08 stsp free(ondisk_path);
6509 0f6d7415 2019-08-08 stsp goto done;
6510 0f6d7415 2019-08-08 stsp }
6511 0f6d7415 2019-08-08 stsp free(ondisk_path);
6512 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6513 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6514 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6515 0f6d7415 2019-08-08 stsp goto done;
6516 0f6d7415 2019-08-08 stsp }
6517 0f6d7415 2019-08-08 stsp }
6518 0f6d7415 2019-08-08 stsp }
6519 0f6d7415 2019-08-08 stsp
6520 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6521 33aa809d 2019-08-08 stsp cpa.action = "revert";
6522 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6523 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6524 2ec1f75b 2019-03-26 stsp done:
6525 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6526 33aa809d 2019-08-08 stsp error == NULL)
6527 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6528 2ec1f75b 2019-03-26 stsp if (repo)
6529 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6530 2ec1f75b 2019-03-26 stsp if (worktree)
6531 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6532 2ec1f75b 2019-03-26 stsp free(path);
6533 d00136be 2019-03-26 stsp free(cwd);
6534 6bad629b 2019-02-04 stsp return error;
6535 c4296144 2019-05-09 stsp }
6536 c4296144 2019-05-09 stsp
6537 c4296144 2019-05-09 stsp __dead static void
6538 c4296144 2019-05-09 stsp usage_commit(void)
6539 c4296144 2019-05-09 stsp {
6540 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6541 5c1e53bc 2019-07-28 stsp getprogname());
6542 c4296144 2019-05-09 stsp exit(1);
6543 33ad4cbe 2019-05-12 jcs }
6544 33ad4cbe 2019-05-12 jcs
6545 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6546 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6547 e2ba3d07 2019-05-13 stsp const char *editor;
6548 e0870e44 2019-05-13 stsp const char *worktree_path;
6549 76d98825 2019-06-03 stsp const char *branch_name;
6550 314a6357 2019-05-13 stsp const char *repo_path;
6551 e0870e44 2019-05-13 stsp char *logmsg_path;
6552 e2ba3d07 2019-05-13 stsp
6553 e2ba3d07 2019-05-13 stsp };
6554 e0870e44 2019-05-13 stsp
6555 33ad4cbe 2019-05-12 jcs static const struct got_error *
6556 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6557 33ad4cbe 2019-05-12 jcs void *arg)
6558 33ad4cbe 2019-05-12 jcs {
6559 76d98825 2019-06-03 stsp char *initial_content = NULL;
6560 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6561 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6562 e0870e44 2019-05-13 stsp char *template = NULL;
6563 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6564 1601cb9f 2020-09-11 naddy int initial_content_len;
6565 97972933 2020-09-11 stsp int fd = -1;
6566 33ad4cbe 2019-05-12 jcs size_t len;
6567 33ad4cbe 2019-05-12 jcs
6568 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6569 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6570 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6571 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6572 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6573 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6574 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6575 33ad4cbe 2019-05-12 jcs return NULL;
6576 33ad4cbe 2019-05-12 jcs }
6577 33ad4cbe 2019-05-12 jcs
6578 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6579 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6580 76d98825 2019-06-03 stsp
6581 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6582 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6583 1601cb9f 2020-09-11 naddy a->branch_name);
6584 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6585 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6586 e0870e44 2019-05-13 stsp
6587 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6588 793c30b5 2019-05-13 stsp if (err)
6589 e0870e44 2019-05-13 stsp goto done;
6590 33ad4cbe 2019-05-12 jcs
6591 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6592 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6593 97972933 2020-09-11 stsp goto done;
6594 97972933 2020-09-11 stsp }
6595 33ad4cbe 2019-05-12 jcs
6596 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6597 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6598 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6599 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6600 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6601 33ad4cbe 2019-05-12 jcs }
6602 33ad4cbe 2019-05-12 jcs
6603 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6604 33ad4cbe 2019-05-12 jcs done:
6605 76d98825 2019-06-03 stsp free(initial_content);
6606 e0870e44 2019-05-13 stsp free(template);
6607 314a6357 2019-05-13 stsp
6608 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6609 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6610 97972933 2020-09-11 stsp
6611 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6612 59f86c76 2020-09-11 stsp if (err == NULL)
6613 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6614 59f86c76 2020-09-11 stsp if (err) {
6615 59f86c76 2020-09-11 stsp free(*logmsg);
6616 59f86c76 2020-09-11 stsp *logmsg = NULL;
6617 3ce1b845 2019-07-15 stsp }
6618 33ad4cbe 2019-05-12 jcs return err;
6619 6bad629b 2019-02-04 stsp }
6620 c4296144 2019-05-09 stsp
6621 c4296144 2019-05-09 stsp static const struct got_error *
6622 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6623 c4296144 2019-05-09 stsp {
6624 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6625 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6626 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6627 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6628 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6629 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6630 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6631 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6632 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6633 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6634 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6635 5c1e53bc 2019-07-28 stsp
6636 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6637 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6638 c4296144 2019-05-09 stsp
6639 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6640 c4296144 2019-05-09 stsp switch (ch) {
6641 c4296144 2019-05-09 stsp case 'm':
6642 c4296144 2019-05-09 stsp logmsg = optarg;
6643 c4296144 2019-05-09 stsp break;
6644 35213c7c 2020-07-23 stsp case 'S':
6645 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6646 35213c7c 2020-07-23 stsp break;
6647 c4296144 2019-05-09 stsp default:
6648 c4296144 2019-05-09 stsp usage_commit();
6649 c4296144 2019-05-09 stsp /* NOTREACHED */
6650 c4296144 2019-05-09 stsp }
6651 c4296144 2019-05-09 stsp }
6652 c4296144 2019-05-09 stsp
6653 c4296144 2019-05-09 stsp argc -= optind;
6654 c4296144 2019-05-09 stsp argv += optind;
6655 c4296144 2019-05-09 stsp
6656 43012d58 2019-07-14 stsp #ifndef PROFILE
6657 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6658 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6659 43012d58 2019-07-14 stsp err(1, "pledge");
6660 43012d58 2019-07-14 stsp #endif
6661 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6662 c4296144 2019-05-09 stsp if (cwd == NULL) {
6663 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6664 c4296144 2019-05-09 stsp goto done;
6665 c4296144 2019-05-09 stsp }
6666 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6667 fa51e947 2020-03-27 stsp if (error) {
6668 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6669 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6670 7d5807f4 2019-07-11 stsp goto done;
6671 fa51e947 2020-03-27 stsp }
6672 7d5807f4 2019-07-11 stsp
6673 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6674 c4296144 2019-05-09 stsp if (error)
6675 a698f62e 2019-07-25 stsp goto done;
6676 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6677 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6678 c4296144 2019-05-09 stsp goto done;
6679 a698f62e 2019-07-25 stsp }
6680 5c1e53bc 2019-07-28 stsp
6681 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6682 916f288c 2019-07-30 stsp worktree);
6683 5c1e53bc 2019-07-28 stsp if (error)
6684 5c1e53bc 2019-07-28 stsp goto done;
6685 c4296144 2019-05-09 stsp
6686 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6687 c9956ddf 2019-09-08 stsp if (error)
6688 c9956ddf 2019-09-08 stsp goto done;
6689 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6690 c9956ddf 2019-09-08 stsp gitconfig_path);
6691 c4296144 2019-05-09 stsp if (error != NULL)
6692 c4296144 2019-05-09 stsp goto done;
6693 c4296144 2019-05-09 stsp
6694 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6695 aba9c984 2019-09-08 stsp if (error)
6696 aba9c984 2019-09-08 stsp return error;
6697 aba9c984 2019-09-08 stsp
6698 314a6357 2019-05-13 stsp /*
6699 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6700 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6701 314a6357 2019-05-13 stsp */
6702 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6703 314a6357 2019-05-13 stsp error = get_editor(&editor);
6704 314a6357 2019-05-13 stsp else
6705 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6706 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6707 c4296144 2019-05-09 stsp if (error)
6708 c4296144 2019-05-09 stsp goto done;
6709 c4296144 2019-05-09 stsp
6710 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6711 087fb88c 2019-08-04 stsp if (error)
6712 087fb88c 2019-08-04 stsp goto done;
6713 087fb88c 2019-08-04 stsp
6714 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6715 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6716 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6717 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6718 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6719 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6720 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6721 916f288c 2019-07-30 stsp goto done;
6722 916f288c 2019-07-30 stsp }
6723 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6724 916f288c 2019-07-30 stsp }
6725 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6726 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6727 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6728 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6729 e0870e44 2019-05-13 stsp if (error) {
6730 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6731 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6732 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6733 c4296144 2019-05-09 stsp goto done;
6734 e0870e44 2019-05-13 stsp }
6735 c4296144 2019-05-09 stsp
6736 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6737 c4296144 2019-05-09 stsp if (error)
6738 c4296144 2019-05-09 stsp goto done;
6739 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6740 c4296144 2019-05-09 stsp done:
6741 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6742 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6743 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6744 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6745 7266f21f 2019-10-21 stsp error == NULL)
6746 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6747 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6748 c4296144 2019-05-09 stsp if (repo)
6749 c4296144 2019-05-09 stsp got_repo_close(repo);
6750 c4296144 2019-05-09 stsp if (worktree)
6751 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6752 c4296144 2019-05-09 stsp free(cwd);
6753 c4296144 2019-05-09 stsp free(id_str);
6754 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6755 e2ba3d07 2019-05-13 stsp free(editor);
6756 aba9c984 2019-09-08 stsp free(author);
6757 234035bc 2019-06-01 stsp return error;
6758 234035bc 2019-06-01 stsp }
6759 234035bc 2019-06-01 stsp
6760 234035bc 2019-06-01 stsp __dead static void
6761 234035bc 2019-06-01 stsp usage_cherrypick(void)
6762 234035bc 2019-06-01 stsp {
6763 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6764 234035bc 2019-06-01 stsp exit(1);
6765 234035bc 2019-06-01 stsp }
6766 234035bc 2019-06-01 stsp
6767 234035bc 2019-06-01 stsp static const struct got_error *
6768 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6769 234035bc 2019-06-01 stsp {
6770 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6771 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6772 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6773 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6774 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6775 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6776 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6777 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6778 9627c110 2020-04-18 stsp int ch;
6779 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6780 234035bc 2019-06-01 stsp
6781 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6782 234035bc 2019-06-01 stsp switch (ch) {
6783 234035bc 2019-06-01 stsp default:
6784 234035bc 2019-06-01 stsp usage_cherrypick();
6785 234035bc 2019-06-01 stsp /* NOTREACHED */
6786 234035bc 2019-06-01 stsp }
6787 234035bc 2019-06-01 stsp }
6788 234035bc 2019-06-01 stsp
6789 234035bc 2019-06-01 stsp argc -= optind;
6790 234035bc 2019-06-01 stsp argv += optind;
6791 234035bc 2019-06-01 stsp
6792 43012d58 2019-07-14 stsp #ifndef PROFILE
6793 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6794 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6795 43012d58 2019-07-14 stsp err(1, "pledge");
6796 43012d58 2019-07-14 stsp #endif
6797 234035bc 2019-06-01 stsp if (argc != 1)
6798 234035bc 2019-06-01 stsp usage_cherrypick();
6799 234035bc 2019-06-01 stsp
6800 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6801 234035bc 2019-06-01 stsp if (cwd == NULL) {
6802 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6803 234035bc 2019-06-01 stsp goto done;
6804 234035bc 2019-06-01 stsp }
6805 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6806 fa51e947 2020-03-27 stsp if (error) {
6807 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6808 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6809 fa51e947 2020-03-27 stsp cwd);
6810 234035bc 2019-06-01 stsp goto done;
6811 fa51e947 2020-03-27 stsp }
6812 234035bc 2019-06-01 stsp
6813 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6814 c9956ddf 2019-09-08 stsp NULL);
6815 234035bc 2019-06-01 stsp if (error != NULL)
6816 234035bc 2019-06-01 stsp goto done;
6817 234035bc 2019-06-01 stsp
6818 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6819 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6820 234035bc 2019-06-01 stsp if (error)
6821 234035bc 2019-06-01 stsp goto done;
6822 234035bc 2019-06-01 stsp
6823 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6824 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6825 234035bc 2019-06-01 stsp if (error != NULL) {
6826 234035bc 2019-06-01 stsp struct got_reference *ref;
6827 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6828 234035bc 2019-06-01 stsp goto done;
6829 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6830 234035bc 2019-06-01 stsp if (error != NULL)
6831 234035bc 2019-06-01 stsp goto done;
6832 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6833 234035bc 2019-06-01 stsp got_ref_close(ref);
6834 234035bc 2019-06-01 stsp if (error != NULL)
6835 234035bc 2019-06-01 stsp goto done;
6836 234035bc 2019-06-01 stsp }
6837 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6838 234035bc 2019-06-01 stsp if (error)
6839 234035bc 2019-06-01 stsp goto done;
6840 234035bc 2019-06-01 stsp
6841 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6842 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6843 234035bc 2019-06-01 stsp if (error != NULL)
6844 234035bc 2019-06-01 stsp goto done;
6845 234035bc 2019-06-01 stsp
6846 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6847 234035bc 2019-06-01 stsp if (error) {
6848 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6849 234035bc 2019-06-01 stsp goto done;
6850 234035bc 2019-06-01 stsp error = NULL;
6851 234035bc 2019-06-01 stsp } else {
6852 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6853 234035bc 2019-06-01 stsp goto done;
6854 234035bc 2019-06-01 stsp }
6855 234035bc 2019-06-01 stsp
6856 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6857 234035bc 2019-06-01 stsp if (error)
6858 234035bc 2019-06-01 stsp goto done;
6859 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6860 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6861 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6862 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6863 03415a1a 2019-06-02 stsp NULL);
6864 234035bc 2019-06-01 stsp if (error != NULL)
6865 234035bc 2019-06-01 stsp goto done;
6866 234035bc 2019-06-01 stsp
6867 9627c110 2020-04-18 stsp if (upa.did_something)
6868 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6869 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6870 234035bc 2019-06-01 stsp done:
6871 234035bc 2019-06-01 stsp if (commit)
6872 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6873 234035bc 2019-06-01 stsp free(commit_id_str);
6874 234035bc 2019-06-01 stsp if (head_ref)
6875 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6876 234035bc 2019-06-01 stsp if (worktree)
6877 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6878 234035bc 2019-06-01 stsp if (repo)
6879 234035bc 2019-06-01 stsp got_repo_close(repo);
6880 c4296144 2019-05-09 stsp return error;
6881 c4296144 2019-05-09 stsp }
6882 5ef14e63 2019-06-02 stsp
6883 5ef14e63 2019-06-02 stsp __dead static void
6884 5ef14e63 2019-06-02 stsp usage_backout(void)
6885 5ef14e63 2019-06-02 stsp {
6886 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6887 5ef14e63 2019-06-02 stsp exit(1);
6888 5ef14e63 2019-06-02 stsp }
6889 5ef14e63 2019-06-02 stsp
6890 5ef14e63 2019-06-02 stsp static const struct got_error *
6891 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6892 5ef14e63 2019-06-02 stsp {
6893 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6894 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6895 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6896 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6897 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6898 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6899 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6900 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6901 9627c110 2020-04-18 stsp int ch;
6902 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6903 5ef14e63 2019-06-02 stsp
6904 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6905 5ef14e63 2019-06-02 stsp switch (ch) {
6906 5ef14e63 2019-06-02 stsp default:
6907 5ef14e63 2019-06-02 stsp usage_backout();
6908 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6909 5ef14e63 2019-06-02 stsp }
6910 5ef14e63 2019-06-02 stsp }
6911 5ef14e63 2019-06-02 stsp
6912 5ef14e63 2019-06-02 stsp argc -= optind;
6913 5ef14e63 2019-06-02 stsp argv += optind;
6914 5ef14e63 2019-06-02 stsp
6915 43012d58 2019-07-14 stsp #ifndef PROFILE
6916 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6917 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6918 43012d58 2019-07-14 stsp err(1, "pledge");
6919 43012d58 2019-07-14 stsp #endif
6920 5ef14e63 2019-06-02 stsp if (argc != 1)
6921 5ef14e63 2019-06-02 stsp usage_backout();
6922 5ef14e63 2019-06-02 stsp
6923 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6924 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6925 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6926 5ef14e63 2019-06-02 stsp goto done;
6927 5ef14e63 2019-06-02 stsp }
6928 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6929 fa51e947 2020-03-27 stsp if (error) {
6930 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6931 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6932 5ef14e63 2019-06-02 stsp goto done;
6933 fa51e947 2020-03-27 stsp }
6934 5ef14e63 2019-06-02 stsp
6935 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6936 c9956ddf 2019-09-08 stsp NULL);
6937 5ef14e63 2019-06-02 stsp if (error != NULL)
6938 5ef14e63 2019-06-02 stsp goto done;
6939 5ef14e63 2019-06-02 stsp
6940 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6941 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6942 5ef14e63 2019-06-02 stsp if (error)
6943 5ef14e63 2019-06-02 stsp goto done;
6944 5ef14e63 2019-06-02 stsp
6945 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6946 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6947 5ef14e63 2019-06-02 stsp if (error != NULL) {
6948 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6949 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6950 5ef14e63 2019-06-02 stsp goto done;
6951 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6952 5ef14e63 2019-06-02 stsp if (error != NULL)
6953 5ef14e63 2019-06-02 stsp goto done;
6954 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6955 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6956 5ef14e63 2019-06-02 stsp if (error != NULL)
6957 5ef14e63 2019-06-02 stsp goto done;
6958 5ef14e63 2019-06-02 stsp }
6959 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6960 5ef14e63 2019-06-02 stsp if (error)
6961 5ef14e63 2019-06-02 stsp goto done;
6962 5ef14e63 2019-06-02 stsp
6963 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6964 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6965 5ef14e63 2019-06-02 stsp if (error != NULL)
6966 5ef14e63 2019-06-02 stsp goto done;
6967 5ef14e63 2019-06-02 stsp
6968 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6969 5ef14e63 2019-06-02 stsp if (error)
6970 5ef14e63 2019-06-02 stsp goto done;
6971 5ef14e63 2019-06-02 stsp
6972 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6973 5ef14e63 2019-06-02 stsp if (error)
6974 5ef14e63 2019-06-02 stsp goto done;
6975 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6976 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6977 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6978 5ef14e63 2019-06-02 stsp goto done;
6979 5ef14e63 2019-06-02 stsp }
6980 5ef14e63 2019-06-02 stsp
6981 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6982 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6983 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
6984 5ef14e63 2019-06-02 stsp if (error != NULL)
6985 5ef14e63 2019-06-02 stsp goto done;
6986 5ef14e63 2019-06-02 stsp
6987 9627c110 2020-04-18 stsp if (upa.did_something)
6988 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6989 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6990 5ef14e63 2019-06-02 stsp done:
6991 5ef14e63 2019-06-02 stsp if (commit)
6992 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6993 5ef14e63 2019-06-02 stsp free(commit_id_str);
6994 5ef14e63 2019-06-02 stsp if (head_ref)
6995 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6996 818c7501 2019-07-11 stsp if (worktree)
6997 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6998 818c7501 2019-07-11 stsp if (repo)
6999 818c7501 2019-07-11 stsp got_repo_close(repo);
7000 818c7501 2019-07-11 stsp return error;
7001 818c7501 2019-07-11 stsp }
7002 818c7501 2019-07-11 stsp
7003 818c7501 2019-07-11 stsp __dead static void
7004 818c7501 2019-07-11 stsp usage_rebase(void)
7005 818c7501 2019-07-11 stsp {
7006 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7007 af54c8f8 2019-07-11 stsp getprogname());
7008 818c7501 2019-07-11 stsp exit(1);
7009 0ebf8283 2019-07-24 stsp }
7010 0ebf8283 2019-07-24 stsp
7011 0ebf8283 2019-07-24 stsp void
7012 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7013 0ebf8283 2019-07-24 stsp {
7014 0ebf8283 2019-07-24 stsp char *nl;
7015 0ebf8283 2019-07-24 stsp size_t len;
7016 0ebf8283 2019-07-24 stsp
7017 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7018 0ebf8283 2019-07-24 stsp if (len > limit)
7019 0ebf8283 2019-07-24 stsp len = limit;
7020 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7021 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7022 0ebf8283 2019-07-24 stsp if (nl)
7023 0ebf8283 2019-07-24 stsp *nl = '\0';
7024 0ebf8283 2019-07-24 stsp }
7025 0ebf8283 2019-07-24 stsp
7026 0ebf8283 2019-07-24 stsp static const struct got_error *
7027 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7028 0ebf8283 2019-07-24 stsp {
7029 5943eee2 2019-08-13 stsp const struct got_error *err;
7030 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7031 5943eee2 2019-08-13 stsp const char *s;
7032 0ebf8283 2019-07-24 stsp
7033 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7034 5943eee2 2019-08-13 stsp if (err)
7035 5943eee2 2019-08-13 stsp return err;
7036 0ebf8283 2019-07-24 stsp
7037 5943eee2 2019-08-13 stsp s = logmsg0;
7038 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7039 5943eee2 2019-08-13 stsp s++;
7040 5943eee2 2019-08-13 stsp
7041 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7042 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7043 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7044 5943eee2 2019-08-13 stsp goto done;
7045 5943eee2 2019-08-13 stsp }
7046 0ebf8283 2019-07-24 stsp
7047 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7048 5943eee2 2019-08-13 stsp done:
7049 5943eee2 2019-08-13 stsp free(logmsg0);
7050 a0ea4fc0 2020-02-28 stsp return err;
7051 a0ea4fc0 2020-02-28 stsp }
7052 a0ea4fc0 2020-02-28 stsp
7053 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7054 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7055 a0ea4fc0 2020-02-28 stsp {
7056 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7057 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7058 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7059 a0ea4fc0 2020-02-28 stsp
7060 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7061 a0ea4fc0 2020-02-28 stsp if (err)
7062 a0ea4fc0 2020-02-28 stsp return err;
7063 a0ea4fc0 2020-02-28 stsp
7064 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7065 a0ea4fc0 2020-02-28 stsp if (err)
7066 a0ea4fc0 2020-02-28 stsp goto done;
7067 a0ea4fc0 2020-02-28 stsp
7068 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7069 a0ea4fc0 2020-02-28 stsp
7070 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7071 a0ea4fc0 2020-02-28 stsp if (err)
7072 a0ea4fc0 2020-02-28 stsp goto done;
7073 a0ea4fc0 2020-02-28 stsp
7074 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7075 a0ea4fc0 2020-02-28 stsp done:
7076 a0ea4fc0 2020-02-28 stsp free(id_str);
7077 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7078 a0ea4fc0 2020-02-28 stsp free(logmsg);
7079 5943eee2 2019-08-13 stsp return err;
7080 818c7501 2019-07-11 stsp }
7081 818c7501 2019-07-11 stsp
7082 818c7501 2019-07-11 stsp static const struct got_error *
7083 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7084 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7085 818c7501 2019-07-11 stsp {
7086 818c7501 2019-07-11 stsp const struct got_error *err;
7087 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7088 818c7501 2019-07-11 stsp
7089 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7090 818c7501 2019-07-11 stsp if (err)
7091 818c7501 2019-07-11 stsp goto done;
7092 818c7501 2019-07-11 stsp
7093 ff0d2220 2019-07-11 stsp if (new_id) {
7094 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7095 ff0d2220 2019-07-11 stsp if (err)
7096 ff0d2220 2019-07-11 stsp goto done;
7097 ff0d2220 2019-07-11 stsp }
7098 818c7501 2019-07-11 stsp
7099 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7100 ff0d2220 2019-07-11 stsp if (new_id_str)
7101 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7102 0ebf8283 2019-07-24 stsp
7103 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7104 0ebf8283 2019-07-24 stsp if (err)
7105 0ebf8283 2019-07-24 stsp goto done;
7106 0ebf8283 2019-07-24 stsp
7107 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7108 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7109 818c7501 2019-07-11 stsp done:
7110 818c7501 2019-07-11 stsp free(old_id_str);
7111 818c7501 2019-07-11 stsp free(new_id_str);
7112 272a1371 2020-02-28 stsp free(logmsg);
7113 818c7501 2019-07-11 stsp return err;
7114 818c7501 2019-07-11 stsp }
7115 818c7501 2019-07-11 stsp
7116 1ee397ad 2019-07-12 stsp static const struct got_error *
7117 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7118 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7119 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7120 818c7501 2019-07-11 stsp {
7121 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7122 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7123 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7124 818c7501 2019-07-11 stsp }
7125 818c7501 2019-07-11 stsp
7126 818c7501 2019-07-11 stsp static const struct got_error *
7127 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7128 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7129 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7130 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7131 ff0d2220 2019-07-11 stsp {
7132 ff0d2220 2019-07-11 stsp const struct got_error *error;
7133 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7134 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7135 ff0d2220 2019-07-11 stsp
7136 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7137 ff0d2220 2019-07-11 stsp if (error)
7138 ff0d2220 2019-07-11 stsp return error;
7139 ff0d2220 2019-07-11 stsp
7140 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7141 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7142 ff0d2220 2019-07-11 stsp if (error) {
7143 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7144 ff0d2220 2019-07-11 stsp goto done;
7145 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7146 ff0d2220 2019-07-11 stsp } else {
7147 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7148 ff0d2220 2019-07-11 stsp free(new_commit_id);
7149 ff0d2220 2019-07-11 stsp }
7150 ff0d2220 2019-07-11 stsp done:
7151 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7152 ff0d2220 2019-07-11 stsp return error;
7153 64c6d990 2019-07-11 stsp }
7154 64c6d990 2019-07-11 stsp
7155 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7156 64c6d990 2019-07-11 stsp const char *path_prefix;
7157 64c6d990 2019-07-11 stsp size_t len;
7158 8ca9bd68 2019-07-25 stsp int errcode;
7159 64c6d990 2019-07-11 stsp };
7160 64c6d990 2019-07-11 stsp
7161 64c6d990 2019-07-11 stsp static const struct got_error *
7162 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7163 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7164 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7165 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7166 64c6d990 2019-07-11 stsp {
7167 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7168 64c6d990 2019-07-11 stsp
7169 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7170 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7171 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7172 64c6d990 2019-07-11 stsp
7173 64c6d990 2019-07-11 stsp return NULL;
7174 64c6d990 2019-07-11 stsp }
7175 64c6d990 2019-07-11 stsp
7176 64c6d990 2019-07-11 stsp static const struct got_error *
7177 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7178 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7179 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7180 64c6d990 2019-07-11 stsp {
7181 64c6d990 2019-07-11 stsp const struct got_error *err;
7182 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7183 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7184 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7185 64c6d990 2019-07-11 stsp
7186 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7187 64c6d990 2019-07-11 stsp return NULL;
7188 64c6d990 2019-07-11 stsp
7189 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7190 64c6d990 2019-07-11 stsp if (err)
7191 64c6d990 2019-07-11 stsp goto done;
7192 64c6d990 2019-07-11 stsp
7193 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7194 64c6d990 2019-07-11 stsp if (err)
7195 64c6d990 2019-07-11 stsp goto done;
7196 64c6d990 2019-07-11 stsp
7197 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7198 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7199 64c6d990 2019-07-11 stsp if (err)
7200 64c6d990 2019-07-11 stsp goto done;
7201 64c6d990 2019-07-11 stsp
7202 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7203 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7204 64c6d990 2019-07-11 stsp if (err)
7205 64c6d990 2019-07-11 stsp goto done;
7206 64c6d990 2019-07-11 stsp
7207 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7208 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7209 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7210 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7211 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7212 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7213 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7214 64c6d990 2019-07-11 stsp done:
7215 64c6d990 2019-07-11 stsp if (tree1)
7216 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7217 64c6d990 2019-07-11 stsp if (tree2)
7218 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7219 64c6d990 2019-07-11 stsp if (commit)
7220 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7221 64c6d990 2019-07-11 stsp if (parent_commit)
7222 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7223 64c6d990 2019-07-11 stsp return err;
7224 ff0d2220 2019-07-11 stsp }
7225 ff0d2220 2019-07-11 stsp
7226 ff0d2220 2019-07-11 stsp static const struct got_error *
7227 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7228 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7229 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7230 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7231 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7232 af61c510 2019-07-19 stsp {
7233 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7234 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7235 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7236 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7237 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7238 af61c510 2019-07-19 stsp
7239 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7240 af61c510 2019-07-19 stsp if (err)
7241 af61c510 2019-07-19 stsp return err;
7242 af61c510 2019-07-19 stsp
7243 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7244 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7245 af61c510 2019-07-19 stsp if (err)
7246 af61c510 2019-07-19 stsp goto done;
7247 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7248 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7249 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7250 af61c510 2019-07-19 stsp if (err) {
7251 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7252 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7253 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7254 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7255 af61c510 2019-07-19 stsp "been reached?!?");
7256 ee780d5c 2020-01-04 stsp }
7257 ee780d5c 2020-01-04 stsp goto done;
7258 af61c510 2019-07-19 stsp } else {
7259 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7260 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7261 af61c510 2019-07-19 stsp if (err)
7262 af61c510 2019-07-19 stsp goto done;
7263 af61c510 2019-07-19 stsp
7264 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7265 af61c510 2019-07-19 stsp if (err)
7266 af61c510 2019-07-19 stsp goto done;
7267 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7268 af61c510 2019-07-19 stsp commit_id = parent_id;
7269 af61c510 2019-07-19 stsp }
7270 af61c510 2019-07-19 stsp }
7271 af61c510 2019-07-19 stsp done:
7272 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7273 af61c510 2019-07-19 stsp return err;
7274 af61c510 2019-07-19 stsp }
7275 af61c510 2019-07-19 stsp
7276 af61c510 2019-07-19 stsp static const struct got_error *
7277 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7278 818c7501 2019-07-11 stsp {
7279 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7280 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7281 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7282 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7283 818c7501 2019-07-11 stsp char *cwd = NULL;
7284 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7285 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7286 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7287 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7288 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7289 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7290 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7291 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7292 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7293 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7294 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7295 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7296 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7297 818c7501 2019-07-11 stsp
7298 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7299 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7300 818c7501 2019-07-11 stsp
7301 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7302 818c7501 2019-07-11 stsp switch (ch) {
7303 818c7501 2019-07-11 stsp case 'a':
7304 818c7501 2019-07-11 stsp abort_rebase = 1;
7305 818c7501 2019-07-11 stsp break;
7306 818c7501 2019-07-11 stsp case 'c':
7307 818c7501 2019-07-11 stsp continue_rebase = 1;
7308 818c7501 2019-07-11 stsp break;
7309 818c7501 2019-07-11 stsp default:
7310 818c7501 2019-07-11 stsp usage_rebase();
7311 818c7501 2019-07-11 stsp /* NOTREACHED */
7312 818c7501 2019-07-11 stsp }
7313 818c7501 2019-07-11 stsp }
7314 818c7501 2019-07-11 stsp
7315 818c7501 2019-07-11 stsp argc -= optind;
7316 818c7501 2019-07-11 stsp argv += optind;
7317 818c7501 2019-07-11 stsp
7318 43012d58 2019-07-14 stsp #ifndef PROFILE
7319 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7320 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7321 43012d58 2019-07-14 stsp err(1, "pledge");
7322 43012d58 2019-07-14 stsp #endif
7323 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7324 818c7501 2019-07-11 stsp usage_rebase();
7325 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7326 818c7501 2019-07-11 stsp if (argc != 0)
7327 818c7501 2019-07-11 stsp usage_rebase();
7328 818c7501 2019-07-11 stsp } else if (argc != 1)
7329 818c7501 2019-07-11 stsp usage_rebase();
7330 818c7501 2019-07-11 stsp
7331 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7332 818c7501 2019-07-11 stsp if (cwd == NULL) {
7333 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7334 818c7501 2019-07-11 stsp goto done;
7335 818c7501 2019-07-11 stsp }
7336 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7337 fa51e947 2020-03-27 stsp if (error) {
7338 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7339 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7340 818c7501 2019-07-11 stsp goto done;
7341 fa51e947 2020-03-27 stsp }
7342 818c7501 2019-07-11 stsp
7343 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7344 c9956ddf 2019-09-08 stsp NULL);
7345 818c7501 2019-07-11 stsp if (error != NULL)
7346 818c7501 2019-07-11 stsp goto done;
7347 818c7501 2019-07-11 stsp
7348 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7349 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7350 7ef62c4e 2020-02-24 stsp if (error)
7351 7ef62c4e 2020-02-24 stsp goto done;
7352 7ef62c4e 2020-02-24 stsp
7353 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7354 7ef62c4e 2020-02-24 stsp worktree);
7355 818c7501 2019-07-11 stsp if (error)
7356 7ef62c4e 2020-02-24 stsp goto done;
7357 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7358 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7359 818c7501 2019-07-11 stsp goto done;
7360 7ef62c4e 2020-02-24 stsp }
7361 818c7501 2019-07-11 stsp
7362 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7363 818c7501 2019-07-11 stsp if (error)
7364 818c7501 2019-07-11 stsp goto done;
7365 818c7501 2019-07-11 stsp
7366 f6794adc 2019-07-23 stsp if (abort_rebase) {
7367 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7368 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7369 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7370 f6794adc 2019-07-23 stsp goto done;
7371 f6794adc 2019-07-23 stsp }
7372 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7373 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7374 3e3a69f1 2019-07-25 stsp worktree, repo);
7375 818c7501 2019-07-11 stsp if (error)
7376 818c7501 2019-07-11 stsp goto done;
7377 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7378 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7379 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7380 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7381 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7382 818c7501 2019-07-11 stsp if (error)
7383 818c7501 2019-07-11 stsp goto done;
7384 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7385 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7386 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7387 818c7501 2019-07-11 stsp }
7388 818c7501 2019-07-11 stsp
7389 818c7501 2019-07-11 stsp if (continue_rebase) {
7390 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7391 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7392 f6794adc 2019-07-23 stsp goto done;
7393 f6794adc 2019-07-23 stsp }
7394 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7395 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7396 3e3a69f1 2019-07-25 stsp worktree, repo);
7397 818c7501 2019-07-11 stsp if (error)
7398 818c7501 2019-07-11 stsp goto done;
7399 818c7501 2019-07-11 stsp
7400 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7401 01757395 2019-07-12 stsp resume_commit_id, repo);
7402 818c7501 2019-07-11 stsp if (error)
7403 818c7501 2019-07-11 stsp goto done;
7404 818c7501 2019-07-11 stsp
7405 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7406 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7407 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7408 818c7501 2019-07-11 stsp goto done;
7409 818c7501 2019-07-11 stsp }
7410 818c7501 2019-07-11 stsp } else {
7411 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7412 818c7501 2019-07-11 stsp if (error != NULL)
7413 818c7501 2019-07-11 stsp goto done;
7414 ff0d2220 2019-07-11 stsp }
7415 818c7501 2019-07-11 stsp
7416 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7417 ff0d2220 2019-07-11 stsp if (error)
7418 ff0d2220 2019-07-11 stsp goto done;
7419 ff0d2220 2019-07-11 stsp
7420 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7421 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7422 a51a74b3 2019-07-27 stsp
7423 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7424 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7425 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7426 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7427 818c7501 2019-07-11 stsp if (error)
7428 818c7501 2019-07-11 stsp goto done;
7429 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7430 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7431 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7432 818c7501 2019-07-11 stsp "with work tree's branch");
7433 818c7501 2019-07-11 stsp goto done;
7434 818c7501 2019-07-11 stsp }
7435 818c7501 2019-07-11 stsp
7436 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7437 a51a74b3 2019-07-27 stsp if (error) {
7438 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7439 a51a74b3 2019-07-27 stsp goto done;
7440 a51a74b3 2019-07-27 stsp error = NULL;
7441 a51a74b3 2019-07-27 stsp } else {
7442 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7443 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7444 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7445 a51a74b3 2019-07-27 stsp goto done;
7446 a51a74b3 2019-07-27 stsp }
7447 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7448 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7449 818c7501 2019-07-11 stsp if (error)
7450 818c7501 2019-07-11 stsp goto done;
7451 818c7501 2019-07-11 stsp }
7452 818c7501 2019-07-11 stsp
7453 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7454 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7455 818c7501 2019-07-11 stsp if (error)
7456 818c7501 2019-07-11 stsp goto done;
7457 818c7501 2019-07-11 stsp
7458 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7459 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7460 fc66b545 2019-08-12 stsp if (pid == NULL) {
7461 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7462 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7463 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7464 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7465 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7466 fc66b545 2019-08-12 stsp if (error)
7467 fc66b545 2019-08-12 stsp goto done;
7468 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7469 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7470 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7471 9627c110 2020-04-18 stsp
7472 fc66b545 2019-08-12 stsp }
7473 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7474 fc66b545 2019-08-12 stsp goto done;
7475 fc66b545 2019-08-12 stsp }
7476 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7477 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7478 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7479 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7480 818c7501 2019-07-11 stsp commit = NULL;
7481 818c7501 2019-07-11 stsp if (error)
7482 818c7501 2019-07-11 stsp goto done;
7483 64c6d990 2019-07-11 stsp
7484 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7485 38b0338b 2019-11-29 stsp if (continue_rebase) {
7486 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7487 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7488 38b0338b 2019-11-29 stsp goto done;
7489 38b0338b 2019-11-29 stsp } else {
7490 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7491 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7492 38b0338b 2019-11-29 stsp char *id_str;
7493 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7494 38b0338b 2019-11-29 stsp new_base_branch);
7495 38b0338b 2019-11-29 stsp if (error)
7496 38b0338b 2019-11-29 stsp goto done;
7497 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7498 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7499 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7500 38b0338b 2019-11-29 stsp free(id_str);
7501 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7502 38b0338b 2019-11-29 stsp new_head_commit_id);
7503 38b0338b 2019-11-29 stsp if (error)
7504 38b0338b 2019-11-29 stsp goto done;
7505 38b0338b 2019-11-29 stsp }
7506 818c7501 2019-07-11 stsp }
7507 818c7501 2019-07-11 stsp
7508 818c7501 2019-07-11 stsp pid = NULL;
7509 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7510 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7511 9627c110 2020-04-18 stsp
7512 818c7501 2019-07-11 stsp commit_id = qid->id;
7513 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7514 818c7501 2019-07-11 stsp pid = qid;
7515 818c7501 2019-07-11 stsp
7516 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7517 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7518 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7519 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7520 818c7501 2019-07-11 stsp if (error)
7521 818c7501 2019-07-11 stsp goto done;
7522 9627c110 2020-04-18 stsp
7523 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7524 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7525 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7526 818c7501 2019-07-11 stsp
7527 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7528 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7529 a0ea4fc0 2020-02-28 stsp if (error)
7530 a0ea4fc0 2020-02-28 stsp goto done;
7531 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7532 818c7501 2019-07-11 stsp break;
7533 01757395 2019-07-12 stsp }
7534 818c7501 2019-07-11 stsp
7535 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7536 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7537 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7538 818c7501 2019-07-11 stsp if (error)
7539 818c7501 2019-07-11 stsp goto done;
7540 818c7501 2019-07-11 stsp }
7541 818c7501 2019-07-11 stsp
7542 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7543 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7544 818c7501 2019-07-11 stsp if (error)
7545 818c7501 2019-07-11 stsp goto done;
7546 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7547 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7548 818c7501 2019-07-11 stsp } else
7549 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7550 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7551 818c7501 2019-07-11 stsp done:
7552 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7553 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7554 818c7501 2019-07-11 stsp free(resume_commit_id);
7555 818c7501 2019-07-11 stsp free(yca_id);
7556 818c7501 2019-07-11 stsp if (commit)
7557 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7558 818c7501 2019-07-11 stsp if (branch)
7559 818c7501 2019-07-11 stsp got_ref_close(branch);
7560 818c7501 2019-07-11 stsp if (new_base_branch)
7561 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7562 818c7501 2019-07-11 stsp if (tmp_branch)
7563 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7564 5ef14e63 2019-06-02 stsp if (worktree)
7565 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7566 5ef14e63 2019-06-02 stsp if (repo)
7567 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7568 5ef14e63 2019-06-02 stsp return error;
7569 0ebf8283 2019-07-24 stsp }
7570 0ebf8283 2019-07-24 stsp
7571 0ebf8283 2019-07-24 stsp __dead static void
7572 0ebf8283 2019-07-24 stsp usage_histedit(void)
7573 0ebf8283 2019-07-24 stsp {
7574 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7575 0ebf8283 2019-07-24 stsp getprogname());
7576 0ebf8283 2019-07-24 stsp exit(1);
7577 0ebf8283 2019-07-24 stsp }
7578 0ebf8283 2019-07-24 stsp
7579 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7580 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7581 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7582 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7583 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7584 0ebf8283 2019-07-24 stsp
7585 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7586 0ebf8283 2019-07-24 stsp unsigned char code;
7587 0ebf8283 2019-07-24 stsp const char *name;
7588 0ebf8283 2019-07-24 stsp const char *desc;
7589 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7590 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7591 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7592 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7593 82997472 2020-01-29 stsp "be used" },
7594 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7595 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7596 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7597 0ebf8283 2019-07-24 stsp };
7598 0ebf8283 2019-07-24 stsp
7599 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7600 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7601 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7602 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7603 0ebf8283 2019-07-24 stsp char *logmsg;
7604 0ebf8283 2019-07-24 stsp };
7605 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7606 0ebf8283 2019-07-24 stsp
7607 0ebf8283 2019-07-24 stsp static const struct got_error *
7608 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7609 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7610 0ebf8283 2019-07-24 stsp {
7611 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7612 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7613 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7614 8138f3e1 2019-08-11 stsp int n;
7615 0ebf8283 2019-07-24 stsp
7616 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7617 0ebf8283 2019-07-24 stsp if (err)
7618 0ebf8283 2019-07-24 stsp goto done;
7619 0ebf8283 2019-07-24 stsp
7620 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7621 0ebf8283 2019-07-24 stsp if (err)
7622 0ebf8283 2019-07-24 stsp goto done;
7623 0ebf8283 2019-07-24 stsp
7624 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7625 0ebf8283 2019-07-24 stsp if (err)
7626 0ebf8283 2019-07-24 stsp goto done;
7627 0ebf8283 2019-07-24 stsp
7628 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7629 0ebf8283 2019-07-24 stsp if (n < 0)
7630 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7631 0ebf8283 2019-07-24 stsp done:
7632 0ebf8283 2019-07-24 stsp if (commit)
7633 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7634 0ebf8283 2019-07-24 stsp free(id_str);
7635 0ebf8283 2019-07-24 stsp free(logmsg);
7636 0ebf8283 2019-07-24 stsp return err;
7637 0ebf8283 2019-07-24 stsp }
7638 0ebf8283 2019-07-24 stsp
7639 0ebf8283 2019-07-24 stsp static const struct got_error *
7640 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7641 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7642 0ebf8283 2019-07-24 stsp {
7643 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7644 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7645 0ebf8283 2019-07-24 stsp
7646 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7647 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7648 0ebf8283 2019-07-24 stsp
7649 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7650 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7651 0ebf8283 2019-07-24 stsp f, repo);
7652 0ebf8283 2019-07-24 stsp if (err)
7653 0ebf8283 2019-07-24 stsp break;
7654 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7655 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7656 083957f4 2020-02-24 stsp if (n < 0) {
7657 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7658 083957f4 2020-02-24 stsp break;
7659 083957f4 2020-02-24 stsp }
7660 083957f4 2020-02-24 stsp }
7661 0ebf8283 2019-07-24 stsp }
7662 0ebf8283 2019-07-24 stsp
7663 0ebf8283 2019-07-24 stsp return err;
7664 0ebf8283 2019-07-24 stsp }
7665 0ebf8283 2019-07-24 stsp
7666 0ebf8283 2019-07-24 stsp static const struct got_error *
7667 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7668 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7669 0ebf8283 2019-07-24 stsp {
7670 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7671 0ebf8283 2019-07-24 stsp int n, i;
7672 514f2ffe 2020-01-29 stsp char *id_str;
7673 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7674 514f2ffe 2020-01-29 stsp
7675 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7676 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7677 514f2ffe 2020-01-29 stsp if (err)
7678 514f2ffe 2020-01-29 stsp return err;
7679 514f2ffe 2020-01-29 stsp
7680 514f2ffe 2020-01-29 stsp n = fprintf(f,
7681 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7682 514f2ffe 2020-01-29 stsp "# commit %s\n"
7683 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7684 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7685 514f2ffe 2020-01-29 stsp if (n < 0) {
7686 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7687 514f2ffe 2020-01-29 stsp goto done;
7688 514f2ffe 2020-01-29 stsp }
7689 0ebf8283 2019-07-24 stsp
7690 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7691 514f2ffe 2020-01-29 stsp if (n < 0) {
7692 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7693 514f2ffe 2020-01-29 stsp goto done;
7694 514f2ffe 2020-01-29 stsp }
7695 0ebf8283 2019-07-24 stsp
7696 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7697 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7698 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7699 0ebf8283 2019-07-24 stsp cmd->desc);
7700 0ebf8283 2019-07-24 stsp if (n < 0) {
7701 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7702 0ebf8283 2019-07-24 stsp break;
7703 0ebf8283 2019-07-24 stsp }
7704 0ebf8283 2019-07-24 stsp }
7705 514f2ffe 2020-01-29 stsp done:
7706 514f2ffe 2020-01-29 stsp free(id_str);
7707 0ebf8283 2019-07-24 stsp return err;
7708 0ebf8283 2019-07-24 stsp }
7709 0ebf8283 2019-07-24 stsp
7710 0ebf8283 2019-07-24 stsp static const struct got_error *
7711 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7712 0ebf8283 2019-07-24 stsp {
7713 0ebf8283 2019-07-24 stsp static char msg[42];
7714 0ebf8283 2019-07-24 stsp int ret;
7715 0ebf8283 2019-07-24 stsp
7716 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7717 0ebf8283 2019-07-24 stsp lineno);
7718 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7719 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7720 0ebf8283 2019-07-24 stsp
7721 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7722 0ebf8283 2019-07-24 stsp }
7723 0ebf8283 2019-07-24 stsp
7724 0ebf8283 2019-07-24 stsp static const struct got_error *
7725 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7726 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7727 0ebf8283 2019-07-24 stsp {
7728 0ebf8283 2019-07-24 stsp const struct got_error *err;
7729 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7730 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7731 0ebf8283 2019-07-24 stsp
7732 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7733 0ebf8283 2019-07-24 stsp if (err)
7734 0ebf8283 2019-07-24 stsp return err;
7735 0ebf8283 2019-07-24 stsp
7736 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7737 0ebf8283 2019-07-24 stsp if (err)
7738 0ebf8283 2019-07-24 stsp goto done;
7739 0ebf8283 2019-07-24 stsp
7740 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7741 5943eee2 2019-08-13 stsp if (err)
7742 5943eee2 2019-08-13 stsp goto done;
7743 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7744 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7745 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7746 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7747 0ebf8283 2019-07-24 stsp }
7748 0ebf8283 2019-07-24 stsp done:
7749 0ebf8283 2019-07-24 stsp if (folded_commit)
7750 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7751 0ebf8283 2019-07-24 stsp free(id_str);
7752 5943eee2 2019-08-13 stsp free(folded_logmsg);
7753 0ebf8283 2019-07-24 stsp return err;
7754 0ebf8283 2019-07-24 stsp }
7755 0ebf8283 2019-07-24 stsp
7756 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7757 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7758 0ebf8283 2019-07-24 stsp {
7759 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7760 0ebf8283 2019-07-24 stsp
7761 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7762 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7763 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7764 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7765 3f9de99f 2019-07-24 stsp folded = prev;
7766 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7767 0ebf8283 2019-07-24 stsp }
7768 0ebf8283 2019-07-24 stsp
7769 0ebf8283 2019-07-24 stsp return folded;
7770 0ebf8283 2019-07-24 stsp }
7771 0ebf8283 2019-07-24 stsp
7772 0ebf8283 2019-07-24 stsp static const struct got_error *
7773 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7774 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7775 0ebf8283 2019-07-24 stsp {
7776 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7777 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7778 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7779 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7780 1601cb9f 2020-09-11 naddy int logmsg_len;
7781 0ebf8283 2019-07-24 stsp int fd;
7782 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7783 0ebf8283 2019-07-24 stsp
7784 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7785 0ebf8283 2019-07-24 stsp if (err)
7786 0ebf8283 2019-07-24 stsp return err;
7787 0ebf8283 2019-07-24 stsp
7788 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7789 0ebf8283 2019-07-24 stsp if (folded) {
7790 0ebf8283 2019-07-24 stsp while (folded != hle) {
7791 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7792 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7793 3f9de99f 2019-07-24 stsp continue;
7794 3f9de99f 2019-07-24 stsp }
7795 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7796 0ebf8283 2019-07-24 stsp logmsg, repo);
7797 0ebf8283 2019-07-24 stsp if (err)
7798 0ebf8283 2019-07-24 stsp goto done;
7799 0ebf8283 2019-07-24 stsp free(logmsg);
7800 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7801 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7802 0ebf8283 2019-07-24 stsp }
7803 0ebf8283 2019-07-24 stsp }
7804 0ebf8283 2019-07-24 stsp
7805 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7806 0ebf8283 2019-07-24 stsp if (err)
7807 0ebf8283 2019-07-24 stsp goto done;
7808 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7809 5943eee2 2019-08-13 stsp if (err)
7810 5943eee2 2019-08-13 stsp goto done;
7811 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
7812 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7813 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
7814 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
7815 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7816 0ebf8283 2019-07-24 stsp goto done;
7817 0ebf8283 2019-07-24 stsp }
7818 0ebf8283 2019-07-24 stsp free(logmsg);
7819 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7820 0ebf8283 2019-07-24 stsp
7821 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7822 0ebf8283 2019-07-24 stsp if (err)
7823 0ebf8283 2019-07-24 stsp goto done;
7824 0ebf8283 2019-07-24 stsp
7825 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7826 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7827 0ebf8283 2019-07-24 stsp if (err)
7828 0ebf8283 2019-07-24 stsp goto done;
7829 0ebf8283 2019-07-24 stsp
7830 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
7831 0ebf8283 2019-07-24 stsp close(fd);
7832 0ebf8283 2019-07-24 stsp
7833 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7834 0ebf8283 2019-07-24 stsp if (err)
7835 0ebf8283 2019-07-24 stsp goto done;
7836 0ebf8283 2019-07-24 stsp
7837 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7838 0ebf8283 2019-07-24 stsp if (err) {
7839 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7840 0ebf8283 2019-07-24 stsp goto done;
7841 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7842 0ebf8283 2019-07-24 stsp }
7843 0ebf8283 2019-07-24 stsp done:
7844 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7845 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7846 0ebf8283 2019-07-24 stsp free(logmsg_path);
7847 0ebf8283 2019-07-24 stsp free(logmsg);
7848 5943eee2 2019-08-13 stsp free(orig_logmsg);
7849 0ebf8283 2019-07-24 stsp free(editor);
7850 0ebf8283 2019-07-24 stsp if (commit)
7851 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7852 0ebf8283 2019-07-24 stsp return err;
7853 5ef14e63 2019-06-02 stsp }
7854 0ebf8283 2019-07-24 stsp
7855 0ebf8283 2019-07-24 stsp static const struct got_error *
7856 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7857 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7858 0ebf8283 2019-07-24 stsp {
7859 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7860 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7861 0ebf8283 2019-07-24 stsp size_t size;
7862 0ebf8283 2019-07-24 stsp ssize_t len;
7863 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7864 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7865 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7866 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7867 0ebf8283 2019-07-24 stsp
7868 0ebf8283 2019-07-24 stsp for (;;) {
7869 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7870 0ebf8283 2019-07-24 stsp if (len == -1) {
7871 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7872 0ebf8283 2019-07-24 stsp if (feof(f))
7873 0ebf8283 2019-07-24 stsp break;
7874 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7875 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7876 0ebf8283 2019-07-24 stsp break;
7877 0ebf8283 2019-07-24 stsp }
7878 0ebf8283 2019-07-24 stsp lineno++;
7879 0ebf8283 2019-07-24 stsp p = line;
7880 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7881 0ebf8283 2019-07-24 stsp p++;
7882 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7883 0ebf8283 2019-07-24 stsp free(line);
7884 0ebf8283 2019-07-24 stsp line = NULL;
7885 0ebf8283 2019-07-24 stsp continue;
7886 0ebf8283 2019-07-24 stsp }
7887 0ebf8283 2019-07-24 stsp cmd = NULL;
7888 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7889 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7890 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7891 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7892 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7893 0ebf8283 2019-07-24 stsp break;
7894 0ebf8283 2019-07-24 stsp }
7895 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7896 0ebf8283 2019-07-24 stsp p++;
7897 0ebf8283 2019-07-24 stsp break;
7898 0ebf8283 2019-07-24 stsp }
7899 0ebf8283 2019-07-24 stsp }
7900 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7901 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7902 0ebf8283 2019-07-24 stsp break;
7903 0ebf8283 2019-07-24 stsp }
7904 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7905 0ebf8283 2019-07-24 stsp p++;
7906 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7907 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7908 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7909 0ebf8283 2019-07-24 stsp break;
7910 0ebf8283 2019-07-24 stsp }
7911 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7912 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7913 0ebf8283 2019-07-24 stsp if (err)
7914 0ebf8283 2019-07-24 stsp break;
7915 0ebf8283 2019-07-24 stsp } else {
7916 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7917 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7918 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7919 0ebf8283 2019-07-24 stsp break;
7920 0ebf8283 2019-07-24 stsp }
7921 0ebf8283 2019-07-24 stsp }
7922 0ebf8283 2019-07-24 stsp free(line);
7923 0ebf8283 2019-07-24 stsp line = NULL;
7924 0ebf8283 2019-07-24 stsp continue;
7925 0ebf8283 2019-07-24 stsp } else {
7926 0ebf8283 2019-07-24 stsp end = p;
7927 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7928 0ebf8283 2019-07-24 stsp end++;
7929 0ebf8283 2019-07-24 stsp *end = '\0';
7930 0ebf8283 2019-07-24 stsp
7931 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7932 0ebf8283 2019-07-24 stsp if (err) {
7933 0ebf8283 2019-07-24 stsp /* override error code */
7934 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7935 0ebf8283 2019-07-24 stsp break;
7936 0ebf8283 2019-07-24 stsp }
7937 0ebf8283 2019-07-24 stsp }
7938 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7939 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7940 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7941 0ebf8283 2019-07-24 stsp break;
7942 0ebf8283 2019-07-24 stsp }
7943 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7944 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7945 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7946 0ebf8283 2019-07-24 stsp commit_id = NULL;
7947 0ebf8283 2019-07-24 stsp free(line);
7948 0ebf8283 2019-07-24 stsp line = NULL;
7949 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7950 0ebf8283 2019-07-24 stsp }
7951 0ebf8283 2019-07-24 stsp
7952 0ebf8283 2019-07-24 stsp free(line);
7953 0ebf8283 2019-07-24 stsp free(commit_id);
7954 0ebf8283 2019-07-24 stsp return err;
7955 0ebf8283 2019-07-24 stsp }
7956 0ebf8283 2019-07-24 stsp
7957 0ebf8283 2019-07-24 stsp static const struct got_error *
7958 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7959 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7960 bfce7f83 2019-07-27 stsp {
7961 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7962 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7963 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7964 5b87815e 2020-03-05 stsp static char msg[92];
7965 bfce7f83 2019-07-27 stsp char *id_str;
7966 bfce7f83 2019-07-27 stsp
7967 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7968 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7969 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7970 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7971 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7972 5b87815e 2020-03-05 stsp
7973 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7974 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7975 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7976 5b87815e 2020-03-05 stsp if (hle == hle2)
7977 5b87815e 2020-03-05 stsp continue;
7978 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7979 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7980 5b87815e 2020-03-05 stsp continue;
7981 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7982 5b87815e 2020-03-05 stsp if (err)
7983 5b87815e 2020-03-05 stsp return err;
7984 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7985 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7986 5b87815e 2020-03-05 stsp free(id_str);
7987 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7988 5b87815e 2020-03-05 stsp }
7989 5b87815e 2020-03-05 stsp }
7990 bfce7f83 2019-07-27 stsp
7991 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7992 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7993 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7994 bfce7f83 2019-07-27 stsp break;
7995 bfce7f83 2019-07-27 stsp }
7996 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7997 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7998 bfce7f83 2019-07-27 stsp if (err)
7999 bfce7f83 2019-07-27 stsp return err;
8000 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8001 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8002 bfce7f83 2019-07-27 stsp free(id_str);
8003 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8004 bfce7f83 2019-07-27 stsp }
8005 bfce7f83 2019-07-27 stsp }
8006 bfce7f83 2019-07-27 stsp
8007 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8008 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8009 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8010 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8011 bfce7f83 2019-07-27 stsp
8012 bfce7f83 2019-07-27 stsp return NULL;
8013 bfce7f83 2019-07-27 stsp }
8014 bfce7f83 2019-07-27 stsp
8015 bfce7f83 2019-07-27 stsp static const struct got_error *
8016 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8017 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8018 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8019 0ebf8283 2019-07-24 stsp {
8020 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8021 0ebf8283 2019-07-24 stsp char *editor;
8022 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8023 0ebf8283 2019-07-24 stsp
8024 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8025 0ebf8283 2019-07-24 stsp if (err)
8026 0ebf8283 2019-07-24 stsp return err;
8027 0ebf8283 2019-07-24 stsp
8028 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8029 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8030 0ebf8283 2019-07-24 stsp goto done;
8031 0ebf8283 2019-07-24 stsp }
8032 0ebf8283 2019-07-24 stsp
8033 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8034 0ebf8283 2019-07-24 stsp if (f == NULL) {
8035 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8036 0ebf8283 2019-07-24 stsp goto done;
8037 0ebf8283 2019-07-24 stsp }
8038 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8039 bfce7f83 2019-07-27 stsp if (err)
8040 bfce7f83 2019-07-27 stsp goto done;
8041 bfce7f83 2019-07-27 stsp
8042 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8043 0ebf8283 2019-07-24 stsp done:
8044 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8045 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8046 0ebf8283 2019-07-24 stsp free(editor);
8047 0ebf8283 2019-07-24 stsp return err;
8048 0ebf8283 2019-07-24 stsp }
8049 0ebf8283 2019-07-24 stsp
8050 0ebf8283 2019-07-24 stsp static const struct got_error *
8051 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8052 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8053 514f2ffe 2020-01-29 stsp struct got_repository *);
8054 0ebf8283 2019-07-24 stsp
8055 0ebf8283 2019-07-24 stsp static const struct got_error *
8056 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8057 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8058 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8059 0ebf8283 2019-07-24 stsp {
8060 0ebf8283 2019-07-24 stsp const struct got_error *err;
8061 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8062 0ebf8283 2019-07-24 stsp char *path = NULL;
8063 0ebf8283 2019-07-24 stsp
8064 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8065 0ebf8283 2019-07-24 stsp if (err)
8066 0ebf8283 2019-07-24 stsp return err;
8067 0ebf8283 2019-07-24 stsp
8068 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8069 0ebf8283 2019-07-24 stsp if (err)
8070 0ebf8283 2019-07-24 stsp goto done;
8071 0ebf8283 2019-07-24 stsp
8072 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8073 0ebf8283 2019-07-24 stsp if (err)
8074 0ebf8283 2019-07-24 stsp goto done;
8075 0ebf8283 2019-07-24 stsp
8076 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8077 083957f4 2020-02-24 stsp rewind(f);
8078 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8079 083957f4 2020-02-24 stsp } else {
8080 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8081 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8082 0ebf8283 2019-07-24 stsp goto done;
8083 083957f4 2020-02-24 stsp }
8084 083957f4 2020-02-24 stsp f = NULL;
8085 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8086 083957f4 2020-02-24 stsp if (err) {
8087 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8088 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8089 083957f4 2020-02-24 stsp goto done;
8090 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8091 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8092 083957f4 2020-02-24 stsp }
8093 0ebf8283 2019-07-24 stsp }
8094 0ebf8283 2019-07-24 stsp done:
8095 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8096 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8097 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8098 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8099 0ebf8283 2019-07-24 stsp free(path);
8100 0ebf8283 2019-07-24 stsp return err;
8101 0ebf8283 2019-07-24 stsp }
8102 0ebf8283 2019-07-24 stsp
8103 0ebf8283 2019-07-24 stsp static const struct got_error *
8104 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8105 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8106 0ebf8283 2019-07-24 stsp {
8107 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8108 0ebf8283 2019-07-24 stsp char *path = NULL;
8109 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8110 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8111 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8112 0ebf8283 2019-07-24 stsp
8113 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8114 0ebf8283 2019-07-24 stsp if (err)
8115 0ebf8283 2019-07-24 stsp return err;
8116 0ebf8283 2019-07-24 stsp
8117 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8118 0ebf8283 2019-07-24 stsp if (f == NULL) {
8119 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8120 0ebf8283 2019-07-24 stsp goto done;
8121 0ebf8283 2019-07-24 stsp }
8122 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8123 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8124 0ebf8283 2019-07-24 stsp repo);
8125 0ebf8283 2019-07-24 stsp if (err)
8126 0ebf8283 2019-07-24 stsp break;
8127 0ebf8283 2019-07-24 stsp
8128 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8129 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8130 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8131 0ebf8283 2019-07-24 stsp if (n < 0) {
8132 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8133 0ebf8283 2019-07-24 stsp break;
8134 0ebf8283 2019-07-24 stsp }
8135 0ebf8283 2019-07-24 stsp }
8136 0ebf8283 2019-07-24 stsp }
8137 0ebf8283 2019-07-24 stsp done:
8138 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8139 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8140 0ebf8283 2019-07-24 stsp free(path);
8141 0ebf8283 2019-07-24 stsp if (commit)
8142 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8143 0ebf8283 2019-07-24 stsp return err;
8144 0ebf8283 2019-07-24 stsp }
8145 0ebf8283 2019-07-24 stsp
8146 bfce7f83 2019-07-27 stsp void
8147 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8148 bfce7f83 2019-07-27 stsp {
8149 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8150 bfce7f83 2019-07-27 stsp
8151 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8152 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8153 bfce7f83 2019-07-27 stsp free(hle);
8154 bfce7f83 2019-07-27 stsp }
8155 bfce7f83 2019-07-27 stsp }
8156 bfce7f83 2019-07-27 stsp
8157 0ebf8283 2019-07-24 stsp static const struct got_error *
8158 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8159 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8160 0ebf8283 2019-07-24 stsp {
8161 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8162 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8163 0ebf8283 2019-07-24 stsp
8164 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8165 0ebf8283 2019-07-24 stsp if (f == NULL) {
8166 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
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 err = histedit_parse_list(histedit_cmds, f, repo);
8171 0ebf8283 2019-07-24 stsp done:
8172 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8173 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8174 0ebf8283 2019-07-24 stsp return err;
8175 0ebf8283 2019-07-24 stsp }
8176 0ebf8283 2019-07-24 stsp
8177 0ebf8283 2019-07-24 stsp static const struct got_error *
8178 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8179 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8180 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8181 0ebf8283 2019-07-24 stsp {
8182 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8183 0ebf8283 2019-07-24 stsp int resp = ' ';
8184 0ebf8283 2019-07-24 stsp
8185 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8186 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8187 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8188 0ebf8283 2019-07-24 stsp resp = getchar();
8189 a61a4414 2019-08-07 stsp if (resp == '\n')
8190 a61a4414 2019-08-07 stsp resp = getchar();
8191 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8192 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8193 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8194 bfce7f83 2019-07-27 stsp repo);
8195 426ebf2e 2019-07-25 stsp if (err) {
8196 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8197 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8198 426ebf2e 2019-07-25 stsp break;
8199 bfce7f83 2019-07-27 stsp prev_err = err;
8200 426ebf2e 2019-07-25 stsp resp = ' ';
8201 426ebf2e 2019-07-25 stsp continue;
8202 426ebf2e 2019-07-25 stsp }
8203 bfce7f83 2019-07-27 stsp break;
8204 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8205 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8206 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8207 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8208 426ebf2e 2019-07-25 stsp if (err) {
8209 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8210 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8211 426ebf2e 2019-07-25 stsp break;
8212 bfce7f83 2019-07-27 stsp prev_err = err;
8213 426ebf2e 2019-07-25 stsp resp = ' ';
8214 426ebf2e 2019-07-25 stsp continue;
8215 426ebf2e 2019-07-25 stsp }
8216 bfce7f83 2019-07-27 stsp break;
8217 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8218 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8219 0ebf8283 2019-07-24 stsp break;
8220 426ebf2e 2019-07-25 stsp } else
8221 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8222 0ebf8283 2019-07-24 stsp }
8223 0ebf8283 2019-07-24 stsp
8224 0ebf8283 2019-07-24 stsp return err;
8225 0ebf8283 2019-07-24 stsp }
8226 0ebf8283 2019-07-24 stsp
8227 0ebf8283 2019-07-24 stsp static const struct got_error *
8228 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8229 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8230 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8231 0ebf8283 2019-07-24 stsp {
8232 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8233 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8234 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8235 3e3a69f1 2019-07-25 stsp branch, repo);
8236 0ebf8283 2019-07-24 stsp }
8237 0ebf8283 2019-07-24 stsp
8238 0ebf8283 2019-07-24 stsp static const struct got_error *
8239 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8240 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8241 0ebf8283 2019-07-24 stsp {
8242 0ebf8283 2019-07-24 stsp const struct got_error *err;
8243 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8244 0ebf8283 2019-07-24 stsp
8245 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8246 0ebf8283 2019-07-24 stsp if (err)
8247 0ebf8283 2019-07-24 stsp goto done;
8248 0ebf8283 2019-07-24 stsp
8249 0ebf8283 2019-07-24 stsp if (new_id) {
8250 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8251 0ebf8283 2019-07-24 stsp if (err)
8252 0ebf8283 2019-07-24 stsp goto done;
8253 0ebf8283 2019-07-24 stsp }
8254 0ebf8283 2019-07-24 stsp
8255 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8256 0ebf8283 2019-07-24 stsp if (new_id_str)
8257 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8258 0ebf8283 2019-07-24 stsp
8259 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8260 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8261 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8262 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8263 0ebf8283 2019-07-24 stsp goto done;
8264 0ebf8283 2019-07-24 stsp }
8265 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8266 0ebf8283 2019-07-24 stsp } else {
8267 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8268 0ebf8283 2019-07-24 stsp if (err)
8269 0ebf8283 2019-07-24 stsp goto done;
8270 0ebf8283 2019-07-24 stsp }
8271 0ebf8283 2019-07-24 stsp
8272 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8273 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8274 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8275 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8276 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8277 0ebf8283 2019-07-24 stsp break;
8278 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8279 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8280 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8281 0ebf8283 2019-07-24 stsp logmsg);
8282 0ebf8283 2019-07-24 stsp break;
8283 0ebf8283 2019-07-24 stsp default:
8284 0ebf8283 2019-07-24 stsp break;
8285 0ebf8283 2019-07-24 stsp }
8286 0ebf8283 2019-07-24 stsp done:
8287 0ebf8283 2019-07-24 stsp free(old_id_str);
8288 0ebf8283 2019-07-24 stsp free(new_id_str);
8289 0ebf8283 2019-07-24 stsp return err;
8290 0ebf8283 2019-07-24 stsp }
8291 0ebf8283 2019-07-24 stsp
8292 0ebf8283 2019-07-24 stsp static const struct got_error *
8293 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8294 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8295 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8296 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8297 0ebf8283 2019-07-24 stsp {
8298 0ebf8283 2019-07-24 stsp const struct got_error *err;
8299 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8300 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8301 0ebf8283 2019-07-24 stsp
8302 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8303 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8304 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8305 0ebf8283 2019-07-24 stsp if (err)
8306 0ebf8283 2019-07-24 stsp return err;
8307 0ebf8283 2019-07-24 stsp }
8308 0ebf8283 2019-07-24 stsp
8309 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8310 0ebf8283 2019-07-24 stsp if (err)
8311 0ebf8283 2019-07-24 stsp return err;
8312 0ebf8283 2019-07-24 stsp
8313 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8314 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8315 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8316 0ebf8283 2019-07-24 stsp if (err) {
8317 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8318 0ebf8283 2019-07-24 stsp goto done;
8319 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8320 0ebf8283 2019-07-24 stsp } else {
8321 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8322 0ebf8283 2019-07-24 stsp free(new_commit_id);
8323 0ebf8283 2019-07-24 stsp }
8324 0ebf8283 2019-07-24 stsp done:
8325 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8326 0ebf8283 2019-07-24 stsp return err;
8327 0ebf8283 2019-07-24 stsp }
8328 0ebf8283 2019-07-24 stsp
8329 0ebf8283 2019-07-24 stsp static const struct got_error *
8330 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8331 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8332 0ebf8283 2019-07-24 stsp {
8333 0ebf8283 2019-07-24 stsp const struct got_error *error;
8334 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8335 0ebf8283 2019-07-24 stsp
8336 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8337 0ebf8283 2019-07-24 stsp repo);
8338 0ebf8283 2019-07-24 stsp if (error)
8339 0ebf8283 2019-07-24 stsp return error;
8340 0ebf8283 2019-07-24 stsp
8341 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8342 0ebf8283 2019-07-24 stsp if (error)
8343 0ebf8283 2019-07-24 stsp return error;
8344 0ebf8283 2019-07-24 stsp
8345 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8346 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8347 0ebf8283 2019-07-24 stsp return error;
8348 ab20a43a 2020-01-29 stsp }
8349 ab20a43a 2020-01-29 stsp
8350 ab20a43a 2020-01-29 stsp static const struct got_error *
8351 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8352 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8353 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8354 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8355 ab20a43a 2020-01-29 stsp {
8356 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8357 ab20a43a 2020-01-29 stsp
8358 ab20a43a 2020-01-29 stsp switch (status) {
8359 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8360 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8361 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8362 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8363 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8364 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8365 ab20a43a 2020-01-29 stsp default:
8366 ab20a43a 2020-01-29 stsp break;
8367 ab20a43a 2020-01-29 stsp }
8368 ab20a43a 2020-01-29 stsp
8369 ab20a43a 2020-01-29 stsp switch (staged_status) {
8370 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8371 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8372 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8373 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8374 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8375 ab20a43a 2020-01-29 stsp default:
8376 ab20a43a 2020-01-29 stsp break;
8377 ab20a43a 2020-01-29 stsp }
8378 ab20a43a 2020-01-29 stsp
8379 ab20a43a 2020-01-29 stsp return NULL;
8380 0ebf8283 2019-07-24 stsp }
8381 0ebf8283 2019-07-24 stsp
8382 0ebf8283 2019-07-24 stsp static const struct got_error *
8383 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8384 0ebf8283 2019-07-24 stsp {
8385 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8386 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8387 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8388 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8389 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8390 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8391 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8392 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8393 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8394 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8395 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8396 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8397 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8398 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8399 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8400 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8401 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8402 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8403 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8404 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8405 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8406 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8407 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8408 0ebf8283 2019-07-24 stsp
8409 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8410 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8411 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8412 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8413 0ebf8283 2019-07-24 stsp
8414 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8415 0ebf8283 2019-07-24 stsp switch (ch) {
8416 0ebf8283 2019-07-24 stsp case 'a':
8417 0ebf8283 2019-07-24 stsp abort_edit = 1;
8418 0ebf8283 2019-07-24 stsp break;
8419 0ebf8283 2019-07-24 stsp case 'c':
8420 0ebf8283 2019-07-24 stsp continue_edit = 1;
8421 0ebf8283 2019-07-24 stsp break;
8422 0ebf8283 2019-07-24 stsp case 'F':
8423 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8424 0ebf8283 2019-07-24 stsp break;
8425 083957f4 2020-02-24 stsp case 'm':
8426 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8427 083957f4 2020-02-24 stsp break;
8428 0ebf8283 2019-07-24 stsp default:
8429 0ebf8283 2019-07-24 stsp usage_histedit();
8430 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8431 0ebf8283 2019-07-24 stsp }
8432 0ebf8283 2019-07-24 stsp }
8433 0ebf8283 2019-07-24 stsp
8434 0ebf8283 2019-07-24 stsp argc -= optind;
8435 0ebf8283 2019-07-24 stsp argv += optind;
8436 0ebf8283 2019-07-24 stsp
8437 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8438 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8439 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8440 0ebf8283 2019-07-24 stsp err(1, "pledge");
8441 0ebf8283 2019-07-24 stsp #endif
8442 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8443 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8444 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8445 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8446 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8447 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8448 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8449 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8450 0ebf8283 2019-07-24 stsp if (argc != 0)
8451 0ebf8283 2019-07-24 stsp usage_histedit();
8452 0ebf8283 2019-07-24 stsp
8453 0ebf8283 2019-07-24 stsp /*
8454 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8455 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8456 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8457 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8458 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8459 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8460 0ebf8283 2019-07-24 stsp */
8461 0ebf8283 2019-07-24 stsp
8462 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8463 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8464 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8465 0ebf8283 2019-07-24 stsp goto done;
8466 0ebf8283 2019-07-24 stsp }
8467 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8468 fa51e947 2020-03-27 stsp if (error) {
8469 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8470 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8471 0ebf8283 2019-07-24 stsp goto done;
8472 fa51e947 2020-03-27 stsp }
8473 0ebf8283 2019-07-24 stsp
8474 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8475 c9956ddf 2019-09-08 stsp NULL);
8476 0ebf8283 2019-07-24 stsp if (error != NULL)
8477 0ebf8283 2019-07-24 stsp goto done;
8478 0ebf8283 2019-07-24 stsp
8479 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8480 0ebf8283 2019-07-24 stsp if (error)
8481 0ebf8283 2019-07-24 stsp goto done;
8482 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8483 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8484 0ebf8283 2019-07-24 stsp goto done;
8485 0ebf8283 2019-07-24 stsp }
8486 0ebf8283 2019-07-24 stsp
8487 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8488 0ebf8283 2019-07-24 stsp if (error)
8489 0ebf8283 2019-07-24 stsp goto done;
8490 0ebf8283 2019-07-24 stsp
8491 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8492 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8493 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8494 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8495 083957f4 2020-02-24 stsp "before the -m option can be used");
8496 083957f4 2020-02-24 stsp goto done;
8497 083957f4 2020-02-24 stsp }
8498 083957f4 2020-02-24 stsp
8499 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8500 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8501 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8502 3e3a69f1 2019-07-25 stsp worktree, repo);
8503 0ebf8283 2019-07-24 stsp if (error)
8504 0ebf8283 2019-07-24 stsp goto done;
8505 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8506 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8507 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8508 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8509 0ebf8283 2019-07-24 stsp if (error)
8510 0ebf8283 2019-07-24 stsp goto done;
8511 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8512 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8513 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8514 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8515 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8516 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8517 0ebf8283 2019-07-24 stsp goto done;
8518 0ebf8283 2019-07-24 stsp }
8519 0ebf8283 2019-07-24 stsp
8520 0ebf8283 2019-07-24 stsp if (continue_edit) {
8521 0ebf8283 2019-07-24 stsp char *path;
8522 0ebf8283 2019-07-24 stsp
8523 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8524 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8525 0ebf8283 2019-07-24 stsp goto done;
8526 0ebf8283 2019-07-24 stsp }
8527 0ebf8283 2019-07-24 stsp
8528 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8529 0ebf8283 2019-07-24 stsp if (error)
8530 0ebf8283 2019-07-24 stsp goto done;
8531 0ebf8283 2019-07-24 stsp
8532 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8533 0ebf8283 2019-07-24 stsp free(path);
8534 0ebf8283 2019-07-24 stsp if (error)
8535 0ebf8283 2019-07-24 stsp goto done;
8536 0ebf8283 2019-07-24 stsp
8537 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8538 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8539 3e3a69f1 2019-07-25 stsp worktree, repo);
8540 0ebf8283 2019-07-24 stsp if (error)
8541 0ebf8283 2019-07-24 stsp goto done;
8542 0ebf8283 2019-07-24 stsp
8543 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8544 0ebf8283 2019-07-24 stsp if (error)
8545 0ebf8283 2019-07-24 stsp goto done;
8546 0ebf8283 2019-07-24 stsp
8547 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8548 0ebf8283 2019-07-24 stsp head_commit_id);
8549 0ebf8283 2019-07-24 stsp if (error)
8550 0ebf8283 2019-07-24 stsp goto done;
8551 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8552 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8553 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8554 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8555 3aac7cf7 2019-07-25 stsp goto done;
8556 3aac7cf7 2019-07-25 stsp }
8557 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8558 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8559 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8560 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8561 0ebf8283 2019-07-24 stsp commit = NULL;
8562 0ebf8283 2019-07-24 stsp if (error)
8563 0ebf8283 2019-07-24 stsp goto done;
8564 0ebf8283 2019-07-24 stsp } else {
8565 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8566 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8567 0ebf8283 2019-07-24 stsp goto done;
8568 0ebf8283 2019-07-24 stsp }
8569 0ebf8283 2019-07-24 stsp
8570 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8571 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8572 0ebf8283 2019-07-24 stsp if (error != NULL)
8573 0ebf8283 2019-07-24 stsp goto done;
8574 0ebf8283 2019-07-24 stsp
8575 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8576 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8577 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8578 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8579 c7d20a3f 2019-07-30 stsp goto done;
8580 c7d20a3f 2019-07-30 stsp }
8581 c7d20a3f 2019-07-30 stsp
8582 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8583 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8584 bfce7f83 2019-07-27 stsp branch = NULL;
8585 0ebf8283 2019-07-24 stsp if (error)
8586 0ebf8283 2019-07-24 stsp goto done;
8587 0ebf8283 2019-07-24 stsp
8588 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8589 0ebf8283 2019-07-24 stsp head_commit_id);
8590 0ebf8283 2019-07-24 stsp if (error)
8591 0ebf8283 2019-07-24 stsp goto done;
8592 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8593 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8594 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8595 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8596 3aac7cf7 2019-07-25 stsp goto done;
8597 3aac7cf7 2019-07-25 stsp }
8598 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8599 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8600 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8601 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8602 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8603 0ebf8283 2019-07-24 stsp commit = NULL;
8604 0ebf8283 2019-07-24 stsp if (error)
8605 0ebf8283 2019-07-24 stsp goto done;
8606 0ebf8283 2019-07-24 stsp
8607 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8608 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8609 c5996fff 2020-01-29 stsp goto done;
8610 c5996fff 2020-01-29 stsp }
8611 c5996fff 2020-01-29 stsp
8612 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8613 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8614 bfce7f83 2019-07-27 stsp if (error)
8615 bfce7f83 2019-07-27 stsp goto done;
8616 bfce7f83 2019-07-27 stsp
8617 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8618 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8619 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8620 6ba2bea2 2019-07-27 stsp if (error) {
8621 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8622 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8623 9627c110 2020-04-18 stsp update_progress, &upa);
8624 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8625 0ebf8283 2019-07-24 stsp goto done;
8626 6ba2bea2 2019-07-27 stsp }
8627 0ebf8283 2019-07-24 stsp } else {
8628 514f2ffe 2020-01-29 stsp const char *branch_name;
8629 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8630 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8631 514f2ffe 2020-01-29 stsp branch_name += 11;
8632 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8633 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8634 6ba2bea2 2019-07-27 stsp if (error) {
8635 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8636 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8637 9627c110 2020-04-18 stsp update_progress, &upa);
8638 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8639 0ebf8283 2019-07-24 stsp goto done;
8640 6ba2bea2 2019-07-27 stsp }
8641 0ebf8283 2019-07-24 stsp
8642 0ebf8283 2019-07-24 stsp }
8643 0ebf8283 2019-07-24 stsp
8644 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8645 0ebf8283 2019-07-24 stsp repo);
8646 6ba2bea2 2019-07-27 stsp if (error) {
8647 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8648 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8649 9627c110 2020-04-18 stsp update_progress, &upa);
8650 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8651 0ebf8283 2019-07-24 stsp goto done;
8652 6ba2bea2 2019-07-27 stsp }
8653 0ebf8283 2019-07-24 stsp
8654 0ebf8283 2019-07-24 stsp }
8655 0ebf8283 2019-07-24 stsp
8656 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8657 4ec14e60 2019-08-28 hiltjo if (error)
8658 0ebf8283 2019-07-24 stsp goto done;
8659 0ebf8283 2019-07-24 stsp
8660 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8661 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8662 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8663 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8664 0ebf8283 2019-07-24 stsp continue;
8665 0ebf8283 2019-07-24 stsp
8666 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8667 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8668 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8669 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8670 0ebf8283 2019-07-24 stsp repo);
8671 ab20a43a 2020-01-29 stsp if (error)
8672 ab20a43a 2020-01-29 stsp goto done;
8673 0ebf8283 2019-07-24 stsp } else {
8674 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8675 ab20a43a 2020-01-29 stsp int have_changes = 0;
8676 ab20a43a 2020-01-29 stsp
8677 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8678 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8679 ab20a43a 2020-01-29 stsp if (error)
8680 ab20a43a 2020-01-29 stsp goto done;
8681 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8682 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8683 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8684 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8685 ab20a43a 2020-01-29 stsp if (error) {
8686 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8687 ab20a43a 2020-01-29 stsp goto done;
8688 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8689 ab20a43a 2020-01-29 stsp goto done;
8690 ab20a43a 2020-01-29 stsp }
8691 ab20a43a 2020-01-29 stsp if (have_changes) {
8692 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8693 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8694 ab20a43a 2020-01-29 stsp if (error)
8695 ab20a43a 2020-01-29 stsp goto done;
8696 ab20a43a 2020-01-29 stsp } else {
8697 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8698 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8699 ab20a43a 2020-01-29 stsp if (error)
8700 ab20a43a 2020-01-29 stsp goto done;
8701 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8702 ab20a43a 2020-01-29 stsp hle, NULL);
8703 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8704 ab20a43a 2020-01-29 stsp commit = NULL;
8705 ab20a43a 2020-01-29 stsp if (error)
8706 ab20a43a 2020-01-29 stsp goto done;
8707 ab20a43a 2020-01-29 stsp }
8708 0ebf8283 2019-07-24 stsp }
8709 0ebf8283 2019-07-24 stsp continue;
8710 0ebf8283 2019-07-24 stsp }
8711 0ebf8283 2019-07-24 stsp
8712 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8713 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8714 0ebf8283 2019-07-24 stsp if (error)
8715 0ebf8283 2019-07-24 stsp goto done;
8716 0ebf8283 2019-07-24 stsp continue;
8717 0ebf8283 2019-07-24 stsp }
8718 0ebf8283 2019-07-24 stsp
8719 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8720 0ebf8283 2019-07-24 stsp hle->commit_id);
8721 0ebf8283 2019-07-24 stsp if (error)
8722 0ebf8283 2019-07-24 stsp goto done;
8723 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8724 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8725 0ebf8283 2019-07-24 stsp
8726 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8727 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8728 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8729 0ebf8283 2019-07-24 stsp if (error)
8730 0ebf8283 2019-07-24 stsp goto done;
8731 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8732 0ebf8283 2019-07-24 stsp commit = NULL;
8733 0ebf8283 2019-07-24 stsp
8734 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8735 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8736 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8737 9627c110 2020-04-18 stsp
8738 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8739 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8740 a0ea4fc0 2020-02-28 stsp repo);
8741 a0ea4fc0 2020-02-28 stsp if (error)
8742 a0ea4fc0 2020-02-28 stsp goto done;
8743 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8744 0ebf8283 2019-07-24 stsp break;
8745 0ebf8283 2019-07-24 stsp }
8746 0ebf8283 2019-07-24 stsp
8747 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8748 0ebf8283 2019-07-24 stsp char *id_str;
8749 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8750 0ebf8283 2019-07-24 stsp if (error)
8751 0ebf8283 2019-07-24 stsp goto done;
8752 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8753 0ebf8283 2019-07-24 stsp id_str);
8754 0ebf8283 2019-07-24 stsp free(id_str);
8755 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8756 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8757 3e3a69f1 2019-07-25 stsp fileindex);
8758 0ebf8283 2019-07-24 stsp goto done;
8759 0ebf8283 2019-07-24 stsp }
8760 0ebf8283 2019-07-24 stsp
8761 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8762 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8763 0ebf8283 2019-07-24 stsp if (error)
8764 0ebf8283 2019-07-24 stsp goto done;
8765 0ebf8283 2019-07-24 stsp continue;
8766 0ebf8283 2019-07-24 stsp }
8767 0ebf8283 2019-07-24 stsp
8768 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8769 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8770 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8771 0ebf8283 2019-07-24 stsp if (error)
8772 0ebf8283 2019-07-24 stsp goto done;
8773 0ebf8283 2019-07-24 stsp }
8774 0ebf8283 2019-07-24 stsp
8775 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8776 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8777 0ebf8283 2019-07-24 stsp if (error)
8778 0ebf8283 2019-07-24 stsp goto done;
8779 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8780 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8781 0ebf8283 2019-07-24 stsp } else
8782 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8783 3e3a69f1 2019-07-25 stsp branch, repo);
8784 0ebf8283 2019-07-24 stsp done:
8785 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8786 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8787 0ebf8283 2019-07-24 stsp free(head_commit_id);
8788 0ebf8283 2019-07-24 stsp free(base_commit_id);
8789 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8790 0ebf8283 2019-07-24 stsp if (commit)
8791 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8792 0ebf8283 2019-07-24 stsp if (branch)
8793 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8794 0ebf8283 2019-07-24 stsp if (tmp_branch)
8795 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8796 0ebf8283 2019-07-24 stsp if (worktree)
8797 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8798 2822a352 2019-10-15 stsp if (repo)
8799 2822a352 2019-10-15 stsp got_repo_close(repo);
8800 2822a352 2019-10-15 stsp return error;
8801 2822a352 2019-10-15 stsp }
8802 2822a352 2019-10-15 stsp
8803 2822a352 2019-10-15 stsp __dead static void
8804 2822a352 2019-10-15 stsp usage_integrate(void)
8805 2822a352 2019-10-15 stsp {
8806 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8807 2822a352 2019-10-15 stsp exit(1);
8808 2822a352 2019-10-15 stsp }
8809 2822a352 2019-10-15 stsp
8810 2822a352 2019-10-15 stsp static const struct got_error *
8811 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8812 2822a352 2019-10-15 stsp {
8813 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8814 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8815 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8816 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8817 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8818 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8819 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8820 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8821 9627c110 2020-04-18 stsp int ch;
8822 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8823 2822a352 2019-10-15 stsp
8824 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8825 2822a352 2019-10-15 stsp switch (ch) {
8826 2822a352 2019-10-15 stsp default:
8827 2822a352 2019-10-15 stsp usage_integrate();
8828 2822a352 2019-10-15 stsp /* NOTREACHED */
8829 2822a352 2019-10-15 stsp }
8830 2822a352 2019-10-15 stsp }
8831 2822a352 2019-10-15 stsp
8832 2822a352 2019-10-15 stsp argc -= optind;
8833 2822a352 2019-10-15 stsp argv += optind;
8834 2822a352 2019-10-15 stsp
8835 2822a352 2019-10-15 stsp if (argc != 1)
8836 2822a352 2019-10-15 stsp usage_integrate();
8837 2822a352 2019-10-15 stsp branch_arg = argv[0];
8838 2822a352 2019-10-15 stsp
8839 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8840 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8841 2822a352 2019-10-15 stsp err(1, "pledge");
8842 2822a352 2019-10-15 stsp
8843 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8844 2822a352 2019-10-15 stsp if (cwd == NULL) {
8845 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8846 2822a352 2019-10-15 stsp goto done;
8847 2822a352 2019-10-15 stsp }
8848 2822a352 2019-10-15 stsp
8849 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8850 fa51e947 2020-03-27 stsp if (error) {
8851 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8852 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8853 fa51e947 2020-03-27 stsp cwd);
8854 2822a352 2019-10-15 stsp goto done;
8855 fa51e947 2020-03-27 stsp }
8856 2822a352 2019-10-15 stsp
8857 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8858 2822a352 2019-10-15 stsp if (error)
8859 2822a352 2019-10-15 stsp goto done;
8860 2822a352 2019-10-15 stsp
8861 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8862 2822a352 2019-10-15 stsp NULL);
8863 2822a352 2019-10-15 stsp if (error != NULL)
8864 2822a352 2019-10-15 stsp goto done;
8865 2822a352 2019-10-15 stsp
8866 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8867 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8868 2822a352 2019-10-15 stsp if (error)
8869 2822a352 2019-10-15 stsp goto done;
8870 2822a352 2019-10-15 stsp
8871 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8872 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8873 2822a352 2019-10-15 stsp goto done;
8874 2822a352 2019-10-15 stsp }
8875 2822a352 2019-10-15 stsp
8876 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8877 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8878 2822a352 2019-10-15 stsp if (error)
8879 2822a352 2019-10-15 stsp goto done;
8880 2822a352 2019-10-15 stsp
8881 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8882 2822a352 2019-10-15 stsp if (refname == NULL) {
8883 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8884 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8885 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8886 2822a352 2019-10-15 stsp goto done;
8887 2822a352 2019-10-15 stsp }
8888 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8889 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8890 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8891 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8892 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8893 2822a352 2019-10-15 stsp goto done;
8894 2822a352 2019-10-15 stsp }
8895 2822a352 2019-10-15 stsp
8896 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8897 2822a352 2019-10-15 stsp if (error)
8898 2822a352 2019-10-15 stsp goto done;
8899 2822a352 2019-10-15 stsp
8900 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8901 2822a352 2019-10-15 stsp if (error)
8902 2822a352 2019-10-15 stsp goto done;
8903 2822a352 2019-10-15 stsp
8904 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8905 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8906 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8907 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8908 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8909 2822a352 2019-10-15 stsp goto done;
8910 2822a352 2019-10-15 stsp }
8911 2822a352 2019-10-15 stsp
8912 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8913 2822a352 2019-10-15 stsp if (error) {
8914 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8915 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8916 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8917 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8918 2822a352 2019-10-15 stsp goto done;
8919 2822a352 2019-10-15 stsp }
8920 2822a352 2019-10-15 stsp
8921 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8922 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8923 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
8924 2822a352 2019-10-15 stsp check_cancelled, NULL);
8925 2822a352 2019-10-15 stsp if (error)
8926 2822a352 2019-10-15 stsp goto done;
8927 2822a352 2019-10-15 stsp
8928 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8929 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8930 2822a352 2019-10-15 stsp done:
8931 715dc77e 2019-08-03 stsp if (repo)
8932 715dc77e 2019-08-03 stsp got_repo_close(repo);
8933 2822a352 2019-10-15 stsp if (worktree)
8934 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8935 2822a352 2019-10-15 stsp free(cwd);
8936 2822a352 2019-10-15 stsp free(base_commit_id);
8937 2822a352 2019-10-15 stsp free(commit_id);
8938 2822a352 2019-10-15 stsp free(refname);
8939 2822a352 2019-10-15 stsp free(base_refname);
8940 715dc77e 2019-08-03 stsp return error;
8941 715dc77e 2019-08-03 stsp }
8942 715dc77e 2019-08-03 stsp
8943 715dc77e 2019-08-03 stsp __dead static void
8944 715dc77e 2019-08-03 stsp usage_stage(void)
8945 715dc77e 2019-08-03 stsp {
8946 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8947 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
8948 715dc77e 2019-08-03 stsp getprogname());
8949 715dc77e 2019-08-03 stsp exit(1);
8950 715dc77e 2019-08-03 stsp }
8951 715dc77e 2019-08-03 stsp
8952 715dc77e 2019-08-03 stsp static const struct got_error *
8953 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8954 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8955 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8956 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8957 408b4ebc 2019-08-03 stsp {
8958 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8959 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8960 408b4ebc 2019-08-03 stsp
8961 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8962 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8963 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8964 408b4ebc 2019-08-03 stsp return NULL;
8965 408b4ebc 2019-08-03 stsp
8966 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8967 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8968 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8969 408b4ebc 2019-08-03 stsp else
8970 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8971 408b4ebc 2019-08-03 stsp if (err)
8972 408b4ebc 2019-08-03 stsp return err;
8973 408b4ebc 2019-08-03 stsp
8974 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8975 408b4ebc 2019-08-03 stsp free(id_str);
8976 dc424a06 2019-08-07 stsp return NULL;
8977 dc424a06 2019-08-07 stsp }
8978 2e1f37b0 2019-08-08 stsp
8979 dc424a06 2019-08-07 stsp static const struct got_error *
8980 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8981 715dc77e 2019-08-03 stsp {
8982 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8983 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8984 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8985 715dc77e 2019-08-03 stsp char *cwd = NULL;
8986 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8987 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8988 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8989 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8990 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8991 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8992 715dc77e 2019-08-03 stsp
8993 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8994 715dc77e 2019-08-03 stsp
8995 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8996 715dc77e 2019-08-03 stsp switch (ch) {
8997 408b4ebc 2019-08-03 stsp case 'l':
8998 408b4ebc 2019-08-03 stsp list_stage = 1;
8999 408b4ebc 2019-08-03 stsp break;
9000 dc424a06 2019-08-07 stsp case 'p':
9001 dc424a06 2019-08-07 stsp pflag = 1;
9002 dc424a06 2019-08-07 stsp break;
9003 dc424a06 2019-08-07 stsp case 'F':
9004 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9005 dc424a06 2019-08-07 stsp break;
9006 35213c7c 2020-07-23 stsp case 'S':
9007 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9008 35213c7c 2020-07-23 stsp break;
9009 715dc77e 2019-08-03 stsp default:
9010 715dc77e 2019-08-03 stsp usage_stage();
9011 715dc77e 2019-08-03 stsp /* NOTREACHED */
9012 715dc77e 2019-08-03 stsp }
9013 715dc77e 2019-08-03 stsp }
9014 715dc77e 2019-08-03 stsp
9015 715dc77e 2019-08-03 stsp argc -= optind;
9016 715dc77e 2019-08-03 stsp argv += optind;
9017 715dc77e 2019-08-03 stsp
9018 715dc77e 2019-08-03 stsp #ifndef PROFILE
9019 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9020 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9021 715dc77e 2019-08-03 stsp err(1, "pledge");
9022 715dc77e 2019-08-03 stsp #endif
9023 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9024 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9025 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9026 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9027 715dc77e 2019-08-03 stsp
9028 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9029 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9030 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9031 715dc77e 2019-08-03 stsp goto done;
9032 715dc77e 2019-08-03 stsp }
9033 715dc77e 2019-08-03 stsp
9034 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9035 fa51e947 2020-03-27 stsp if (error) {
9036 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9037 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9038 715dc77e 2019-08-03 stsp goto done;
9039 fa51e947 2020-03-27 stsp }
9040 715dc77e 2019-08-03 stsp
9041 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9042 c9956ddf 2019-09-08 stsp NULL);
9043 715dc77e 2019-08-03 stsp if (error != NULL)
9044 715dc77e 2019-08-03 stsp goto done;
9045 715dc77e 2019-08-03 stsp
9046 dc424a06 2019-08-07 stsp if (patch_script_path) {
9047 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9048 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9049 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9050 dc424a06 2019-08-07 stsp patch_script_path);
9051 dc424a06 2019-08-07 stsp goto done;
9052 dc424a06 2019-08-07 stsp }
9053 dc424a06 2019-08-07 stsp }
9054 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9055 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9056 715dc77e 2019-08-03 stsp if (error)
9057 715dc77e 2019-08-03 stsp goto done;
9058 715dc77e 2019-08-03 stsp
9059 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9060 6d022e97 2019-08-04 stsp if (error)
9061 6d022e97 2019-08-04 stsp goto done;
9062 715dc77e 2019-08-03 stsp
9063 6d022e97 2019-08-04 stsp if (list_stage)
9064 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9065 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9066 2e1f37b0 2019-08-08 stsp else {
9067 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9068 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9069 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9070 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9071 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9072 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9073 2e1f37b0 2019-08-08 stsp }
9074 ad493afc 2019-08-03 stsp done:
9075 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9076 2e1f37b0 2019-08-08 stsp error == NULL)
9077 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9078 ad493afc 2019-08-03 stsp if (repo)
9079 ad493afc 2019-08-03 stsp got_repo_close(repo);
9080 ad493afc 2019-08-03 stsp if (worktree)
9081 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9082 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9083 ad493afc 2019-08-03 stsp free((char *)pe->path);
9084 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9085 ad493afc 2019-08-03 stsp free(cwd);
9086 ad493afc 2019-08-03 stsp return error;
9087 ad493afc 2019-08-03 stsp }
9088 ad493afc 2019-08-03 stsp
9089 ad493afc 2019-08-03 stsp __dead static void
9090 ad493afc 2019-08-03 stsp usage_unstage(void)
9091 ad493afc 2019-08-03 stsp {
9092 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9093 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9094 ad493afc 2019-08-03 stsp getprogname());
9095 ad493afc 2019-08-03 stsp exit(1);
9096 ad493afc 2019-08-03 stsp }
9097 ad493afc 2019-08-03 stsp
9098 ad493afc 2019-08-03 stsp
9099 ad493afc 2019-08-03 stsp static const struct got_error *
9100 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9101 ad493afc 2019-08-03 stsp {
9102 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9103 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9104 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9105 ad493afc 2019-08-03 stsp char *cwd = NULL;
9106 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9107 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9108 9627c110 2020-04-18 stsp int ch, pflag = 0;
9109 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9110 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9111 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9112 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9113 ad493afc 2019-08-03 stsp
9114 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9115 ad493afc 2019-08-03 stsp
9116 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9117 ad493afc 2019-08-03 stsp switch (ch) {
9118 2e1f37b0 2019-08-08 stsp case 'p':
9119 2e1f37b0 2019-08-08 stsp pflag = 1;
9120 2e1f37b0 2019-08-08 stsp break;
9121 2e1f37b0 2019-08-08 stsp case 'F':
9122 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9123 2e1f37b0 2019-08-08 stsp break;
9124 ad493afc 2019-08-03 stsp default:
9125 ad493afc 2019-08-03 stsp usage_unstage();
9126 ad493afc 2019-08-03 stsp /* NOTREACHED */
9127 ad493afc 2019-08-03 stsp }
9128 ad493afc 2019-08-03 stsp }
9129 ad493afc 2019-08-03 stsp
9130 ad493afc 2019-08-03 stsp argc -= optind;
9131 ad493afc 2019-08-03 stsp argv += optind;
9132 ad493afc 2019-08-03 stsp
9133 ad493afc 2019-08-03 stsp #ifndef PROFILE
9134 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9135 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9136 ad493afc 2019-08-03 stsp err(1, "pledge");
9137 ad493afc 2019-08-03 stsp #endif
9138 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9139 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9140 2e1f37b0 2019-08-08 stsp
9141 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9142 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9143 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9144 ad493afc 2019-08-03 stsp goto done;
9145 ad493afc 2019-08-03 stsp }
9146 ad493afc 2019-08-03 stsp
9147 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9148 fa51e947 2020-03-27 stsp if (error) {
9149 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9150 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9151 ad493afc 2019-08-03 stsp goto done;
9152 fa51e947 2020-03-27 stsp }
9153 ad493afc 2019-08-03 stsp
9154 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9155 c9956ddf 2019-09-08 stsp NULL);
9156 ad493afc 2019-08-03 stsp if (error != NULL)
9157 ad493afc 2019-08-03 stsp goto done;
9158 ad493afc 2019-08-03 stsp
9159 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9160 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9161 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9162 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9163 2e1f37b0 2019-08-08 stsp patch_script_path);
9164 2e1f37b0 2019-08-08 stsp goto done;
9165 2e1f37b0 2019-08-08 stsp }
9166 2e1f37b0 2019-08-08 stsp }
9167 2e1f37b0 2019-08-08 stsp
9168 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9169 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9170 ad493afc 2019-08-03 stsp if (error)
9171 ad493afc 2019-08-03 stsp goto done;
9172 ad493afc 2019-08-03 stsp
9173 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9174 ad493afc 2019-08-03 stsp if (error)
9175 ad493afc 2019-08-03 stsp goto done;
9176 ad493afc 2019-08-03 stsp
9177 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9178 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9179 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9180 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9181 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9182 9627c110 2020-04-18 stsp if (!error)
9183 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9184 715dc77e 2019-08-03 stsp done:
9185 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9186 2e1f37b0 2019-08-08 stsp error == NULL)
9187 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9188 0ebf8283 2019-07-24 stsp if (repo)
9189 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9190 715dc77e 2019-08-03 stsp if (worktree)
9191 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9192 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9193 715dc77e 2019-08-03 stsp free((char *)pe->path);
9194 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9195 715dc77e 2019-08-03 stsp free(cwd);
9196 01073a5d 2019-08-22 stsp return error;
9197 01073a5d 2019-08-22 stsp }
9198 01073a5d 2019-08-22 stsp
9199 01073a5d 2019-08-22 stsp __dead static void
9200 01073a5d 2019-08-22 stsp usage_cat(void)
9201 01073a5d 2019-08-22 stsp {
9202 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9203 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9204 01073a5d 2019-08-22 stsp exit(1);
9205 01073a5d 2019-08-22 stsp }
9206 01073a5d 2019-08-22 stsp
9207 01073a5d 2019-08-22 stsp static const struct got_error *
9208 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9209 01073a5d 2019-08-22 stsp {
9210 01073a5d 2019-08-22 stsp const struct got_error *err;
9211 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9212 01073a5d 2019-08-22 stsp
9213 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9214 01073a5d 2019-08-22 stsp if (err)
9215 01073a5d 2019-08-22 stsp return err;
9216 01073a5d 2019-08-22 stsp
9217 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9218 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9219 01073a5d 2019-08-22 stsp return err;
9220 01073a5d 2019-08-22 stsp }
9221 01073a5d 2019-08-22 stsp
9222 01073a5d 2019-08-22 stsp static const struct got_error *
9223 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9224 01073a5d 2019-08-22 stsp {
9225 01073a5d 2019-08-22 stsp const struct got_error *err;
9226 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9227 56e0773d 2019-11-28 stsp int nentries, i;
9228 01073a5d 2019-08-22 stsp
9229 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9230 01073a5d 2019-08-22 stsp if (err)
9231 01073a5d 2019-08-22 stsp return err;
9232 01073a5d 2019-08-22 stsp
9233 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9234 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9235 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9236 01073a5d 2019-08-22 stsp char *id_str;
9237 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9238 01073a5d 2019-08-22 stsp break;
9239 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9240 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9241 01073a5d 2019-08-22 stsp if (err)
9242 01073a5d 2019-08-22 stsp break;
9243 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9244 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9245 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9246 01073a5d 2019-08-22 stsp free(id_str);
9247 01073a5d 2019-08-22 stsp }
9248 01073a5d 2019-08-22 stsp
9249 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9250 01073a5d 2019-08-22 stsp return err;
9251 01073a5d 2019-08-22 stsp }
9252 01073a5d 2019-08-22 stsp
9253 01073a5d 2019-08-22 stsp static const struct got_error *
9254 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9255 01073a5d 2019-08-22 stsp {
9256 01073a5d 2019-08-22 stsp const struct got_error *err;
9257 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9258 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9259 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9260 01073a5d 2019-08-22 stsp char *id_str = NULL;
9261 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9262 01073a5d 2019-08-22 stsp
9263 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9264 01073a5d 2019-08-22 stsp if (err)
9265 01073a5d 2019-08-22 stsp return err;
9266 01073a5d 2019-08-22 stsp
9267 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9268 01073a5d 2019-08-22 stsp if (err)
9269 01073a5d 2019-08-22 stsp goto done;
9270 01073a5d 2019-08-22 stsp
9271 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9272 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9273 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9274 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9275 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9276 01073a5d 2019-08-22 stsp char *pid_str;
9277 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9278 01073a5d 2019-08-22 stsp if (err)
9279 01073a5d 2019-08-22 stsp goto done;
9280 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9281 01073a5d 2019-08-22 stsp free(pid_str);
9282 01073a5d 2019-08-22 stsp }
9283 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9284 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9285 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
9286 01073a5d 2019-08-22 stsp
9287 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9288 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9289 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
9290 01073a5d 2019-08-22 stsp
9291 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9292 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9293 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9294 01073a5d 2019-08-22 stsp done:
9295 01073a5d 2019-08-22 stsp free(id_str);
9296 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9297 01073a5d 2019-08-22 stsp return err;
9298 01073a5d 2019-08-22 stsp }
9299 01073a5d 2019-08-22 stsp
9300 01073a5d 2019-08-22 stsp static const struct got_error *
9301 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9302 01073a5d 2019-08-22 stsp {
9303 01073a5d 2019-08-22 stsp const struct got_error *err;
9304 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9305 01073a5d 2019-08-22 stsp char *id_str = NULL;
9306 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9307 01073a5d 2019-08-22 stsp
9308 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9309 01073a5d 2019-08-22 stsp if (err)
9310 01073a5d 2019-08-22 stsp return err;
9311 01073a5d 2019-08-22 stsp
9312 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9313 01073a5d 2019-08-22 stsp if (err)
9314 01073a5d 2019-08-22 stsp goto done;
9315 01073a5d 2019-08-22 stsp
9316 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9317 e15d5241 2019-08-22 stsp
9318 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9319 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9320 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9321 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9322 01073a5d 2019-08-22 stsp break;
9323 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9324 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9325 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9326 01073a5d 2019-08-22 stsp break;
9327 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9328 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9329 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9330 01073a5d 2019-08-22 stsp break;
9331 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9332 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9333 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9334 01073a5d 2019-08-22 stsp break;
9335 01073a5d 2019-08-22 stsp default:
9336 01073a5d 2019-08-22 stsp break;
9337 01073a5d 2019-08-22 stsp }
9338 01073a5d 2019-08-22 stsp
9339 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9340 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9341 e15d5241 2019-08-22 stsp
9342 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9343 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9344 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
9345 01073a5d 2019-08-22 stsp
9346 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9347 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9348 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9349 01073a5d 2019-08-22 stsp done:
9350 01073a5d 2019-08-22 stsp free(id_str);
9351 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9352 01073a5d 2019-08-22 stsp return err;
9353 01073a5d 2019-08-22 stsp }
9354 01073a5d 2019-08-22 stsp
9355 01073a5d 2019-08-22 stsp static const struct got_error *
9356 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9357 01073a5d 2019-08-22 stsp {
9358 01073a5d 2019-08-22 stsp const struct got_error *error;
9359 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9360 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9361 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9362 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9363 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9364 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9365 01073a5d 2019-08-22 stsp
9366 01073a5d 2019-08-22 stsp #ifndef PROFILE
9367 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9368 01073a5d 2019-08-22 stsp NULL) == -1)
9369 01073a5d 2019-08-22 stsp err(1, "pledge");
9370 01073a5d 2019-08-22 stsp #endif
9371 01073a5d 2019-08-22 stsp
9372 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9373 01073a5d 2019-08-22 stsp switch (ch) {
9374 896e9b6f 2019-08-26 stsp case 'c':
9375 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9376 896e9b6f 2019-08-26 stsp break;
9377 01073a5d 2019-08-22 stsp case 'r':
9378 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9379 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9380 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9381 9ba1d308 2019-10-21 stsp optarg);
9382 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9383 01073a5d 2019-08-22 stsp break;
9384 896e9b6f 2019-08-26 stsp case 'P':
9385 896e9b6f 2019-08-26 stsp force_path = 1;
9386 896e9b6f 2019-08-26 stsp break;
9387 01073a5d 2019-08-22 stsp default:
9388 01073a5d 2019-08-22 stsp usage_cat();
9389 01073a5d 2019-08-22 stsp /* NOTREACHED */
9390 01073a5d 2019-08-22 stsp }
9391 01073a5d 2019-08-22 stsp }
9392 01073a5d 2019-08-22 stsp
9393 01073a5d 2019-08-22 stsp argc -= optind;
9394 01073a5d 2019-08-22 stsp argv += optind;
9395 01073a5d 2019-08-22 stsp
9396 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9397 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9398 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9399 01073a5d 2019-08-22 stsp goto done;
9400 01073a5d 2019-08-22 stsp }
9401 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9402 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9403 01073a5d 2019-08-22 stsp goto done;
9404 01073a5d 2019-08-22 stsp if (worktree) {
9405 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9406 01073a5d 2019-08-22 stsp repo_path = strdup(
9407 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9408 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9409 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9410 01073a5d 2019-08-22 stsp goto done;
9411 01073a5d 2019-08-22 stsp }
9412 01073a5d 2019-08-22 stsp }
9413 01073a5d 2019-08-22 stsp }
9414 01073a5d 2019-08-22 stsp
9415 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9416 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9417 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9418 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9419 01073a5d 2019-08-22 stsp }
9420 01073a5d 2019-08-22 stsp
9421 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9422 01073a5d 2019-08-22 stsp free(repo_path);
9423 01073a5d 2019-08-22 stsp if (error != NULL)
9424 01073a5d 2019-08-22 stsp goto done;
9425 01073a5d 2019-08-22 stsp
9426 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9427 896e9b6f 2019-08-26 stsp if (error)
9428 896e9b6f 2019-08-26 stsp goto done;
9429 896e9b6f 2019-08-26 stsp
9430 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9431 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9432 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9433 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9434 01073a5d 2019-08-22 stsp if (error)
9435 01073a5d 2019-08-22 stsp goto done;
9436 01073a5d 2019-08-22 stsp
9437 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9438 896e9b6f 2019-08-26 stsp if (force_path) {
9439 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9440 896e9b6f 2019-08-26 stsp argv[i]);
9441 896e9b6f 2019-08-26 stsp if (error)
9442 896e9b6f 2019-08-26 stsp break;
9443 896e9b6f 2019-08-26 stsp } else {
9444 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9445 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9446 896e9b6f 2019-08-26 stsp if (error) {
9447 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9448 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9449 896e9b6f 2019-08-26 stsp break;
9450 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9451 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9452 896e9b6f 2019-08-26 stsp if (error)
9453 896e9b6f 2019-08-26 stsp break;
9454 896e9b6f 2019-08-26 stsp }
9455 896e9b6f 2019-08-26 stsp }
9456 01073a5d 2019-08-22 stsp
9457 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9458 01073a5d 2019-08-22 stsp if (error)
9459 01073a5d 2019-08-22 stsp break;
9460 01073a5d 2019-08-22 stsp
9461 01073a5d 2019-08-22 stsp switch (obj_type) {
9462 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9463 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9464 01073a5d 2019-08-22 stsp break;
9465 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9466 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9467 01073a5d 2019-08-22 stsp break;
9468 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9469 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9470 01073a5d 2019-08-22 stsp break;
9471 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9472 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9473 01073a5d 2019-08-22 stsp break;
9474 01073a5d 2019-08-22 stsp default:
9475 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9476 01073a5d 2019-08-22 stsp break;
9477 01073a5d 2019-08-22 stsp }
9478 01073a5d 2019-08-22 stsp if (error)
9479 01073a5d 2019-08-22 stsp break;
9480 01073a5d 2019-08-22 stsp free(label);
9481 01073a5d 2019-08-22 stsp label = NULL;
9482 01073a5d 2019-08-22 stsp free(id);
9483 01073a5d 2019-08-22 stsp id = NULL;
9484 01073a5d 2019-08-22 stsp }
9485 01073a5d 2019-08-22 stsp done:
9486 01073a5d 2019-08-22 stsp free(label);
9487 01073a5d 2019-08-22 stsp free(id);
9488 896e9b6f 2019-08-26 stsp free(commit_id);
9489 01073a5d 2019-08-22 stsp if (worktree)
9490 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9491 01073a5d 2019-08-22 stsp if (repo) {
9492 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9493 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9494 01073a5d 2019-08-22 stsp if (error == NULL)
9495 01073a5d 2019-08-22 stsp error = repo_error;
9496 b2118c49 2020-07-28 stsp }
9497 b2118c49 2020-07-28 stsp return error;
9498 b2118c49 2020-07-28 stsp }
9499 b2118c49 2020-07-28 stsp
9500 b2118c49 2020-07-28 stsp __dead static void
9501 b2118c49 2020-07-28 stsp usage_info(void)
9502 b2118c49 2020-07-28 stsp {
9503 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9504 b2118c49 2020-07-28 stsp getprogname());
9505 b2118c49 2020-07-28 stsp exit(1);
9506 b2118c49 2020-07-28 stsp }
9507 b2118c49 2020-07-28 stsp
9508 b2118c49 2020-07-28 stsp static const struct got_error *
9509 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9510 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9511 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9512 b2118c49 2020-07-28 stsp {
9513 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9514 b2118c49 2020-07-28 stsp char *id_str = NULL;
9515 b2118c49 2020-07-28 stsp char datebuf[128];
9516 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9517 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9518 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9519 b2118c49 2020-07-28 stsp
9520 b2118c49 2020-07-28 stsp /*
9521 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9522 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9523 b2118c49 2020-07-28 stsp */
9524 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9525 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9526 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9527 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9528 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9529 b2118c49 2020-07-28 stsp }
9530 b2118c49 2020-07-28 stsp
9531 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9532 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9533 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9534 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9535 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9536 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9537 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9538 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9539 b2118c49 2020-07-28 stsp else
9540 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9541 b2118c49 2020-07-28 stsp
9542 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9543 b2118c49 2020-07-28 stsp if (tm == NULL)
9544 b2118c49 2020-07-28 stsp return NULL;
9545 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9546 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9547 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9548 b2118c49 2020-07-28 stsp
9549 b2118c49 2020-07-28 stsp if (blob_id) {
9550 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9551 b2118c49 2020-07-28 stsp if (err)
9552 b2118c49 2020-07-28 stsp return err;
9553 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9554 b2118c49 2020-07-28 stsp free(id_str);
9555 b2118c49 2020-07-28 stsp }
9556 b2118c49 2020-07-28 stsp
9557 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9558 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9559 b2118c49 2020-07-28 stsp if (err)
9560 b2118c49 2020-07-28 stsp return err;
9561 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9562 b2118c49 2020-07-28 stsp free(id_str);
9563 b2118c49 2020-07-28 stsp }
9564 b2118c49 2020-07-28 stsp
9565 b2118c49 2020-07-28 stsp if (commit_id) {
9566 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9567 b2118c49 2020-07-28 stsp if (err)
9568 b2118c49 2020-07-28 stsp return err;
9569 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9570 b2118c49 2020-07-28 stsp free(id_str);
9571 b2118c49 2020-07-28 stsp }
9572 b2118c49 2020-07-28 stsp
9573 b2118c49 2020-07-28 stsp return NULL;
9574 b2118c49 2020-07-28 stsp }
9575 b2118c49 2020-07-28 stsp
9576 b2118c49 2020-07-28 stsp static const struct got_error *
9577 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9578 b2118c49 2020-07-28 stsp {
9579 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9580 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9581 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9582 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9583 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9584 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9585 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9586 b2118c49 2020-07-28 stsp
9587 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9588 b2118c49 2020-07-28 stsp
9589 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9590 b2118c49 2020-07-28 stsp switch (ch) {
9591 b2118c49 2020-07-28 stsp default:
9592 b2118c49 2020-07-28 stsp usage_info();
9593 b2118c49 2020-07-28 stsp /* NOTREACHED */
9594 b2118c49 2020-07-28 stsp }
9595 01073a5d 2019-08-22 stsp }
9596 b2118c49 2020-07-28 stsp
9597 b2118c49 2020-07-28 stsp argc -= optind;
9598 b2118c49 2020-07-28 stsp argv += optind;
9599 b2118c49 2020-07-28 stsp
9600 b2118c49 2020-07-28 stsp #ifndef PROFILE
9601 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9602 b2118c49 2020-07-28 stsp NULL) == -1)
9603 b2118c49 2020-07-28 stsp err(1, "pledge");
9604 b2118c49 2020-07-28 stsp #endif
9605 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9606 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9607 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9608 b2118c49 2020-07-28 stsp goto done;
9609 b2118c49 2020-07-28 stsp }
9610 b2118c49 2020-07-28 stsp
9611 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9612 b2118c49 2020-07-28 stsp if (error) {
9613 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9614 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9615 b2118c49 2020-07-28 stsp goto done;
9616 b2118c49 2020-07-28 stsp }
9617 b2118c49 2020-07-28 stsp
9618 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9619 b2118c49 2020-07-28 stsp if (error)
9620 b2118c49 2020-07-28 stsp goto done;
9621 b2118c49 2020-07-28 stsp
9622 b2118c49 2020-07-28 stsp if (argc >= 1) {
9623 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9624 b2118c49 2020-07-28 stsp worktree);
9625 b2118c49 2020-07-28 stsp if (error)
9626 b2118c49 2020-07-28 stsp goto done;
9627 b2118c49 2020-07-28 stsp show_files = 1;
9628 b2118c49 2020-07-28 stsp }
9629 b2118c49 2020-07-28 stsp
9630 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9631 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9632 b2118c49 2020-07-28 stsp if (error)
9633 b2118c49 2020-07-28 stsp goto done;
9634 b2118c49 2020-07-28 stsp
9635 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9636 b2118c49 2020-07-28 stsp if (error)
9637 b2118c49 2020-07-28 stsp goto done;
9638 b2118c49 2020-07-28 stsp
9639 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9640 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9641 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9642 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9643 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9644 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9645 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9646 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9647 b2118c49 2020-07-28 stsp
9648 b2118c49 2020-07-28 stsp if (show_files) {
9649 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9650 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9651 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9652 b2118c49 2020-07-28 stsp continue;
9653 b2118c49 2020-07-28 stsp /*
9654 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9655 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9656 b2118c49 2020-07-28 stsp */
9657 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9658 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9659 b2118c49 2020-07-28 stsp }
9660 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9661 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9662 b2118c49 2020-07-28 stsp if (error)
9663 b2118c49 2020-07-28 stsp goto done;
9664 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9665 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9666 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9667 b2118c49 2020-07-28 stsp break;
9668 b2118c49 2020-07-28 stsp }
9669 b2118c49 2020-07-28 stsp }
9670 b2118c49 2020-07-28 stsp }
9671 b2118c49 2020-07-28 stsp done:
9672 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9673 b2118c49 2020-07-28 stsp free((char *)pe->path);
9674 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9675 b2118c49 2020-07-28 stsp free(cwd);
9676 b2118c49 2020-07-28 stsp free(id_str);
9677 b2118c49 2020-07-28 stsp free(uuidstr);
9678 0ebf8283 2019-07-24 stsp return error;
9679 0ebf8283 2019-07-24 stsp }